CollisionDetection help
in
Programming Questions
•
2 years ago
Ok, so I am making this platform game and I have my tilemap, walking and jumping animations. There is, however, one big problem. I am struggling to figure out how to implement collisionDetection. If anyone would like to help me and educate me on how to do this with the code I have, that would be AMAZING!
The images and tiles can be downloaded here:
http://rapidshare.com/files/429128248/data.zip
Here is the code:
int tileXPos;
int tileYPos;
int scrollNum=0;
int scrollNumY=0;
boolean hScroll=true;
boolean hScrollL=true;
boolean vScroll=true;
boolean vScrollU=true;
int bgX, bgY; //background image x and y positions
Player Mandrill= new Player();
PImage mmmBackground;
String images[]= {"blank.gif","tree_1.gif","tree_2.gif","tree_3.gif",
"tree_4.gif","tree_5.gif","tree_6.gif","tree_7.gif","tree_8.gif",
"tree_9.gif","tree_10.gif","tree_11.gif","tree_12.gif","tree_13.gif",
"tree_14.gif","tree_15.gif","tree_16.gif","tree_17.gif","tree_18.gif",
"tree_19.gif","tree_20.gif","tree_21.gif","tree_22.gif","tree_23.gif",
"tree_24.gif","tree_25.gif","tree_26.gif","tree_27.gif","tree_28.gif",
"tree_29.gif","Platform1.png","platform2.png","platform3.png",
"tree_bush.gif","rock 1.png","rock2.png","rock3.png","rock6.png",
"bridge_1.png","bridge_2.png","bridge_3.png"};
TileMap map;
void setup() {
size(550,400);
map= new TileMap(100,20);
frameRate(12);
mmmBackground = loadImage("level_1_background.png");
for(int i=0;i<100;i++)
{
for(int j=0;j<20;j++)
{
int imageIndex= tile_map[j][i];
String imageName=images[imageIndex];
map.setTile(loadImage(imageName),j,i);
}
}
// Gatta remember to initialize him
Mandrill = new Player();
// Loads all of the images before the game starts so that
// loading images during run-time doesnt slow the game down
Mandrill.loadImages();
}
void draw()
{
bgX=constrain(bgX,0,mmmBackground.width-width);
bgY=constrain(bgY,0,mmmBackground.height-height);
set(-bgX,0,mmmBackground);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),(32*j)-scrollNum,(32*i)-scrollNumY);
tileXPos=(32*j)-scrollNum;
tileYPos=(32*i)-scrollNumY;
}
}
checkKeys();
Mandrill.update();
Mandrill.checkState();
if (Mandrill.dirState == "right")
{
image(Mandrill.imagesR[Mandrill.imageIndexR], Mandrill.xPos,Mandrill.yPos);
}
else if (Mandrill.dirState == "left")
{
image(Mandrill.imagesL[Mandrill.imageIndexL], Mandrill.xPos,Mandrill.yPos);
}
// all the code for standing still PLUS blinking!!!!
else if (Mandrill.dirState == "chill")
{
// faces right if hes chillin' right
if (Mandrill.chillState == "right")
{
image(Mandrill.imagesR[0], Mandrill.xPos,Mandrill.yPos);
}
// faces left if hes chillin' left
if (Mandrill.chillState == "left")
{
image(Mandrill.imagesL[0], Mandrill.xPos,Mandrill.yPos);
}
}
if (Mandrill.jumpState == "jump" || Mandrill.jumpState == "falling")
{
if (Mandrill.chillState == "right")
{
set(-bgX,0,mmmBackground);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),(32*j)-scrollNum,(32*i)-scrollNumY);
tileXPos=(32*j)-scrollNum;
tileYPos=(32*i)-scrollNumY;
}
}
image(Mandrill.imagesR[5], Mandrill.xPos,Mandrill.yPos);
}
// faces left if hes chillin' left
if (Mandrill.chillState == "left")
{
set(-bgX,0,mmmBackground);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),(32*j)-scrollNum,(32*i)-scrollNumY);
tileXPos=(32*j)-scrollNum;
tileYPos=(32*i)-scrollNumY;
}
}
image(Mandrill.imagesL[5], Mandrill.xPos,Mandrill.yPos);
}
}
if(tileXPos <= 516) hScroll = false;
else hScroll = true;
if(tileXPos >=3200) hScrollL=false;
else hScrollL=true;
if(tileYPos>=734) vScroll=false;
else vScroll=true;
if(tileYPos <=410) vScrollU=false;
else vScrollU=true;
}
void checkKeys()
{
if (keyPressed)
{
if (keyCode == RIGHT)
{
if(hScroll == false)
{
scrollNum-=0;
}
else
{
if(Mandrill.xPos>width/2)
{
scrollNum+=14;
bgX++;
}
}
Mandrill.dirState = "right";
}
if (keyCode == LEFT)
{
if(hScrollL==false)
{
scrollNum+=0;
}
else
{
if(Mandrill.xPos<100)
{
scrollNum-=14;
bgX--;
}
}
Mandrill.dirState ="left";
}
if (keyCode == UP && Mandrill.jumpState == "none")
{
if(vScroll==false)
{
scrollNumY-=0;
}
else
{
if(Mandrill.yPos<100)
{
scrollNumY-=10;
}
if(Mandrill.yPos>400)
{
scrollNumY+=10;
}
}
Mandrill.jumpState = "jump";
}
}
else
{
Mandrill.dirState = "chill";
}
}
int[][]tile_map={
{0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,6,7,8,9,10,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,7,
8,9,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,11,12,13,14,
15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,11,12,13,14,15,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,31,31,31,31,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,16,17,18,19,
20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,16,17,18,19,20,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,
0,0,0,0,0,31,31,31,35,35,35,35,33,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,22,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,21,22,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,30,31,32,0,0,0,0,
30,31,31,31,31,31,31,31,31,31,32,0,0,
0,0,0,0,0,0,0,0,0,0,33,33,0,0,0,0,0,
0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,23,24,25,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,31,0,34,36,37,0,0,0,0,34,
35,35,35,35,35,35,35,35,35,37,0,0,0,
0,0,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,0
},
{0,0,0,0,0,0,0,33,33,0,0,33,0,26,27,28,
29,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,26,27,28,29,33,0,0,0,0,0,0,0,33,33,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,35,35,35,35,37,0,0,0,0,0,0,
0,0,0,0,0,0,35,35,35,35,35,35,35,35,37,
0
},
{0,30,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,32,0,0,0,0,0,30,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,0,
0,0,0,31,31,32,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,36,36,37,0,0,0,0,0,0,0,0,
0,33,33,0,0,35,35,35,35,35,35,35,37,0,
0
},
{0,34,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,0,0,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,0,
0,0,0,35,35,37,0,30,31,32,0,0,0,0,0,0,
0,0,36,36,36,0,0,0,0,0,0,0,0,0,0,31,
31,31,31,31,31,31,31,32,36,36,36,36,0,
0,0
},
{0,0,34,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,37,0,0,0,30,32,0,0,34,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,31,31,31,31,35,37,0,0,35,35,35,32,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,35,35,35,35,35,35,35,35,37,0,0,
0,0,0,0,0
},
{0,0,0,34,36,36,36,35,35,35,36,36,36,
36,36,36,37,0,0,0,0,36,36,0,0,0,34,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,35,35,35,35,
31,32,0,0,0,0,38,39,39,39,39,40,0,0,
0,0,0,0,0,33,35,35,35,35,35,35,35,37,
0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,36,36,36,0,0,0,0,0,0,0,
33,0,0,0,0,0,0,0,0,0,34,36,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,37,
0,0,0,0,34,35,35,35,35,35,31,31,31,
31,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,36,35,35,36,36,36,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,
31,32,0,0,0,0,0,0,0,0,0,0,34,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,37,0,0,0,0,0,
0,34,35,35,35,35,35,35,35,35,0,0,0,0,
0,0,34,35,35,35,35,35,35,35,37,0,36,
36,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,36,
36,36,0,0,0,0,0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35,35,35,35,35,35,35,35,35,35,35,
37,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,34,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,37,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0
}
};
class Player
{
float xPos=32;
float yPos=170;
float hM=0;
float vM=5;
String dirState="right";
String chillState="right";
String jumpState="none";
String shootState="shoot";
int maxImagesR=9;
int imageIndexR=0;
int maxImagesL=9;
int imageIndexL=0;
PImage[] imagesR= new PImage[maxImagesR];
PImage[] imagesL= new PImage[maxImagesL];
void loadImages()
{
for(int i=0;i<imagesR.length;i++)
{
imagesR[i]=loadImage("mandrillR"+i+".gif");
}
for(int j=0;j<imagesL.length;j++)
{
imagesL[j]=loadImage("mandrillL"+j+".gif");
}
}
void runRight()
{
dirState="right";
if(imageIndexR<=4)
{
imageIndexR+=1;
xPos+=4;
}
if(imageIndexR >4)
{
imageIndexR=2;
}
}
void runLeft()
{
dirState="left";
if(imageIndexL<=4)
{
imageIndexL+=1;
xPos-=4;
}
if(imageIndexL>4)
{
imageIndexL=2;
}
}
void chill()
{
dirState="chill";
if(chillState=="right")
{
imageIndexR=0;
}
if(chillState=="left")
{
imageIndexL=0;
}
xPos-=0;
}
void update()
{
//println(dirState);
}
void jumping()
{
if(jumpState=="jump")
{
yPos-=6;
if(yPos<=100)
{
jumpState="falling";
}
}
}
// void shooting()
// {
// if(shootState=="shoot")
// {
// }
// }
void falling()
{
if(jumpState=="falling")
{
yPos+=5;
if(yPos>=170)
{
jumpState="none";
yPos=170;
}
}
}
void checkState()
{
if(dirState=="right")
{
runRight();
chillState="right";
}
else if(dirState=="left")
{
runLeft();
chillState="left";
}
else if(dirState=="chill")
{
chill();
}
if(jumpState=="jump")
{
jumping();
}
else if(jumpState=="falling")
{
falling();
}
}
}
class TileMap
{
public int width, height;
PImage[][] tiles;
TileMap(int x, int y)
{
this.width = x;
this.height = y;
tiles = new PImage[y][x];
}
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
void setTile(PImage image, int x, int y)
{
tiles[x][y] = image;
}
PImage getTile(int x, int y)
{
if(x < 0 || x > width || y < 0 || y > height)
{
println("Tile doesn't exist");
return null;
}
else
{
return tiles[y][x];
}
}
}
1