Page 1 of 1

Performing A Proper RayCast From A Vehicle

Posted: Fri Feb 08, 2013 8:35 am
by knightbenax
Hello, i happpen to be running into a problem, i can seem to perform a proper raycast from the sides of a BEPUphysics Vehicle to the sides of a static mesh. It keeps returning the distance as zero.

Wat am i doing wrong?

Thanks in advance

Here is my code snippet

Code: Select all

public ModelMesh leftRailings;
if (mesh.Name == "Railing_L") {
                        leftRailings = mesh;
                        //System.Windows.Forms.MessageBox.Show("true");
                    }

Desertmap c = (Desertmap)newTrack;
            currentCar.checkRayIntersection(c.leftRailings);

public bool checkRayIntersection(ModelMesh sphere) {
            if (sphere != null) {
                Ray front = new Ray(Vehicle.Body.Position, Vector3.Right);
                float? distance = front.Intersects(sphere.BoundingSphere);

                
                if (distance < 5)
                {
                    System.Windows.Forms.MessageBox.Show("true" + distance.ToString() + sphere.BoundingSphere.Radius.ToString() );
                    return true;
                }
            }
            return false;
        }

Re: Performing A Proper RayCast From A Vehicle

Posted: Fri Feb 08, 2013 9:18 pm
by Norbo
That code tests a ray starting at the car's position, pointing in the world direction (1,0,0) as opposed to the car's right direction, against a bounding sphere surrounding an entire mesh.

The distance is zero because the ray is starting inside the bounding sphere.

You should use the BEPUphysics ray casting methods instead. For example, if you wanted to test a ray against everything in the Space except for the car body itself, the test would look like this:

Code: Select all

            space.RayCast(ray, candidate => candidate != Vehicle.Body.CollisionInformation, out result);
If you wanted to test a ray against only a StaticMesh, then it would look like this:

Code: Select all

            staticMesh.RayCast(ray, 100, out hit);
The number in the above is just the maximum distance that the ray is allowed to go, in units of the ray direction's length.

Re: Performing A Proper RayCast From A Vehicle

Posted: Sat Feb 09, 2013 8:36 am
by knightbenax
Thank you very much for your speedy reply, I would try it and post my feedbacks

Re: Performing A Proper RayCast From A Vehicle

Posted: Mon Feb 11, 2013 3:33 pm
by knightbenax
How can i perform the RayCast using a ray, going from the right side of the Vehicle? Vector3.Right() doesn't seem to be working for me

Re: Performing A Proper RayCast From A Vehicle

Posted: Mon Feb 11, 2013 8:34 pm
by Norbo
If you want to point the ray out the right side of the vehicle, use the vehicle.Body.OrientationMatrix.Right for the ray direction. Equivalently, transform Vector3.Right by the vehicle's orientation.

Re: Performing A Proper RayCast From A Vehicle

Posted: Thu Feb 14, 2013 9:02 pm
by knightbenax
Thank you, i have being able to perform the raycast correctly, now i am having trouble getting the appropriate data from the RayCastResult value. What i need are the object the ray hit and the distance between.

I saw the RayCastResult.HitObject, but it doesn't provide my any pointers as to the object in my space, it is.

Re: Performing A Proper RayCast From A Vehicle

Posted: Thu Feb 14, 2013 10:02 pm
by Norbo
The hit object is a BroadPhaseEntry. BroadPhaseEntry objects are objects that live in the broad phase, and so can be found for queries and collisions. For example, a StaticMesh is a BroadPhaseEntry. The object stored in the entity.CollisionInformation property is a BroadPhaseEntry.

So, if you want to know if you hit a StaticMesh, you could try to cast the hit object to a StaticMesh. If you want to know if you hit an entity, you could try casting the hit object to an EntityCollidable. The entity associated with the EntityCollidable is stored in the EntityCollidable.Entity property.

It is sometimes more convenient to use of the BroadPhaseEntry.Tag property. Arbitrary data can be stored within the tag. For example, you could put a reference to your game logic object in the tag. That would allow you to skip casts entirely. (Note: Both the BroadPhaseEntry and Entity have Tag properties; they are separate. That is, an Entity.Tag property does not necessarily match the value of the Entity.CollisionInformation.Tag property.)

Re: Performing A Proper RayCast From A Vehicle

Posted: Mon Feb 18, 2013 10:57 pm
by knightbenax
I still can't find the distance of the ray between the ray starting position and where it intersects.

I've tried substracting their Vector3 positions, doesn't still give any thing meaningful

How can i find the distance of a ray?

Re: Performing A Proper RayCast From A Vehicle

Posted: Mon Feb 18, 2013 11:10 pm
by Norbo
There's a couple of simple ways.

The distance a ray travels is given by the distance between the hit location and the ray origin, or (hit.Location - ray.Origin).Length(). This will give you the result you want.

You can be a little trickier, though. Note that the hit location is given by the equation:

Code: Select all

hit.Location = ray.Origin + hit.T * ray.Direction;
So, by substitution, the distance can also be expressed as ((ray.Origin + hit.T * ray.direction) - ray.Origin).Length(), which simplifies directly to (hit.T * ray.Direction).Length(). You can pull hit.T outside of the length (for reasons easy to show but irrelevant), leaving distance = hit.T * ray.Direction.Length(). So, for example, if ray.Direction.Length() == 1, then the distance is exactly equal to hit.T.

Re: Performing A Proper RayCast From A Vehicle

Posted: Thu Feb 28, 2013 3:17 pm
by knightbenax
Thanks a lot, tried it and it worked. :D

Thank you very much.