• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Xanather

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 this post


Link to post
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...

Anyway thanks for the reply,
Xanather.
0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

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  
Followers 0