Raytest seems to be inconsistent?

Post Reply
Brian Beuken
Posts: 46
Joined: Sat Jul 29, 2017 9:19 pm

Raytest seems to be inconsistent?

Post by Brian Beuken »

Thought this might be helpful to others as it confused me for a few days.

I am trying to do a broad occlude on some characters in my demo, using a ray cast from my main player character (which will usually be fps, so a camera) to the position of the character being tested. So I pass Player and Enemy positions as the from, to values in the ray cast.

I switched myself to overhead view so I could visualize my ray and indeed the ray is going from the player to the test enemy.

My occlusion test is simple, if my ray hits a wall before it hits the target character, I don't draw the character saving some time.

I've positioned myself in such a way that I have 2 walls between me and a test character, which is stationary. and a line drawn between them demonstrates that my test character is clearly not visible to the player as 2 walls are in the way. I draw a red line from my player to the m_hitPointWorld to see if it has indeed detected the wall.

The Ray cast is detecting a hit with ray.hasHit(), but seems to sometimes hit the wall, and sometimes report a contact with my player itself (a distance of 0), resulting in no red line and confusion over whether to draw the character, resulting in random flashing of the character on screen

I couldn't make head nor tail of it, 2 stationary objects with 2 walls between them, sometimes the ray cast reports hitting the wall, other times it reports hitting the source player...which means I can't really tell when I should draw the test model or not.

eventually I realised that as my ray was sourcing from the player (normally the camera) it was simply detecting it hit the players collision shell from the inside. I worked out the normalized direction and offset the start of the ray to be outside the capsule...

Code: Select all

      btVector3 Position  = btVector3(Player->WorldPosition.x, Player->WorldPosition.y, Player->WorldPosition.z);
		btVector3 PPosition  = btVector3(WorldPosition.x, WorldPosition.y, WorldPosition.z);
		btVector3 Direction = PPosition - Position;
		Direction.normalize();
		Position = Position + (Direction*3); // move it to the edge of the capsule, if the ray starts inside it can be random
And that stopped the casts being returned on my player and the broad occlusion now works fine.
I can't quite find a specific flag that will eliminate the source character from collision tests to simplify things, is there one?

I hope this is helpful to others having issues with raycasts
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Raytest seems to be inconsistent?

Post by Dirk Gregorius »

This is actually a common problem with physics integration into the game. I usually wrap the raycast API of the physics engine and allow the user to specify the filter plus a list of ignored entities. E.g in your case I would call something like:

pScene->CastRay( RayStart, RayEnd, Filter, PlayerID );

The additional filtering is usually implemented in a custom callback function.
Brian Beuken
Posts: 46
Joined: Sat Jul 29, 2017 9:19 pm

Re: Raytest seems to be inconsistent?

Post by Brian Beuken »

yeah good advice, My offsetting is working perfectly though so for now i'll leave it. I don't know enough about the filtering methods to really use it, I was hoping there was some way to remove/flag an object out of the physics world, do the cast, then restore it, but that does not see to be available.

oh well, we have to work with what we have I suppose.
majicDave
Posts: 1
Joined: Fri May 03, 2019 12:51 am

Re: Raytest seems to be inconsistent?

Post by majicDave »

Hi, I was just googling for this problem, and your posts helped me track it down. I just thought I'd post my exact problem to help out future googlers. For me, I was accidentally adding a box shape/body with a huge size to the scene. So when ray casting I was always inside this giant box, and the result was that sometimes the ray test would hit what I expected, and sometimes it would give no results.
Post Reply