Unigine.Physics Class
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
Properties
float TotalTime#
float SimulationTime#
float ResponseTime#
int NumJoints#
int NumIslands#
int NumContacts#
int NumBodies#
float CollisionTime#
float IntegrateTime#
int Frame#
int NumIterations#
int NumFrozenFrames#
float Time#
// AppWorldLogic.cs
/* ... */
public override bool Init() {
// to prevent physics from being automatically calculated with each update, set one of the following:
Physics.Enabled = true;
// or
Physics.Scale = 0.0f;
}
public override bool Update() {
// add the time elapsed from the last physics update to the next time count cycle:
Physics.Time += ifps);
}
/* ... */
float Scale#
float PenetrationTolerance#
float PenetrationFactor#
float MaxLinearVelocity#
float MaxAngularVelocity#
float LinearDamping#
float IFps#
vec3 Gravity#
float FrozenLinearVelocity#
float FrozenAngularVelocity#
float AngularDamping#
float Distance#
float Budget#
string Data#
bool IsFixed#
bool IsStable#
bool IsEnabled#
Members
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.Object GetIntersection ( vec3 p0, vec3 p1, int mask, Node[] exclude, 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
- vec3 p0 - Line start point coordinates.
- vec3 p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- Node[] exclude - Array of nodes to be excluded.
- PhysicsIntersection intersection - PhysicsIntersection class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Object GetIntersection ( vec3 p0, vec3 p1, int mask, 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 we specify a 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 with a shape or a collision 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 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.
public override bool Update() {
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
Player player = Game.Player;
if (player == 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(out p0, out p1, mouse_x, mouse_y, width, height);
// create the instance of the PhysicsIntersection object to save the information about the intersection
PhysicsIntersection intersection = new PhysicsIntersection();
// get an intersection
Unigine.Object obj = Physics.getIntersection(p0, p1, 1, intersection);
// if the intersection has been occurred, change the parameter of the object's material
if (obj != null)
{
for (int i = 0; i < obj.NumSurfaces; i++)
{
obj.setMaterialParameterFloat4("albedo_color", new vec4(1.0f, 1.0f, 0.0f, 1.0f), i);
}
// if the intersected object has a shape, show the information about the intersection
Shape shape = intersection.Shape;
if (shape != null)
{
Log.Message("physics intersection info: point: {0} shape: {1}\n", intersection.Point.GetType(), shape.Type.GetType());
}
}
return true;
}
Arguments
- vec3 p0 - Line start point coordinates.
- vec3 p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- PhysicsIntersection intersection - PhysicsIntersection class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Object GetIntersection ( vec3 p0, vec3 p1, int mask, 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
- vec3 p0 - Line start point coordinates.
- vec3 p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- PhysicsIntersectionNormal intersection - PhysicsIntersectionNormal class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.Object GetIntersection ( vec3 p0, vec3 p1, int mask, Node[] exclude, 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
- vec3 p0 - Line start point coordinates.
- vec3 p1 - Line end point coordinates.
- int mask - Physics intersection mask. If 0 is passed, the function will return NULL.
- Node[] exclude - Array of nodes to be excluded.
- PhysicsIntersectionNormal intersection - PhysicsIntersectionNormal class instance containing intersection data.
Return value
The first intersected object, if found; otherwise, 0.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.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.void AddUpdateNode ( 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
- Node node - Node to be updated.
void addUpdateNodes ( 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
- Node[] nodes - Nodes to be updated.
int LoadSettings ( string name, bool clear = false ) #
Loads the physics settings from a given file.Arguments
- string 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 ( Xml xml ) #
Loads physics settings from the Xml.Arguments
- Xml xml - Xml node.
Return value
Returns 1 if settings are loaded successfully; otherwise, 0.int RemoveScene ( int id ) #
Removes the previously saved physics scene.Arguments
- int id - ID number of the scene.
Return value
Returns 1 if the scene is removed successfully; otherwise, 0.int RestoreScene ( int id ) #
Restores the previously saved physics scene from the buffer.Arguments
- int id - ID number of the scene.
Return value
Returns 1 if the scene is restored successfully; otherwise, 0.int RestoreState ( Stream stream ) #
Restores physics settings from the stream.Arguments
- Stream stream - Stream to restore settings from.
Return value
Returns 1 if settings are restored successfully; otherwise, 0.int SaveScene ( ) #
Saves the current physics scene (physical properties of all objects) into the buffer.Return value
Scene buffer ID.int SaveSettings ( string name, int force = 0 ) #
Saves the current physics settings to a given file.Arguments
- string name - Path to a target file.
- int force - Forced saving of physics settings.
Return value
Returns 1 if the settings are saved successfully; otherwise, 0.int SaveState ( Stream stream ) #
Saves physics settings into the stream.Arguments
- Stream stream - Stream to save settings into.
Return value
Returns 1 if settings are saved successfully; otherwise, 0.int SaveWorld ( Xml xml, int force = 0 ) #
Saves physics settings into the Xml.Arguments
- Xml xml - Xml node.
- int force - Forced saving of physics settings.