Jump to content

[Rage] Pointing at Cars


SiriusWolf

Recommended Posts

First let me say I realize these are both RAGE specific questions, but those forums are basically dead and I feel this is the best place to get an answer.
(I like it here better anyways. :cool: ) I wanted to know what the best way to get a vehicle (or entity of the vehicle preferably) that you're looking directly at.

I'm not just shooting for the closest, if anyone's worked in Unity think raycast. I've tested so far using World.TraceLine() going from the player forward,
but I can fully assure you it's not getting what I want it to, because even using the trace flags for only vehicles, I'd swear it's not picking up the right one.
I felt like the results that were printing were off so I stopped traffic and stood dead in front of a not moving car and it reported a speed of ~20mp/h after the math.
(I'm trying to identify properties of the entity & vehicle I'm looking at only if it's a vehicle.)

My second question would be what is considered to be (I know it's opinionated) the proper way to draw text and image on the screen.
Ultimately I want the information shown in a box on the bottom of the screen. I'm sure there's a dozen ways to do this but I'm looking for
what any of you would be consider the proper way.

 

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

In regards to the "What vehicle is the user looking at".. this may not be entirely what you're looking for but it may get you started.

 

private Vector3 GetCameraPosition()
        {
            return NativeFunction.Natives.GetGameplayCamCoord<Vector3>();
        }

        private Vector3 GetCameraRot()
        {
            return NativeFunction.Natives.GetGameplayCamRot<Vector3>(0);
        }

        public static Vector3 RotationToDirection(Vector3 Rotation)
        {
            float z = Rotation.Z;
            float num = z * 0.0174532924f;
            float x = Rotation.X;
            float num2 = x * 0.0174532924f;
            float num3 = Math.Abs((float)Math.Cos((double)num2));
            return new Vector3
            {
                X = (float)((double)((float)(-(float)Math.Sin((double)num))) * (double)num3),
                Y = (float)((double)((float)Math.Cos((double)num)) * (double)num3),
                Z = (float)Math.Sin((double)num2)
            };
        }

        private Vector3 GetCameraLookingAt()
        {
            float forwardAmount = 10f; //Any amount forward of the player
            Vector3 camPos = GetCameraPosition();
            Vector3 camRot = GetCameraRot();
            Vector3 cameraDir = RotationToDirection(camRot);
            return camPos + cameraDir * MARKER_OFFSET.OFFSET_FORWARD;
        }

 

 

You can then use GetCameraLookingAt() and pass it off to either the rage function to get all vehicles and determine which is in the vector3 or you can use the native to get any random vehicle in the sphere.

I use the functions to create a checkpoint marker to visually give me a clue for what I'm looking at.

Link to comment
Share on other sites

I just thought this up in my head, but I doubt it's an efficient method of accomplishing this.

You could get a list of nearby vehicles (sort the list by distance with a LINQ query), and then calculate the heading to each vehicle, and compare each value to the player's current heading.

You could accomplish this with a LINQ query or two, but it may not be very efficient.

Stealth22
LSPDFR Tester | Plugin Developer
My Plugins: Code 3 Callouts | Traffic Control | Keep Calm | ALPR+

Please do not PM me for any kind of technical support.
I unfortunately do not have enough free time to answer every PM that I get. For issues with my plugins, please post in the comments section of the file, or it's forum thread. You'll get a much quicker response from me there than if you send me a PM; I do my best to respond to every question in the comments sections. For API/programming questions, please post them in the API Development forum, so all developers can benefit from the answer as well. Thanks!

Link to comment
Share on other sites

So I tried the camera idea, but when there's another vehicle close I can't figure how to get the exact one.
I then tried using a few natives to see what the player's free aiming at and then to see if it's a vehicle.

But as soon as I go on duty it crashes the plugin. Even on dev mode the console only gives me 
Rage.Native>nativeArgument.op_Implicit(IHandleable value) which to me sounds like it's trying to get a reference it doesn't have.
Here's what I tried;

Ped player = Game.LocalPlayer.Character;
Entity target = null;
if (Rage.Native.NativeFunction.CallByName<bool>("GET_ENTITY_PLAYER_IS_FREE_AIMING_AT", Game.LocalPlayer, target))
  {
  if(Rage.Native.NativeFunction.CallByName<bool>("IS_ENTITY_A_VEHICLE", target))
  {

Not sure where to go from there, because where I'm aiming is a big part of what I'm trying to do.

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

BOOL GET_ENTITY_PLAYER_IS_FREE_AIMING_AT(Player player, Entity *entity)

I'm still a bit new to c# but have you tried to pass off a pointer handler to the entity and not the actual entity object? I'll play around with it later tonight to see what I can come up with.

The rage sample project for natives show how to work with handlers

 


            // Pointers cannot be used directly with dynamic dispatching. If a native requires pointers, convert it to an IntPtr.
            Ped ped = Game.LocalPlayer.Character.GetNearbyPeds(1).First();
            uint pedHandle = ped.Handle; // Handle of first character around the player's character.
            NativeFunction.Natives.DeletePed(new IntPtr(&pedHandle));

 

Link to comment
Share on other sites

No need to do it so complicated with pointers and whatnot.

I suggest doing what @Stealth22 suggested but have a check against Game.LocalPlayer.Character.GetOffsetPosition(Vector3.RelativeFront * 2f) which is a point directly in front of the player taking into account their direction&heading.

My YouTube: Click here. 

My Discord Server - https://discord.gg/0taiZvBSiw5qGAXU

Useful post? Let me and others know by clicking the Like button.
Check out my many script modifications! 
Having issues? LSPDFR Troubleshooter by Albo1125.

Link to comment
Share on other sites

I was able to use the native in less than a few minutes. It was able to detect a car that I spawned and then a random ped across the street.

unsafe Entity DetectEntityPlayerAimingAt()
    {

        uint handle;
        Rage.Native.NativeFunction.CallByName<bool>("GET_ENTITY_PLAYER_IS_FREE_AIMING_AT", Game.LocalPlayer, &handle);
        if (handle == 0)
        {
            return null;
        }
        return World.GetEntityByHandle<Rage.Entity>(handle);
    }

 

Link to comment
Share on other sites

I feel like @ainesophaur had the right idea, I actually hadn't tried a handle in the natives yet, not used to doing it that way. (My C# work is all Unity based. lol)
So I did in fact do something similar to the last post from Ain where I just used a handle to ref the pointer instead of iterating through vehicles and it worked out.
Once I read your post last night I realized what the null reference was. (Used to better error reporting I suppose.) My issue now is that it when I use the native is entity a vehicle it's only returning parked vehicles. (ie stopped vehicles with no driver) Gonna try the tracing methods and see what I get I'll update everyone if I have something.

Now is there a way to force a reticle so that I don't -have- to be aiming a weapon to use that? Guess I'll have to play with it, but thank you folks.

Edited by SiriusWolf

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

I believe you may be able to use the camera code I posted above to get the vector3. You can then use the rage.graphics library to draw a circle at x and Y. I know there is a function to convert a world vector to a screen vector2. If you can figure that out then you can check for entity's within the point of the marker

Link to comment
Share on other sites

Ya I was just in the process of looking over it, but instead of return camPos + cameraDir * MARKER_OFFSET.OFFSET_FORWARD; would I use
return camPos + cameraDir * forwardAmount; or return camPos + cameraDir + forwardAmount; ? Because I'm trying to get a position directly in front.

The reason I didn't go with that originally is because it's a static amount, if I say 10f for example it's gonna be 10, not the first thing between here and 10.
I was looking at the world probe native trace might try that and see if I can get the proper results.

 

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

23 hours ago, ainesophaur said:

Use a key press event to change the depth distance ;)

Ya I totally get that @ainesophaur but it's the opposite of what I was trying to do (guess I should've been more specific.)
My idea was to create a handheld radar gun, originally I was using the native because the free aim was an easy way to get an ent then pass the native checking if it's a car and do math to get the correct speed. The prob is that requires pointing a weapon, which even with the keepcalm plugin makes people spaz. So I was gonna use a world trace from the camera position forward a bit and use the result to get the first ent it hit. Still trying to figure out if OffsetForward is local or global. (Is +Z from the player or +Z in the world.) I'll let you know what I come up with though. Hence why I didn't want @ a specific position, cus then it's not hitting a specific target.

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

Ya I did actually try that and it helps for one ped (& only sometimes) but I'll probably have to create a bubble around the point and get the nearby ones, because they freak out as well.
That's problem B, because for some reason the natives I'm using are crashing, I'm using handlers for the pointers instead of just passing existing ones. Rage only really tells me it's the native call that's causing the crash so I can't really figure out what about it is doing it, I added some console outputs in there but I'm still fiddling with it to find out where the stop is coming from. Usually using VS with Unity I get a lot more information so I'm just adjusting one line at a time until I hit the bugger causing the issue.

Edited by SiriusWolf

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

Yes of course, I first tried using a normal pointer then new IntPtr(&varName) still get memory based errors:

Spoiler

[5/1/2016 2:32:01 PM.001] LSPD First Response: Exception type: System.AccessViolationException
[5/1/2016 2:32:01 PM.002] LSPD First Response: Exception message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
[5/1/2016 2:32:01 PM.002] LSPD First Response: ------------------------------
[5/1/2016 2:32:01 PM.002] LSPD First Response: Inner exceptions:
[5/1/2016 2:32:01 PM.003] LSPD First Response: ------------------------------
[5/1/2016 2:32:01 PM.003] LSPD First Response: Stack trace:
[5/1/2016 2:32:01 PM.004] LSPD First Response: at CNativeInvocationInfoV.PushArgument<__int64>(CNativeInvocationInfoV* , Int64 )
[5/1/2016 2:32:01 PM.005] at Rage.Native.NativeFunction.CallByAddress(IntPtr address, Type returnType, NativeArgument[] arguments)
[5/1/2016 2:32:01 PM.005] at Rage.Native.NativeFunction.CallByHash(UInt64 nativeHash, Type returnType, NativeArgument[] arguments)
[5/1/2016 2:32:01 PM.006] at Rage.Native.NativeFunction.CallByName[ReturnType](String nativeName, NativeArgument[] arguments)

Which tells me either the function isn't passing values correctly or one of the arguments aren't correct. (For clarification I don't need to wrap the arguments into a variable all together correct?)

Edited by SiriusWolf

lspdBar.jpg.6029ce0e1ffa1ca9c5c005b82b9d02f5.jpg

Grammar and punctuation are the difference between; "I helped my uncle, Jack, off his horse." and...I helped my uncle jack off his horse

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...