Everything worked normal until I decided to add friction, so character would stop immediately but then a problem appeared. Now when I move the cube, sometimes it goes up and down a little, even though when I set linear velocity, y is always zero. Maybe I'm interpreting friction wrong? How is it measured?
I also have one other problem, the spheres, even though I made sure they have precise bounding box, are a little bit sunk on the ground plane.
This is how I add box:
Code: Select all
void WindowsPhysicsManager::addBox(Node* node) {
Physics* pa = static_cast<Physics*>(node->getResource(Resource::PHYSICS));
BoundingBox* bb = static_cast<BoundingBox*>(pa->getBoundingVolume());
Vec3& box = bb->getSizes();
btCollisionShape* groundShape = new btBoxShape(
btVector3(box.getX() * 0.5f, box.getY() * 0.5f, box.getZ() * 0.5f));
collisionShapes_.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
Vec3& origin = bb->getCenter();
groundTransform.setOrigin(
btVector3(
node->getPos().getX(),
node->getPos().getY(),
node->getPos().getZ()));
btVector3 localInertia(0,0,0);
if (pa->getMass() > GHOST_DELTA) {
groundShape->calculateLocalInertia(pa->getMass(), localInertia);
}
btDefaultMotionState* myMotionState =
new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(
pa->getMass(), myMotionState, groundShape, localInertia);
rbInfo.m_friction = 1.0f;
btRigidBody* body = new btRigidBody(rbInfo);
body->setUserPointer(static_cast<void*>(node));
dynamicsWorld_->addRigidBody(body);
}
Code: Select all
void WindowsPhysicsManager::addSphere(Node* node) {
Physics* pa = static_cast<Physics*>(node->getResource(Resource::PHYSICS));
BoundingSphere* bs = static_cast<BoundingSphere*>(pa->getBoundingVolume());
btCollisionShape* colShape = new btSphereShape(
btScalar(bs->getRadius()));
collisionShapes_.push_back(colShape);
btTransform startTransform;
startTransform.setIdentity();
Vec3& origin = bs->getCenter();
startTransform.setOrigin(
btVector3(
node->getPos().getX(),
node->getPos().getY(),
node->getPos().getZ()));
bool isDynamic = (pa->getMass() > GHOST_DELTA);
btVector3 localInertia(0,0,0);
if (isDynamic) {
colShape->calculateLocalInertia(pa->getMass(), localInertia);
}
btDefaultMotionState* myMotionState =
new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(
pa->getMass(), myMotionState, colShape,localInertia);
rbInfo.m_angularDamping = 1.0f;
btRigidBody* body = new btRigidBody(rbInfo);
body->setUserPointer(static_cast<void*>(node));
dynamicsWorld_->addRigidBody(body);
}
Code: Select all
void WindowsPhysicsManager::move(Node* node, Vec3& pos) {
btCollisionObjectArray arr = dynamicsWorld_->getCollisionObjectArray();
int size = arr.size();
for (int i = 0; i < size; i++) {
if (arr[i]->getUserPointer() == node) {
//btTransform tr;
//tr.setIdentity();
//tr.setOrigin(btVector3(pos.getX(), pos.getY(), pos.getZ()));
btRigidBody* body = btRigidBody::upcast(arr[i]);
body->activate();
//body->applyCentralForce(btVector3(0.0f, 10000.0f, 0.0f));
body->setLinearVelocity(
btVector3(pos.getX(), pos.getY(), pos.getZ()));
//body->setCenterOfMassTransform(tr);
}
}
}
Any help would be appreciated.