Simple Mesh-Mesh collision

View previous topic View next topic Go down

Simple Mesh-Mesh collision

Post by templar_vii on Wed Dec 09, 2015 1:09 pm

This is a simple example how to perform a Mesh-Mesh collision.
I'm not sure if I've done all right, but it's working pretty good.
The version used is 1.3.

Code:

void CheckCollision()
{
    IceMaths::Point vertices0[] =
    {
          Point(0, 0, 0),
          Point(1, 0, 0),
          Point(1, 1, 0),
          Point(0, 1, 0)
    };

    IceMaths::Point vertices1[] =
    {
          Point(0, 0, 1),
          Point(1, 0, 1),
          Point(1, 1, 1),
          Point(0, 1, 1)
    };

    Matrix4x4* transformation0 = new Matrix4x4(
          1, 0, 0, 0,
          0, 1, 0, 0,
          0, 0, 1, 0,
          0, 0, 0, 1);

    Matrix4x4* transformation1 = new Matrix4x4(
          1, 0, 0, 0,
          0, 1, 0, 0,
          0, 0, 1, 0,
          0, 0, 0, 1);

    IndexedTriangle indices[] =
    {
          *(new IndexedTriangle(0, 1, 2)),
          *(new IndexedTriangle(0, 2, 3))
    };

    MeshInterface* mesh0 = GetMeshInterface(indices, vertices0, 2, 4);
    MeshInterface* mesh1 = GetMeshInterface(indices, vertices1, 2, 4);
 
    Model* model0 = GetModel(mesh0);
    Model* model1 = GetModel(mesh1);

    BVTCache* cache = new BVTCache();
    cache->Model0 = model0;
    cache->Model1 = model1;

    AABBTreeCollider* collider = new AABBTreeCollider();
    bool checkDone = collider->Collide(*cache, transformation0, transformation1);
    Assert::IsTrue(checkDone);

    bool contactFound = collider->GetContactStatus();
    Assert::IsFalse(contactFound);
}

Model* GetModel(MeshInterface* mesh)
{
    BuildSettings* buildSettings = new BuildSettings();
    buildSettings->mLimit = 1;
    buildSettings->mRules = SPLIT_BEST_AXIS | SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER;

    OPCODECREATE* data = new OPCODECREATE();
    data->mCanRemap = false;
    data->mIMesh = mesh;
    data->mKeepOriginal = false;
    data->mNoLeaf = false;
    data->mQuantized = true;
    data->mSettings = *buildSettings;

    Model* model = new Model();
    bool modelCreated = model->Build(*data);
    Assert::IsTrue(modelCreated);
 
    return model;
}

MeshInterface* GetMeshInterface(IndexedTriangle* indices, Point* vertices, udword nrTriangles, udword nrVertices)
{
    MeshInterface* meshInterface = new MeshInterface();

    meshInterface->SetNbTriangles(nrTriangles);
    meshInterface->SetNbVertices(nrVertices);
 
    bool pointersSet = meshInterface->SetPointers(indices, vertices);
    Assert::IsTrue(pointersSet);

    udword degeneratedFaces = meshInterface->CheckTopology();
    Assert::IsTrue(degeneratedFaces == 0);
    Assert::IsTrue(meshInterface->IsValid());

    return meshInterface;
}

templar_vii
Admin

Posts : 7
Join date : 2015-12-09

View user profile http://opcode.forumotion.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum