Page 1 of 1

Problem with ray casting :(

Posted: Sat Dec 03, 2011 4:09 am
by tehDrop
Hi, this is my first post! :D

Before getting to the point, I just wanna say that BEPU physics developers are amazing!!! You created one of the best and easiest to use physics engines and you're giving it all away for free! Thank you for that.

Now I'm having some issues with ray casting... I've searched a lot for this but no one seems to have this issue (probably cause I'm a noob :lol:)

Basically I'm using the full character controller from the samples and I can't change the minimum distance (when the character is active) required in order to hit a target... I tried to change this line:

startPosition = camera.Position + 5 * camera.WorldMatrix.Forward;

to

startPosition = camera.Position + camera.WorldMatrix.Forward;

and it works but there's still a minimum distance required to hit a target and sometimes the ray hits targets that are not in sight... please help!!

Thanks.

Re: Problem with ray casting :(

Posted: Sat Dec 03, 2011 4:51 am
by Norbo
Before getting to the point, I just wanna say that BEPU physics developers are amazing!!! You created one of the best and easiest to use physics engines and you're giving it all away for free! Thank you for that.
Thanks and you're welcome :)
and it works but there's still a minimum distance required to hit a target and sometimes the ray hits targets that are not in sight... please help!!
In the demos, there's two lines that need to be changed. The one you found, and then a few lines lower, there's the grabber distance property:

Code: Select all

                     if (character.IsActive)
                            grabDistance = raycastResult.HitData.T + 5;
                        else
                            grabDistance = raycastResult.HitData.T;
If you set the position offset to zero, the T value offset needs to be zero too.

That whole raycasting section is a bit old and could be made more straightforward by using a tailored ray cast filter. I went ahead and uploaded a new version to the development fork (http://bepuphysics.codeplex.com/SourceC ... evelopment). In it, the character-specific if statements are gone in favor of changing the ray cast filter to always ensure that the character's body is not subject to grabbing:

Code: Select all

        bool RayCastFilter(BroadPhaseEntry entry)
        {
            return entry != character.CharacterController.Body.CollisionInformation && entry.CollisionRules.Personal <= CollisionRule.Normal;
        }
The reason why it's using the CollisionInformation property of the character body rather than the body itself is that the CollisionInformation is the entity's collision proxy that lives in the collision detection pipeline. The ray cast uses the broad phase to accelerate queries, so the proxy is the thing actually being hit.

Re: Problem with ray casting :(

Posted: Sat Dec 03, 2011 5:12 am
by Norbo
I should also mention- if you do actually want a nonzero minimum distance in the filter-based approach, it's as simple as adding an extra condition that result.T > someMinimumDistance. In other words:

Code: Select all

        if (Space.RayCast(new Ray(Game.Camera.Position, Game.Camera.WorldMatrix.Forward), 1000, rayCastFilter, out raycastResult) && raycastResult.HitData.T > minimum)

The filter can't be used to handle the minimum distance because it is run prior to the actual ray test.

If you want to look beyond the first impact, you can either go back to offsetting the ray's start location (and the grabber's distance value), or you can use the other Space.RayCast overload which outputs a list of impacts. Sort the impacts by time and pick the first impact which is beyond the minimum.

Re: Problem with ray casting :(

Posted: Sat Dec 03, 2011 2:34 pm
by tehDrop
Yeah I didn't realize the ray was hitting my own character :roll:

It works perfectly now.

Thank you so much! :D