Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Contract
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

[C#] How do raycasts work?


Pandassaurus
 Share

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
 Share

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