testing jump using ray cast

Post Reply
PgrAm
Posts: 12
Joined: Sun Apr 15, 2012 3:34 pm

testing jump using ray cast

Post by PgrAm »

Hi I'm writing a game using bullet and I'm having trouble testing whether or not my character should be able to jump.
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
	}
}

}
So where did I screw up and how do I fix it?

thanks :D
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: testing jump using ray cast

Post by xexuxjy »

Can't see anything obvious, though you may want to update your callback to filter out various collision groups so you're only checking for collisions against 'scenery' (assuming it has it's own group) . In your check for the hit body can you see which object it is colliding with if not the player?
You may also want to adjust the start point of your ray down by your characters bounding box so you're starting at their feet rather then the middle of their body, then you could have a shorter dest ray.
Post Reply