GetEntityUpVector and GetEntityRightVector to complement GetEntityForwardVector (XNL_GetEntityUpVector, XNL_GetEntityRightVector)

Not really a ‘how-to’ or tutorial, but more so a useful snippet for many I suppose, which is why I decided to share it.

In needed something working effectively to detect the upward vector of an object (also when it has been ‘tipped over’), and was struggling a while with re-calculating angles just to discover that some objects will give issues when doing so for what I needed.

Like most (i assume) i know about GetEntityForwardVector and how to place an object in front of the player for example with it. I however noticed that the other two functions (GetEntityUpVector and GetEntityRightVector) aren’t “exposed” within :mascot:FiveM.

So I decided to do a bit of digging and came up with the following two functions:

function XNL_GetEntityUpVector(Entity)
	local ForwardVector, RightVector, UpVector, PosVector = GetEntityMatrix(Entity)
	return UpVector
end

function XNL_GetEntityRightVector(Entity)
	local ForwardVector, RightVector, UpVector, PosVector = GetEntityMatrix(Entity)
	return RightVector
end

I on purpose put my commonly used _XNL prefix infront of these functions just in case that these functions/callbacks might be added to FiveM in the future (so that these won’t cause a conflict then).

These two can be used the exact same way as GetEntityForwardVector.

Mini Example:

RegisterCommand("TestUpVector", function(source, args)
	local PlayerCords = GetEntityCoords(GetPlayerPed(-1))
	UpVec = XNL_GetEntityUpVector(GetPlayerPed(-1))
	local NewXup, NewYup, NewZup   = table.unpack(PlayerCords + UpVec * 3.0) -- Creating New coordinates '3 meters' above the player

	AddExplosion(NewXup, NewYup, NewZup, 22, 1.0, true, false, 0.0) -- Adds Flare
end)

Just paste the above sample code in an existing CLIENT lua script to test it.
OBVIOUSLY it’s not difficult to spawn items above the player by just using getting it’s coordinates and then adding to the Z coordinate, however what I needed it for wasn’t going to work this way.

I can’t (and won’t) spoil yet what I needed it for (I’m planning on releasing it in the near future though), but I can assure you that It does work for detecting the UpVector for objects which are spawned by script and then tipped/kicked/pushed over… With all the tests we have done it will indeed spawn our test above our ‘fallen over object’. Which means that if the object was laying tipped over on the floor it would also spawn our test object above it laying on the floor… And if it was just standing up, it would spawn it floating above it in the air…

And that was exactly what I/we needed. So long story I know, but I’m hoping this text also elaborates the usefulness of functions like these for beginners :slight_smile:


Q: Can I use this in my code/Project
A: Yeah, duh sure it’s just a few simple lines.

Q: Do I need to give credits when I use these snippets?
A: For those couple of lines? Hell no :wink: :rofl:

Q: Can I use these in my Paid content?
A: While I’m usually against using my FREE code in paid content by others (because I personally feel like we should just help each other /others by advancing in development techniques by sharing sources and just supporting by donations when we find it appropiate), These lines are WAY to simple and not even worth it to “restrict” or “copyright” :rofl: So be my guest, I hope it helped atleast one other person besides myself that I’ve “written” those two functions.


So if you like the code or it was useful to use atleast click the ’ :heart:’ to show some appreciation and to make others aware of these two simple functions :slight_smile:

Happy Coding You all,

5 Likes

This is great! I’m trying to understand how they’re calculated - You wouldn’t happen to now that by chance would you?

How they are calculated (internally) I don’t know sorry.

Recent months I’ve also been way to busy to actually spend more “in-depth” development time on my FiveM mods and server, thus did not have the time to check into it. I however didn’t need to know the ‘exact internals’ either at the time being. I just needed some of these functions to create some new mods (which I HOPE to release soon, IF i can find some spare time) :slight_smile:

An entity transform matrix is quite literally a set of forward/up/right vectors. These aren’t ‘calculated’ at all in any sense as it is the internal representation of the entity transform.