Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculating next step coordinates

Started by dirtysanchez Jun 11, 2009 at 1:37 PM 1 replies 850+ views
Original Post
dirtysanchez
dirtysanchez
Hi all :) I am developing some drafts for my new game for few hours now. My map is defined with; #define WALKABLE 1 #define BLOCKED 0 struct _M{ int texture; int walkable; }MAP[100][100]; Some of my MAP places are blocked by editor, this is simple. I am walking using 'W,S,A,D' and using rotation by mouse. To compute the walk forward math's i use: PLAYERS[a].x+= ((float)sin(PLAYERS[a].rot*piover180))*PLAYER_SPEED; // Move On The X-Plane Based On Player Direction PLAYERS[a].z+= ((float)cos(PLAYERS[a].rot*piover180))*PLAYER_SPEED; The thing is: How to caltulate next tile position of my player to predict eventual walking on BLOCKED tile? Knowing the angle of rotation and actual position - how can this be calculated? many thanks for any reply, i am stuck here..
Xynapse | http://szczerbiec.net/
fng
fng
What you need to do is to check whether the new position is within a blocked tile and depending on that accept the update, or try to get as close to the tile as possible (which would result in touching a wall). And after that you update the position. But be careful, if the player moves too fast, this might not detect collisions between the current and the new state (also called tunneling effect). Easiest measure would be to restrict the maximum velocity of the player.

You will also have to calculate the tile coordinates from a player coordinate which would be something like
Additionally you probably need a bounding sphere for the tile, as I guess the player is bigger than just 1 pixel. Make sure you take this into account when checking for collisions.

  // calculate the velocity of the player  velocity.x+= ((float)sin(PLAYERS[a].rot*piover180))*PLAYER_SPEED;  velocity.z+= ((float)cos(PLAYERS[a].rot*piover180))*PLAYER_SPEED;   // depending whether you move up/down, left/right you have to add  // radius or subtract it  int tilex = (int) round (posx +- radius + velocity.x)  int tilez = (int) round (posz +- radius + velocity.z)  // is the next position in a blocked tile?  if (MAP[tilex + 100][tilez + 100] == BLOCKED)    // calculate how far you can go until you    // crash into the tile -> t  else    // accept the full -> t = 1


To calculate the closest position, you have first to check on which side of the tile you will crash into. Once this is done, you have to calculate how far left/right or up/down you can move until you touch the side of the tile.

If you calculated that the player will touch the right side you end up calculating
  t = (tile_right_x - posx + radius) / velocity.x


Then you can update the position with
  posx += t * velocity.x  posz += t * velocity.z


Cheers,
fng
-- blog: www.fysx.org
dirtysanchez
dirtysanchez
Great stuff,
many many thanks!

I'll post an update when i finish up with this implementation.


Best regards!
Xynapse | http://szczerbiec.net/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.