Public Group

# tile collision?

This topic is 4962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites

but can you tell me what is variable 'world' for?
also can u explain the code for me

##### Share on other sites
ok i can make speed=0;

but how can i know that i have collided with the brick?

##### Share on other sites
let me try explaining...

class Actor {
//actor's speed
Vector speed;
//actor's current pos
Vector pos;
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 on other sites
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.

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×