Way of an Idea Box2D prototype – Step 3

Welcome to the 3rd part. In part 2 we allowed the player to draw the chalk track in a “paused” Box2D environment and then run the simulation.

Now it’s time to delete our chalk track.

A bit of theory: although the simulation is paused, the chalk bodies are already placed in the Box2D world. So we can easily select them with our old friend GetBodyAtMouse function.

Then we need to know whether the selected body is a chalk or not… we don’t want to delete other level assets or even the ball!!

So we must check if the userData of the selected body (that is the attached sprite) is a chalk. If true, we can remove the sprite from the stage and destroy the body.

This is the code:

And this is the result:

Draw the track with your mouse, press SPACE to begin the simulation and delete the chalk pressing 0 (zero) and moving the mouse over the chalk.

No need to download anything since you can easily cut-paste this code on the Step 2 example

  • Guest

    nice…

  • shoot

    So how would one go about doing dynamic objects in this way?
    (if mass is changed, it will just fall apart and I haven’t found a way to make it compound myself)

  • Very nice! Is it the same way linerider works?

  • the original linerider was made long before box2d

  • NEWBIE

    I tried using this code with quickbox2d
    I tried using the drawing part, it come out weird errors…can you help me?
    I just want the replace the ball with quickobject.