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
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