# 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.

## Math::vec3getArea()

Returns areas of shape projections on three axes: x, y, and z.

### Return value

A triple of projection areas.

## voidsetBody(const Ptr<Body> & body)

Sets a body, to which the shape belongs.

### Arguments

• const Ptr<Body> & body - Body, to which the shape belongs.

## Ptr<Body>getBody()

Returns the body, to which the shape belongs.

### Return value

Body, to which the shape belongs.

## voidsetBodyShapeTransform(const Math::mat4 & transform)

Sets a transformation matrix for the shape (in the coordinates of the body). This matrix describes position and orientation of the shape. This method is identical to setShapeTransform().

### Arguments

• const Math::mat4 & transform - Transformation matrix

## Math::mat4getBodyShapeTransform()

Returns the transformation matrix of the shape (in the coordinates of the body). This matrix describes position and orientation of the shape. This method is identical to getShapeTransform().

### Return value

Transformation matrix

## Math::vec3getCenterOfMass()

Returns local coordinates of the center of mass of the shape.

### Return value

Coordinates of the center of mass.

## intgetCollision(Vector< Ptr<Contact> > & 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:

1. The surface is enabled.
2. Per-surface Collision flag is enabled.
3. The surface has a material assigned.
4. A property inherited from the surface_base is assigned to the surface and the collision parameter of this property is set to 1 (e.g. via the setParameterInt() method).
Notice
The shape must be enabled.

### Arguments

• Vector< Ptr<Contact> > & contacts - Output buffer containing information on all detected physical contacts for the shape (if any). Information on each contact can be handled via the Contact class.
• float ifps - Inverse FPS value.

### Return value

1 if collisions are found; otherwise, 0.

## intgetCollision(const Ptr<Object> & object, Vector< Ptr<Contact> > & 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:

1. The surface is enabled.
2. Per-surface Collision flag is enabled.
3. The surface has a material assigned.
4. A property inherited from the surface_base is assigned to the surface and the collision parameter of this property is set to 1 (e.g. via the setParameterInt() method).
Notice
The shape must be enabled.

### Arguments

• const Ptr<Object> & object - Contact object smart pointer.
• Vector< Ptr<Contact> > & contacts - Output buffer containing information on all detected physical contacts for the shape (if any). Information on each contact can be handled via the Contact class.
• float ifps - Inverse FPS value.

### Return value

1 if collisions are found; otherwise, 0.

### Arguments

• int mask - Integer, each bit of which is a mask.

### Return value

Integer, each bit of which is a mask.

## voidsetContinuous(int continuous)

Enables or disables continuous collision detection for spheres or capsules. Enabled CDD incurs almost no performance penalty. Disabling CCD allows to avoid physics artifacts, if there are any.

### Arguments

• int continuous - Positive value to enable CCD; 0 to disable.

## intisContinuous()

Returns a value indicating if continuous collision detection for spheres or capsules is enabled. Enabled CCD incurs almost no performance penalty. Disabling CCD allows to avoid physics artifacts, if there are any.

### Return value

1 if CCD is enabled; otherwise, 0.

## voidsetDensity(float density)

Sets density of a shape. Changing the density influences the mass, that is computed by multiplying shape volume by density.

### Arguments

• float density - Density value. If a negative value is provided, 0 will be used instead.

## floatgetDensity()

Returns the current density of a shape.

Density value.

## voidsetEnabled(int enable)

Enables or disables physical interactions with the shape.

### Arguments

• int enable - Positive number to enable physical interactions, 0 to disable them.

## intisEnabled()

Returns a value indicating if physical interactions with the shape are enabled.

### Return value

1 if physical interactions with the shape are enabled; otherwise, 0.

## intisEnabledSelf()

Returns a value indicating if the shape is enabled.

### Return value

1 if the shape is enabled; otherwise, 0.

Sets an bit mask to prevent collisions of the shape with other ones. This mask is independent of the collision mask. For shapes with matching collision masks not to collide, at least one bit of their exclusion mask should match.

