What is the logic behind collision detection in 2d games?

arandomguy

I want to create a 2D game with a tile based map. My main problem is collision. If there is a tree in my way, how will I make my sprite not move through the tree?

Omar Himada

Pseudo-code:

some_event() {
    if (bullet.x == monster.x && bullet.y == monster.y) {
        collision_occurs();
    }
}

Of course the semantics such as which event will be fired and whether or not an event handler makes more sense (i.e.: collision_occurs() when the x and y coordinates meet, rather than if they meet while some_event() is fired) depend on the game.

If you were to analyze this more you would notice that the bullet and monster aren't 1 pixel each, so it would look more like:

// While approaching from the left
if ((bullet.x + (bullet.radius)) >= (monster.x + (monster.radius)))

These fine details come after. Essentially you have moving objects and these objects share coordinates. When these coordinates, as properties of their representational objects, are near enough, a "collision" occurs and some methodology follows.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is AABB - Collision detection?

From Dev

2D Collision detection not working

From Dev

Collision detection in java 2d game

From Dev

Collision detection not working in Unity 2D

From Dev

Unity 2d collision detection not registering

From Dev

Trouble with java 2D collision detection

From Dev

Unity 2D Collision Detection

From Dev

What is the logic behind needing to convert a 1D array to 2D to concatenate?

From Dev

What is the logic behind needing to convert a 1D array to 2D to concatenate?

From Dev

What is the logic behind the algorithm

From Dev

Collision Detection on 2 objects

From Dev

HTML Canvas game: 2D collision detection

From Dev

Collision detection in a 2D maze with thick walls

From Dev

Collision detection in 2D game without tiles XNA

From Dev

2D Monogame: Rectangle collision detection, side specific

From Dev

Issue with Collision Detection in Pygame for simple 2D Platformer

From Dev

2D Per pixel collision detection not working

From Dev

Collision Detection with Ceramic Tile Engine & Box 2D

From Dev

Collision detection in 2D game without tiles XNA

From Dev

How to react on collision detection in 2d platformers

From Dev

2D Monogame: Rectangle collision detection, side specific

From Dev

Collision Detection in Java 2D Not Working Properly

From Dev

What is the logic behind string removal?

From Dev

What logic is behind NaN evaluation?

From Dev

What is the logic behind this prolog code?

From Dev

What is the logic behind this how it is working?

From Dev

What is the logic behind d3.js nice() ticks

From Dev

Collision detection of 2 gifs on canvas

From Dev

What's this collision-detection-method called?

Related Related

HotTag

Archive