BODIES

21
Jun
2012

Bodies have position and velocity. You can apply forces, torques, and impulses to bodies. Bodies can be static, kinematic, or dynamic. Here are the body type definitions:

b2_staticBody (Type = 0)

A static body does not move under simulation and behaves as if it has infinite mass. Internally, Box2D stores zero for the mass and the inverse mass. Static bodies can be moved manually by the user. A static body has zero velocity. Static bodies do not collide with other static or kinematic bodies.

b2_kinematicBody (Type = 1)

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other static or kinematic bodies. A kinematic body is very similar to a static body in that when it collides with a dynamic body it always holds its ground and forces the dynamic body to retreat out of the way. The difference is that a kinematic body can move.

b2_dynamicBody (Type = 2)

A dynamic body is fully simulated. They can be moved manually by the user, but normally they move according to forces. A dynamic body can collide with all body types. A dynamic body always has finite, non-zero mass. If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram.

Bodies are the backbone for fixtures. Bodies carry fixtures and move them around in the world. Bodies are always rigid bodies in Box2D. That means that two fixtures attached to the same rigid body never move relative to each other.


Fixtures have collision geometry and density. Normally, bodies acquire their mass properties from the fixtures. However, you can override the mass properties after a body is constructed. This is discussed below.

You usually keep pointers to all the bodies you create. This way you can query the body positions to update the positions of your graphical entities. You should also keep body pointers so you can destroy them when you are done with them.

Don't worry I will explain what fixtures are & what is this body pointer in my next post.

Defining a body

		var bodyDef = new b2BodyDef;
		bodyDef.type = b2Body.b2_dynamicBody;
		bodyDef.position.Set(4,8);
		bodyDef.userData = 'BOX';
The above piece of code defines a "Dynamic Body". If we want to define a "Static" or "Kinematic" body, replace line 2 with
bodyDef.type = b2Body.b2_staticBody;
or
bodyDef.type = b2Body.b2_kinematicBody;
The above code (4 lines of code) defines a "Dynamic Body" at x = 4 & y = 8.

This x & y position is the center of the body in case "BOX" & "CIRCLE".

In case of "EDGE" & "POLYGON", 4 units and 8 units are added to the 'x' and 'y' coordinate of each vertices. If 'x' were -4 and 'y' were -8, they will be subtracted to the 'x' and 'y' coordinate of each vertices. Meaning the whole body will be shifted by 'x' in X direction and by 'y' in Y direction.

User Data is just a pointer to hold some information about your body. It totally depends on you what you want to store. You may delete the line 3 if you want. But they are very useful.

NOTE: The +ve X Axis is towards "RIGHT" and +ve Y Axis is "DOWNWARDS".

Now, use this definition to create the actual body instance:
var myBody = world.CreateBody(bodyDef);
"myBody" is the name of the Body.

NOTE: A body does not have any size, shape, so we don't define those here. It has no mass yet - the usual way of providing a mass for a body is by adding fixtures to it. Fixtures in next post.

Setting Body Properties (for full list click here. Then select b2Body from the menu.)

Starting Position & Angle

myBody.SetPositionAndAngle(new b2Vec2(10,0),Math.PI/2);
The intial position of body "myBody" is set to x = 10 & y = 0 and an angle of 90 degrees Clockwise.

Linear Velocity

myBody.SetLinearVelocity(new b2Vec2(4, 3));
Initial Velocity is set to resultant of the set vector which is square root of (4*4 + 3*3) which is equal to 5 (units/sec) & at an angle of inverse of tangent of (3/4) with respect of X Axis Clockwise.

Angular Velocity

myBody.SetAngularVelocity(Math.PI);
Angular Velocity is set to 180 degrees per Second Clockwise.

Getting body properties (for full list click here. Then select b2Body from the menu.)

var pos = new b2Vec2; //Created a new Vector for storing position vector
var vel = new b2Vec2; //Created a new Vector for storing velocity vector
float angle; // Created a new "float" variable for storing body angle
pos = myBody.GetPosition(); //Get Position Vector
angle = (myBody.GetAngle())*(180/Math.PI); //The Multiplication factor converts returned Radian value into Degrees
vel = myBody.GetLinearVelocity(); // Get linear velocity vector
var x_pos = pos.x; // Position with respect to X Axis
var y_pos = pos.y; // Position with respect to Y Axis
var x_vel = vel.x; // X Component of Velocity
var y_vel = vel.y; // X Component of Velocity
I hope the comments explained the usage.

Iterating over all the bodies in the world

Use the following code to iterate over all the bodies in the world.
var b = new b2BodyDef;
for(b = world.GetBodyList(); b; b = b.GetNext())	{
	//Perform some action with the body
}

Destroying body

If we want to remove the body from the world, then we are going to use the following code.
world.DestroyBody(myBody);

By administrator at 12:35:59 PM 0 Comment(s)

Comments

There are no comments yet. Be the first one to add a comment!

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha