Physics Simulation Forum

 

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Apr 15, 2012 3:51 pm 
Offline

Joined: Sun Apr 15, 2012 3:34 pm
Posts: 8
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:
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


Top
 Profile  
 
PostPosted: Mon Apr 16, 2012 11:15 am 
Offline

Joined: Wed Jan 07, 2009 11:43 am
Posts: 98
Location: London
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group