Unigine::Physics Class
Header: | #include <UniginePhysics.h> |
Controls the simulation of physics. For more information on principles and implementation of physics in real-time rendering, see the articles Execution Sequence, Physics and Simulation of Physics.
See Also#
- The Creating a Car with Suspension Joints usage example demonstrating how to set up physics parameters
- A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/physics/ folder
Physics Class
Enums
UPDATE_MODE#
Physics update mode.Name | Description |
---|---|
UPDATE_MODE_BEFORE_RENDERING = 0 | Update Before Rendering. Physics update (along with the spatial tree update and user callbacks) is executed in the Main thread just before rendering is performed (render). The number of physics ticks executed before the rendering frame here is defined by the physics and the Engine framerates. This mode is the most clear and straightforward (everything is executed safely in a strictly determined order) with no frame lag (results of physics calculations are applied in the current frame). But, on the other hand, this mode is the slowest as there are no asynchronous parallel calculations (everything's in the Main thread). Use this mode in case the time lag is unacceptabe for your application (you need all physics calculations to be applied in the current frame) and you want maximum simplicity and strictly determined order of execution for user code (physicsUpdate and physics callbacks). |
UPDATE_MODE_ASYNC_RENDERING = 1 | Asynchronous update mode. Physics update is performed asynchronously to rendering. In case of several physics ticks per one rendering frame (when the Engine framerate is lower, or catching up is performed), only the first one is executed in parallel, then the physics module waits for the completion of the rendering process, returns to the Main thread and executes the rest of the physics ticks. There is a frame lag (results of physics calculations are applied in the next frame) and there is some ambiguity regarding the time, when user code (physicsUpdate and physics callbacks) is to be executed in case of several physics ticks per one rendering frame (some part is executed before rendering while the other just after it). This mode is the fastest one and is used by default. |
Members
void setAngularDamping ( float damping ) #
Updates the current angular damping value.Arguments
- float damping - New angular damping. If a negative value is provided, 0 will be used instead.
float getAngularDamping ( ) #
Returns the current angular damping value.Return value
Angular damping.Ptr<Body> getBody ( int id ) #
Returns a body with a given ID.Arguments
- int id - Body ID.
Return value
Body with a given ID or NULL (0), if there is no body with a given ID.int isBody ( int id ) #
Checks if a body with a given ID exists.Arguments
- int id - Body ID.
Return value
1 if a body with a given ID exists; otherwise, 0.void setBudget ( float budget ) #
Sets the physics simulation budget. Physics isn't simulated when time is out of the budget.Arguments
- float budget - The budget value in seconds.
float getBudget ( ) #
Returns the physics simulation budget. Physics isn't simulated when time is out of the budget.Return value
The budget value in seconds. The default value is 1/20.void setData ( const char * data ) #
Sets user data associated with the world. In the *.world file, the data is set in the data tag.Arguments
- const char * data - New user data.
const char * getData ( ) #
Returns user string data associated with the world. This string is written directly into the data tag of the *.world file.Return value
User string data.void setDistance ( float distance ) #
Updates a distance after which the physics will not be simulated.Arguments
- float distance - Distance in units.
float getDistance ( ) #
Returns a distance after which the physics will not be simulated.Return value
Distance in units.void setEnabled ( int enable ) #
Enables or disables physics simulation.Arguments
- int enable - 1 to enable physics, 0 to disable it.
int isEnabled ( ) #
Returns a value indicating if physics simulation is enabled. The default is 1.Return value
1 if physics is enabled; otherwise, 0.void setSyncEngineUpdateWithPhysics ( bool fixed ) #
Sets a flag to synchronize the Engine FPS to physics one. Such FPS limitation allows to calculate physics each rendered frame (rather then interpolate it when this flag is unset). In this mode, there are no twitching of physical objects if they have non-linear velocities. If the Engine FPS is lower than the physics one, this flag has no effect.Arguments
- bool fixed - true to synchronize the Engine FPS to physics one; false to interpolate physics if the Engine FPS is higher.
bool isSyncEngineUpdateWithPhysics ( ) #
Returns a flag indicating if the Engine FPS is synchronized to physics one. Such FPS limitation allows to calculate physics each rendered frame (rather then interpolate it when this flag is unset). In this mode, there are no twitching of physical objects if they have non-linear velocities. If the Engine FPS is lower than the physics one, this flag has no effect.Return value
true if the Engine FPS is synchronized to physics one; false if the physics is interpolated if the Engine FPS is higher.int getFrame ( ) #
Returns the current frame of physics update.Return value
Frame number.void setFrozenAngularVelocity ( float velocity ) #
Updates the angular velocity threshold for freezing object simulation. If the object angular velocity remains lower than this threshold during the number of Frozen frames (together with linear one), it stops to be updated.Arguments
- float velocity - New "freeze" angular velocity. If a negative value is provided, 0 will be used instead.
float getFrozenAngularVelocity ( ) #
Returns the current angular velocity threshold for freezing object simulation. An object stops to be updated if its angular velocity remains lower than this threshold during the number of Frozen frames (together with linear one).Return value
"Freeze" angular velocity.void setFrozenLinearVelocity ( float velocity ) #
Updates the linear velocity threshold for freezing object simulation. If the object linear velocity remains lower than this threshold during the number of Frozen frames (together with angular one), it stops to be updated.Arguments
- float velocity - New "freeze" linear velocity. If a negative value is provided, 0 will be used instead.
float getFrozenLinearVelocity ( ) #
Returns the current linear velocity threshold for freezing object simulation. An object stops to be updated if its linear velocity remains lower than this threshold during the number of Frozen frames (together with angular one).Return value
"Freeze" linear velocity.void setGravity ( const Math::vec3 & gravity ) #
Updates the current gravity value.Arguments
- const Math::vec3 & gravity - New gravity.
Math::vec3 getGravity ( ) #
Returns the current gravity value.Return value
Gravity.void setIFps ( float ifps ) #
Updates a frame duration. In fact, this function updates the FPS count used to calculate physics.Arguments
- float ifps - Frame duration (1/FPS).
float getIFps ( ) #
Returns a physics frame duration.Return value
Frame duration (1 / FPS).float getIntegrateTime ( ) #
Returns the duration of the integrate phase, in which physics simulation results are applied to bodies.Return value
An integrate phase duration value, milliseconds.Ptr<Object> getIntersection ( const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, const Ptr<PhysicsIntersection> & intersection ) #
Performs tracing from the p0 point to the p1 point to find a collision object located on that line. If an object is assigned a body, intersection occurs with its shape. If an object has no body, this function detects intersection with surfaces (polygons) of objects with intersection flag set. Intersection is found only for objects with a matching mask if their ID is not found in the exclude list. Intersection is Intersection does not work for disabled objects.
Arguments
- const Math::Vec3 & p0 - Line start point coordinates.
- const Math::Vec3 & p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- const Vector< Ptr<Node> > & exclude - Array of nodes to be excluded.
- const Ptr<PhysicsIntersection> & intersection - PhysicsIntersection class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Ptr<Object> getIntersection ( const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Ptr<PhysicsIntersection> & intersection ) #
Performs tracing from the p0 point to the p1 point to find a collision object located on that line. If an object is assigned a body, intersection occurs with its shape. If an object has no body, this function detects intersection with surfaces (polygons) of objects with intersection flag set. Physics intersection shall only be detected for objects with a matching mask. Intersection does not work for disabled objects.
Usage Example
The following example shows how you can get the intersection information by using the PhysicsIntersection class. In this example the line is an invisible traced line from the point of the camera (vec3 p0) to the point of the mouse pointer (vec3 p1). The executing sequence is the following:
- Define and initialize two points (p0 and p1) by using the Player::getDirectionFromScreen() function.
- Create an instance of the PhysicsIntersection class to get the intersection information.
- Check, if there is an intersection with an object. The getIntersection() function returns an intersected object when the object intersects with the traced line.
- In this example, when the object intersects with the traced line, all the surfaces of the intersected object change their material parameters. If the object has a shape, its information will be shown in the console. The PhysicsIntersection class instance gets the coordinates of the intersection point and the Shape class object. You can get all these fields by using getShape(), getPoint() functions.
int AppWorldLogic::update() {
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
PlayerPtr player = Game::getPlayer();
if (player.get() == NULL)
return 0;
// get width and height of the current application window
int width = App::getWidth();
int height = App::getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App::getMouseX();
int mouse_y = App::getMouseY();
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player->getDirectionFromScreen(p0, p1, mouse_x, mouse_y, width, height);
// create the instance of the PhysicsIntersection object to save the information about the intersection
PhysicsIntersectionPtr intersection = PhysicsIntersection::create();
// get an intersection
ObjectPtr object = Physics::getIntersection(p0, p1, 1, intersection);
// if the intersection has been occurred, change the parameter of the object's material
if (object)
{
for (int i = 0; i < object->getNumSurfaces(); i++)
{
object->setMaterialParameterFloat4("albedo_color", vec4(1.0f, 1.0f, 0.0f, 1.0f), i);
}
// if the intersected object has a shape, show the information about the intersection
ShapePtr shape = intersection->getShape();
if (shape)
{
Log::message("physics intersection info: point: %s shape: %s\n", typeid(intersection->getPoint()).name(), typeid(shape->getType()).name());
}
}
return 1;
}
Arguments
- const Math::Vec3 & p0 - Line start point coordinates.
- const Math::Vec3 & p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- const Ptr<PhysicsIntersection> & intersection - PhysicsIntersection class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Ptr<Object> getIntersection ( const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Ptr<PhysicsIntersectionNormal> & intersection ) #
Performs tracing from the p0 point to the p1 point to find a collision object located on that line. If an object is assigned a body, intersection occurs with its shape. If an object has no body, this function detects intersection with surfaces (polygons) of objects with intersection flag set. Physics intersection shall only be detected for objects with a matching mask. Intersection does not work for disabled objects.
Arguments
- const Math::Vec3 & p0 - Line start point coordinates.
- const Math::Vec3 & p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- const Ptr<PhysicsIntersectionNormal> & intersection - PhysicsIntersectionNormal class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Ptr<Object> getIntersection ( const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, const Ptr<PhysicsIntersectionNormal> & intersection ) #
Performs tracing from the p0 point to the p1 point to find a collision object located on that line. If an object is assigned a body, intersection occurs with its shape. If an object has no body, this function detects intersection with surfaces (polygons) of objects with intersection flag set. Intersection is found only for objects with a matching mask if their ID is not found in the exclude list. Intersection is Intersection does not work for disabled objects.
Arguments
- const Math::Vec3 & p0 - Line start point coordinates.
- const Math::Vec3 & p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- const Vector< Ptr<Node> > & exclude - Array of nodes to be excluded.
- const Ptr<PhysicsIntersectionNormal> & intersection - PhysicsIntersectionNormal class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Ptr<Joint> getJoint ( int id ) #
Returns a joint with a given ID.Arguments
- int id - Joint ID.
Return value
Joint with a given ID or NULL (0), if there is no joint with a given ID.int isJoint ( int id ) #
Checks if a joint with a given ID exists.Arguments
- int id - Joint ID.
Return value
1 if a joint with a given ID exists; otherwise, 0.void setLinearDamping ( float damping ) #
Updates the current linear damping value.Arguments
- float damping - New linear damping. If a negative value is provided, 0 will be used instead.
float getLinearDamping ( ) #
Returns the current linear damping value.Return value
Linear damping.void setMaxAngularVelocity ( float velocity ) #
Updates the maximum possible angular velocity.Arguments
- float velocity - New maximum velocity value. If a negative value is provided, 0 will be used instead.
float getMaxAngularVelocity ( ) #
Returns the current maximum possible angular velocity.Return value
Maximum possible angular velocity.void setMaxLinearVelocity ( float velocity ) #
Updates the maximum possible linear velocity.Arguments
- float velocity - New maximum velocity value. If a negative value is provided, 0 will be used instead.
float getMaxLinearVelocity ( ) #
Returns the current maximum possible linear velocity.Return value
Maximum possible linear velocity.float getCollisionTime ( ) #
Returns the duration of the collision detection phase, during which collisions between objects are found.Return value
Collision detection phase duration, in milliseconds.int getNumBodies ( ) #
Returns the number of bodies present within the physics radius.Return value
The number of bodies.int getNumContacts ( ) #
Returns the number of contacts within the physics radius; it includes contacts between the bodies (their shapes) and body-mesh contacts.Return value
The number of contacts.void setNumFrozenFrames ( int frames ) #
Updates the number of frames, during which an object should keep certain angular and linear velocities to become frozen.Arguments
- int frames - Number of frames. If a non-positive value is provided, 1 will be used instead.
int getNumFrozenFrames ( ) #
Returns the current number of frames, during which an object should keep certain angular and linear velocities to become frozen.Return value
Number of frames.int getNumIslands ( ) #
Returns the number of physical islands within the physics radius that could be calculated separately. The lower this number, the less efficient multi-threading is, if enabled.Return value
The number of physical islands.void setNumIterations ( int iterations ) #
Updates the number of iterations used to solve contacts and constraints. Note that if this value is too low, the precision of calculations will suffer.Arguments
- int iterations - New number of iterations. If a non-positive value is provided, 1 will be used instead.
int getNumIterations ( ) #
Returns the current number of iterations used to solve contacts and constraints.Return value
Current number of iterations.int getNumJoints ( ) #
Returns the number of joints within the physics radius.Return value
The number of joints.void setPenetrationFactor ( float factor ) #
Updates the current penalty force factor.Arguments
- float factor - New penetration factor. 0 means no penalty force in contacts. The provided value is saturated in the range [0; 1].
float getPenetrationFactor ( ) #
Returns a penalty force factor. 0 means no penalty force in contacts. The maximum value is 1.Return value
Current penetration factor.void setPenetrationTolerance ( float tolerance ) #
Updates the current penetration tolerance.Arguments
- float tolerance - New penetration tolerance. If a negative value is provided, 0 will be used instead, however, this value should be greater than 0 for stable simulation.
float getPenetrationTolerance ( ) #
Returns a value indicating how deeply one object can penetrate another.Return value
Current penetration tolerance.float getResponseTime ( ) #
Returns the duration value of the response phase, in which collision response is calculated and joints are solved.Return value
A response phase duration value, milliseconds.void setScale ( float scale ) #
Updates a value that is used to scale a frame duration. The provided value is saturated in the range [0;16].Arguments
- float scale - Scaling factor.
float getScale ( ) #
Returns a value used to scale a frame duration.Return value
Value to scale the frame duration.Ptr<Shape> getShape ( int id ) #
Returns a shape with a given ID.Arguments
- int id - Shape ID.
Return value
Shape with a given ID or NULL (0), if there is no shape with a given ID.int isShape ( int id ) #
Checks if a shape with a given ID exists.Arguments
- int id - Shape ID.
Return value
1 if a shape with a given ID exists; otherwise, 0.float getSimulationTime ( ) #
Returns the duration of all of the simulation phases added together.Return value
A simulation phases duration value, milliseconds.void setDeterministic ( bool stable ) #
Sets a value indicating if objects are updated in a definite order or not. Deterministic mode ensures that all contacts are solved in the predefined order and visualization of physics in the world is repetitive (on one computer). When this mode is enabled the Engine performs additional sorting of bodies, shapes and joints inside islands after building them. Deterministic mode is unavailable in case there are missed frames - it is simply impossible. Moreover, there may be differences between visualization of physics on different hardware (e.g., AMD and Intel).Please note that deterministic mode does not come for free, it may eat up 10-20% of the frame rate, and it also depends on the scene a lot.
Arguments
- bool stable - true to indicate that the objects are updated in a definite order; false to indicate that an objects update order may change. The default is false (the update order may change).
bool isDeterministic ( ) #
Returns a value indicating if objects are updated in a definite order or not.Deterministic mode ensures that all contacts are solved in the predefined order and visualization of physics in the world is repetitive (on one computer). When this mode is enabled the Engine performs additional sorting of bodies, shapes and joints inside islands after building them. Deterministic mode is unavailable in case there are missed frames - it is simply impossible. Moreover, there may be differences between visualization of physics on different hardware (e.g., AMD and Intel).Please note that deterministic mode does not come for free, it may eat up 10-20% of the frame rate, and it also depends on the scene a lot.
Return value
true if the objects are updated in a definite order; otherwise false. The default is false (the update order may change).void setCurrentSubframeTime ( float time ) #
Forces simulation of physics for a given time. It means, until the set time elapses, physics will be calculated each physics tick (frame) that occurs depending on physics frame rate. It allows you to control the starting point for physics simulation.// AppWorldLogic.cpp
/* ... */
int AppWorldLogic::init() {
// to prevent physics from being automatically calculated with each update, set one of the following:
Physics::setEnabled(0)
// or
Physics::setScale(0)
}
int AppWorldLogic::update() {
// add the time elapsed from the last physics update to the next time count cycle:
Physics::setCurrentSubframeTime(Physics::getCurrentSubframeTime()+ifps));
}
/* ... */
Arguments
- float time - Time to continue updating physics in seconds.
float getCurrentSubframeTime ( ) #
Returns the current time that can be used when shifting between physics update frames.Return value
Time in seconds.float getTotalTime ( ) #
Returns the total time that both rendering and calculating of the frame took (the duration of the main loop in the application execution sequence).Return value
The total time value, milliseconds.void addUpdateNode ( const Ptr<Node> & node ) #
Adds the node for which physical state should be updated. If a node is not added with this function, it won't be updated when out of physics simulation distance.Arguments
void addUpdateNodes ( const Vector<Ptr<Node>> & nodes ) #
Adds the nodes for which physical state should be updated. If nodes are not added with this function, they won't be updated when out of physics simulation distance.Arguments
int loadSettings ( const char * name, bool clear = false ) const#
Loads the physics settings from a given file.Arguments
- const char * name - Path to an XML file with desired settings.
- bool clear - Clear flag. Set true to clear settings before loading (new settings shall be applied right after loading them), or false not to clear.
Return value
Returns 1 if the settings are loaded successfully; otherwise, 0.int loadWorld ( const Ptr<Xml> & xml ) #
Loads physics settings from the Xml.Arguments
Return value
Returns 1 if settings are loaded successfully; otherwise, 0.int saveScene ( ) #
Saves the current physics scene (physical properties of all objects) into the buffer.Return value
Scene buffer ID.int restoreScene ( int id ) #
Restores the previously saved physics scene from the buffer.Arguments
- int id - Buffer ID.
Return value
Returns 1 if the scene is restored successfully; otherwise, 0.int removeScene ( int id ) #
Removes the previously saved physics scene.Arguments
- int id - Buffer ID.
Return value
Returns 1 if the scene is removed successfully; otherwise, 0.bool saveState ( const Ptr<Stream> & stream ) #
Saves physics settings into the stream.Example using saveState() and restoreState() methods:
// set state
Physics::setNumIterations(1); // NumIterations = 1
// save state
BlobPtr blob_state = Blob::create();
Physics::saveState(blob_state);
// change state
Physics::setNumIterations(16); // now NumIterations = 16
// restore state
blob_state->seekSet(0); // returning the carriage to the start of the blob
Physics::restoreState(blob_state); // restore NumIterations = 1
Arguments
Return value
true if settings are saved successfully; otherwise, false.bool restoreState ( const Ptr<Stream> & stream ) #
Restores physics settings from the stream.Example using saveState() and restoreState() methods:
// set state
Physics::setNumIterations(1); // NumIterations = 1
// save state
BlobPtr blob_state = Blob::create();
Physics::saveState(blob_state);
// change state
Physics::setNumIterations(16); // now NumIterations = 16
// restore state
blob_state->seekSet(0); // returning the carriage to the start of the blob
Physics::restoreState(blob_state); // restore NumIterations = 1
Arguments
Return value
true if settings are restored successfully; otherwise, false.bool saveSettings ( const char * name, int force = 0 ) #
Saves the current physics settings to a given file.Arguments
- const char * name - Path to an xml file to which the settings will be saved.
- int force - Forced saving of physics settings.
Return value
true if the settings are saved successfully; otherwise, false.bool saveWorld ( const Ptr<Xml> & xml, int force = 0 ) #
Saves physics settings to the given Xml node.Arguments
Return value
true if settings are saved successfully; otherwise, false.void setStableFPS ( bool stablefps ) #
Returns a value indicating if frame time stabilization is enabled. In case the current Engine framerate is much higher than the fixed Physics framerate (e.g. 120 FPS vs 60 FPS), the physics won't be updated each rendering frame (e.g. it may update during every second frame). The resulting frame time will become unstable, shorter-longer-shorter-longer (render -> render+physics -> render -> render+physics...). This option ensures stable frame time for smoother user experience removing unwanted "hiccups" (however, the average framerate is decreased).Arguments
- bool stablefps - true to enable frame time stabilization; false - to disable it.
bool isStableFPS ( ) const#
Returns a value indicating if frame time stabilization is enabled. In case the current Engine framerate is much higher than the fixed Physics framerate (e.g. 120 FPS vs 60 FPS), the physics won't be updated each rendering frame (e.g. it may update during every second frame). The resulting frame time will become unstable, shorter-longer-shorter-longer (render -> render+physics -> render -> render+physics...). This option ensures stable frame time for smoother user experience removing unwanted "hiccups" (however, the average framerate is decreased).Return value
true if frame time stabilization is enabled; otherwise, false.void setUpdateMode ( Physics::UPDATE_MODE mode ) #
Sets the update mode to be used for physics. Physics can be updated either asynchronously (in parallel with rendering) or in the Main thread before rendering. The async mode is the fastest one and is used by default, however, it has a one-frame lag (calculation results are applied in the next frame) and some nuances regarding user code execution in some cases.Arguments
- Physics::UPDATE_MODE mode - New physics update mode to be set.
Physics::UPDATE_MODE getUpdateMode ( ) const#
Returns the current physics update mode. Physics can be updated either asynchronously (in parallel with rendering) or in the Main thread before rendering. The async mode is the fastest one and is used by default, however, it has a one-frame lag (calculation results are applied in the next frame) and some nuances regarding user code execution in some cases.Return value
Current physics update mode.void setMissedFrameLifetime ( float lifetime ) #
Sets the lifetime for missed frames. This value defines how long missed frames are to be kept in the catch-up buffer. In case the current Engine framerate is lower than the fixed Physics framerate, some of the physics frames get skipped and the simulation starts looking like in a slo-mo effect (e.g., if the target physics framerate is 60 FPS, when the Engine updates at 30 FPS, the simulation will look 2 times slower). The Physics module will try to catch up everything missed later, when possible (e.g. when the Engine framerate grows higher, while waiting for GPU to complete rendering). The missed frames are kept in a buffer for some time (lifetime), as it expires the frame is removed from the buffer and becomes lost forever.Arguments
- float lifetime - Lifetime for missing frame, in milliseconds. Setting too high values may result in significant memory consumption in case of low hardware capabilities (no catching-up performed with a growing number of missed frames).