Unigine::PhysicalTrigger Class
Header: | #include <UniginePhysicals.h> |
Inherits: | Physical |
Physical triggers fire callbacks when a physical object gets inside or outside of them. To be detected by the trigger, physical objects are required to have at the same time both:
- Bodies (with matching Physical Mask)
For BodyDummy to trigger PhysicalTrigger, you need to call updateContacts() first.
- Shapes (with matching Collision mask)
To force update of the physical trigger updateContacts() can be called. After that, you can access all updated data about the contacts in the same frame. However, callback functions will still be executed only when the next engine function is called: that is, before flush() (in the current frame), or before the update() (in the next frame) — whatever comes first.
See Also#
- A C++ API sample located in the <UnigineSDK>/source/samples/Api/Nodes/PhysicalTrigger folder
- A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Nodes/PhysicalTrigger folder
- A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/physicals/ folder:
- trigger_00
- trigger_01
- trigger_02
Usage Example#
In this example a physical trigger and two boxes, each with a body and a shape, are created. When a box with matching physical mask enters the physical trigger the trigger_enter() function is called, when it leaves the trigger - the trigger_leave() function is called.
In the AppWorldLogic.h file let us add the following code:
// AppWorldLogic.h
#include <UniginePhysics.h>
#include <UniginePhysicals.h>
#include <UnigineConsole.h>
#include <UnigineGame.h>
/* ... */
class AppWorldLogic : public Unigine::WorldLogic {
public:
/* .. */
private:
/* .. */
// pointer to the physical trigger
Unigine::PhysicalTriggerPtr trigger;
// pointers to physical objects
Unigine::ObjectMeshDynamicPtr box1;
Unigine::ObjectMeshDynamicPtr box2;
/* .. */
};
In the AppWorldLogic.cpp file let us add the following code:
// AppWorldLogic.cpp
/* .. */
using namespace Unigine;
using namespace Math;
// callback function to be fired when a physical object enters the trigger
void trigger_enter(BodyPtr body)
{
// trying to get an object from the body
ObjectPtr obj = body->getObject();
if (!obj)
return;
// enabling material emission for all object's surfaces
for (int i = 0; i < obj->getNumSurfaces(); i++)
obj->setMaterialState("emission", 1, i);
// displaying the name of the object entering trigger area
Log::message("\n %s has entered the trigger area!", body->getObject()->getName());
}
// callback function to be fired when a physical object leaves the trigger
void trigger_leave(BodyPtr body)
{
// trying to get an object from the body
ObjectPtr obj = body->getObject();
if (!obj)
return;
// disabling material emission for all object's surfaces
for (int i = 0; i < obj->getNumSurfaces(); i++)
obj->setMaterialState("emission", 0, i);
// displaying the name of the object leaving trigger area
Log::message("\n %s has left the trigger area!", body->getObject()->getName());
}
/// function, creating a named box having a specified size, color and transformation with a body and a shape
ObjectMeshDynamicPtr createBodyBox(char *name, vec3 size, float mass, vec4 color, Mat4 transform, int physical_mask)
{
// creating geometry and setting up its parameters (name, material and transformation)
ObjectMeshDynamicPtr OMD = Primitives::createBox(size);
OMD->setWorldTransform(transform);
OMD->setMaterial("mesh_base", "*");
OMD->setMaterialParameter("albedo_color", color, 0);
OMD->setName(name);
// adding physics, i.e. a rigid body and a box shape with specified mass
BodyRigidPtr body = BodyRigid::create(OMD->getObject());
body->addShape(ShapeBox::create(size)->getShape(), translate(vec3(0.0f)));
OMD->getBody()->getShape(0)->setMass(mass);
// setting the physical mask of the body
body->setPhysicalMask(physical_mask);
// passing the node to the Editor
Editor::get()->addNode(OMD->getNode());
OMD->release();
return OMD;
}
/* .. */
int AppWorldLogic::init() {
//enabling visualizer to render bounds of the physical trigger
Console::get()->run("show_visualizer 1");
// creating a physical trigger and passing it to the Editor
trigger = PhysicalTrigger::create(3,vec3(2.0f, 2.0f, 1.0f));
trigger->release();
Editor::get()->addNode(trigger->getNode());
// setting trigger's position
trigger->setPosition(Vec3(0.0f, 0.0f, 1.0f));
// setting trigger's physical mask equal to 1
trigger->setPhysicalMask(1);
// retrieving trigger size
vec3 size = trigger->getSize();
// displaying trigger size and shape type
Log::message("\n Trigger parameters size(%f, %f ,%f) type: %d", size.x, size.y, size.z, trigger->getShapeType());
// adding trigger enter callback function
trigger->addEnterCallback(MakeCallback(&trigger_enter));
// adding trigger leave callback function
trigger->addLeaveCallback(MakeCallback(&trigger_leave));
// creating a box with a body and physical mask value equal to 2 to be ignored by the trigger
box1 = createBodyBox("Box1", vec3(0.2f), 5.0f, vec4(1.0f, 0.0f, 0.0f, 1.0f), translate(Vec3(0.0f, 0.0f, 2.22f)),2);
// creating a box with a body and physical mask value equal to 1 to affect the trigger
box2 = createBodyBox("Box2", vec3(0.2f), 0.0f, vec4(1.0f, 1.0f, 0.0f, 1.0f), translate(Vec3(3.5f, 0.0f, 1.2f)),1);
// displaying physical masks of both boxes and the trigger
Log::message("\n Box1 Physical mask: %d", box1->getBody()->getPhysicalMask());
Log::message("\n Box2 Physical mask: %d", box2->getBody()->getPhysicalMask());
Log::message("\n Trigger Physical mask: %d", trigger->getPhysicalMask());
return 1;
}
int AppWorldLogic::update() {
// showing the bounds of the physical trigger
trigger->renderVisualizer();
// changing the position of the second box
box2->setWorldPosition(box2->getWorldPosition() - Vec3(0.5f * Game::get()->getIFps(), 0.0f, 0.0f));
return 1;
}
int AppWorldLogic::flush() {
// updating information on trigger contacts
trigger->updateContacts();
return 1;
}
int AppWorldLogic::shutdown() {
// clearing trigger callbacks
trigger->clearEnterCallbacks();
trigger->clearLeaveCallbacks();
return 1;
}
/* .. */
PhysicalTrigger Class
Members
static PhysicalTriggerPtr create ( int type, const Math::vec3 & size )
Constructor. Creates a physical trigger of the specified shape and size.Arguments
- int type - Shape of the physical trigger:
- 0 = Sphere
- 1 = Capsule
- 2 = Cylinder
- 3 = Box
- const Math::vec3 & size - Size of the physical trigger:
- Radius, in case of a sphere
- Radius and height, in case of a capsule or a cylinder
- Dimensions, in case of the box
Ptr<PhysicalTrigger> cast( const Ptr<Node> & node )
Casts a PhysicalTrigger out of the Node instance.Arguments
- const Ptr<Node> & node - Pointer to Node.
Return value
Pointer to PhysicalTrigger.Ptr<PhysicalTrigger> cast( const Ptr<Physical> & base )
Casts a PhysicalTrigger out of the Physical instance.Arguments
- const Ptr<Physical> & base - Pointer to Physical.
Return value
Pointer to PhysicalTrigger.Ptr<Body> getBody( int num )
Returns the specified body with which a physical trigger intersects.Arguments
- int num - Body number.
Return value
Intersected body.void setCollisionMask( int mask )
Sets the collision bit mask for the trigger:- the trigger will be activated if the entered body will have a matching physical mask and at the same time its shape will have a matching collision mask.
Arguments
- int mask - Integer, each bit of which is a mask.
int getCollisionMask( )
Sets the collision bit mask for the trigger:- the trigger will be activated if the entered body will have a matching physical mask and at the same time its shape will have a matching collision mask.
Return value
Integer, each bit of which is a mask.float getContactDepth( int contact )
Returns penetration depth by the given contact.Arguments
- int contact - Contact number.
Return value
Penetration depth.Math::vec3 getContactNormal( int contact )
Returns a normal of the contact point, in world coordinates.Arguments
- int contact - Contact number.
Return value
Normal of the contact point.Ptr<Object> getContactObject( int contact )
Returns an object participating in the contact with a physical trigger.Arguments
- int contact - Contact number.
Return value
Object in contact.Math::Vec3 getContactPoint( int contact )
Returns world coordinates of the contact point.Arguments
- int contact - Contact number.
Return value
Contact point.Ptr<Shape> getContactShape( int contact )
Returns a shape that collided with a physical trigger.Arguments
- int contact - Contact number.
Return value
Shape in contact.int getContactSurface( int contact )
Returns the surface of the current object, which is in contact.Arguments
- int contact - Contact number.
Return value
Surface number.void * addEnterCallback( Unigine::CallbackBase1< Ptr<Body> > * func )
Sets the callback function to be fired on entering the physical trigger.Adds a callback function to be fired when a body enters the physical trigger. The callback function must receive a Body as its first argument. In addition, it can also take 2 arguments of any type.// implement the enter callback
void AppWorldLogic::enter_callback(BodyPtr body){
Log::message("\nA body named %s has entered the trigger\n", body->getName());
}
PhysicalTriggerPtr trigger;
int AppWorldLogic::init() {
// create a box-type physical trigger
trigger = PhysicalTrigger::create(3, Math::vec3(3.0f));
// add the enter callback to be fired when a body enters the physical trigger
trigger->addEnterCallback(MakeCallback(this, &AppWorldLogic::enter_callback));
return 1;
}
Arguments
- Unigine::CallbackBase1< Ptr<Body> > * func - Callback pointer.
Return value
ID of the last added enter callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeEnterCallback( void * id )
Removes the specified callback from the list of enter callbacks.Arguments
- void * id - Enter callback ID obtained when adding it.
Return value
True if the enter callback with the given ID was removed successfully; otherwise false.void clearEnterCallbacks( )
Clears all added enter callbacks.void setEnterCallbackName( const char * name )
Sets a UnigineScript callback function to be fired on entering the physical trigger.- Unlike setEnterCallback() , this callback function accepts a body that entered the physical trigger and the physical trigger itself as arguments.
Arguments
- const char * name - Name of the UnigineScript callback function.
const char * getEnterCallbackName( )
Returns the name of the UnigineScript callback function fired on entering the physical trigger. This callback function is set via setEnterCallbackName() .Return value
Name of the UnigineScript callback function.void setExclusionMask( int mask )
Sets an bit mask to prevent detecting collisions with shapes and bodies. This mask is independent of the collision mask. To avoid detecting collisions by a physical trigger for bodies and shapes with matching collision masks, at least one bit in exclusion masks should match. 0 is to collide with all bodies and shapes with a matching collision mask.Arguments
- int mask - Integer, each bit of which is a mask.
int getExclusionMask( )
Returns the bit mask that prevent detecting collisions with shapes and bodies. This mask is independent of the collision mask. To avoid detecting collisions by a physical trigger for bodies and shapes with matching collision masks, at least one bit in exclusion masks should match.Return value
Integer, each bit of which is a mask.void * addLeaveCallback( Unigine::CallbackBase1< Ptr<Body> > * func )
Adds a callback function to be fired when a body leaves the physical trigger. The callback function must receive a Body as its first argument. In addition, it can also take 2 arguments of any type.// implement the leave callback
void AppWorldLogic::leave_callback(BodyPtr body){
Log::message("\nA body named %s has left the trigger\n", body->getName());
}
PhysicalTriggerPtr trigger;
int AppWorldLogic::init() {
// create a box-type physical trigger
trigger = PhysicalTrigger::create(3, Math::vec3(3.0f));
// add the leave callback to be fired when a body leaves the physical trigger
trigger->addLeaveCallback(MakeCallback(this, &AppWorldLogic::leave_callback));
return 1;
}
Arguments
- Unigine::CallbackBase1< Ptr<Body> > * func - Callback pointer.
Return value
ID of the last added leave callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeLeaveCallback( void * id )
Removes the specified callback from the list of leave callbacks.Arguments
- void * id - Leave callback ID obtained when adding it.
Return value
True if the leave callback with the given ID was removed successfully; otherwise false.void clearLeaveCallbacks( )
Clears all added leave callbacks.void setLeaveCallbackName( const char * name )
Sets the name of a UnigineScript callback function to be fired on leaving the physical trigger.- Unlike setLeaveCallback() , this callback function accepts a body that left the physical trigger and physical trigger itself as arguments.
Arguments
- const char * name - Name of the UnigineScript callback function.
const char * getLeaveCallbackName( )
Returns the name of the UnigineScript callback function fired on leaving the physical trigger. This callback function is set via setLeaveCallbackName() .Return value
Name of the UnigineScript callback function.int getNumBodies( )
Returns the total number of bodies intersecting with the physical trigger.Return value
Number of bodies.int getNumContacts( )
Returns the total number of contacts with bodies, shapes and colliding surfaces in which a physical trigger participated.Return value
Number of contacts.void setShapeType( int type )
Sets the shape type of the physical trigger.Arguments
- int type - Shape type of the physical trigger:
- 0 - Sphere
- 1 - Capsule
- 2 - Cylinder
- 3 - Box
int getShapeType( )
Returns the shape type of the physical trigger.Return value
Shape type of the physical trigger:- 0 - Sphere
- 1 - Capsule
- 2 - Cylinder
- 3 - Box
void setSize( const Math::vec3 & size )
Sets the size of the physical trigger.Arguments
- const Math::vec3 & size - New size of the physical trigger:
- Radius, in case of a sphere (pass the radius in the first element of the vector).
- Radius and height, in case of a capsule or a cylinder (pass the radius as the first vector element and the height as the second element).
- Dimensions along the X, Y and Z axes, in case of the box.
Math::vec3 getSize( )
Returns the current size of the physical trigger:- Radius, in case of a sphere (pass the radius in the first element of the vector).
- Radius and height, in case of a capsule or a cylinder (pass the radius as the first vector element and the height as the second element).
- Dimensions along the X, Y and Z axes, in case of the box.