[Help] Trig Function (to find point based on heading) not working - Any ideas why?

Hey Everyone!

So I’m trying to come up with something. The idea is. From a starting point on the map (in vector 3 coord units of course - x, y, z) and a heading (in degrees or radians):

Come up with a point that follows that degree heading to some point further out.
In other words, I’m trying to make a line extend x meters out by a heading.

Here’s where I’m stuck…I see various stack exchange links providing some variation of the following equation:

endy = y + length * math.sin(math.rad(angle))
endx = x + length * math.cos(math.rad(angle))

However, this doesn’t translate in remotely the correct position and I’m not sure why. I’m thinking it has something to do with the fact that this is for 2d space and we’re working with 3d space…

I also know that the GTA V heading in certain vehicles is off compared to the heading on the code-space layer, so maybe that’s it, but I wanted to shoot this out there in case anyone’s worked on a similar challenge before (I’m still searching, and as I come across stuff, I’ll post, but) any help as always is appreciated!

Cheers!

1 Like

Came across a REALLY, REALLY awesome ebook called vgmath:
https://gamemath.com/book/polarspace.html#3d_polar

Which talks about the difference between cartesian coordinates (xyz) and polar coordinates (start at a centerpoint, then move out x units, at an angle defined arbitrarily).

In laymans terms, so far the equations provided are a derivation of the following cartesian-to-polar coordinate translation:

x = r * cos * θ

and

y = r * sin * θ

Where ‘r’ is the distance from the origin (0, 0) out to some point, however, all polar coordinates start at the positive X axis (aka an arrow pointing right). So to point in a different direction, you must give it an angle. This angle is defined as θ (aka ‘Theta’)

It’s looking like our original theory was right - in the 2d coordinate space, your equation isn’t as complex…

So far so good…I’ll keep posting as I find more

Adding to @Wetter42 's answer:
The “heading” in GTA V at 0° is north
Try adding 90° to your angle and see if it works :wink:

I remember having to do something similar in my OrbitCam script.

1 Like

Still reading the article, but a suggestion is a lead - I’ll give it a try and let you know if it’s a slam dunk. From what I remember, heading at 0 is a good thing right? Is it because polar coordinates start at what is considered to be heading 90?

Either way, I’ll test this out and let cha know what I find!

You fawking madman! It worked!

Now that I think about it, the polar coordinate system starts at +90. And luckily for me:

math.rad()

Accounts for multiples of 360 so I don’t have to say:

if angle + 90 >= 360 blah, blah, blah

Dude! You solutioned me! I don’t know how to mark this as a solution, but once my forum interface gets it’s shite together, you have my vote! THANK YOU! <3

1 Like

Well your citation says that the positive x axis is basically 0° usually. But in GTA the heading when at 0° is north (positive y axis).
So it’s basically turned by 90°.

1 Like

Right, but I never put 2 and 2 together that GTA’s heading normal was different from polar heading normal…so thanks for helping my brain make the distinction! I appreciate cha!

Beer / or tea / or juice on me! :beers:

My brain is weird as it doesn’t knock knowledge against other knowledge until the full picture is there…abstract concepts usually battle in my head, but only when I can build out those abstract concepts to a relatively clear degree…So I’m hoping (for the sake of my ego) that I would’ve came to the same conclusion, but it would’ve definitely taken me a bit more time…I’m still going to finish this chapter either way!