### Arguments

• int mask - Integer, each bit of which is a mask.

Returns the bit mask that prevents collisions of the shape with other ones. This mask is independent of the collision mask. For shape with matching collision masks not to collide, at least one bit of their exclusion mask should match.

### Return value

Integer, each bit of which is a mask.

## voidsetFriction(float friction)

Sets a friction of the shape surface.

### Arguments

• float friction - Friction of the shape surface. If a negative value is provided, 0 will be used instead.

## floatgetFriction()

Returns the friction of the shape surface.

### Return value

Friction of the shape surface.

## intsetID(int id)

Sets the unique ID for the shape.

### Arguments

• int id - Unique ID.

### Return value

1 if the ID is set successfully; otherwise, 0.

## intgetID()

Returns the unique ID of the shape.

Unique ID.

## intgetIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, const Ptr<PhysicsIntersectionNormal> & intersection)

Performs tracing from the p0 point to the p1 point to find a shape located on that line. Intersection is found only for objects with a matching mask.

Notice
World space coordinates are used for this function.

### Arguments

• const Math::Vec3 & p0 - Start point of the line.
• const Math::Vec3 & p1 - End point of the line.
• const Ptr<PhysicsIntersectionNormal> & intersection - Intersection mask.

### Return value

Reference to the first intersected shape, if found; otherwise - 0.

## intgetIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, const Ptr<PhysicsIntersection> & intersection)

Performs tracing from the p0 point to the p1 point to find a shape located on that line. Intersection is found only for objects with a matching mask.

Notice
World space coordinates are used for this function.

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.
Source code (C++)
``````/* ... */
// initialize points of the mouse direction
Vec3 p0, p1;

// get the current player (camera)
PlayerPtr player = Game::get()->getPlayer();
if (player.get() == NULL)
return 0;
// get width and height of the current application window
int width = App::get()->getWidth();
int height = App::get()->getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App::get()->getMouseX();
int mouse_y = App::get()->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();
// 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: (%f %f %f) \n", intersection->getPoint().x, intersection->getPoint().y, intersection->getPoint().z);
}
/* ... */``````

### Arguments

• const Math::Vec3 & p0 - Start point of the line.
• const Math::Vec3 & p1 - End point of the line.
• const Ptr<PhysicsIntersection> & intersection - Intersection mask.

### Return value

Reference to the first intersected shape, if found; otherwise - 0.

## intgetIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, Math::Vec3 * ret_point, Math::vec3 * ret_normal)

Performs tracing from the p0 point to the p1 point to find a shape located on that line. Intersection is found only for objects with a matching mask.

Notice
World space coordinates are used for this function.

### Arguments

• const Math::Vec3 & p0 - Start point of the line.
• const Math::Vec3 & p1 - End point of the line.
• Math::Vec3 * ret_point - Intersection mask.
• Math::vec3 * ret_normal - Variable defining which type of intersection object will be returned:

### Return value

Reference to the first intersected shape, if found; otherwise - 0.

Sets an intersection mask for the shape.

### Arguments

• int mask - Integer, each bit of which is a mask.

Returns an intersection mask of the shape.

### Return value

Integer, each bit of which is a mask.

## voidsetMass(float mass)

