Difference between revisions of "R3d.ConvexMeshShape"

From GiderosMobile
(wip)
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
  
 
=== Description ===
 
=== Description ===
The ConvexMeshShape class can be used to describe the shape of a convex mesh centered at the origin of the collider.
+
The ConvexMeshShape class can be used to describe the shape of a convex mesh centered at the origin of the collider. An example of a ConvexMeshShape is a disco ball.
  
 +
In order to create a ConvexMeshShape, you first need to create an array of PolygonFace to describe each face of your mesh. You also need to have an array with the vertices coordinates and an array with the vertex indices of each face of your mesh. Then, you have to create a PolygonVertexArray with your vertices coordinates and indices array. You also need to specify your array of PolygonFace. Then, you have to create a PolyhedronMesh with your PolygonVertexArray. Once this is done, you can create the ConvexMeshShape by passing your PolyhedronMesh in parameter.
  
 +
'''The vertex coordinates and indices array are copied into the ConvexMesh. Therefore, you can release the memory of the PolygonVertexArray after the ConvexMesh creation. However, the mesh data is stored inside the ConvexMesh. Therefore, you should not release it until all the ConvexMeshShapes that have been instantiated from it are destroyed'''
  
In order to create a ConvexMeshShape, we first need to create a ConvexMesh. A ConvexMesh can be instanciated once and used to create multiple ConvexMeshShapes with different scaling if necessary.
+
'''You need to make sure that the mesh you provide is indeed convex'''
  
There are two ways to create a ConvexMesh:
+
'''You should provide the simplest possible convex mesh. It means that you need to avoid coplanar faces in your convex mesh shape. Coplanar faces have to be merged together. Remember that convex meshes are not limited to triangular faces, you can create faces with more than three vertices'''
*If you know the vertices and faces of your mesh, you can use the following method to create a ConvexMesh:
 
  
PhysicsCommon::createConvexMesh(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& messages)
+
'''Meshes with duplicated vertices are not supported. The number of vertices you pass to create the PolygonVertexArray must be exactly the number of vertices in your convex mesh. The PhysicsCommon::createConvexMesh() will also report errors if some faces of your mesh have almost zero area (degenerated face). You will need to fix those errors in order to create the ConvexMesh'''
  
To use this method, you need to create an array of PolygonFace to describe each face of your mesh. You also need to have an array with the vertices coordinates and an array with the vertex indices of each face of you mesh. Then, you have to create a PolygonVertexArray with your vertices coordinates and indices array. You also need to specify your array of PolygonFace. Then, you can call the previous method to instantiate a ConvexMesh using your PolygonVertexArray.
+
'''When you specify the vertices for each face of your convex mesh, be careful with their order. The vertices of a face must be specified in counter clockwise order as seen from the outside of your convex mesh'''
  
The following example shows how to create a ConvexMeshShape when you know the vertices and faces of your mesh. In this example, we create a cube as a convex mesh shape. Of course, this is only for the example. If you really need a cube collision shape, you should use the BoxShape instead.
+
'''You also need to make sure that the origin of your mesh is inside the convex mesh. A mesh with an origin outside the convex mesh is not currently supported by the library'''
<syntaxhighlight lang="c">
 
// Array with the vertices coordinates of the convex mesh
 
float vertices[24] = {-3, -3, 3,
 
3, -3, 3,
 
3, -3, -3,
 
-3, -3, -3,
 
-3, 3, 3,
 
3, 3, 3,
 
3, 3, -3,
 
-3, 3, -3};
 
// Array with the vertices indices for each face of the mesh
 
int indices[24] = {0, 3, 2, 1,
 
4, 5, 6, 7,
 
0, 1, 5, 4,
 
1, 2, 6, 5,
 
2, 3, 7, 6,
 
0, 4, 7, 3};
 
// Description of the six faces of the convex mesh
 
PolygonVertexArray::PolygonFace* polygonFaces = new PolygonVertexArray::PolygonFace[6];
 
PolygonVertexArray::PolygonFace* face = polygonFaces;
 
