Unigine::Body Class
Header: | #include <UniginePhysics.h> |
This class is used to simulate physical bodies that allow an object to participate in physical interactions. A body can have one or several collision shapes assigned and can be connected together with joints.
To transform a body, one of the following functions can be used:
All of these functions take effect when physics calculations are over and updatePhysics() is performed. Only after that transformations of the body are applied to the rendered node. If a node needs to be transformed immediately after its physical body, flushTransform() is to be called.The simulation of the body can be frozen (if a setFrozen flag is set).
Bodies interact with each other via joints or contacts. A contact can be handled by any of the bodies that participate in it. To which body a contact is assigned is random. If the contact is assigned to and handled by the body it is called an internal one, otherwise it is called external (handled by another body). The total number of contacts for the body includes all, internal and external ones. Iterating through internal contacts is much faster than through external ones, thus you might want a certain body to handle most of the contacts it participates in. This can be done for a rigid body by raising a priority for it via the setHighPriorityContacts() method.
Within the body contacts are referred to via their numbers, in the range from 0 to the total number of contacts. While globally each contact has an ID to refer to it, this can be used .
You can set callbacks for a body to handle certain events:
- addFrozenCallback() - to perform some actions when a body freezes.
- addPositionCallback() - to perform some actions when a body changes its position.
- addContactEnterCallback() - to perform some actions when a contact emerges (body starts touching another body or collidable surface).
- addContactLeaveCallback() - to perform some actions when a contact ends (body stops touching another body or collidable surface).
- addContactsCallback() - to get all contacts of the body including new ones (enter) and the ending ones (leave). Leave contacts are removed after the callback execution stage, so this is the only point where you can still get them.
See Also#
- The Creating and Attaching a Cloth usage example demonstrating how to create objects, assign bodies, and add shapes to them
- A C++ API sample located in the <UnigineSDK>/source/samples/Api/Physics/BodyCallbacks folder
- A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks folder
- A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/physics/ folder:
- callbacks_00
- callbacks_01
- callbacks_02
- The Handling Contacts on Collision usage example
Body Class
Enums
TYPE#
Type of the body defining its physical properties.Name | Description |
---|---|
BODY_DUMMY = 0 | This body is used to create an immovable collider for an object. |
BODY_RIGID = 1 | This is a basic type of body describing a rigid object. |
BODY_RAGDOLL = 2 | This body contains joints connecting parts of the body (represented with rigid bodies). |
BODY_FRACTURE = 3 | This body simulates breakable objects. |
BODY_ROPE = 4 | This body simulates ropes. |
BODY_CLOTH = 5 | This body simulates cloth. |
BODY_WATER = 6 | This body simulates water and other fluids. |
BODY_PATH = 7 | This body simulates a path along which rigid bodies are moving, for example, like a train along the railtrack. |
NUM_BODIES = 8 | The number of bodies. |
Members
Ptr<Body> createBody ( int type ) #
Creates a new body of the specified type.Arguments
- int type - Body type. One of the BODY_* values.
Return value
New created body smart pointer.Ptr<Body> createBody ( const char * type_name ) #
Creates a new body of the specified type.Arguments
- const char * type_name - Body type name.
Return value
New created body smart pointer.void setID ( int id ) #
Sets the unique ID for the body.Arguments
- int id - Unique ID.
int getID ( ) const#
Returns the unique ID of the body.Return value
Unique ID.Body::TYPE getType ( ) const#
Returns the type of the body.Return value
One of the BODY_* pre-defined variables.const char * getTypeName ( ) const#
Returns the name of the body type.Return value
Type name.const char * getTypeName ( int type ) #
Returns the name of a body type with a given ID.Arguments
- int type - Body type ID. One of the BODY_* values.
Return value
Body type name.bool setObject ( const Ptr<Object> & object, bool update ) #
Sets an object, which the body approximates.Arguments
- const Ptr<Object> & object - Object to approximate.
- bool update - Update flag: 1 to update the object after assigning the body (by default), 0 not to update right after body assignment.
Return value
true if the body is assigned to the specified object successfully; otherwise, false.void setObject ( const Ptr<Object> & val ) #
Sets an object, which the body approximates.Arguments
Ptr<Object> getObject ( ) const#
Returns the object, which is approximated with the body.Return value
Approximated object.void setEnabled ( bool enable ) #
Enables or disables physical interactions with the body.Arguments
- bool enable - 1 to enable physical interactions, 0 to disable them.
bool isEnabled ( ) const#
Returns a value indicating if physical interactions with the body are enabled.Return value
1 if physical interactions with the body are enabled; otherwise, 0.bool isEnabledSelf ( ) const#
Returns a value indicating if the body is enabled.Return value
1 if the body is enabled; otherwise, 0.void setFrozen ( bool frozen ) #
Freezes or unfreezes the body. When a body is frozen, it is not simulated (though its contacts are still calculated), until a collision with a frozen body occurs or some force is applied.Arguments
- bool frozen - 1 to freeze the object, 0 to unfreeze it.
bool isFrozen ( ) const#
Returns a value indicating if the body is frozen. When a body is frozen, it is not simulated (though its contacts are still calculated), until a collision with a frozen body occurs or some force is applied.Return value
1 if the body is frozen; otherwise, 0.void setImmovable ( bool immovable ) #
Sets a value indicating if the body is immovable (static).Arguments
- bool immovable - 1 if the body is immovable (static); otherwise, 0.
bool isImmovable ( ) const#
Return a value indicating if the body is immovable (static).Return value
1 if the body is immovable (static); otherwise, 0.void setGravity ( bool gravity ) #
Sets a value indicating if gravity is affecting the body.Arguments
- bool gravity - 1 if the body is affected by gravity; otherwise, 0.
bool isGravity ( ) const#
Returns a value indicating if gravity is affecting the body.Return value
1 if the body is affected by gravity; otherwise, 0.void setName ( const char * name ) #
Sets the name of the body.Arguments
- const char * name - Name of the body.
const char * getName ( ) const#
Returns the name of the body.Return value
Name of the body.void setPhysicalMask ( int mask ) #
Sets the bit mask for interactions with physicals. Two objects interact, if they both have matching masks.Arguments
- int mask - Integer, each bit of which is a mask.
int getPhysicalMask ( ) const#
Returns the bit mask for interactions with physicals. Two objects interact, if they both have matching masks.Return value
Integer, each bit of which is a mask.void setTransform ( const Math::Mat4 & transform ) #
Sets a transformation matrix for the body (in world coordinates). This method resets body's linear and angular velocities to defaults, sets forces and torques to zeros, nullifies counted down frozen frames. It is called, for example, when the node is dragged to a new position in the editor.Arguments
- const Math::Mat4 & transform - Transformation matrix. This matrix describes position, orientation and scale of the body.
Math::Mat4 getTransform ( ) const#
Returns the transformation matrix of the body (in world coordinates). This matrix describes position and orientation of the body.Return value
Transformation matrix.void setPreserveTransform ( const Math::Mat4 & transform ) #
Sets a transformation matrix for the body (in world coordinates). This method safely preserves body's linear and angular velocities. It changes only body coordinates - all other body parameters stay the same.Arguments
- const Math::Mat4 & transform - Transformation matrix. This matrix describes position, orientation and scale of the body.
void setVelocityTransform ( const Math::Mat4 & transform ) #
Sets a transformation matrix (in world coordinates) and computes linear and angular velocities of the body depending on its trajectory from the current position to the specified one. The time used in calculations corresponds to physics ticks. It clears forces and torques to zeros and nullifies counted down frozen frames.Arguments
- const Math::Mat4 & transform - Transformation matrix. This matrix describes position, orientation and scale of the body.
void flushTransform ( ) const#
Forces to set the transformations of the body for the node.void setPosition ( const Math::Vec3 & pos ) #
Updates the body position (in world coordinates). Body's linear and angular velocities will be reset to 0.Arguments
- const Math::Vec3 & pos - New position in the world coordinates.
Math::Vec3 getPosition ( ) const#
Returns the body position (in world coordinates).Return value
The body position in the world coordinates.void setRotation ( const Math::quat & rot ) #
Updates the body rotation (in world coordinates).Arguments
- const Math::quat & rot - New rotation in the world coordinates.
Math::quat getRotation ( ) const#
Returns the body rotation (in world coordinates).Return value
The body rotation in the world coordinates.void setDirection ( const Math::vec3 & dir, const Math::vec3 & up ) #
Updates the direction vector of the body (in world coordinates). By default, a direction vector points along -Z axis. This function changes its direction and reorients the body.Arguments
- const Math::vec3 & dir - New direction vector in the world coordinates. The direction vector always has unit length.
- const Math::vec3 & up - New up vector in the world coordinates.
Math::vec3 getDirection ( ) const#
Returns the normalized direction vector of the body (in world coordinates). By default, a direction vector points along -Z axis. It always has an unit length.Return value
Normalized direction vector in the world coordinates.Ptr<Body> getParent ( ) const#
Returns the parent of the body.Return value
Parent body.int isChild ( const Ptr<Body> & body ) const#
Checks if a given body is a child of the body.Arguments
- const Ptr<Body> & body - Body to check.
Return value
1 if the provided body is a child; otherwise, 0.int getNumChildren ( ) const#
Returns the number of child bodies.Return value
Number of children.int findChild ( const char * name ) const#
Searches for a child body with a given name.Arguments
- const char * name - Name of the child body.
Return value
Number of the child in the list of children, if it is found; otherwise, -1.Ptr<Body> getChild ( int num ) const#
Returns a given child body.Arguments
- int num - Child number.
Return value
Corresponding body.void addShape ( const Ptr<Shape> & shape, const Math::mat4 & transform ) #
Adds a shape to the list of shapes comprising the body.Arguments
- const Ptr<Shape> & shape - New shape to add.
- const Math::mat4 & transform - Shape transformation matrix (in the body's coordinate system).
void addShape ( const Ptr<Shape> & shape ) #
Adds a shape to the list of shapes comprising the body.Arguments
void removeShape ( const Ptr<Shape> & shape, bool destroy = 0 ) #
Removes a given shape from the body.Arguments
- const Ptr<Shape> & shape - Shape to be removed.
- bool destroy - Flag indicating whether the shape is to be destroyed after removal: use 1 to destroy the shape after removal, or 0 if you plan to use the shape later. The default value is 0.
void removeShape ( int num, bool destroy = 0 ) #
Removes a shape with a given number from the body.Arguments
- int num - Shape number.
- bool destroy - Flag indicating whether the shape is to be destroyed after removal: use 1 to destroy the shape after removal, or 0 if you plan to use the shape later. The default value is 0.
void clearShapes ( int destroy = 0 ) #
Clears all shapes from the body.Arguments
- int destroy - Flag indicating whether shapes are to be destroyed after removal: use 1 to destroy shapes after removal, or 0 if you plan to use them later. The default value is 0.
int isShape ( const Ptr<Shape> & shape ) const#
Checks if a given shape belongs to the body.Arguments
Return value
1 if the shape belongs to the body; otherwise, 0.bool insertShape ( int pos, const Ptr<Shape> & shape ) #
Inserts a given shape at the specified position in the list of body's shapes.Arguments
- int pos - Position in the list at which the shape is to be inserted in the range from 0 to the number of shapes.
- const Ptr<Shape> & shape - Shape to be inserted.
Return value
1 if a shape was successfully inserted; otherwise, 0.bool insertShape ( int pos, const Ptr<Shape> & shape, const Math::mat4 & transform ) #
Inserts a given shape at the specified position in the list of body's shapes and sets the specified transformation for it.Arguments
- int pos - Position in the list at which the shape is to be inserted in the range from 0 to the number of shapes.
- const Ptr<Shape> & shape - Shape to be inserted.
- const Math::mat4 & transform - Shape's transformation (in the body's coordinate system).
Return value
1 if a shape was successfully inserted; otherwise, 0.int getNumShapes ( ) const#
Returns the number of shapes comprising the body.Return value
Number of shapes.int findShape ( const char * name ) const#
Searches for a shape with a given name.Arguments
- const char * name - Name of the shape.
Return value
Number of the shape in the list of shapes, if it is found; otherwise, -1.Ptr<Shape> getShape ( int num ) const#
Returns a given shape.Arguments
- int num - Shape number.
Return value
Corresponding shape object.void setShapeTransform ( int num, const Math::mat4 & transform ) #
Sets a transformation matrix for a given shape (in local coordinates). This matrix describes position and orientation of the shape.Arguments
- int num - Shape number.
- const Math::mat4 & transform - Transformation matrix (in the body's coordinate system).
Math::mat4 getShapeTransform ( int num ) const#
Returns the transformation matrix of a given shape (in local coordinates). This matrix describes position and orientation of the shape.Arguments
- int num - Shape number.
Return value
Transformation matrix.void updateShapes ( ) #
Updates all shapes of the body.void addJoint ( const Ptr<Joint> & joint ) #
Adds a joint to the body.Arguments
void removeJoint ( const Ptr<Joint> & joint ) #
Removes a given joint from the body.Arguments
void removeJoint ( int num ) #
Removes a joint with a given number from the body.Arguments
- int num - Joint number.
void insertJoint ( const Ptr<Joint> & joint, int num ) #
Inserts a given joint at the specified position in the list of body's joints.Arguments
- const Ptr<Joint> & joint - Joint to be inserted.
- int num - Position in the list at which the joint is to be inserted in the range from 0 to the number of joints.
int isJoint ( const Ptr<Joint> & joint ) const#
Checks if a given joint belongs to the body.Arguments
Return value
1 if the joint belongs to the body; otherwise, 0.int getNumJoints ( ) const#
Returns the number of joints in the body.Return value
Number of joints.int findJoint ( const char * name ) const#
Searches for a joint with a given name.Arguments
- const char * name - Name of the joint.
Return value
Number of the joint in the list of joints, if it is found; otherwise, -1.Ptr<Joint> getJoint ( int num ) const#
Returns a given joint.Arguments
- int num - Joint number.
Return value
Corresponding joint.Ptr<Shape> getIntersection ( const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, Math::Vec3 * ret_point, Math::vec3 * ret_normal ) #
Performs tracing from the p0 point to the p1 point to find a body shape intersected by this line. Intersection is found only for objects with a matching intersection mask. On success ret_point and ret_normal shall contain information about intersection.
Arguments
- const Math::Vec3 & p0 - Start point of the line (in world coordinates).
- const Math::Vec3 & p1 - End point of the line (in world coordinates).
- int mask - Intersection mask.
- Math::Vec3 * ret_point - Container to which contact point coordinates (if any) shall be put (in world coordinate system).
- Math::vec3 * ret_normal - Container to which contact point normal coordinates (if any) shall be put (in world coordinate system).
Return value
First intersected shape, if found; otherwise, 0.int getNumContacts ( ) const#
Returns the total number of contacts in which the body participates. It includes internal (handled by the body) and external contacts (handled by other bodies).Return value
Number of contacts.unsigned long long getContactID ( int num ) const#
Returns the contact ID by the contact number.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Contact ID.int findContactByID ( unsigned long long id ) const#
Returns the number of the contact by its ID.Arguments
- unsigned long long id - Contact ID.
Return value
Number of the contact with the specified ID if it exists, otherwise -1.bool isContactInternal ( int num ) const#
Returns a value indicating whether the contact with the specified number is internal (handled by the body) or not (handled by another body). A contact can be handled by any of the bodies that participate in it. To which body a contact is assigned is random. If the contact is assigned to and handled by the body it is called an internal one, otherwise it is called external (handled by another body). The total number of contacts for the body includes all, internal and external ones. Iterating through internal contacts is much faster than through external ones, thus you might want a certain body to handle most of the contacts it participates in. This can be done for a rigid body by raising a priority for the body via the setHighPriorityContacts() method.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
true if the contact contact with the specified number is internal; otherwise false.bool isContactEnter ( int num ) const#
Returns a value indicating if the body has begun touching another body at the contact point with the specified number (the contact has just occurred).Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
true if the body has begun touching another body at the contact point with the specified number (the contact has just occurred); otherwise false.bool isContactLeave ( int num ) const#
Returns a value indicating if the body has stopped touching another body at the contact point with the specified number.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
true if the body has stopped touching another body at the contact point with the specified number; otherwise false.bool isContactStay ( int num ) const#
Returns a value indicating if the body keeps touching another body at the contact point with the specified number (the contact lasts).Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
true if the body keeps touching another body at the contact point with the specified number (the contact lasts); otherwise false.Math::Vec3 getContactPoint ( int num ) const#
Returns world coordinates of the contact point.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Contact point (in world coordinates).Math::vec3 getContactNormal ( int num ) const#
Returns a normal of the contact point, in world coordinates.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Contact normal (in world coordinates).Math::vec3 getContactVelocity ( int num ) const#
Returns relative velocity at the given contact point.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Velocity vector.float getContactImpulse ( int num ) const#
Returns the relative impulse at the given contact point.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Impulse value.float getContactTime ( int num ) const#
Returns the time when the given contact occurs. By CCD (for spheres or capsules), it returns the time starting from the current physics simulation tick to the moment when the calculated contact is bound to happen. By non-continuous collision detection, 0 is always returned.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Time of the calculated contact to happen, in seconds.float getContactDepth ( int num ) const#
Returns the depth by which the body penetrated with an obstacle by the given contact. This distance is measured along the contact normal.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Penetration depth, in units.float getContactFriction ( int num ) const#
Returns relative friction at the given contact point.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Friction value.float getContactRestitution ( int num ) const#
Returns relative restitution at the given contact point.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Restitution.Ptr<Body> getContactBody0 ( int num ) const#
Returns the first body participating in a given contact. This is not necessarily the current body.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
First body.Ptr<Body> getContactBody1 ( int num ) const#
Returns the second body participating in a given contact. This is not necessarily the current body.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Second body.Ptr<Shape> getContactShape0 ( int num ) const#
Returns the first shape participating in a given contact. This shape does not necessarily belong to the current body.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
First shape.Ptr<Shape> getContactShape1 ( int num ) const#
Returns the second shape participating in a given contact. This shape does not necessarily belong to the current body.Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Second shape.Ptr<Object> getContactObject ( int num ) const#
Returns an object participating in the contact (used for collisions with non-physical object).Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Object in contact.int getContactSurface ( int num ) const#
Returns the surface of the current object, which is in contact (used for collisions with non-physical object).Arguments
- int num - Contact number in the range from 0 to the total number of contacts.
Return value
Surface number.void * addFrozenCallback ( Unigine::CallbackBase1< Ptr<Body> > * func ) #
Adds a callback function to be called when a given body freezes. The signature of the frozen callback function must be as follows:void frozen_callback_function_name(BodyPtr body);
You can set a callback function as follows:
addFrozenCallback(MakeCallback(frozen_callback_function_name));
Example: Setting a body frozen callback function for a certain class:
class SomeClass {
/*...*/
// body for which a frozen callback function is to be set
Unigine::BodyPtr body;
/*...*/
};
/*...*/
// callback function
void SomeClass::on_freezing(BodyPtr body)
{
// insert your code handling freezing here
}
void SomeClass::registerCallback()
{
// setting the on_freezing() function to handle freezing for the body
body->addFrozenCallback(MakeCallback(this, &SomeClass::on_freezing));
}
Arguments
- Unigine::CallbackBase1< Ptr<Body> > * func - Callback pointer.
Return value
ID of the last added frozen callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeFrozenCallback ( void * id ) #
Removes the specified callback from the list of frozen callbacks.Arguments
- void * id - Frozen callback ID obtained when adding it.
Return value
True if the frozen callback with the given ID was removed successfully; otherwise false.void clearFrozenCallbacks ( ) #
Clears all added frozen callbacks.void * addPositionCallback ( Unigine::CallbackBase1< Ptr<Body> > * func ) #
Adds a callback function to be called when a given body moves a certain distance (rotation is not taken into account). The signature of the position callback function must be as follows:void position_callback_function_name(BodyPtr body);
You can set a callback function as follows:
addPositionCallback(MakeCallback(position_callback_function_name));
Example: Setting a body position callback function for a certain class:
class SomeClass {
/*...*/
// body for which a position callback function is to be set
Unigine::BodyPtr body;
/*...*/
};
/*...*/
// callback function
void SomeClass::on_position(BodyPtr body)
{
// insert your code handling position changes here
}
void SomeClass::registerCallback()
{
// setting the on_position() function to handle position changes for the body
body->addPositionCallback(MakeCallback(this, &SomeClass::on_position));
}
Arguments
- Unigine::CallbackBase1< Ptr<Body> > * func - Callback pointer.
Return value
ID of the last added position callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removePositionCallback ( void * id ) #
Removes the specified callback from the list of position callbacks.Arguments
- void * id - Position callback ID obtained when adding it.
Return value
True if the position callback with the given ID was removed successfully; otherwise false.void clearPositionCallbacks ( ) #
Clears all added position callbacks.void * addContactsCallback ( Unigine::CallbackBase1< Ptr<Body> > * func ) #
Adds a callback function to be called after adding new contacts and before removing the ones that cease to exist. This callback can be used to get all contacts of the body including new ones (enter) and the ending ones (leave). Leave contacts are removed after the callback execution stage, so this is the only point where you can still get them. The signature of the contacts callback function must be as follows:void contacts_callback_function_name(BodyPtr body);
You can set a callback function as follows:
addContactsCallback(MakeCallback(contacts_callback_function_name));
Example: Setting a body contacts callback function for a certain class:
class SomeClass {
/*...*/
// body for which a contacts callback function is to be set
Unigine::BodyPtr body;
/*...*/
};
/*...*/
// callback function
void SomeClass::on_contacts(BodyPtr body)
{
// insert your code handling contacts here
}
void SomeClass::registerCallback()
{
// setting the on_contacts() function to handle contacts for the body
body->addContactsCallback(MakeCallback(this, &SomeClass::on_contacts));
}
Arguments
- Unigine::CallbackBase1< Ptr<Body> > * func - Callback pointer.
Return value
ID of the last added contact callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeContactsCallback ( void * id ) #
Removes the specified callback from the list of contacts callbacks. This callback is called after adding new contacts and before removing the ones that cease to exist. It can be used to get all contacts of the body including new ones (enter) and the ending ones (leave). Leave contacts are removed after the callback execution stage, so this is the only point where you can still get them.Arguments
- void * id - Contact callback ID obtained when adding it.
Return value
true if the contacts callback with the given ID was removed successfully; otherwise false.void clearContactsCallbacks ( ) #
Clears all added contacts callbacks. This callback is called after adding new contacts and before removing the ones that cease to exist. It can be used to get all contacts of the body including new ones (enter) and the ending ones (leave). Leave contacts are removed after the callback execution stage, so this is the only point where you can still get them.void * addContactEnterCallback ( Unigine::CallbackBase2< Ptr<Body>, int> * func ) #
Adds a callback function to be called when a contact with the body emerges (the body begins touching another body). The signature of the callback function must be as follows:void contact_enter_callback_function_name(BodyPtr body, int num); // num is the contact ID
You can set a callback function as follows:
addContactEnterCallback(MakeCallback(contact_enter_callback_function_name));
Example: Setting a body contact enter callback function for a certain class:
class SomeClass {
/*...*/
// body for which a contact enter callback function is to be set
Unigine::BodyPtr body;
/*...*/
};
/*...*/
// callback function (num is the contact ID)
void SomeClass::on_contact_enter(BodyPtr body, int num)
{
// insert your code handling contacts here
}
void SomeClass::registerCallback()
{
// setting the on_contact_enter() function to handle emerging contacts for the body
body->addContactEnterCallback(MakeCallback(this, &SomeClass::on_contact_enter));
}
Arguments
- Unigine::CallbackBase2< Ptr<Body>, int> * func - Callback pointer.
Return value
ID of the last added contact enter callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeContactEnterCallback ( void * id ) #
Removes the specified callback from the list of contact enter callbacks. These callbacks are called when a contact with the body emerges (the body begins touching another body).Arguments
- void * id - Contact callback ID obtained when adding it.
Return value
true if the contact enter callback with the given ID was removed successfully; otherwise false.void clearContactEnterCallbacks ( ) #
Clears all added contact enter callbacks. These callbacks are called when a contact with the body emerges (the body begins touching another body).void * addContactLeaveCallback ( Unigine::CallbackBase2< Ptr<Body>, int> * func ) #
Adds a callback function to be called when a contact with the body ends (the body stops touching another body). The signature of the callback function must be as follows:void contact_leave_callback_function_name(BodyPtr body, int num); // num is the contact ID
You can set a callback function as follows:
addContactLeaveCallback(MakeCallback(contact_leave_callback_function_name));
Example: Setting a body contact leave callback function for a certain class:
class SomeClass {
/*...*/
// body for which a contact leave callback function is to be set
Unigine::BodyPtr body;
/*...*/
};
/*...*/
// callback function (num is the contact ID)
void SomeClass::on_contact_leave(BodyPtr body, int num)
{
// insert your code handling contacts here
}
void SomeClass::registerCallback()
{
// setting the on_contact_leave() function to handle ending contacts for the body
body->addContactLeaveCallback(MakeCallback(this, &SomeClass::on_contact_leave));
}
Arguments
- Unigine::CallbackBase2< Ptr<Body>, int> * func - Callback pointer.
Return value
ID of the last added contact leave callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeContactLeaveCallback ( void * id ) #
Removes the specified callback from the list of contact leave callbacks. These callbacks are called when a contact with the body ends (the body stops touching another body).Arguments
- void * id - Contact callback ID obtained when adding it.
Return value
true if the contact leave callback with the given ID was removed successfully; otherwise false.void clearContactLeaveCallbacks ( ) #
Clears all added contact leave callbacks. These callbacks are called when a contact with the body ends (the body stops touching another body).void renderContacts ( ) #
Renders all contact points of the body including internal and external ones (handled by other bodies).void renderExternalContacts ( ) #
Renders all external contacts of the body (handled by other bodies).void renderInternalContacts ( ) #
Renders all internal contacts of the body (handled by it).void renderJoints ( ) #
Renders joints to which the body is connected.void renderShapes ( ) #
Renders shapes comprising the body.void renderVisualizer ( ) #
Renders shapes, joints and contact points of the body.Ptr<Body> clone ( const Ptr<Object> & object ) const#
Clones the body and assigns a copy to a given object.Arguments
Return value
Copy of the body.void swap ( const Ptr<Body> & body ) const#
Swaps the bodies saving the pointers.Arguments
- const Ptr<Body> & body - Body to swap.
bool saveState ( const Ptr<Stream> & stream ) const#
Saves the state of a given body into a binary stream.Example using saveState() and restoreState() methods:
// set the body state
body->setPosition(vec3(1, 1, 0));
// save state
BlobPtr blob_state = Blob::create();
body->saveState(blob_state);
// change the state
body->setPosition(vec3(0, 0, 0));
// restore state
blob_state->seekSet(0); // returning the carriage to the start of the blob
body->restoreState(blob_state);
Arguments
Return value
true if the body state is successfully saved; otherwise, false.bool restoreState ( const Ptr<Stream> & stream ) #
Restores the state of a given body from a binary stream.Example using saveState() and restoreState() methods:
// set the body state
body->setPosition(vec3(1, 1, 0));
// save state
BlobPtr blob_state = Blob::create();
body->saveState(blob_state);
// change the state
body->setPosition(vec3(0, 0, 0));
// restore state
blob_state->seekSet(0); // returning the carriage to the start of the blob
body->restoreState(blob_state);