[SOLVED] Can't get ray picking/ray casting to work

Post Reply
helpless
Posts: 3
Joined: Tue Oct 25, 2016 12:42 pm

[SOLVED] Can't get ray picking/ray casting to work

Post by helpless »

I can't manage to get my ray picking to work.

This is the code that initiates Bullet (code is literally copied from documentation, although split up in source and header files):

Code: Select all

collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
overlappingPairCache = new btDbvtBroadphase();
solver = new btSequentialImpulseConstraintSolver();
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
The above is located in my Physics class. Then I create a collision shape in one of my game objects:

Code: Select all

// Sphere, radius 2
collisionShape = new btSphereShape(2.0f);
// No rotation, and worldPos _ALWAYS_ equals -20,0,0
motionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,0), btVector3(worldPos.x, worldPos.y, worldPos.z)));
// No weight since it's a static object. Just using Bullet to pick it.
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, motionState, collisionShape, btVector3(0,0,0));
rigidBody = new btRigidBody(rigidBodyCI);
rigidBody->setUserPointer(this);
// This function just passes rigidbody to dynamicsWorld->addRigidBody()
engine->Physics()->addRigidBody(rigidBody); 
Have I done everything correctly so far? I've tried to add a collision sphere (radius 2) to the position of the game object in world space.

I've spent literally two days trying to get this picking working, but all to no avail. Just now I tried to pick using a static ray, just to verify that it worked, but it didn't. This is the code for that (located in the Physics class):

Code: Select all

btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(0,0,0), btVector3(-30, 0, 0));
dynamicsWorld->rayTest(btVector3(0,0,0), btVector3(-30, 0, 0), RayCallback);

if(RayCallback.hasHit())
    std::cout << "Hit!\n";
else
    std::cout << "Miss.\n";
The above code tests using a ray starting at 0,0,0 and ending at -30,0,0. This should, if I'm not really misunderstanding something, intersect the collision sphere, yet I never get a hit. Why is that?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Can't get ray picking/ray casting to work

Post by benelot »

Hello helpless,

First of all, where is your worldPos? I can not see it being defined anywhere. I assume from the comment that it is always -20,0,0.

Second, even though it does not apply to your situation but it is good to know: A ray does only work from outside to the inside, otherwise it does not generate a hit. Imagine a ball that you can not hit when starting the line from inside but only when starting from outside.

Third, your quaternion is illposed. A unity quaternion is btQuaternion(0,0,0,1), you can also get this from btQuaternion::getIdentity() (or similar, you will find out).

Can you check if the quaternion problem solved fixes your issue?

Otherwise, just start from the ExampleBrowser RaytestDemo, which shows successful raycasting.
helpless
Posts: 3
Joined: Tue Oct 25, 2016 12:42 pm

Re: Can't get ray picking/ray casting to work

Post by helpless »

benelot wrote:Third, your quaternion is illposed.
Yeah, thanks for pointing that out! I had misunderstood how quaternions worked, but now that I set it to an identity (didn't even know they existed), it works.

Thank you very much!
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Can't get ray picking/ray casting to work

Post by benelot »

Quaternions are crazy and super non-intuitive, but very powerful. I had the same issues are you as well. Another issue happening to me very often is to think that they get properly initialized without any intervention. That is why I know about identity.
Post Reply