raylib adding a static terrain (ODE)

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!

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!

Leave a Reply

Your email address will not be published. Required fields are marked *