[SOLVED] Can't get ray picking/ray casting to work
Posted: Tue Oct 25, 2016 12:45 pm
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):
The above is located in my Physics class. Then I create a collision shape in one of my game objects:
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):
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?
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);
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);
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";