Skip to content
Kenkron edited this page Feb 9, 2012 · 4 revisions

Fizzy is a library designed to provide all the functionality of JBox2D (which it is built off of), but make everything a little simpler. To use fizzy, you will need the source code athttps://github.com/nanodeath/fizzy/zipball/master. This source code will then need to be compiled using it's build script (build.xml). This will create a fizzy jar you can use in your project.

Creating the World

To create a world in Fizzy,use the code:

/** The physics world we run the simulation in */ private World world;

and to initialize:

world = new World(LftBound, TopBound, RgtBound, BotBound, Gravity, Iterations);

  • lft, top, rgt, and bot bounds mark the boundaries of the simulation. Anything outside of these bounds is considered too far away to be important.
  • Gravity, as the name implies, provides the gravitational acceleration of all the objects in the physics simulation
  • Iterations marks the number of times collisions are simulated each update. This is important for simulations where more than two objects collide at once. For example, three boxes stacked on top of each other. When the physics engine collides the bottom two boxes, it will slightly effect the collision between the top two boxes. More iterations in the simulation will help minimize the inaccuracy this causes, but will also take more time.

Adding Bodies

Adding bodies to the world requires a few steps:

1) Create a new Shape for your body

To do this, create a new Shape object. This can be a Circle, Polygon, or Rectangle, all of which should be taken from the Fizzy package (not slick.geom).

Remember: all Box2D shapes must be convex!

If you need to create a concave shape, you should make your shape with the Compound Shape class. This will allow you to divide your concave shapes into convex pieces, and combine all of those int one shape for your body. The reason this is allowed when concave shapes are not is that Box2D can candle collisions for each shape one at a time, even if they are attached to the same body. Fortunately, you need not worry about exactly how this works. It's already handled by Fizzy.

Code for creating a simple box would look like this:

Shape rectangle=new Rectangle(width,height) rectangle.setOffset(x, y, angle);

In the code above, it is clear that the rectangle is created with the width and height as a parameter. However, it may not be clear what the setOffset does. Box2D stores the location, angle, and velocities of a body relative to a certian position on the Shapes of the body. However, this position is not always easy to track with an image. A circle in Box2D will move from its center, so if that circle represents a ball, and the image of the ball is set to the location of the circle, the upper left corner of the image will rest on the center of the ball. The setOffset method allows you to change where the shape is in relation to its origin to best suit your needs. Bear in mind that the axis of rotation will also be affected by the offset.

code for a compound shape might look like this:

CompoundShape shape = new CompoundShape(); shape.add(new Circle(radius)); shape.add(new Rectangle(width1, height1).setOffset(dX1, dY1, dAngle1)); shape.add(new Rectangle(width2, height2).setOffset(dX2, dY2, dAngle2));

note that in this case, the setOffset is used again. In this case, the offset allows each sub-shape to be at a different location, making the CompoundShape as flexible as possible.

2) Create the Body itself

The body is very easy to create. The code looks like this:

Body newBody = new Body(shape, x, y, static);

In this code, a body is create that has the given Shape and begins at the x and y coordinates provided. 'static' is a boolean value used to determine whether the object moves. If it is set to true, the object will not be affected by anything in the physics world, but other objects will be effected by it. This is useful when you need to create ground or walls. Making 'static' true will cause this body to be a dynamic body, which will be affected by gravity and bounce off of other objects freely.

3) Set the attributes of the body

Aside from their shape and ability to move, bodies have other attributes that can be changed as well.

body.setRestitution(bounciness); body.setFriction(friction); body.setDamping(slowness); body.setDensity(density);

These are three methods that change some of the ways 'body' will interact with other bodies.

setRestitution will change how much this body bounces off of other bodies. Bear in mind that the bounciness of this object will also be affected by the bounciness of the thing it hits.

setFriction will determine how much this object drags against other objects, and like restitution, friction on this body will be affected by the friction of the bodies it rubs against.

setDamping is adds something like wind resistance, or constant friction to the body. A body with damping will tend to slow down whenever it moves, and will slow down faster with a higher damping. This is particularly useful if you are making a game with a top-down perspective, where everything that moves is sliding along the ground.

setDensity will change how massive your object is. Remember that more mass requires more force to move.

Note: If you have a body with a compound shape, you can set these properties for each sub-shape individually.

4) Add the Body to the World

Finally, now that you have a well designed body, you can add it to the World you made way back up at "Creating the World." Coding this is very simple:

world.add(newBody);

Its a simple line,but until you add it, the world will not know your new body exists.

Making things in the World Happen

Once you have a Physics simulation set up, you will need to start running the simulations. As you are running the simulations, you will likely want to move objects in the world, and apply forces of your own.

Simulating the World

The code for simulating your physics world looks like this:

world.step(timeInSeconds);

This line of code will simulate the world for the given amount of time. Note that this time is in SECONDS, not milliseconds. This means that you should not supply the updates delta method directly, but should instead supply delta*0.001f in most cases.

In order to best update your game, the update method should handle input and AI before updating the world, so that the physics are as up to date as possible for each render.

Also, be aware that identical simulations will differ slightly if the time step varies a bit, and if the time step varies by a lot, all bets are off. It is best to keep a physics simulation time steps within a certain range to prevent bizzare or innacurate behavior. It should be noted that, as stated in the Box2D manual, it is much more accurate to have a simulation with a large number of updates than a simulation with a large number of iterations. Be careful that you don't set the iterations of your world so high that it slows down your game.

Adding Forces and Impulses

If you wish to move something in your physics simulation, you may find it useful to add a specific push to an object to make the simulation behave a specifically, but consistently. The two ways of doing this are by using these two methods:

body.applyForce(x,y); body.applyTorque(torque); body.applyImpulse(x,y); body.applyAngularImpulse(angularImpulse);

applyForce, which effects direction, and applyTorque, which effects angle, are both best used for effecting a body over time, like gravity, or the pull of a magnet. Forces will scale depending on how much time has passed, meaning that they are consistent over a variable timestep, and will not react wildly if you decide to use a higher or lower FPS. However, if it is used for a single, instant push, it will still try to scale with the timestep and will become inconsistent.

applyImpulse and applyAngularImpulse immediately add a specific amount of momentum to the other object. This is best used for events that happen in an instant, like making a player jump, or simulating the knock-back of an explosion. If used over a long period of time it will vary depending on the game's FPS, but is guaranteed to give the same amount of force in a single frame, regardless of how long that frame lasts.

Remember that because these are ultimately simulated by the physics engine, they automatically affect an object depending on it's mass.

Clone this wiki locally