Skip to content

Instantly share code, notes, and snippets.

@mmmovania
Last active August 29, 2015 13:57
void AddRigidBodies() {
//add the falling box
{
hkVector4 halfExtents(0.5f, 0.5f, 0.125f);
hkpBoxShape* boxShape = new hkpBoxShape(halfExtents);
boxShape->setRadius(0.001f);
hkpRigidBodyCinfo ci;
ci.m_shape = boxShape;
ci.m_motionType = hkpMotion::MOTION_DYNAMIC;
const hkReal boxMass(10.0f);
hkMassProperties massProps;
hkpInertiaTensorComputer::computeShapeVolumeMassProperties(boxShape, boxMass, massProps);
ci.setMassProperties(massProps);
for(int i=0;i<MAX_BOXES;++i) {
if(i==(MAX_BOXES-1))
ci.m_motionType = hkpMotion::MOTION_FIXED;
ci.m_position = hkVector4(0.0f,1.0f+i*1.125f,0.0f);
hkpRigidBody* rigidBody = new hkpRigidBody(ci);
boxes.push_back(static_cast<hkpRigidBody*>(g_pWorld->addEntity(rigidBody)));
}
boxShape->removeReference();
//now add springs between adjacent boxes
hkpStiffSpringConstraintData* spring = new hkpStiffSpringConstraintData();
hkpStiffSpringConstraintData* spring2 = new hkpStiffSpringConstraintData();
for(size_t i=0;i<boxes.size()-1;i++) {
hkTransform b1 = boxes[i]->getTransform();
hkTransform b2 = boxes[i+1]->getTransform();
hkVector4f t1 = b1.getTranslation();
hkVector4f t2 = b2.getTranslation();
t1.add(hkVector4f(-0.5,0.5,0));
t2.add(hkVector4f(-0.5,-0.5,0));
spring->setInWorldSpace(b1,b2, t1, t2);
{
hkpConstraintInstance* constraint = new hkpConstraintInstance(boxes[i], boxes[i+1], spring );
g_pWorld->addConstraint(constraint);
constraint->removeReference();
}
t1.add(hkVector4f(1,0,0));
t2.add(hkVector4f(1,0,0));
spring2->setInWorldSpace(b1,b2, t1, t2);
{
hkpConstraintInstance* constraint = new hkpConstraintInstance(boxes[i], boxes[i+1], spring2 );
g_pWorld->addConstraint(constraint);
constraint->removeReference();
}
}
spring->removeReference();
spring2->removeReference();
}
//create the static box where the smaller box will fall
{
hkVector4 halfExtents(20.0f, 2.0f, 20.f);
hkpBoxShape* boxShape = new hkpBoxShape(halfExtents);
hkpRigidBodyCinfo ci;
ci.m_shape = boxShape;
ci.m_position = hkVector4(0, -2, 0);
ci.m_motionType = hkpMotion::MOTION_FIXED;
boxShape->setRadius(0.001f);
hkpRigidBody* rigidBody = new hkpRigidBody(ci);
boxShape->removeReference();
g_pWorld->addEntity(rigidBody)->removeReference();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment