Followers 0

# 2D tile based collision

## 10 posts in this topic

I have been seriously trying at this for just about a day now. Such as simple thing is creating big problems.

I am trying to implement 2D tile based collision. All have failed. Should I create a algorithm that calculates the velocity to apply to the position (the velocity calculated by the algorithm will insure that the non-static (moving) entity will not hit solid tiles) or create a algorithm that calculates the position of the entity after the velocity has been applied to it (resulting in directly changing the position to ensure that it will not collide with another tile). All in all this is a generic question, how can this be done properly? (A real example of what I want to do is terraria's character movement).

All replies are appriciated.
Thanks, Xanather.

Edit: One of the algorithms ive tried to implemented checks each velocity axis negativity/positivity and works along that. Edit 2: Entities have the same axis scale as the tiles (1, 1) would mean the player would be drawn at (16, 16) on the screen).

[source lang="csharp"] Vector2 result = position + velocity;
Vector2 nposition = new Vector2(position.X + velocity.X, position.Y);
Vector2 topleft = new Vector2(nposition.X - (width / 2), nposition.Y - (height / 2));
Vector2 topright = new Vector2(nposition.X + (width / 2), nposition.Y - (height / 2));
Vector2 bottomleft = new Vector2(nposition.X - (width / 2), nposition.Y + (height / 2));
Vector2 bottomright = new Vector2(nposition.X + (width / 2), nposition.Y + (height / 2));
if (velocity.X > 0)
{
for (int y = (int)topright.Y; y < (int)bottomright.Y + 1; y++)
{
if (engine.main.tiles[(int)topright.X, y].solid)
{
result.X = (int)topright.X - (width / 2);
break;
}
}
}
else if (velocity.X < 0)
{
for (int y = (int)topleft.Y; y < (int)bottomleft.Y + 1; y++)
{
if (engine.main.tiles[(int)topleft.X, y].solid)
{
result.X = (int)topleft.X + 1 + (width / 2);
break;
}
}
}
nposition = new Vector2(position.X, position.Y + velocity.Y);
topleft = new Vector2(nposition.X - (width / 2), nposition.Y - (height / 2));
topright = new Vector2(nposition.X + (width / 2), nposition.Y - (height / 2));
bottomleft = new Vector2(nposition.X - (width / 2), nposition.Y + (height / 2));
bottomright = new Vector2(nposition.X + (width / 2), nposition.Y + (height / 2));
if (velocity.Y > 0)
{
for (int x = (int)bottomleft.X; x < bottomright.X + 1; x++)
{
if (engine.main.tiles[x, (int)bottomleft.Y].solid)
{
result.Y = (int)bottomleft.Y - (height / 2);
}
}
}
else if (velocity.Y < 0)
{
for (int x = (int)topleft.X; x < topright.X + 1; x++)
{
if (engine.main.tiles[x, (int)topleft.Y].solid)
{
result.Y = (int)topleft.Y + 1 + (height / 2);
}
}
}
return result;[/source] Edited by Xanather
0

##### Share on other sites
Yeah, that sounds about right, but I am working with a tile based world which is contained within a 2 dimensional array (unless I should create a bounding box for each tile, which i don't think is really necessary unless I either:
1. calculate the bounding boxes (rectangle) every update, or,
2. save the bounding box for each tile object and with many tiles (16000x4000) RAM usage will increase three times over (rectangle has 4 integers = 16 bytes).

What I really want is a collision algorithm that would indeed push back the player if it were to intersect with a solid x/y tile, and the velocity at which the object is traveling does not determine weather the collision check will be successful or not (say if the object was moving at 10000km/h it should not skip tiles in the collision check due to its speed).

I hope you understand what I'm saying Maybe I am just horrible at explaining things...

Xanather.
0

##### Share on other sites
Yeah but I don't even know why I should use bounding boxes in the first place if I am just checking for collision against static (not moving) tiles. It would be much more efficient to check for the x/y tiles that are within the players bounds (using the method i first posted) and see if they are solid?

Your idea #2 looks like it could work though, thanks I will try and implement that. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Xanather. Edited by Xanather
0

##### Share on other sites
I will look at that thanks
0

##### Share on other sites
For tile based collision, what I do is I check the tiles close to my player, so I won't have to check my entire array all the time, while at the same time temporarily creating the rectangles to check for collision. so basically you could do something like this:

[CODE]
for(int x = Player.Position.X - collisionRange, x < Player.Position.X + collisionRange; x++)
{
for(int y = Player.Position.Y - collisionRange, y < Player.Position.Y + collisionRange; y++)
{
if(!world[x, y].IsCollidable)
continue;
if(Player.BoundingRectangle.Intersects(New Rectangle(x, y, TileSize, TileSize)
{
//Collision Logic goes here.
}
}
}
[/CODE]

Note that doing it this way, you need to know the players position in your Grid space. This is easily calculated by dividing your worldspace position with the size of your tiles.
At least this is the approach I more or less use in my game.
0

##### Share on other sites
Hi Xanther,

I have a snippet of code that handles movement in a 2d platformer-type game that I've posted in other topics. It handles jumping, but it also handles collision in X direction, and moving the player out of the collision zone. It should work with almost any situation and map for falling off ledges, hitting head on platform above you, or moving against a wall, which slides up and down, you will wlak past once it's out of the collision zone. BTW, the numbers below are just guesses, make them what you want to make a good game.
[code]
// Speed player moves left or right
#define MOVEMENT_SPEED 10.0f
// initial velocity given to player when he jumps
#define JUMP_VELOCITY 20.0f
void Player::HandleInput()
{
if (LeftIsPressed()) {
this.xVelocity = -MOVEMENT_SPEED;
}
else if (RightIsPressed()) {
this.xVelocity = MOVEMENT_SPEED;
else {
this.xVelocity = 0.0f;
}
// Only jump if we're not already jumping or falling
if (JumpIsPressed() && this.OnGround) {
this.yVelocity = -JUMP_VELOCITY;
}
}
// defines amount to increase downward velocity every frame
#define GRAVITY_FORCE 4.0f
void Player::Update()
{
// Apply downward force to player
this.yVelocity += GRAVITY_FORCE;
// Move the Player
this.xLocation += this.xVelocity;
this.yLocation += this.yVelocity;
// Check we've collide with something above or below us
bool CollideBelow;
if (CheckCollisionY(CollideBelow)) {
// move us back to previous location and Stop Y Velocity
this.yLocation -= this.yVelocity;
this.yVelocity = 0.0f;
if (CollideBelow) {
this.OnGround = true;
}
}
else {
this.OnGround = false;
}
// Check if we've collided with anything on our left or right
if (CheckCollisionX()) {
// move us back to previous location and Stop X Velocity
this.xLocation -= this.xVelocity;
this.xVelocity = 0.0f;
}
}
[/code]

Have fun and Good Luck! Edited by BeerNutts
2

##### Share on other sites
This one is actually a very good resource that describe different approaches for this, you should take a look:
http://www.gamedev.net/page/resources/_/technical/game-programming/the-guide-to-implementing-2d-platformers-r2936
2

##### Share on other sites
There is an article describing a relatively simple collision detection technique which can deal with high speed objects:

http://www.wildbunny.co.uk/blog/2011/03/25/speculative-contacts-an-continuous-collision-engine-approach-part-1/

There is also code which i guess uses this method but I did not look into it:

http://www.wildbunny.co.uk/blog/how-to-make-a-platform-game-source-code-options/
2

## Create an account

Register a new account