Most visited

Recently visited

PhysicsBody

public class PhysicsBody
extends Object

java.lang.Object
   ↳ com.viro.core.PhysicsBody


PhysicsBody encapsulates the physics simulation of a particular Node. The physics simulation includes computing the effects of gravity, friction, and user-supplied forces. The simulation is updated each frame prior to rendering. In order to add physics to a Node, invoke initPhysicsBody(RigidBodyType, float, PhysicsShape).

For an extended discussion of physics in Viro, refer to the Physics Guide.

Summary

Nested classes

interface PhysicsBody.CollisionListener

Callback interface for responding to PhysicsBody collisions. 

enum PhysicsBody.RigidBodyType

The RigidBodyType of a PhysicsBody controls how the body responds to forces and collisions. 

Public methods

void applyForce(Vector force, Vector position)

Apply a constant force to this PhysicsBody.

void applyImpulse(Vector impulse, Vector position)

Apply an instantaneous force to this PhysicsBody.

void applyTorque(Vector torque)

Apply a constant rotational force to this PhysicsBody.

void applyTorqueImpulse(Vector torque)

Apply an instantaneous rotational force to this PhysicsBody.

void clearForce()

Clear any constant forces that were applied to this PhysicsBody.

float getFriction()

Get the friction value for this PhysicsBody.

float getMass()

Return the mass of this PhysicsBody in kg.

float getRestitution()

Get the restitution value of this PhysicsBody.

PhysicsBody.RigidBodyType getRigidBodyType()

Get the PhysicsBody.RigidBodyType that is set on this PhysicsBody

PhysicsShape getShape()

Get the PhysicsShape that is used to approximate the PhysicsBody.

boolean getUseGravity()

Return true if this PhysicsBody responds to gravity.

Vector getVelocity()

Get the velocity of this PhysicsBody.

boolean isEnabled()

Return true if the physics simulation is enabled for this PhysicsBody.

void setCollisionListener(PhysicsBody.CollisionListener listener)

Set the PhysicsBody.CollisionListener to use for this PhysicsBody.

void setEnabled(boolean enabled)

The enabled flag is used to enable or disable the physics simulation for this PhysicsBody.

void setFriction(float friction)

Set the friction of the PhysicsBody, between 0.0 and 1.0.

void setMass(float mass)

The mass of the PhysicsBody in kilograms.

void setMomentOfInertia(Vector momentOfInertia)

Set the moment of inertia for the PhysicsBody.

void setRestitution(float restitution)

Set the restitution of the PhysicsBody.

void setShape(PhysicsShape shape)

Set the PhysicsShape that should be used to approximate the PhysicsBody during the physics simulation.

void setUseGravity(boolean useGravity)

Set to true to make this PhysicsBody respond to gravity.

void setVelocity(Vector velocity, boolean isConstant)

Set the velocity of this PhysicsBody, independent from all forces.

Public methods

applyForce

void applyForce (Vector force, 
                Vector position)

Apply a constant force to this PhysicsBody. Constant forces can be useful in situations for moving objects in constant motion; for example, when adding thrust to a spaceship being launched into the sky. For each force, you can specify both its magnitude (in newtons) and its location (on the object).

Parameters
force Vector: The magnitude of the force along each principal axis (X, Y, and Z).

position Vector: The location in the PhysicsBody's local coordinate system at which the force is applied.

applyImpulse

void applyImpulse (Vector impulse, 
                Vector position)

Apply an instantaneous force to this PhysicsBody. Impulse forces are useful in situations where you would like to apply an instantaneous burst of force to an object. These are often useful for launching projectile objects.

Parameters
impulse Vector: The magnitude of the impulse along each principal axis (X, Y, and Z).

position Vector: The location in the PhysicsBody's local coordinate system at which the impulse is applied.

applyTorque

void applyTorque (Vector torque)

Apply a constant rotational force to this PhysicsBody. Applying torque to a body changes its angular velocity, rotating it without changing its linear acceleration. The torque vector is specified as a magnitude along each principal axis. Applying a torque of {0.0, 0.0, 1.0} causes the PhysicsBody to spin counterclockwise around the Z-axis.

Parameters
torque Vector: The magnitude of the torque along each principal axis (X, Y, and Z).

applyTorqueImpulse

void applyTorqueImpulse (Vector torque)

Apply an instantaneous rotational force to this PhysicsBody.

Parameters
torque Vector: The magnitude of the torque impulse along each principal axis (X, Y, and Z).

clearForce

void clearForce ()

Clear any constant forces that were applied to this PhysicsBody.

getFriction

float getFriction ()

Get the friction value for this PhysicsBody.

