Bullet physics collisions with IMeshBuffer

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
DanzoSD
Posts: 6
Joined: Tue Jul 23, 2013 6:03 am
Location: EU
Contact:

Bullet physics collisions with IMeshBuffer

Post by DanzoSD »

Implementation of Bullet's btStridingMeshInterface for doing collsions with triangles in IMeshBuffer*.

Simple implementation that has been tested with the following:
* vertex type must be irr::video::EVT_STANDARD, i.e. irr::video::S3DVertex* pointers
* 16 or 32 bit vertex indexes
* default meshes, meaning no hardware mesh buffers, propably.

CVteStridingMeshItrf.h:

Code: Select all

 
#ifndef __VTE_MESH_UTILITIES_H__
#define __VTE_MESH_UTILITIES_H__
 
#include <btBulletDynamicsCommon.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
class CVteStridingMeshItrf : public btStridingMeshInterface {
public:
    CVteStridingMeshItrf( IMeshBuffer* mbfr = 0 );
    CVteStridingMeshItrf& setMeshBuffer( IMeshBuffer* mbfr );
    virtual void getLockedVertexIndexBase( unsigned char** vertexbase, int& numverts, 
            PHY_ScalarType& type, int& stride, unsigned char** indexbase, int& indexstride, 
            int& numfaces, PHY_ScalarType& indicestype, int subpart=0 );
    virtual void unLockVertexBase( int subpart ) {}
    //
    virtual void preallocateVertices( int numverts ) {return;}
    virtual void preallocateIndices( int numindices ) {return;}
    virtual int getNumSubParts()const {return 1;}
    virtual void getLockedReadOnlyVertexIndexBase(
            const unsigned char** vertexbase, int& numverts, PHY_ScalarType& type,
            int& stride, const unsigned char** indexbase, int& indexstride,
            int& numfaces, PHY_ScalarType& indicestype, int subpart=0 )const
    {
        ((CVteStridingMeshItrf*)this)->getLockedVertexIndexBase(
                (unsigned char**)vertexbase, numverts, type, stride,
                (unsigned char**)indexbase, indexstride, numfaces, indicestype, subpart );
    }
    virtual void unLockReadOnlyVertexBase( int subpart )const{
        ((CVteStridingMeshItrf*)this)->unLockVertexBase(subpart);
    }
private:
    IMeshBuffer* Mbfr;
};
 
#endif //__VTE_MESH_UTILITIES_H__
 
CVteStridingMeshItrf.cpp:

Code: Select all

 
#include "CVteStridingMeshItrf.h"
#include <stdio.h>
#include <assert.h>
 
CVteStridingMeshItrf::CVteStridingMeshItrf( IMeshBuffer* mbfr )
    : Mbfr(0)
{
    if(mbfr)
        setMeshBuffer(mbfr);
}
CVteStridingMeshItrf& CVteStridingMeshItrf::setMeshBuffer( IMeshBuffer* mbfr )
{
    assert(mbfr);
    Mbfr = mbfr;
    assert( Mbfr->getVertexType() == EVT_STANDARD );
    assert( !(Mbfr->getIndexCount() % 3) );
    return *this;
}
void CVteStridingMeshItrf::
getLockedVertexIndexBase( unsigned char** vrtBase, int& nVrts, PHY_ScalarType& type,
        int& stride, unsigned char** idxBase, int& idxStrd, int& nFcs,
        PHY_ScalarType& indicesType, int subpart )
{
    assert(Mbfr);
    S3DVertex* vrts = (S3DVertex*)Mbfr->getVertices();
    core::vector3df* vecAPtr = &vrts->Pos;
    *vrtBase = (unsigned char*)vecAPtr;
    nVrts    = Mbfr->getVertexCount();
    stride   = sizeof(S3DVertex);
    type     = PHY_FLOAT;
    //
    *idxBase    = (unsigned char*)Mbfr->getIndices();
    nFcs        = Mbfr->getIndexCount() / 3;
    idxStrd     = Mbfr->getIndexType() == EIT_16BIT ? sizeof(u16)*3 : sizeof(u32)*3;
    indicesType = Mbfr->getIndexType() == EIT_16BIT ? PHY_SHORT : PHY_INTEGER;
}
 
Post Reply