tile collision?

Started by
5 comments, last by GameDev.net 18 years, 10 months ago
hi everyone, i have created a tank game,i have used tiles for background,tiles are river,bricks,grass. i have created a structure struct tiles { int x; int y; BOOL blocked; . . } blocked is used to know if we the tile allows us to go through. now i want to know how can i do a collision detection for eg:when a tank hits a river or a brick tile it should not be able to move. can anyone tell me how can i achieve this? thank u very much.
Advertisement
class Actor {  Vector speed;  Vector pos;  World * world;  virtual void onUpdate( float timeStep ) {    Vector newPos = pos + speed * timeStep;    Tile * tile = world->tileAtPos( newPos );    if( tile->blocked ) {      // can't move this way      speed = 0;      return;    }    pos = newPos;  }};

enum Bool { True, False, FileNotFound };
thank u for replying,

but can you tell me what is variable 'world' for?
also can u explain the code for me
ok i can make speed=0;

but how can i know that i have collided with the brick?
let me try explaining...

class Actor {
//actor's speed
Vector speed;
//actor's current pos
Vector pos;
//pointer to your tile array
World * world;

virtual void onUpdate( float timeStep ) {
//New position where your actor would be
Vector newPos = pos + speed * timeStep;
//get tile at new position
Tile * tile = world->tileAtPos( newPos );
//check if that tile is blocked
if( tile->blocked ) {
//if it is, cant move there and return
// can't move this way
speed = 0;
return;
}
//if code reaches here, this means that the new tile is not blocked
//update actor's position
pos = newPos;
}
};
ok thank u hplus0603 and Kayx

i'll try that method,

i'll come back if i have any other questions.

thank you all once again.
I should mention that if you have a low framerate and you are traveling at a faster speed which places you at a position that is more than just one block, you may want to check the path as well for any obstaticles that are in the way. For instance if the new position was 2-4 blocks away and you are in a forest, you would not want your object to suddenly walk through a tree or something.

This topic is closed to new replies.

Advertisement