Hi,
I'm new and trying to do much the same you were. I thought I could ask a few questions.
The code I am using doesn't detect collisions on the btheightfield (I'm sure due to my lack of knowledge) and so I instantly start to plummet, also, when I stop pressing 'w' for forward my ghost object continues to move away from my rendering cam (I want them to be in sync). Here is the code that I thought would just automatically test for collisions (granted it is a patchwork):
Code: Select all
void Character::Move(btKinematicCharacterController* theCharacter,btGhostObject* m_ghostObject, const btTransform &charTransform,scene::C3DCamera* cam,const btDiscreteDynamicsWorld& world,bool gForward, bool gBackward, bool gLeft, bool gRight)
{
///set walkDirection for our character
btTransform xform;
xform = charTransform;
btVector3 forwardDir = xform.getBasis()[2];
// printf("forwardDir=%f,%f,%f\n",forwardDir[0],forwardDir[1],forwardDir[2]);
btVector3 upDir = xform.getBasis()[1];
btVector3 strafeDir = xform.getBasis()[0];
forwardDir.normalize ();
upDir.normalize ();
strafeDir.normalize ();
btVector3 walkDirection = btVector3(0.0, 0.0, 0.0);
btScalar walkVelocity = btScalar(1.1); // 4 km/h -> 1.1 m/s
btScalar walkSpeed = walkVelocity;
//use the convex sweep test to find a safe theCharacterPositionition for the camera (not blocked by static geometry)
btVector3 cameraPosition = tk.ConvertToBtVector(cam->getPosition() );
btTransform cameraFrom,cameraTo;
cameraFrom.setIdentity();
cameraFrom.setOrigin(xform.getOrigin());
cameraTo.setIdentity();
cameraTo.setOrigin(cameraPosition);
btCollisionWorld::ClosestConvexResultCallback cb( xform.getOrigin(), cameraTo.getOrigin() );
cb.m_collisionFilterMask = btBroadphaseProxy::StaticFilter;
btSphereShape cameraSphere(0.2f);
world.convexSweepTest(&cameraSphere,cameraFrom,cameraTo,cb);
if (cb.hasHit()) // I never get inside here
{
m_useWalkDirection = false;
btScalar minFraction = cb.m_closestHitFraction;//btMax(btScalar(0.3),cb.m_closestHitFraction);
cameraPosition.setInterpolate3(cameraFrom.getOrigin(),cameraTo.getOrigin(),minFraction);
cameraPosition.rotate(btVector3( 0, 1, 0), walkDirection.getY() );
m_useWalkDirection = true
}
//rotate view
if (gLeft)
{
btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),0.01));
m_ghostObject->getWorldTransform ().setBasis(orn);
}
if (gRight)
{
btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),-0.01));
m_ghostObject->getWorldTransform ().setBasis(orn);
}
if (gForward)
walkDirection += forwardDir;
if (gBackward)
walkDirection -= forwardDir;
gForward = false;
gBackward = false;
gLeft= false;
gRight = false;
theCharacter->setWalkDirection(walkDirection*walkSpeed);
cam->setPosition(tk.ConvertToLfVector(xform.getOrigin() ) );
}
Thanks for a push in the right direction.