Posts
Search
Contact
Cookies
About
RSS

raylib adding a static terrain (ODE)

Added 14 Jun 2019, 11:17 a.m. edited 18 Jun 2023, 1:12 a.m.

Last time we were colliding with just a simple ground plane, while this is okay, what looks at together better (and more useful) is a static mesh instead!

https://youtu.be/JOo8FVO_Gdg

Unusually this time there is no code to download :-o but wait don't worry because by the end you'll be able to take the code from the last tutorial and add a static mesh for yourself!

After initializing ODE, we can load up our static mesh, but first we must build some fake vertex indices - if your model does get loaded with indices you will have to convert them from the Mesh array of shorts to an array of integers, as I'm using a Wavefront OBJ then the vertex array is a simple flat array, alas ODE insists on having indices....

    // create some decidedly sub optimal indices!
    int nV = terrain.meshes[0].vertexCount;
    int *groundInd = RL_MALLOC(nV*sizeof(int));
    for (int i = 0; i<nV; i++ ) {
        groundInd[i] = i;
    }

It's not great but what ya gonna do! Our next step is deceptively simple but years ago I remember it causing be no end of grief...

    dTriMeshDataID triData = dGeomTriMeshDataCreate();
    dGeomTriMeshDataBuildSingle(triData, terrain.meshes[0].vertices,
                            3 * sizeof(float), nV,
                            groundInd, nV,
                            3 * sizeof(int));
    dCreateTriMesh(space, triData, NULL, NULL, NULL);

of course you'll need to free groundInd before you exit the application... dCreateTriMesh returns a dGeom, however in this instance we have no need for it, so we simply discard it...

There you go, not too difficult, but a really useful addition to any "world"

Enjoy!