question about collision detection in a small 2d game(like pacman)

Started by
31 comments, last by graveyard filla 20 years, 1 month ago
please note in the above code, player.tile_x and player.tile_y is actual screen coordinates/32 (well, i guess technically my screen coordinates are actually the tile_x*32 or tile_y*32), and player.x and player.y is the actual screen coordinates by themselves.

I also have 2 functions called Get_xPos() and Get_yPos()

they return tile_x*32 + offset_x or tile_y*32 + offset_y.

i use these Get() functions when i draw, so i basically do DRAW(player.Get_xPos(),player.Get_yPos())

[edited by - graveyard filla on March 2, 2004 3:05:33 AM]
FTA, my 2D futuristic action MMORPG
Advertisement

Alright, I will try to help as much as I can.

First of all, your code looks alright except for the last 2 if statements, I think you should have the offset_y stuff to be the other way around.


As for your pac man jiggling around, does that happen when you press the keys or does he do that when you don''t press any keys? I dont know why he would do that.

Dont try collision detection until you get him moving right. Collision detection becomes a bit more complex because you are able to move into two tiles at the same time, so you should check collision for the two tiles that you can move into.

Here is how I did mine:

I first add a constant to the offset for the direction I need to move.

// I add moveDistance to the offset for each direction I have to move.if(moveLeft){	offX -= moveDistance;} else if(moveRight){	offX += moveDistance;}if(moveUp){	offY -= moveDistance;}else if(moveDown){	offY += moveDistance;}



Then I check for collision. After that, I set the offset for the new position. To do this, I use a constant with which I check if the offset is greater positively or smaller negatively. So this way your player can be still in the tile but looks like he is moving into another tile. I add 50 (the width of the tile) if my offset is bigger than this constant, and move the player into the next tile. I subtract 50 if my offset is smaller negatively,l and move the player into the next tile. You should do this for all 4 of the directions: up down left and right.

bool isMoving = false;// check for collisionif(board.IsCollisionAbs(x, y, offX, offY, moveDir))	return;// now set offset for new positionif(offX < -TILECHANGE){	x--;	offX = 50.0f + offX;}else if(offX > TILECHANGE){	x++;	offX = offX - 50.0f;}if(offY < -TILECHANGE){	y--;	offY = offY + 50.0f;}else if(offY > TILECHANGE){	y++;	offY = offY - 50.0f;}


Try to get the movng done right before you do the collision detection.
--------------------------A good discussion is like a miniskirt; Short enough to pertain interest and long enough to cover the subject..
Actually I do know why he jiggles around.

Your move statements are not independent. They are dependent on each other. Specifically up and down are dependent on each other and left and right are dependent on each other.

So when you do your code, do it so that you can only move either up OR down, not both:

if ( player.offset_x > 32/2 ){		player.xPos++;		player.offset_x *= -1;}else if (player.offset_x < 32/2)   ///// <----- note the 'else'{		player.xPos--;		player.offset_x *= -1;}


Same for your left and right movements.

The player will move into a tile then move back because of the way you setup your if statements. So use 'else', like how I did in my code.


[edited by - bigbadboo on March 2, 2004 10:23:32 AM]
--------------------------A good discussion is like a miniskirt; Short enough to pertain interest and long enough to cover the subject..

This topic is closed to new replies.

Advertisement