Iphone Cocos2d Day 3 – Implementing Box2d into game

So I started off on day three wanting to add a button to make the player jump but was not liking the results of my many attamps. So I took a step back and thought of my future game theme. I want to create a game based on a player being on mars (Concur Mars)….so I decided the player would have a jet pack and float up instead of jumping.

To do this I already had the best tool….Box2d…I had already decided to work with box2d but had not implemented it yet. After all its only day 3 of programming. So I decided to spend the day figuring out some of the basics.

To update my existing project I had to do a few things.

– Add a World

– Create some bodies to place in the word.

The world only took a few line of code which I took from the HelloWorldScene.mm (which is included with the template) Then using the same template file I grabbed the code for creating the bodies.

First was turning my animated sprite into a body which took about 20 lines. Then create a new section in the init method for creating the ground. We need to have at least those two bodies so the our player can collide with the ground and stand still. If there was no ground then he/she would just fall because of gravity.

Gravity is great in Box2d. This is what makes everything so realistic and when you apply other forces like LinearVelocity to a body, you can achieve some nice effects.

I did not really need any outside sources to do everything today because the cocos2d template came with what I used. But I will be looking at some tutorials for tomorrow when I try to create “coins” in the air for the player to fly up and grab.

The only other thing I can mention about today is that I had to modify the joypad code to make the player float around. Insead of creating x,y coordinates and changing the player position, I had to create values for velocity and apply that force to the player. I’m not 100% happy yet with it, but is a good place to start.

The video below will show you where I am at with the project. Right now I am still just learning and will begin into my actual game components very soon. But I wanted to ask for any comments or suggestion on things I might do better or even things I may need to know moving forward.

Once I have a good grasp of things and start finalizing the game I will create tutorials to show how I am doing things (For the beginners out there like me). Tutorials which will be much clearer then what I have done so far.

Comment below and subscribe to the right. See you on day 4!

Here is the video for Day 3 :

4 Responses to Iphone Cocos2d Day 3 – Implementing Box2d into game

  1. Atlus says:

    thanks again for your efforts but I cant make it any further with this example or the example included with cocos2d. I keep getting tons of errors. and it seems you have changed your sprite code a bit as well since the last video. look forward to more information

  2. paulsondev says:

    Take a look at the tutorial 1 post when I upload it. It will be longer with more detail and include the source.

    Also it describes how to use scrolling tilemap WITH collision.

    I will get a more detailed sprite tutorial soon.

  3. Michael says:

    Thanks again for your great tutorials; I have just one quick question:
    it looks as though you’ve used a variable of b2World named “world”, the only thing I can figure out is where it should be declared. Currently, Xcode tells me world hasn’t been declared in this scope, which is true since I don’t have it declared anywhere. I tried it in the GameScene.h file, but it gives me several errors..
    Since you have world in more that one method, it has to be a global variable, but everywhere I declare it Xcode I get errors!
    thanks so much again!

    • Michael says:

      Oops, I seem to have found the answer by looking around in the HelloWorld.h file! Oh well, anyone who has the same question now can find the answer!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: