Skip to content

Instantly share code, notes, and snippets.

@indefinit
Created January 9, 2015 05:25
Show Gist options
  • Save indefinit/825eceffc50bd5fa44ac to your computer and use it in GitHub Desktop.
Save indefinit/825eceffc50bd5fa44ac to your computer and use it in GitHub Desktop.
testing bullet physics collision in Cinder
Voxel* VoxelMapperApp::findVoxel(btRigidBody* pBody) {
// search through our list of gameobjects finding
// the one with a rigid body that matches the given one
for (std::vector<Voxel>::iterator iter = mVoxels.begin(); iter != mVoxels.end(); ++iter) {
if ((iter)->getPhyObj().get()->getRigidBody().get() == pBody) {
// found the body, so return the corresponding Voxel object
return &(*iter);//this feels janky!
}
}
return 0;
}
void VoxelMapperApp::onCollisionBegin( btRigidBody *rigid0, btRigidBody *rigid1 )
{
using namespace bullet;
Voxel *phyObj0 = findVoxel( (btRigidBody*) rigid0);
phyObj0->setCol(ColorAT<float>(0,1,0));//arbitrary color green
Voxel *phyObj1 = findVoxel( (btRigidBody*) rigid1);
phyObj1->setCol(ColorAT<float>(0,0,1));//arbitrary color blue
}
void VoxelMapperApp::onCollisionEnd( btRigidBody *rigid0, btRigidBody *rigid1 )
{
using namespace bullet;
Voxel *phyObj0 = findVoxel( (btRigidBody*) rigid0);
phyObj0->setCol(ColorAT<float>(1,1,0));
Voxel *phyObj1 = findVoxel( (btRigidBody*) rigid1);
phyObj1->setCol(ColorAT<float>(1,0,0));
}
void VoxelMapperApp::createVoxel(const glm::vec3 &pos)
{
auto scale = vec3( 1, 1, 1 );
// We create a Cube batch. The cube's points are conveniently from 0, 0, 0 origin, with a scale of 1, 1, 1.
auto batch = gl::Batch::create( geom::Cube(), gl::getStockShader( gl::ShaderDef().color() ) );
// All I need to do is create a Box Shape at a scale of 1, 1, 1.
auto rigidBody = bullet::RigidBody::create( bullet::RigidBody::Format()
.collisionShape( bullet::createSphereShape( scale.x ) )
.mass( 0 )
.initialPosition( pos )
.addToWorld( true ) );
// Then put them together in the Voxel object.
mVoxels.emplace_back( batch, rigidBody );
rigidBody->setUserPointerRigidBody(&mVoxels.back());
}
void VoxelMapperApp::createMovable(const glm::vec3 &pos)
{
auto scale = vec3( 1, 1, 1 );
// We create a Cube batch. The cube's points are conveniently from 0, 0, 0 origin, with a scale of 1, 1, 1.
auto batch = gl::Batch::create( geom::Cube(), gl::getStockShader( gl::ShaderDef().color() ) );
// All I need to do is create a Box Shape at a scale of 1, 1, 1.
auto rigidBody = bullet::RigidBody::create( bullet::RigidBody::Format()
.collisionShape( bullet::createBoxShape( scale ) )
.mass( 1 )
.initialPosition( pos )
.addToWorld( true ) );
// Then put them together in the Voxel object.
mVoxels.emplace_back( batch, rigidBody );
rigidBody->setUserPointerRigidBody(&mVoxels.back());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment