zoza.blogg.se

Artifact of the week: Collision Box2D

Publicerad 2014-02-13 03:50:00 i 5SD033,

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:
 
 
 
 
 

Chains Box2D

Publicerad 2014-02-10 03:08:58 i Allmänt,

Alright, I managed to get Box2D to work with SFML. With some help i also got gravity to work and after that collision with a rectangle.  The reason why I chose rectangle was that it was the easiest way to see that Box2D really worked for us as we only knew that some functions such as create a body and gravity(which we dont even need in our game). The main thing we wanted Box2D was collision, so that we could create fully functional rooms that weren't made up of squares so that we could get the rooms to look like we wanted them. After that I amde so that you were able to walk with the player instead of just being pulled down by gravity as i felt that it was something that needed to be done anyway and that it was easier to check the collision if you can check from different angles.
 
It was pretty simple to make the player to walk in all the directions but to make it stop was another thing. SFML already have a simple way to make an object move, so all i had to do was to add the position to the body of Box2D istead of the player from SFML. What SFML does not have thoguh is a Keypressedonce function and when i looked after an alternativ solution I found out that Events apparently was not fit to use when moving cuz it was not smooth enough. I figured that out by just placing a if else under that If:s that was making the player able to move.
Until this point the caracter was only able to move in straight lines and not diagonally and I struggled a while to make this happen. I got help from a friend to make this happen and the solution was not too complex this time either.
 
After that was done the chains that I suggested we would use was the next step. They are points which are connected to each other with a vector, with connected I mean that they create a wall between them. So they are very effiecient to use to make boundries in a top down game from what I have seen so far. The only problem is that its some work to get there. We were probably not the quickest in setting the whole thing up. But there were many problems with different releases for both sfml and box2d that made it quite difficult to make it work. We will have a sprint meeting tomorrow to see what the next tasks are. But i think it is something along the lines to make enemies and AI to enemies, make the camera move and make the player able to shoot. After that except from stating win/lose conditions and make the time and helth we have a fully functional game. It will not be pretty at that point but it would work and be classified as a game. So I feel confident to make it now. It felt a bit rough when I didnt know if Box2D would work and we faced the option to face that we spent a week to nothing and instead use regular collisions. 
 
I was also working with our last project and it we solved many of the problems we had. We worked on one computer except for when i was playtesting to find bugs. So I did not code much but instead observed what my group partner did as he knows the code best and can bugfix much much much more efficiantly. We still have some stuff left, like some memory leaks, but it should not be any problems to do it.
 
Lastly I wnet through the core of the game that my other group member did so that I am familliar with it. He uses a different structure than the one i previously worked with but it works like that as well.

Collisionnnn

Publicerad 2014-02-05 02:33:29 i Allmänt,

So I have been working on collision today with another group member, we did it on one computer although we had different views on how to include the header files from box2d. One way is to just put them into the rest of the header files that are already in there and they will be "linked" automatically from the link that was made from SFML. The other is to take the whole box2d file into the project and then link again. I like the latter one but since it didnt really matter how we did it when we just was trying to figure box2d and collision out, I proposed a race to see which way was faster. I lost in speed and somehow the program was not happy with my link either so I think I should check that out. The problem that arised was that we did not have anything drawn out yet in the main project so we tried to combine the code of box2d with SFML and either make something that checks the collision in the prompter or to make bodies and se if it works. Therefore we thought that maybe this task should have been places after we had written the player and walls etc. So we ended it there for the day and went home and are checking on a way to watch collisions. Thats about it.

Senaste inläggen

Kategorier

Arkiv

Prenumerera och dela