Sets a mass of the shape. Changing the mass influences the density, that is computed by dividing the mass by shape volume.
Notice
If g (Earth's gravity) equals to 9.8 m/s 2, and 1 unit equals to 1 m, a shape with mass value of 1 weighs 1 kg.

### Arguments

• float mass - Mass of the shape. If a negative value is provided, 0 will be used instead.

## floatgetMass()

Returns the mass of the shape.
Notice
If g (Earth's gravity) equals to 9.8 m/s 2, and 1 unit equals to 1 m, the mass is measured in kilograms.

### Return value

Mass of the shape.

## voidsetName(const char * name)

Sets the name of the shape.

### Arguments

• const char * name - Name of the shape.

## const char *getName()

Returns the name of the shape.

### Return value

Name of the shape.

## intgetNumber()

Returns the number of shape instances.

### Return value

Number of shape instances.

## voidsetRestitution(float restitution)

Sets a restitution of the shape surface.

### Arguments

• float restitution - Restitution of the shape surface. The provided value will be saturated in the range [0; 1].

## floatgetRestitution()

Returns the restitution of the shape surface.

### Return value

Restitution of the shape surface.

## Ptr<Shape>getShape()

Returns a Shape pointer.

Shape pointer.

## voidsetPosition(const Math::Vec3 & position)

Sets shape position. This method resets shape's velocity to 0.

### Arguments

• const Math::Vec3 & position - New shape's position, in world coordinates.

## intisIdentity()

Returns a value indicating if the shape has a scale equal to 1 and no rotation.

### Return value

1 if the shape has a scale equal to 1 and no rotation; otherwise, 0.

## voidsetTransform(const Math::Mat4 & transform)

Sets a transformation matrix for the shape (in world coordinates). This matrix describes position and orientation of the shape. This method resets shape's linear velocity to 0.

### Arguments

• const Math::Mat4 & transform - Transformation matrix

## Math::Mat4getTransform()

Returns the transformation matrix of the shape (in world coordinates). This matrix describes position and orientation of the shape.

### Return value

Transformation matrix

## intgetType()

Returns the type of the shape.

### Return value

One of the SHAPE_* pre-defined variables.

## const char *getTypeName()

Returns the name of the shape type.

Type name.

## const char *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.

Shape type name.

## voidsetVelocity(const Math::vec3 & velocity, float ifps)

Sets a new velocity vector for the shape.

### Arguments

• const Math::vec3 & velocity - Velocity vector, each component represents shape's velocity along the corresponding axis, in units per second.
• float ifps - Inverse FPS value.

## Math::vec3getVelocity()

Returns the current velocity vector of the shape.

### Return value

Current celocity vector, each component represents shape's velocity along the corresponding axis, in units per second.

## floatgetVolume()

Returns the volume of the shape.

### Return value

Volume of the shape in cubic units.

## Ptr<Shape>clone()

Clones the shape.

### Return value

Copy of the shape.

## voidrenderVisualizer(const Math::vec4 & color)

Renders the shape.
Notice
You should enable the engine visualizer by the show_visualizer 1 console command.

### Arguments

• const Math::vec4 & color - Color, in which the shape will be rendered.

## intrestoreState(const Ptr<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.
Warning
This function is deprecated and will be removed in the next release.

### Arguments

• const Ptr<Stream> & stream - Stream with saved node state data.

### Return value

1 if node state is successfully restored; otherwise, 0.

## intsaveState(const Ptr<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.
Warning
This function is deprecated and will be removed in the next release.

### Arguments

• const Ptr<Stream> & stream - Stream to save node state data.

### Return value

1 if node state is successfully saved; otherwise, 0.

## voidswap(const Ptr<Shape> & shape)

Swaps the shapes saving the pointers.

### Arguments

• const Ptr<Shape> & shape - A shape to swap.

## Ptr<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 smart pointer.

## Ptr<Shape>createShape(const char * type_name)

Creates a new shape of the specified type.

### Arguments

• const char * type_name - Shape type name.

### Return value

New created shape smart pointer.

## voidrelease()

Releases the Shape (sets the owner flag to 0 for the pointer). The Shape should be handled by the class after this function is called.

## voidgrab()

Sets the owner flag to 1 for the Shape pointer. The Shape should not be handled by the class after this function is called.

## intisOwner()

Returns the owner flag. If the pointer is the owner, on its deletion the shape also will be deleted. Use grab() and release() functions to change ownership.

The owner flag.

Box.

Capsule.

Convex hull.

Cylinder.

## int SHAPE_SPHERE

### Description

Sphere.
Last update: 2018-08-10