Artifact of the week: Collision

Alright, last week I worked on Learning Box2D and make a project where I would use it together with SFML. We(me and another coder in the group)  had some problems initially  as we first had wrong versions of both above listed “tools”. So after we figured out that the SFML that we had been using before we started was 64 bits and that the Box2D we had wasn’t compiled for VS 2012 either we got them working together. Box2D is not that hard in it self but it is a bit different because you don’t always know what is going on behind the scenes when you first use it. At first I though that it was enough to just write the world, the bodies, set the gravity and draw a “sprite” in the form of a circle and designate it to the position of the body. It was then supposed to fall and when it hit the other box it was supposed to stop. When we started the project it did not do anything at all though.
The reason was that you still needed to make some more variables that are used to update the player.
After that we had a Ball that was affected by gravity. We then tested to make a simple rectangle to see if the collision would work. It did and so I started to work on Chains. They are made up by multiple vertices that are connected with lines that works very well as walls since its easy to form them to whatever form the room have.
To test this I wanted the player to be able to move freely and not just be dragged down by gravity. So I disabled gravity and used the SFML inputs to start with. I couldn’t use them without modifying them a bit since it was the Box body i wanted to move and not the SFML “sprite”. After that was done I put the chains in the project and hoped that it would work. At that point I had not created a fixture as I thought it was only used for making the bodies visible. But it apparently has to do with collision. After I fixed that something still was wrong. Box2D makes the whole window to have negative y value as it uses the top left corner as the origin instead of the lower left. So when I had set out the positions as positives figures thinking that it was somewhere on the window it was actually above the window.
So here is an example of a chain:

2 thoughts on “Artifact of the week: Collision”

  1. What up! Well written, sir.

    At first I didn’t understand why you used gravity but then I understood when I read about Box2D. I would have liked to read more about Box2D in your post and your opinions on it. A link to their homepage would have been appreciated.

    Perhaps you would have saved some time using keyboard input from the start when testing collision instead of the Box2D gravity since input is very straight forward and gravity might be harder to use.

    I understand that using pre-written code can help a lot and save time. I usually write all my code from scratch and this results in bulky and inefficient code. Searching for code already written is probably a good idea. I don’t do it a lot right now but I probably should. I will try to be better at this in the future, thanks for the idea!

    Why did you choose to use Box2D, a gravity and physics engine, in a space shooter without gravity? This would have been interesting to hear about.

    That Box2D uses the top left corner as origin is pretty standard as I have understood it. Have you used the bottom left before? I think that it’s better to get used to the top left since everyone uses it.

    Again, well written and keep up the good work!

    1. Hey man, I’m sorry that this will be a super late reply. At first I wasn’t sure if I was supposed to answer the questions or if they were just retorical to show where I could improve my text. But after a thinking over it a couple of times I think I should answer them either way.

      The choice of Box2D was made because everyone that we initially talked to about it seemed to think that it was great. It might be a bit overkill to use it without gravity, but the thing we wanted it most for was to make walls. After that there where methods we could use to keep using Box2D but we felt like it wasn’t as beneficial to us anymore since it still was a new engine and we did not neccesarily need the help with circle-circle collision and so on. Even though we had some trouble setting up box2D i would say that it was worth it, since it is so easy to make walls that follow the texture in the room.

      It is true that you use the top left corner as origin usually. The difference is in which way the coordinate system is turned.
      In my case with Box2D, when i want to place an object in the middle of a 1024, 640 screen. That would be the coordinates (512, -320).

      You probably already googled it but here is the manual and website of Box2D

      Click to access manual.pdf

      http://box2d.org/

      Again sry for the late reply.

      See ya in class =)

Leave a comment