Collision not working

Started by
0 comments, last by KnolanCross 10 years, 8 months ago

Hello, I have been trying to get collision to work for a few days and still nothing, I need help. I am trying to test collision against an object in an object map(std::vector<Object> m_Objects;). Every object has its own position and size. I pass in the object map and start iterating through the objects that are on screen and then seeing if the object and player are colliding. I have tried setting all objects to have a tile number, a position on the map on a specific tile(the objects do not move). Then I tested to see if the next tile number the player was going to was an object, and if so, not to move that way, but that failed. Then I tried using the pixels with the players position and size as well as the object, and that did not work. I am using C++ and SFML 2.0, Thanks for any help.

Advertisement

What kind of objects?

The simplest one you can use are circles, all you have to do is check the distance of center between two objects, if the distance is smaller than the radius of both objects added, then they are colliding, in other words:

dist = sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));

if (dist < r1 + r2) colliding;

else not colliding;

Where x and y are the positon of the center of the object an r is its radius.

For bounding boxes there are a few aways, but basicaly you will need to determine the positon of the the four limits of your box (up, down, left and right) and check against the same limits of other objects. This is a good place to check: http://gamedev.stackexchange.com/questions/586/what-is-the-fastest-way-to-work-out-2d-bounding-box-intersection

About the tile map, you should also notice that you need to check which title each of the four limits of your object is and see if the four tiles are empty. I would recomend that you implement this first as a single step only map and then move it in small position increments.

For instance, imagine this tile map:


  0 1 2 3 x
0 . p . .
1 . . x .
2 . . . .
y

Where:

. = walkable area.

p = player

x = path blocking object.

Say the tile size is 64 x 64. The player will begin on position (64, 0) and tile (0, 1), when you command it to move south, you check tile (1, 1), find it is empty and move him to position (64, 64), tile (1, 1). If try to move right, you check tile (2, 1), it is not empty, so you can't move.

After you get this simple implementation working, expand it to one where you have all the limits of the box around the character and move its position by one by each step. In this case, player begins at postion (64, 0), but its limits are [(64, 0), (64, 64). (128, 0), (128, 64)], which are all on tile (1, 1). As he moves you uptate all the position calculate all the tiles and check if the movement is valid.

Good luck.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

This topic is closed to new replies.

Advertisement