Difference between revisions of "R3d.ConvexMeshShape"

From GiderosMobile
(wip)
(what!)
Line 7: Line 7:
 
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.
  
 
+
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.
 
 
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.
 
 
 
There are two ways to create a ConvexMesh:
 
*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)
 
 
 
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.
 
 
 
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.
 
<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.
 
  
 
{|-
 
{|-

Revision as of 05:50, 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.

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.

Methods

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

r3d.ConvexMeshShape:setScale scales the collision convex mesh shape