easier to read Bullet to GLM conversions and back

This commit is contained in:
Andrew Meadows 2014-12-30 13:15:57 -08:00
parent 5e2246625b
commit 6f72d4ad81
5 changed files with 33 additions and 65 deletions

View file

@ -18,25 +18,20 @@
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
inline void bulletToGLM(const btVector3& b, glm::vec3& g) {
g = glm::vec3(b.getX(), b.getY(), b.getZ());
inline glm::vec3 bulletToGLM(const btVector3& b) {
return glm::vec3(b.getX(), b.getY(), b.getZ());
}
inline void bulletToGLM(const btQuaternion& b, glm::quat& g) {
g.x = b.getX();
g.y = b.getY();
g.z = b.getZ();
g.w = b.getW();
inline glm::quat bulletToGLM(const btQuaternion& b) {
return glm::quat(b.getW(), b.getX(), b.getY(), b.getZ());
}
inline void glmToBullet(const glm::vec3& g, btVector3& b) {
b.setX(g.x);
b.setY(g.y);
b.setZ(g.z);
inline btVector3 glmToBullet(const glm::vec3& g) {
return btVector3(g.x, g.y, g.z);
}
inline void glmToBullet(const glm::quat& g, btQuaternion& b) {
b = btQuaternion(g.x, g.y, g.z, g.w);
inline btQuaternion glmToBullet(const glm::quat& g) {
return btQuaternion(g.x, g.y, g.z, g.w);
}
#endif // USE_BULLET_PHYSICS

View file

@ -56,25 +56,15 @@ MotionType EntityMotionState::computeMotionType() const {
// (2) at the beginning of each simulation frame for KINEMATIC RigidBody's --
// it is an opportunity for outside code to update the object's simulation position
void EntityMotionState::getWorldTransform(btTransform& worldTrans) const {
btVector3 pos;
glmToBullet(_entity->getPositionInMeters() - ObjectMotionState::getWorldOffset(), pos);
worldTrans.setOrigin(pos);
btQuaternion rot;
glmToBullet(_entity->getRotation(), rot);
worldTrans.setRotation(rot);
worldTrans.setOrigin(glmToBullet(_entity->getPositionInMeters() - ObjectMotionState::getWorldOffset()));
worldTrans.setRotation(glmToBullet(_entity->getRotation()));
}
// This callback is invoked by the physics simulation at the end of each simulation frame...
// iff the corresponding RigidBody is DYNAMIC and has moved.
void EntityMotionState::setWorldTransform(const btTransform& worldTrans) {
glm::vec3 pos;
bulletToGLM(worldTrans.getOrigin(), pos);
_entity->setPositionInMeters(pos + ObjectMotionState::getWorldOffset());
glm::quat rot;
bulletToGLM(worldTrans.getRotation(), rot);
_entity->setRotation(rot);
_entity->setPositionInMeters(bulletToGLM(worldTrans.getOrigin()) + ObjectMotionState::getWorldOffset());
_entity->setRotation(bulletToGLM(worldTrans.getRotation()));
glm::vec3 v;
getVelocity(v);
@ -118,17 +108,17 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_
if (_outgoingPacketFlags & EntityItem::DIRTY_POSITION) {
btTransform worldTrans = _body->getWorldTransform();
bulletToGLM(worldTrans.getOrigin(), _sentPosition);
_sentPosition = bulletToGLM(worldTrans.getOrigin());
properties.setPosition(_sentPosition + ObjectMotionState::getWorldOffset());
bulletToGLM(worldTrans.getRotation(), _sentRotation);
_sentRotation = bulletToGLM(worldTrans.getRotation());
properties.setRotation(_sentRotation);
}
if (_outgoingPacketFlags & EntityItem::DIRTY_VELOCITY) {
if (_body->isActive()) {
bulletToGLM(_body->getLinearVelocity(), _sentVelocity);
bulletToGLM(_body->getAngularVelocity(), _sentAngularVelocity);
_sentVelocity = bulletToGLM(_body->getLinearVelocity());
_sentAngularVelocity = bulletToGLM(_body->getAngularVelocity());
// if the speeds are very small we zero them out
const float MINIMUM_EXTRAPOLATION_SPEED_SQUARED = 4.0e-6f; // 2mm/sec
@ -148,7 +138,7 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_
_sentMoving = false;
}
properties.setVelocity(_sentVelocity);
bulletToGLM(_body->getGravity(), _sentAcceleration);
_sentAcceleration = bulletToGLM(_body->getGravity());
properties.setGravity(_sentAcceleration);
properties.setAngularVelocity(_sentAngularVelocity);
}

View file

@ -84,29 +84,23 @@ void ObjectMotionState::setVolume(float volume) {
}
void ObjectMotionState::setVelocity(const glm::vec3& velocity) const {
btVector3 v;
glmToBullet(velocity, v);
_body->setLinearVelocity(v);
_body->setLinearVelocity(glmToBullet(velocity));
}
void ObjectMotionState::setAngularVelocity(const glm::vec3& velocity) const {
btVector3 v;
glmToBullet(velocity, v);
_body->setAngularVelocity(v);
_body->setAngularVelocity(glmToBullet(velocity));
}
void ObjectMotionState::setGravity(const glm::vec3& gravity) const {
btVector3 g;
glmToBullet(gravity, g);
_body->setGravity(g);
_body->setGravity(glmToBullet(gravity));
}
void ObjectMotionState::getVelocity(glm::vec3& velocityOut) const {
bulletToGLM(_body->getLinearVelocity(), velocityOut);
velocityOut = bulletToGLM(_body->getLinearVelocity());
}
void ObjectMotionState::getAngularVelocity(glm::vec3& angularVelocityOut) const {
bulletToGLM(_body->getAngularVelocity(), angularVelocityOut);
angularVelocityOut = bulletToGLM(_body->getAngularVelocity());
}
// RELIABLE_SEND_HACK: until we have truly reliable resends of non-moving updates
@ -154,9 +148,8 @@ bool ObjectMotionState::shouldSendUpdate(uint32_t simulationFrame, float subStep
// compute position error
glm::vec3 extrapolatedPosition = _sentPosition + dt * (_sentVelocity + (0.5f * dt) * _sentAcceleration);
glm::vec3 position;
btTransform worldTrans = _body->getWorldTransform();
bulletToGLM(worldTrans.getOrigin(), position);
glm::vec3 position = bulletToGLM(worldTrans.getOrigin());
float dx2 = glm::distance2(position, extrapolatedPosition);
const float MAX_POSITION_ERROR_SQUARED = 0.001f; // 0.001 m^2 ~~> 0.03 m
@ -173,8 +166,7 @@ bool ObjectMotionState::shouldSendUpdate(uint32_t simulationFrame, float subStep
extrapolatedRotation = glm::angleAxis(dt * spin, axis) * _sentRotation;
}
const float MIN_ROTATION_DOT = 0.98f;
glm::quat actualRotation;
bulletToGLM(worldTrans.getRotation(), actualRotation);
glm::quat actualRotation = bulletToGLM(worldTrans.getRotation());
return (glm::dot(actualRotation, extrapolatedRotation) < MIN_ROTATION_DOT);
}

View file

@ -62,9 +62,7 @@ void ShapeInfoUtil::collectInfoFromShape(const btCollisionShape* shape, ShapeInf
switch(type) {
case BOX_SHAPE: {
const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
glm::vec3 halfExtents;
bulletToGLM(boxShape->getHalfExtentsWithMargin(), halfExtents);
info.setBox(halfExtents);
info.setBox(bulletToGLM(boxShape->getHalfExtentsWithMargin()));
}
break;
case SPHERE_SHAPE: {
@ -99,9 +97,8 @@ btCollisionShape* ShapeInfoUtil::createShapeFromInfo(const ShapeInfo& info) {
const QVector<glm::vec3>& data = info.getData();
switch(info.getType()) {
case BOX_SHAPE: {
btVector3 halfExtents;
glmToBullet(data[0], halfExtents);
shape = new btBoxShape(halfExtents);
// data[0] is halfExtents
shape = new btBoxShape(glmToBullet(data[0]));
}
break;
case SPHERE_SHAPE: {
@ -110,11 +107,9 @@ btCollisionShape* ShapeInfoUtil::createShapeFromInfo(const ShapeInfo& info) {
}
break;
case CYLINDER_SHAPE: {
btVector3 halfExtents;
glmToBullet(data[0], halfExtents);
// NOTE: default cylinder has (UpAxis = 1) axis along yAxis and radius stored in X
// halfExtents = btVector3(radius, halfHeight, unused)
shape = new btCylinderShape(halfExtents);
// data[0] = btVector3(radius, halfHeight, unused)
shape = new btCylinderShape(glmToBullet(data[0]));
}
break;
case CAPSULE_SHAPE: {

View file

@ -19,8 +19,7 @@
#ifdef USE_BULLET_PHYSICS
void BulletUtilTests::fromBulletToGLM() {
btVector3 bV(1.23f, 4.56f, 7.89f);
glm::vec3 gV;
bulletToGLM(bV, gV);
glm::vec3 gV = bulletToGLM(bV);
if (gV.x != bV.getX()) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: x mismatch bullet.x = " << bV.getX() << " != glm.x = " << gV.x << std::endl;
@ -39,8 +38,7 @@ void BulletUtilTests::fromBulletToGLM() {
axis.normalize();
btQuaternion bQ(axis, angle);
glm::quat gQ;
bulletToGLM(bQ, gQ);
glm::quat gQ = bulletToGLM(bQ);
if (gQ.x != bQ.getX()) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: x mismatch bullet.x = " << bQ.getX() << " != glm.x = " << gQ.x << std::endl;
@ -61,8 +59,7 @@ void BulletUtilTests::fromBulletToGLM() {
void BulletUtilTests::fromGLMToBullet() {
glm::vec3 gV(1.23f, 4.56f, 7.89f);
btVector3 bV;
glmToBullet(gV, bV);
btVector3 bV = glmToBullet(gV);
if (gV.x != bV.getX()) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: x mismatch glm.x = " << gV.x << " != bullet.x = " << bV.getX() << std::endl;
@ -81,8 +78,7 @@ void BulletUtilTests::fromGLMToBullet() {
axis.normalize();
btQuaternion bQ(axis, angle);
glm::quat gQ;
bulletToGLM(bQ, gQ);
glm::quat gQ = bulletToGLM(bQ);
if (gQ.x != bQ.getX()) {
std::cout << __FILE__ << ":" << __LINE__
<< " ERROR: x mismatch glm.x = " << gQ.x << " != bullet.x = " << bQ.getX() << std::endl;