Jump to content

[C#] How do raycasts work?


Pandassaurus

Recommended Posts

Pandassaurus

Hi everyone,

 

I was wondering how raycast results work in c#. I played around with them a little and found them to be very buggy. Could anyone explain/ link me to an explanation? My goal is to get the coords of the surface you're looking at, whatever you hit, however far.

 

Thanks for the help?

Edited by Pandassaurus
Link to comment
Share on other sites

Are you asking for an explanation on raycasting or how to use it in GTA V?

Link to comment
Share on other sites

Pandassaurus

Are you asking for an explanation on raycasting or how to use it in GTA V?

I understand what they are, but want to know how to use it in gta

Link to comment
Share on other sites

Pandassaurus

I got them to work! I found an open source mod (thanks to Enumerator) that used them in vb and translated it over to C#.

            Vector3 camPos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD);            Vector3 camRot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT);            float retz = camRot.Z * 0.0174532924F;            float retx = camRot.X * 0.0174532924F;            float absx = (float) Math.Abs(Math.Cos(retx));            Vector3 camStuff =  new Vector3((float) Math.Sin(retz) * absx  * -1, (float) Math.Cos(retz) * absx, (float) Math.Sin(retx));            RaycastResult ray = World.Raycast(camPos, camPos + camStuff * 1000, IntersectOptions.Everything);            Ped ped = World.CreateRandomPed(ray.HitCoords);

It works pretty well, except that it does crash quite often. I'm not sure why this is, but am looking into it.

 

edit: I think the crashing had more to do with the ped spawning that i used for testing than with the actual rays.

Edited by Pandassaurus
Link to comment
Share on other sites

  • 2 weeks later...
identitymatrix

I got them to work! I found an open source mod (thanks to Enumerator) that used them in vb and translated it over to C#.

            Vector3 camPos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD);            Vector3 camRot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT);            float retz = camRot.Z * 0.0174532924F;            float retx = camRot.X * 0.0174532924F;            float absx = (float) Math.Abs(Math.Cos(retx));            Vector3 camStuff =  new Vector3((float) Math.Sin(retz) * absx  * -1, (float) Math.Cos(retz) * absx, (float) Math.Sin(retx));            RaycastResult ray = World.Raycast(camPos, camPos + camStuff * 1000, IntersectOptions.Everything);            Ped ped = World.CreateRandomPed(ray.HitCoords);

It works pretty well, except that it does crash quite often. I'm not sure why this is, but am looking into it.

 

edit: I think the crashing had more to do with the ped spawning that i used for testing than with the actual rays.

 

That cam stuff is rotation to direction.

 

For some reason, enumerator uses natives for everything despite it being in the scripthookvdotnet api.

 

Here is a reusable func for getting direction from a rotation vector

 

Public Shared Function RotationToDirection(ByVal Rotation As GTA.Math.Vector3) As GTA.Math.Vector3        Dim rotZ As Double = DegreeToRadian(Rotation.Z)        Dim rotX As Double = DegreeToRadian(Rotation.X)        Dim multiXY As Double = Math.Abs(Convert.ToDouble(Math.Cos(rotX)))        Dim res As GTA.Math.Vector3        res.X = Convert.ToDouble(-Math.Sin(rotZ)) * multiXY        res.Y = Convert.ToDouble(Math.Cos(rotZ)) * multiXY        res.Z = Convert.ToDouble(Math.Sin(rotX))        Return res    End Function
Dim rres As RaycastResult            Dim cpos As Vector3 = GameplayCamera.Position            rres = World.Raycast(cpos, cpos + RotationToDirection(GameplayCamera.Rotation) * 1000, IntersectOptions.Everything)            If rres.DitHitAnything Then                msg(rres.HitCoords.ToString)            End If
Edited by TheVideoVolcano
Link to comment
Share on other sites

  • 7 months later...

Thank you very much, just wanted to let you know, even if its months old by now, it still gets read by people (like me) who find it very useful!

Link to comment
Share on other sites

mockba.the.borg

Hi everyone ... I am having trouble with the Ray Cast due to the range it intersects object. It seems I can only match some vehicles and peds up to about 32 meters away.

Other vehicles, like jets, the blimp and the metrotrain, for example, will match up to 1000 meters away.

I though that the raycast would match anything as long as it is visible, is it true? Or configurable?

 

Thanks for any help.

 

Mockba.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.