Game Development Reference
In-Depth Information
When applying forces, we also have the option to apply the force on any point on the
object other than the center of mass. This is simply given as a position vector of the
location, where the force is being applied from its center of mass. Torques, however,
are always applied around the center of mass. To create a more complex torque,
such as a wrench (force at one end and a connection at the other), you would need to
apply a body force to a position on the end of the wrench with the other end attached
to a constrained joint.
Impulses are instant changes in the velocity (or angular velocity) of the body. Rather
than applying a force that accelerates a body from its initial velocity, applying an
impulse immediately increases to the new velocity. This would be same as adding
impulse/mass to the velocity of the body. Applying a "negative impulse" would be
subtracting impulse/mass from the current velocity.
Diving deep into collision detection
The
RigidBody
class has a
collisions
array. On every rigid body collision, a
CollisionInfo
object is added to the array. The
CollisionInfo
object has the
following properties:
CollisionInfo.prototype.objInfo=null;
CollisionInfo.prototype.dirToBody=null;
CollisionInfo.prototype.pointInfo=null;
CollisionInfo.prototype.satisfied=null;
We would like to limit our discussion to the
objInfo
property. The
objInfo
property is of the
CollDetectInfo
type. The
CollDetectInfo
object has the
following properties:
CollDetectInfo.prototype.body0=null;
CollDetectInfo.prototype.body1=null;
The variables,
body0
and
body1
, are of the
RigidBody
type. Hence, we can access
information about both objects from the
objInfo
object. The sample code to access
the collision information is as follows:
var collidingBody=this.rigidBody.collisions[0].objInfo.body1;
JigLib also offers events to detect collisions. The
JCollisionEvent
=function(body,
impulse) is raised when a collision occurs.