Returns
float The friction, between 0.0 and 1.0.

getMass

float getMass ()

Return the mass of this PhysicsBody in kg.

Returns
float The mass in kg.

getRestitution

float getRestitution ()

Get the restitution value of this PhysicsBody.

Returns
float The restitution value.

getRigidBodyType

PhysicsBody.RigidBodyType getRigidBodyType ()

Get the PhysicsBody.RigidBodyType that is set on this PhysicsBody

Returns
PhysicsBody.RigidBodyType PhysicsBody.RigidBodyType

getShape

PhysicsShape getShape ()

Get the PhysicsShape that is used to approximate the PhysicsBody.

Returns
PhysicsShape The solid volume of the PhysicsBody.

getUseGravity

boolean getUseGravity ()

Return true if this PhysicsBody responds to gravity.

Returns
boolean True if this PhysicsBody uses gravity.

getVelocity

Vector getVelocity ()

Get the velocity of this PhysicsBody. Note this only returns the last applied *constant* velocity of this PhysicsBody, as set by setVelocity(Vector, boolean). It does not return the real-time actual velocity of the PhysicsBody.

Returns
Vector The last applied constant velocity.

isEnabled

boolean isEnabled ()

Return true if the physics simulation is enabled for this PhysicsBody.

Returns
boolean True if enabled.

setCollisionListener

void setCollisionListener (PhysicsBody.CollisionListener listener)

Set the PhysicsBody.CollisionListener to use for this PhysicsBody. The listener receives a callback each time this PhysicsBody collides with another.

Parameters
listener PhysicsBody.CollisionListener: The listener to use. Null to remove the listener.

setEnabled

void setEnabled (boolean enabled)

The enabled flag is used to enable or disable the physics simulation for this PhysicsBody. If false, the Node will not be impacted by the physics simulation.

Parameters
enabled boolean: True to enable the physics simulation.

setFriction

void setFriction (float friction)

Set the friction of the PhysicsBody, between 0.0 and 1.0. Friction determines the body's resistance to sliding motion, effectively simulating the "roughness" of a surface. If two sliding bodies each have a value of 0, then they will slide completely freely against one another; if they both have a value of 1.0, then the bodies will not slide at all against one another.

Defaults to 0.5.

Parameters
friction float: The friction value between 0.0 and 1.0.

setMass

void setMass (float mass)

The mass of the PhysicsBody in kilograms. Determines how the body is impacted by forces and collisions with other bodies. Only used for DYNAMIC bodies.

Parameters
mass float: The mass in kg.

setMomentOfInertia

void setMomentOfInertia (Vector momentOfInertia)

Set the moment of inertia for the PhysicsBody. The moment of inertia determines how the body responds to torque. The given vector indicates the moment of inertia for each axis, X, Y, and Z. By default, the moment of inertia is automatically determined by the shape and mass used.

Parameters
momentOfInertia Vector: The moment of inertia along the X, Y, and Z axes.

setRestitution

void setRestitution (float restitution)

Set the restitution of the PhysicsBody. Restitution controls how "bouncy" an object or surface is. When two objects collide, some of the energy of the collision is used to deform the collided objects, some is used rebound the objects from one another, and some is lost to heat. Restitution is a measure of how much of that kinetic energy is used for objects to rebound from one another. To take a real-world example, a massive bowling bowl has a low coefficient of restitution whereas a basketball has a high coefficient of restitution.

Restitution of 1.0 implies a perfectly elastic collision: the ball will bounce right back up to its initial height. Restitution of 0.0 represents a completely inelastic collision: the ball will stick to the floor after colliding.

Defaults to 0.0.

Parameters
restitution float: The restitution value between 0.0 and 1.0.

setShape

void setShape (PhysicsShape shape)

Set the PhysicsShape that should be used to approximate the PhysicsBody during the physics simulation.

Parameters
shape PhysicsShape: The solid volume of the PhysicsBody.

setUseGravity

void setUseGravity (boolean useGravity)

Set to true to make this PhysicsBody respond to gravity. Gravity globally accelerates physics bodies in a specific direction. It can be set in the PhysicsWorld.

Defaults to true.

Parameters
useGravity boolean: True to use gravity.

setVelocity

void setVelocity (Vector velocity, 
                boolean isConstant)

Set the velocity of this PhysicsBody, independent from all forces. Used to directly move an object without applying a force. Units are m/s. Invoking this method will override any forces that are already applied on this PhysicsBody.

Parameters
velocity Vector: The velocity to set.

isConstant boolean: True if this velocity should be applied every frame. False if this should be an instantaneous application of the velocity for only one frame (e.g. like an impulse).

Hooray!