Page 1 of 1

raycast help

Posted: Sat Jan 29, 2011 4:57 pm
by ron
can i get a help for raycasting
basically an example for the usage of raycasting

Re: raycast help

Posted: Sat Jan 29, 2011 5:46 pm
by Norbo
To provide details, I would need additional context, but here's an overview:

-You can raycast against the space using the Space.RayCast method. This fires a ray against anything that can be tested within the space, such as Entities and StaticTriangleGroups.
-The Space.RayCast method has overloads which return the first hit as well as every hit along the ray, depending on what you need. The multi-hit version can be used for filtering results.
-Individual objects like Entities and the StaticTriangleGroup have their own ray testing method. The Space calls these methods when performing the Space.RayCast, and you can do the same thing if you want.

You can find examples of raycast usage within the SimpleCharacterController and in StandardDemo's Grabber Input handling region.

v0.15.0 cleans up some of the raycasting architecture as well.

Re: raycast help

Posted: Sat Jan 29, 2011 7:56 pm
by ron
can i reduce the ray speed?

Re: raycast help

Posted: Sat Jan 29, 2011 8:15 pm
by Norbo
No, not exactly. Raycasts are instant queries, not bullets. If you wanted to simulate a bullet moving at slow speeds, you could do part of the whole raycast each frame instead of the whole raycast in one frame. You could do this by raycasting 10 units, setting your origin to the new advanced position, then in the next frame casting another 10 units, and so on until a hit is found.

Re: raycast help

Posted: Sat Jan 29, 2011 8:17 pm
by ron
can i get an example code for it
plzzzzzzzzz

Re: raycast help

Posted: Sat Jan 29, 2011 10:10 pm
by Norbo
Here's some pseudo-pseudocode.

-When you fire a bullet, it has a position. The first position is that of the gun muzzle. Use this position as a ray origin. The direction of the ray is the direction in which you want to shoot.
-Each frame, perform a raycast using that ray origin and ray direction. The maximum length of the raycast defines how far the bullet goes in one frame. If the raycast hits something, stop. If it doesn't, set the position = position + direction * maximumLength and repeat the process on the next frame.
-To know if it hit something, check the boolean return value of the Space.RayCast. The hit location/normal will be in the out parameters if it did hit something.

Re: raycast help

Posted: Sun Jan 30, 2011 3:02 am
by ron
for ex
i am doing a raycast with from an entity,
so if now i check space.raycast(parameters),
will it check for the entity ray?

Re: raycast help

Posted: Sun Jan 30, 2011 3:29 am
by Norbo
I'm not exactly sure what you mean. If you mean a ray is originating within an entity and is directed outward, the Space.RayCast will indeed hit the entity that contains the ray first. You could avoid this by either having the origin start outside of the entity, or using the multiple-hit overload of Space.RayCast (which takes lists) and filtering the hits somehow (perhaps pick the first hit such that "hitEntities != shooterEntity").

raycast help

Posted: Tue Feb 01, 2011 4:15 pm
by ron
scene.BroadPhase.RayCast(origin, direction, range, true, entities, location, Normals, timeofimpact)


can i get an explanation for the location, normal and time of impact parameters

Re: raycast help

Posted: Tue Feb 01, 2011 6:30 pm
by Norbo
-Location is the spot where the hit is. If a bullet was a ray, it would be the bullet hole's location.
-Normal is the direction pointing out of the thing that was hit. If a bullet hit a wall, the normal would be the direction perpendicular to the wall's surface. Note that it is not necessarily guaranteed to be normalized (i.e. normal vector length == 1).
-Time of impact is the 't' parameter along the ray where the hit location is. In equation form, hitLocation = rayOrigin + t * rayDirection.

Re: raycast help

Posted: Wed Feb 02, 2011 4:21 am
by ron
and wht does the entity.raytest do
does it only check whether the current ray hit the entity?

Code: Select all

if (s.RayCast(origin, direction, maxlen, true,  enlist,  entitypos,  enorm, toi))
                {
                    maxlen+=10;
                    if (maxlen > 500 && maxlen<=1000)
                    {
                        //Exit();
                        last.Draw();
                        play.addingsphere();
                        if (maxlen == 1000)
                        {
                            maxlen = 0;
                        }
                    }

                    foreach (Entity en in enlist) 
                    {
                        Vector3 place,normal;
                        
                        place = en.WorldTransform.Translation;
                        float dist = place.X - direction.X;
                        normal = Vector3.Left;


                        if (en.RayTest(origin, direction, maxlen, true, out place, out normal,out dist))
                        {
                            Exit();
                        }
                    }

                }
and wht is the difference between space.raycast and space.broadphase.raycast

Re: raycast help

Posted: Wed Feb 02, 2011 4:52 am
by Norbo
Entity.RayTest performs a raycast against a specific entity. It provides a boolean return value representing whether or not the ray hit, as well as other information in the out parameters.

Keep in mind that if a Space.RayCast hits an entity, you do not need to re-raycast against that entity with the same ray, since the answer has already been computed. Each index of the lists corresponds to a hit. So hitEntity has a location of hitLocations. In v0.15.0, the per-object raycast results are bundled into a single struct to make this simpler and more obvious.

Also, when you call a function that has out parameters, their input values are unused and overwritten by the results of the function. That's why they are labeled 'out'; they are another way to get data out of a function than just the return value.

and wht is the difference between space.raycast and space.broadphase.raycast

Space.RayCast tests everything in the Space. This includes Entities as well as raycastable containers like StaticTriangleGroups. Broadphase.RayCast tests against everything in the broadphase, which includes entities.

Note that in v0.15.0, this is changing a little. The broadphase will accelerate queries to get objects with bounding boxes that overlap a ray or query bounding shape, but will not internally perform raycasts against the detailed shapes. Furthermore, the broadphase contains many different kinds of object, not just Entities. StaticTriangleMeshes exist within the BroadPhase, for example. The Space.RayCast uses this query to determine the objects near a ray, and then also performs the detailed raycasts.