# Heading indicator that works when entity is upside down

Discussion in 'Snippets' started by Katalina, May 17, 2017.

1. ### KatalinaMemberDeveloper

Messages:
23
26
Joined:
May 14, 2017
This sounds mildly insane, I know.

So the game has two easy methods of getting your heading, one in the entity's Z rotation, and another in natives. You might use either, they both return roughly the same. They work fine if you follow gravity, but airplanes don't exactly agree with gravity. You'll notice that if your plane is upside down, you'll be heading true north but your heading indicator says you're going south? What the?!

So instead, here's some math in JS to do the work for you.
Code:
```const normalHdgVector = {
x: API.returnNative('0x8BB4EF4214E0E6D5', 7, veh), // float ENTITY::GET_ENTITY_FORWARD_X
y: API.returnNative('0x866A4A5FAE349510', 7, veh) // float ENTITY::GET_ENTITY_FORWARD_Y
}

// ^ the above object is a normalized Vector2 (e.g. instead of -180 to +180, it's -1.0 to +1.0)
// this means we can do an atan2 then convert it's radians to degrees! (and slightly exploit it so it's easier correction)
let hdg = Math.round(Math.atan2(normalHdgVector.x, normalHdgVector.y) * 180 / Math.PI)

// hdg is now what an entity's Z rotation is when on flat ground! A little correction...
if (hdg < 0) {
hdg = Math.abs(hdg)
} else if (hdg > 0) {
hdg = 360 - hdg
}

// The value we have is mirrored, so this flips it.
hdg = 360 - hdg
```
And now you can do rolls and not get mega confused at what direction you're facing.

Jstylezzz likes this.