Unigine.Shape Class
This class creates collision shapes that approximate the finite volume of physical bodies and allow them to collide. Shapes are assigned to a body and are positioned in its local coordinates.
See Also#
- The Enabling Selective Surface-Based Collision usage example demonstrating how to apply the collision mask
- A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/shapes/ folder
Shape Class
Enums
TYPE#
Types of collision shapes.Properties
bool IsIdentity#
mat4 BodyShapeTransform#
mat4 Transform#
vec3 CenterOfMass#
mat3 Inertia#
float Volume#
vec3 Area#
float Restitution#
float Friction#
float Density#
float Mass#
int ExclusionMask#
int CollisionMask#
int PhysicsIntersectionMask#
string Name#
bool Continuous#
bool IsEnabledSelf#
bool Enabled#
Body Body#
int Number#
string TypeName#
Shape.TYPE Type#
int ID#
vec3 Velocity#
vec3 Position#
Members
int GetCollision ( ShapeContact[] OUT_contacts, float ifps ) #
Performs collision check for the shape and puts information on all contacts to the output buffer.Collisions with the surface can be found only if the following conditions are fulfilled:
- The surface is enabled.
- Per-surface Collision flag is enabled.
- The surface has a material assigned.
Arguments
- ShapeContact[]
OUT_contacts - Output buffer containing information on all detected physical contacts for the shape (if any). Information on each contact can be handled via the ShapeContact class.This output buffer is to be filled by the Engine as a result of executing the method.
- float ifps - Inverse FPS value.
Return value
1 if collisions are found; otherwise, 0.int GetCollision ( Object object, ShapeContact[] OUT_contacts, float ifps ) #
Performs collision check for the shape and puts information on all contacts and contact object to the output buffer.Collisions with the surface can be found only if the following conditions are fulfilled:
- The surface is enabled.
- Per-surface Collision flag is enabled.
- The surface has a material assigned.
Arguments
- Object object - Object to be ignored when detecting collisions. This parameter is used when it is necessary to ignore collisions of the shape with the object it belongs to.
- ShapeContact[]
OUT_contacts - Output buffer containing information on all detected physical contacts for the shape (if any). Information on each contact can be handled via the ShapeContact class.This output buffer is to be filled by the Engine as a result of executing the method.
- float ifps - Inverse FPS value.
Return value
1 if collisions are found; otherwise, 0.int GetIntersection ( vec3 p0, vec3 p1, PhysicsIntersectionNormal intersection ) #
Performs tracing from the p0 point to the p1 point to find a shape intersected by this line. Intersection is found only for objects with a matching intersection mask.
Arguments
- vec3 p0 - Start point of the line.
- vec3 p1 - End point of the line.
- PhysicsIntersectionNormal intersection - PhysicsIntersectionNormal class instance containing intersection information
Return value
1 if an intersection was detected; otherwise - 0.int GetIntersection ( vec3 p0, vec3 p1, PhysicsIntersection intersection ) #
Performs tracing from the p0 point to the p1 point to find a shape intersected by this line. Intersection is found only for objects with a matching intersection mask.
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). It is supposed that you have a dynamic mesh with a body and a shape assigned. 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 information of the intersection point.
- Check, if there is a intersection with a shape and save the result in the integer variable.
- In this example, if there is an intersection of mouse direction with a shape, the PhysicsIntersection class instance gets the intersection point. The result is shown in the console.
/* ... */
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
Player player = Game.Player;
if (player == null)
return;
// get size (width and height) of the current application window
EngineWindow main_window = WindowManager.MainWindow;
if (!main_window)
Engine.Quit();
ivec2 main_size = main_window.Size;
// get the current X and Y coordinates of the mouse pointer
int mouse_x = Input.MousePosition.x - main_window.Position.x;
int mouse_y = Input.MousePosition.y - main_window.Position.y;
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player.GetDirectionFromScreen(out p0, out p1, 0, 0, mouse_x, mouse_y, main_size.x, main_size.y);
// create the instance of the PhysicsIntersection object to save the information about the intersection
PhysicsIntersection intersection = new PhysicsIntersection();
// create an integer variable to check the result of intersection
int result = 0;
result = shape.GetIntersection(p0, p1, intersection);
// if there was an intersection, show the message in console
if (result != 0)
{
Log.Message("Intersection point: ({0} {1} {2}) \n", intersection.Point.x, intersection.Point.y, intersection.Point.z);
}
/* ... */
Arguments
- vec3 p0 - Start point of the line.
- vec3 p1 - End point of the line.
- PhysicsIntersection intersection - PhysicsIntersection class instance containing intersection information.
Return value
1 if an intersection was detected; otherwise - 0.int GetIntersection ( vec3 p0, vec3 p1, vec3[] OUT_ret_point, vec3[] OUT_ret_normal ) #
Performs tracing from the p0 point to the p1 point to find a shape intersected by this line. Intersection is found only for objects with a matching intersection mask.
Arguments
- vec3 p0 - Start point of the line (in world coordinates).
- vec3 p1 - End point of the line (in world coordinates).
- vec3[]
OUT_ret_point - Container to which contact point coordinates (if any) shall be put (in world coordinate system).This output buffer is to be filled by the Engine as a result of executing the method.
- vec3[]
OUT_ret_normal - Container to which contact point normal coordinates (if any) shall be put (in world coordinate system).This output buffer is to be filled by the Engine as a result of executing the method.
Return value
1 if an intersection was detected; otherwise - 0.string GetTypeName ( int type ) #
Returns the name of a shape type with a given ID.Arguments
- int type - Shape type ID. One of the SHAPE_* values.
Return value
Shape type name.void SetVelocity ( vec3 velocity, float ifps ) #
Sets a new velocity vector for the shape.Arguments
- vec3 velocity - Velocity vector, each component represents shape's velocity along the corresponding axis, in units per second.
- float ifps - Inverse FPS value.
Shape Clone ( ) #
Clones the shape.Return value
Copy of the shape.void RenderVisualizer ( vec4 color ) #
Renders the shape.Arguments
- vec4 color - Color, in which the shape will be rendered.
bool SaveState ( Stream stream ) #
Saves the state of a given node into a binary stream.- If a node is a parent for other nodes, states of these child nodes need to be saved manually.
- To save the state from a buffer, file or a message from a socket, make sure the stream is opened. For buffers and files, you also need to set the proper position for reading.
Example using saveState() and restoreState() methods:
// set the shape state
shape.Friction = 0.8f;
// save state
Blob blob_state = new Blob();
shape.SaveState(blob_state);
// change the state
shape.Friction = 0.4f;
// restore state
blob_state.SeekSet(0); // returning the carriage to the start of the blob
shape.RestoreState(blob_state);
Arguments
- Stream stream - Stream to save node state data.
Return value
true if the node state is saved successfully; otherwise, false.int RestoreState ( Stream stream ) #
Restores the state of a given node from a binary stream.- If a node is a parent for other nodes, states of these child nodes need to be restored manually.
- To save the state into a buffer, file or a message from a socket, make sure the stream is opened. If necessary, you can set a position for writing for buffers and files.
Example using saveState() and restoreState() methods:
// set the shape state
shape.Friction = 0.8f;
// save state
Blob blob_state = new Blob();
shape.SaveState(blob_state);
// change the state
shape.Friction = 0.4f;
// restore state
blob_state.SeekSet(0); // returning the carriage to the start of the blob
shape.RestoreState(blob_state);
Arguments
- Stream stream - Stream with saved node state data.
Return value
true if the node state is restored successfully; otherwise, false.void Swap ( Shape shape ) #
Swaps the shapes saving the pointers.Arguments
- Shape shape - A shape to swap.
Shape CreateShape ( int type ) #
Creates a new shape of the specified type.Arguments
- int type - Body type. One of the SHAPE_* values.
Return value
New created shape instance.Shape CreateShape ( string type_name ) #
Creates a new shape of the specified type.Arguments
- string type_name - Shape type name.
Return value
New created shape instance.Shape.TYPE GetTypeID ( string type ) #
Arguments
- string type