Unigine::PhysicalTrigger Class
Header: | #include <UniginePhysicals.h> |
Inherits from: | Physical |
Physical triggers trigger events 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, handler functions will still be executed only when the next engine function is called: that is, before updatePhysics() (in the current frame), or before the update() (in the next frame) — whatever comes first.
See Also#
- A video tutorial on How To Use Physical Triggers to Catch Physical Objects
- An article on Event Handling
- 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:
#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;
// buffer to store subscriptions for trigger events
Unigine::EventConnections connections;
// pointers to physical objects
Unigine::ObjectMeshDynamicPtr box1;
Unigine::ObjectMeshDynamicPtr box2;
};
In the AppWorldLogic.cpp file let us add the following code:
#include "AppWorldLogic.h"
using namespace Unigine;
using namespace Math;
// handler function to be executed when a physical object enters the trigger
void trigger_enter(const 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());
}
// handler function to be executed when a physical object leaves the trigger
void trigger_leave(const 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(const 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->setMaterialParameterFloat4("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);
body->addShape(ShapeBox::create(size), translate(vec3(0.0f)));
OMD->getBody()->getShape(0)->setMass(mass);
// setting the physical mask of the body
body->setPhysicalMask(physical_mask);
return OMD;
}
int AppWorldLogic::init()
{
//enabling visualizer to render bounds of the physical trigger
Console::run("show_visualizer 1");
// creating a physical trigger
trigger = PhysicalTrigger::create(Shape::SHAPE_BOX, vec3(2.0f, 2.0f, 1.0f));
// 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());
// subscribing for the trigger enter event
trigger->getEventEnter().connect(connections, trigger_enter);
// subscribing for the trigger leave event
trigger->getEventLeave().connect(connections, 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::getIFps(), 0.0f, 0.0f));
return 1;
}
int AppWorldLogic::updatePhysics()
{
// updating information on trigger contacts
trigger->updateContacts();
return 1;
}
int AppWorldLogic::shutdown()
{
// removing subscriptions to all trigger events
connections.disconnectAll();
return 1;
}
PhysicalTrigger Class
Members
void setSize ( ) #
Arguments
- size - The 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.
getSize() const#
Return value
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.
void setShapeType ( int type ) #
Arguments
- int type - The shape type of the physical trigger:
- 0 - Sphere
- 1 - Capsule
- 2 - Cylinder
- 3 - Box
int getShapeType() const#
Return value
Current shape type of the physical trigger:- 0 - Sphere
- 1 - Capsule
- 2 - Cylinder
- 3 - Box
int getNumContacts() const#
Return value
Current number of contacts.int getNumBodies() const#
Return value
Current number of bodies.void setLeaveCallbackName ( const char * name ) #
Arguments
- const char * name - The name of the callback function.
const char * getLeaveCallbackName() const#
Return value
Current name of the callback function.void setExclusionMask ( int mask ) #
Arguments
- int mask - The integer, each bit of which is a mask.
int getExclusionMask() const#
Return value
Current integer, each bit of which is a mask.void setEnterCallbackName ( const char * name ) #
Arguments
- const char * name - The name of the callback function.
const char * getEnterCallbackName() const#
Return value
Current name of the callback function.void setCollisionMask ( int mask ) #
- 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 - The integer, each bit of which is a mask.
int getCollisionMask() const#
- 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
Current integer, each bit of which is a mask.Event<const Ptr<Body> &> getEventLeave() const#
Usage Example
// implement the Leave event handler
void leave_event_handler(const Ptr<Body> & body)
{
Log::message("\Handling Leave event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections leave_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
publisher->getEventLeave().connect(leave_event_connections, leave_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
publisher->getEventLeave().connect(leave_event_connections, [](const Ptr<Body> & body) {
Log::message("\Handling Leave event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
leave_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection leave_event_connection;
// subscribe to the Leave event with a handler function keeping the connection
publisher->getEventLeave().connect(leave_event_connection, leave_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
leave_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
leave_event_connection.setEnabled(true);
// ...
// remove subscription to the Leave event via the connection
leave_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A Leave event handler implemented as a class member
void event_handler(const Ptr<Body> & body)
{
Log::message("\Handling Leave event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
publisher->getEventLeave().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the Leave event with a handler function
publisher->getEventLeave().connect(leave_event_handler);
// remove subscription to the Leave event later by the handler function
publisher->getEventLeave().disconnect(leave_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId leave_handler_id;
// subscribe to the Leave event with a lambda handler function and keeping connection ID
leave_handler_id = publisher->getEventLeave().connect([](const Ptr<Body> & body) {
Log::message("\Handling Leave event (lambda).\n");
}
);
// remove the subscription later using the ID
publisher->getEventLeave().disconnect(leave_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all Leave events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
publisher->getEventLeave().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
publisher->getEventLeave().setEnabled(true);
Return value
Event reference.Event<const Ptr<Body> &> getEventEnter() const#
Usage Example
// implement the Enter event handler
void enter_event_handler(const Ptr<Body> & body)
{
Log::message("\Handling Enter event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections enter_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
publisher->getEventEnter().connect(enter_event_connections, enter_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
publisher->getEventEnter().connect(enter_event_connections, [](const Ptr<Body> & body) {
Log::message("\Handling Enter event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
enter_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection enter_event_connection;
// subscribe to the Enter event with a handler function keeping the connection
publisher->getEventEnter().connect(enter_event_connection, enter_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
enter_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
enter_event_connection.setEnabled(true);
// ...
// remove subscription to the Enter event via the connection
enter_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A Enter event handler implemented as a class member
void event_handler(const Ptr<Body> & body)
{
Log::message("\Handling Enter event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
publisher->getEventEnter().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the Enter event with a handler function
publisher->getEventEnter().connect(enter_event_handler);
// remove subscription to the Enter event later by the handler function
publisher->getEventEnter().disconnect(enter_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId enter_handler_id;
// subscribe to the Enter event with a lambda handler function and keeping connection ID
enter_handler_id = publisher->getEventEnter().connect([](const Ptr<Body> & body) {
Log::message("\Handling Enter event (lambda).\n");
}
);
// remove the subscription later using the ID
publisher->getEventEnter().disconnect(enter_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all Enter events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
publisher->getEventEnter().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
publisher->getEventEnter().setEnabled(true);
Return value
Event reference.static PhysicalTriggerPtr create ( Shape::TYPE type, const Math::vec3 & size ) #
Constructor. Creates a physical trigger of the specified shape and size.Arguments
- Shape::TYPE 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<Body> getBody ( int num ) #
Returns the specified body that intersects the physical trigger.Arguments
- int num - Body number.
Return value
Intersected body.float getContactDepth ( int contact ) const#
Returns penetration depth by the given contact.Arguments
- int contact - Contact number.
Return value
Penetration depth.Math::vec3 getContactNormal ( int contact ) const#
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 ) const#
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 ) const#
Returns world coordinates of the contact point.Arguments
- int contact - Contact number.
Return value
Contact point.Ptr<Shape> getContactShape ( int contact ) const#
Returns a shape that collided with a physical trigger.Arguments
- int contact - Contact number.
Return value
Shape in contact.int getContactSurface ( int contact ) const#
Returns the surface of the current object, which is in contact .Arguments
- int contact - Contact number.