Sign in to follow this  
ballack

tile collision?

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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;
}
};

Share this post


Link to post
Share on other sites
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;
}
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this