0
votes

I have a simple question about a game I am making using box2D and cocos2D. I started using the physics engine yesterday so I am rather inexperienced with its usage and capabilities. My game involves rolling a ball around the screen using the accelerometer. I want to add holes to the ground that if the ball rolls into, it will then require a greater acceleration via the accelerometer to escape the hole pocket. I've toyed with friction, linear damping, modifying the accelerometer's gravity vector, and tried adding attractive forces but I haven't had too much success and some of it doesn't really simulate well what I want happening. Basically I just want to create some sensors and give them the properties of a small pocket a ball can fall in to. Any tips and advice is much appreciated. Thanks

2
I would have thought that adding an attractive force toward the pocket center would work just fine. Maybe you could show us how you did that.iforce2d

2 Answers

0
votes

I suggest you to use Level Helper . Its an awesome tool to create physic based games.

You can find it here

0
votes

You could take the y position of the ball every frame and if it is below a certain threshold, then it is in a hole. Based off on this, if the ball is in a hole, reduce the sensitivity. When the ball exits the hole, put the sensitivity back to normal.

As for creating the holes, use Vertex Helper to create bodies that will correspond to your sprites.

If you need more explanation, feel free to ask.

Elaborated:

Example

Now, basically in the picture, I am depicting what I tried to describe earlier. All you really have to do is to change the tilt sensitivity if the player's Y position is below a certain point. I just used 50 as an example.

Some pseudocode:

- (void)update:(ccTime)dt
{
    if (player.position.y >= 50) { //If the player's y position is above or equal to 50
        if (sensitivity != normalSensitivity) { //We don't need to set it every frame, so lets check
            sensitivity = normalSensitivity;
        }
    }
    if (player.position.y < 50) { //If player's position is below our threshold of 50
        if (sensitivity != limitedSensitivity) { //Check so we don't set the sensitivity every frame
            sensitivity = limitedSensitivity;
        }
    }
}

Now, as far as Vertex Helper is concerned, it is an open source tool (I believe) that helps you define vertices for custom shapes that can then be copied and pasted directly into your box2d or chipmunk cocos2d project. It can be fond here.

I suggest googling around for tutorials on how this is used. It is very simple, but you may need a quick reference to get you started.

Finally, something to remember, is that box2d can only work with convex shapes, not concave. A convex shape is a shape where in is impossible to draw a line from any vertex to another without passing through the shape itself. Basically something that has no indentations in it.

Convex vs concave

I hope this helped. I'm not sure I can elaborate anymore than I have, but if you have more specific questions, feel free to ask.