for (int f = 0; f < 6; f++) {
 
// First vertex of the face in the indices array
 
face->indexBase = f * 4;
 
// Number of vertices in the face
 
face->nbVertices = 4;
 
face++;
 
}
 
// Create the polygon vertex array
 
PolygonVertexArray* polygonVertexArray = new PolygonVertexArray(
 
8, vertices, 3 * sizeof(float), indices, sizeof(int), 6, polygonFaces,
 
PolygonVertexArray::VertexDataType::VERTEX_FLOAT_TYPE,
 
PolygonVertexArray::IndexDataType::INDEX_INTEGER_TYPE);
 
// Create the convex mesh
 
std::vector<rp3d::Message> messages;
 
ConvexMesh* convexMesh = physicsCommon.createConvexMesh(polygonVertexArray, messages);
 
// Display the messages (info, warning and errors)
 
if (messages.size() > 0) {
 
for (const rp3d::Message& message: messages) {
 
std::string messageType;
 
switch(message.type) {
 
case rp3d::Message::Type::Information:
 
messageType = "info";
 
break;
 
case rp3d::Message::Type::Warning:
 
messageType = "warning";
 
break;
 
case rp3d::Message::Type::Error:
 
messageType = "error";
 
break;
 
}
 
std::cout << "Message (" << messageType << "): " << message.text << std::endl;
 
}
 
}
 
// Make sure there was no errors during mesh creation
 
assert(convexMesh != nullptr);
 
</syntaxhighlight>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
In order to create a convex mesh shape, you first need to create an array of PolygonFace to describe each face of your mesh. You also need to have an array with the vertices coordinates and an array with the vertex indices of each face of your mesh. Then, you have to create a PolygonVertexArray with your vertices coordinates and indices array. You also need to specify your array of PolygonFace. Then, you have to create a PolyhedronMesh with your PolygonVertexArray. Once this is done, you can create the ConvexMeshShape by passing your PolyhedronMesh in parameter.
 
  
 
{|-
 
{|-

Latest revision as of 06:03, 19 December 2025

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2019.10

Description

The ConvexMeshShape class can be used to describe the shape of a convex mesh centered at the origin of the collider. An example of a ConvexMeshShape is a disco ball.

In order to create a ConvexMeshShape, you first need to create an array of PolygonFace to describe each face of your mesh. You also need to have an array with the vertices coordinates and an array with the vertex indices of each face of your mesh. Then, you have to create a PolygonVertexArray with your vertices coordinates and indices array. You also need to specify your array of PolygonFace. Then, you have to create a PolyhedronMesh with your PolygonVertexArray. Once this is done, you can create the ConvexMeshShape by passing your PolyhedronMesh in parameter.

The vertex coordinates and indices array are copied into the ConvexMesh. Therefore, you can release the memory of the PolygonVertexArray after the ConvexMesh creation. However, the mesh data is stored inside the ConvexMesh. Therefore, you should not release it until all the ConvexMeshShapes that have been instantiated from it are destroyed
You need to make sure that the mesh you provide is indeed convex
You should provide the simplest possible convex mesh. It means that you need to avoid coplanar faces in your convex mesh shape. Coplanar faces have to be merged together. Remember that convex meshes are not limited to triangular faces, you can create faces with more than three vertices
Meshes with duplicated vertices are not supported. The number of vertices you pass to create the PolygonVertexArray must be exactly the number of vertices in your convex mesh. The PhysicsCommon::createConvexMesh() will also report errors if some faces of your mesh have almost zero area (degenerated face). You will need to fix those errors in order to create the ConvexMesh
When you specify the vertices for each face of your convex mesh, be careful with their order. The vertices of a face must be specified in counter clockwise order as seen from the outside of your convex mesh
You also need to make sure that the origin of your mesh is inside the convex mesh. A mesh with an origin outside the convex mesh is not currently supported by the library

Methods

r3d.ConvexMeshShape.new creates a new collision convex mesh shape

r3d.ConvexMeshShape:setScale scales the collision convex mesh shape