Raycast unexpected hits

Humungo
Posts: 5
Joined: Tue Oct 30, 2012 9:34 am

Raycast unexpected hits

Post by Humungo »

Hey all,

I'm relatively new to bullet (I've been using it in my current project for the past few months), I've managed to solve most of my issues so far excluding this one issue I've been having with the btDynamicsWorld.rayTest on an "AllHitsRayResultCallback". I've set up a full 3D world and am casting a ray from the position of the camera to world objects to determine when I can pick up an object, which is fine the majority of the time, excluding when I am standing on a standard btBoxShape rigidbody and casting over the corner of the box to an object below it when they ray will -Occasionally- hit the object correctly, but slight differences in camera position and orientation will cause the ray to hit the object that the player is standing on even though it should be missing.

Here are some screenshots of my issue:
Image

Image

The lines drawn in the images are from the camera position (the ray origin), to the hit point of the ray result, and from the ray hit point to the ray hit point plus the normal of the collision. (to make the hit point slightly more obvious for testing). They seem to be hitting the plane of the box that the player is above (or beside as it works when looking up and over the box), without taking into account if this is off the box or not.
These hits are returned inconsistently and slightly rotating the camera or moving it can cause it to swap between hitting it and not hitting it (as stated above), this is additionally confusing as all other physics objects in the world (such as the other blocks shown in the image), collide exactly as expected with the ground boxes.

I've spent a fair amount of time attempting to debug this issue, and I have found that the ray is simply returning the ground box that I am standing on as the nearest collision, when it should not be one of the collisions in the result at all (through checking user pointer information).

This is the code I am using to generate the physics boxes is as follow:

Code: Select all

objects[i]->boxShape = new btBoxShape(btVector3(tempScale.x/2, tempScale.y/2, tempScale.z/2));
objects[i]->boxShape->setLocalScaling(btVector3(1,1,1));
objects[i]->body = new btRigidBody(0, 0, objects[i]->boxShape);
objects[i]->body->getWorldTransform().setOrigin(GetbtVect(tempPos));
objects[i]->body->getWorldTransform().setRotation(btQuaternion(tempRotation.x,tempRotation.y,tempRotation.z,tempRotation.w));
objects[i]->body->setUserPointer(objects[i]);
objects[i]->body->setRestitution(0.2);
objects[i]->body->setCollisionFlags(objects[i]->body->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
//objects[i]->boxShape->setMargin(0.0f);
int tempInt = COL_REG;
PhysicsMgr::Get().GetWorld().addRigidBody(objects[i]->body, COL_REG, tempInt);
All of the data used to initialise the objects is loaded in correctly from an XML level file which I have debugged through and found no issue. The ray is created as such:

Code: Select all

btVector3 tempStart = GetbtVect(mRenderer->GetCamera()->getDerivedPosition());
btVector3 tempEnd = GetbtVect(mRenderer->GetCamera()->getDerivedPosition() + (mRenderer->GetCamera()->getDerivedDirection() * mPickUpDistance));

btCollisionWorld::AllHitsRayResultCallback result(tempStart, tempEnd);
PhysicsMgr::Get().GetWorld().rayTest(tempStart, tempEnd, result);
and then I find the nearest object in the result (which is repeated if the objects is of certain types within the world) as such:

Code: Select all

for (int i = 0; i < result.m_collisionObjects.size(); ++i)
{
	// Run through the collision objects to find the closest
	float tempDist = (result.m_hitPointWorld[i] - tempStart).length2();
	if (tempDist < nearest && tempDist > currentDistance)
	{
		// The object is closer than the one stored so far and further away than the previous one
		nearest = tempDist;
		nearestObj = i;
	}
}
I don't believe there is any issues with the way that this ray is created/used as it works in every case excluding this one, if anyone has any ideas on how to fix it (or something else I may be doing wrong) it would be greatly appreciated. If you need any other information I'm happy to give more screenshots, code or videos if need be.

My last resort is to perform my own ray test against a box that I create separate from the bullet box with my own ray testing code and seeing if that hits to then exclude the result from further checks, but if it's avoidable I would prefer to not.

Thanks for your help,
Humungo
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Raycast unexpected hits

Post by Erwin Coumans »

It is not entirely clear if the ray test is very far off, or if the ray is close to hitting?

Can you double check and register the exact ray 'from' and 'to' vectors and the size and transform of the btBoxShape?
Humungo
Posts: 5
Joined: Tue Oct 30, 2012 9:34 am

Re: Raycast unexpected hits

Post by Humungo »

Thanks for the quick reply, Erwin.

The scale is set to x=4, y=1.5, z=6,

The position of the object is: 26, 3.5, -42 and it has no rotation (it's rotation is given as an identity quaternion)


As for a ray that hits the object,

the start vector is: x=22.912844, y=6.5958710, z=-40.781124

and the end vector is: x=18.730534, y=3.8580775, z=-40.649158

the hit position returned is: x=20.474958, y=5.0000000, z=-40.704201

with the x scale of the object being 4 and its position being 26, it seems it should not hit below 22 on the x axis. Also, if I move my camera below 22 on the x axis, no rays hit the box.


If I create a new ray

with the start vector: x=23.178329, y=6.5958757, z=-40.751637

and an end vector: x=19.237700, y=3.5174584, z=-40.764778
The ray misses as expected
Humungo
Posts: 5
Joined: Tue Oct 30, 2012 9:34 am

Re: Raycast unexpected hits

Post by Humungo »

My apologies, it appears I was using a slightly out of date version of bullet, but I have now updated to the most recent version on the website.

However, the issue is better, but not fixed. I'm still having the same issue, but it's now within 0.5 units of the block I'm standing on so it's less noticeable but still an issue, when I get a chance I will gather some more data.
Humungo
Posts: 5
Joined: Tue Oct 30, 2012 9:34 am

Re: Raycast unexpected hits

Post by Humungo »

Okay, I've managed to do some more testing now with the most recent bullet version, and it appears that all the hits are within 0.1 units of the boxshape. Here is an example of one of the hits against the same box as before:

The start position is x=22.577168, y=6.5958614, z=-40.682407

The end position is x=20.672050, y=1.9726791, z=-40.635212

The hit position is x=21.919546, y=5.0000000, z=-40.666115

Again, with the box scale and position, I would not have expected a hit below 22 x.

It's not a huge issue now not being able to pick up in this small gap, but it's the 'flickering' (in that the ray will sometimes hit and sometimes not at slight different positions/rotations) that is the main issue.

Hopefully these values help, I'm not sure if there's a collision/accuracy margin that I am missing or if there's some other way to solve it, but your help is greatly appreciated.