The character should only be able to jump If they are standing on something so I decided to cast a ray downward and if that ray hits something then they will jump. But here is my problem the ray cast seems to always succeed and the player can jump if they are touching anything not just standing on it. So here is the code I am using, its probably wrong I couldn't find much documentation on this so I was hoping I could find help here:
Code: Select all
void Player::jump()
{
if(hascollided) // precomputed value, don't want to waste time with the ray test if I already know the player is in the air
{
btVector3 start = body->getCenterOfMassPosition(); // get the player's position
btVector3 dest = start;
dest.setY(dest.getY() - 2.0f); // make the destination of the ray 2 units below the player
fprintf(LogFile, "ray start: %f %f %f\n", start.getX(), start.getY(), start.getZ()); // log data for debug purposes
fprintf(LogFile, "ray end: %f %f %f\n", dest.getX(), dest.getY(), dest.getZ()); // log data for debug purposes
btCollisionWorld::ClosestRayResultCallback ray(start, dest); // Do the ray test (I think?)
dynamicsWorld->rayTest(start, dest, ray);
if (ray.hasHit())
{
fprintf(LogFile, "hit something\n");
btRigidBody* hit = btRigidBody::upcast(ray.m_collisionObject); // find what the ray hit
if(hit != body) // If the ray doesn't hit the player itself
{
btVector3 velocity = body->getLinearVelocity();
velocity.setY(velocity.getY() + 4.0f);
body->setLinearVelocity(velocity);
jumping = true;
}
}
else
{
fprintf(LogFile, "missed\n"); // log data for debug purposes
}
}
}
thanks