Box2D
Physics simulation
Bacon2D includes Box2D for physics simulation, making it easy to add physics properties to game entities.
Box2D is a 2D rigid body simulation library for games. Programmers can use it in their games to make objects move in realistic ways and make the game world more interactive.
Box2D is widely used by many game engines and games, in multiple programming languages. Thus there are lots of great references available to help learn how to effectlive use it in your game. Please note, Box2D uses the term "body" for any item that is participating in the Box2D "world". The world defines the physics world of the game, and all bodies found in the world can interact/react with other bodies in the world. In Bacon2D the "world" is provided as part of the Scene, if the scene has the physics property set to true. Bacon2D provides the Entity component, which when physics are enabled in the scene, becomes a body in the Box2D world. When you see the terms body and world used, you can think of them as a scene and entity in Bacon2D.
Core concepts
- Shape: A shape is 2D geometrical object, such as a circle or polygon. See also Fixture.
- Body: A chunk of matter that is so strong that the distance between any two bits of matter on the chunk is constant. In Bacon2D, your entities are bodies in Box2D See also Entity.
- World: In Box2D, the World defines the physics container in which all bodies become part of. In Bacon2D, the world is provided by the Scene if physics is enabled. See also Scene.
- Fixture: A fixture binds a shape to a body and adds material properties such as density, friction, and restitution. A fixture puts a shape into the collision system (broad-phase) so that it can collide with other shapes. See also Fixture.
- Constraint: A constraint is a physical connection that removes degrees of freedom from bodies. A 2D body has 3 degrees of freedom (two translation coordinates and one rotation coordinate). If we take a body and pin it to the wall (like a pendulum) we have constrained the body to the wall. At this point the body can only rotate about the pin, so the constraint has removed 2 degrees of freedom.
- Contact constraint: A special constraint designed to prevent penetration of rigid bodies and to simulate friction and restitution. You do not create contact constraints; they are created automatically by Box2D.
- Joint: This is a constraint used to hold two or more bodies together. Box2D supports several joint types: revolute, prismatic, distance, and more. Some joints may have limits and motors.
- Joint limit: A joint limit restricts the range of motion of a joint. For example, the human elbow only allows a certain range of angles.
- Joint motor: A joint motor drives the motion of the connected bodies according to the joint's degrees of freedom. For example, you can use a motor to drive the rotation of an elbow.
- Solver: The physics world has a solver that is used to advance time and to resolve contact and joint constraints. The Box2D solver is a high performance iterative solver that operates in order N time, where N is the number of constraints.
- Continuous collision: The solver advances bodies in time using discrete time steps. Without intervention this can lead to tunneling.
Forces and Impulses
You can apply forces, torques, and impulses to a body. When you apply a force or an impulse, you provide a world point where the load is applied. This often results in a torque about the center of mass.
Applying a force, torque, or impulse wakes the body. Sometimes this is undesirable. For example, you may be applying a steady force and want to allow the body to sleep to improve performance. In this case you can use the following code.
- Entity
- Fixture
- Joint
- DistanceJoint
- PrismaticJoint
- RevoluteJoint
- MotorJoint
- WeldJoint
- PulleyJoint
- FrictionJoint
- WheelJoint
- MouseJoint
- GearJoint
- RopeJoint
- RayCast
Available through:
import Bacon2D 1.0
Please file bugs on Github