Zohaib
Zohaib

Reputation: 2865

How to make path in box2d in iPhone

I have a path which will be on bottom of the screen where insects will walk. But my path is not a straight line, it have ups and down. So how can I my insects walk my jagged line? The black line is my path. I want my insects to follow the up and down parts and I would like to achieve this using box2d. Any help? I don't know from where tostart to make this work. I haven't yet started to code. How can I make a bodyDef in box2d for the picture path below?

enter image description here

Upvotes: 1

Views: 346

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59496

I assume that with the word "path" you mean the ground represented in the image, otherwise you can ignore the rest of my answer ;)

My suggestion is to follow these steps:

  1. Create a body definition for the ground image. I use PhysicsEditor (free trial) that automatically creates the body definition for an image. If your image is transparent the body definition will map pretty well the shape in the image. PhysicsEditor exports the body definition via .plist which can be easily loaded via code.
  2. Create a body definition for your insects (as above).
  3. In your code create a static body for the ground using the ground body definition.
  4. Create a dynamic body for each insect using the definition made at bullet 2.
  5. Add gravity.

Now you'll see your insects on the ground. If you drag them (using the Mouse Joint provided by box2d) you will see that they react realistically to the ground and do not intersect it.

Finally you need to apply a force (in box2d) in order to autonomously move the insects.

This is just an high-level description of the steps you should follow. More specific information for each step is provided by the PhysicEditor documentation and box2d documentation.

I hope that this is going to help you.

Upvotes: 1

Related Questions