Mapping entity orientation to direction

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2529
Joined: Fri Nov 06, 2015 7:41 pm

Mapping entity orientation to direction

Post by Deadlock989 »

Re: https://lua-api.factorio.com/latest/Lua ... rientation

I have long suspected that for entities with arbitrary animation directions (vehicles, turrets, radar) there is not a linear relationship between the RealOrientation of the entity and the animation frame. I have struggled for a long, long time with projectile/beam origin offsets for turrets and finally resorted to rendering and testing like this:

orientations2.jpg
orientations2.jpg (188.61 KiB) Viewed 1494 times

This helped a lot but for scripted effects it still wasn't quite right. It seemed as though the animation frame used was sometimes off.

So I tested it. This is a 64-direction vehicle with a test sprite sheet. The large number here is the animation frame number, the smaller rendered text below is the fraction value that the entity's orientation was set to:

orientations.jpg
orientations.jpg (328.02 KiB) Viewed 1494 times

Notice the repetitions and missing numbers. If the relationship was linear then for each frame number N, the number below should be (N-1)/64. But it clearly isn't. You can see this in effect for yourself if you build a custom radar (radars are a weird entity with an orientation that advances per tick instead of being an actual animation) and watch it turning - it clearly speeds up and slows down as it approaches the vertical and horizontal directions.

So what is the formula in the source code for mapping orientation to direction?
Image

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5156
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Small documentation improvement requests

Post by Klonan »

Deadlock989 wrote:
Sun Aug 01, 2021 1:27 pm
So what is the formula in the source code for mapping orientation to direction?
There is some info here:
viewtopic.php?f=23&t=30074

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2529
Joined: Fri Nov 06, 2015 7:41 pm

Re: Small documentation improvement requests

Post by Deadlock989 »

Klonan wrote:
Sun Aug 01, 2021 2:45 pm
Deadlock989 wrote:
Sun Aug 01, 2021 1:27 pm
So what is the formula in the source code for mapping orientation to direction?
There is some info here:
viewtopic.php?f=23&t=30074
Thanks. From that I found a way of converting turret orientation to the frame number ("direction"), it's probably a stupid/horrible way of doing it but it works:

Code: Select all

local function reverse_projected_orientation(turn)
	turn = turn + 0.25
	local x = math.sin(turn * math.pi * 2)
	local y = -math.cos(turn * math.pi * 2)
	y = y * math.cos(math.pi / 4)
	return (math.atan2(x, -y) / (math.pi * 2)) - 0.25
end

local function orientation_to_direction(orientation,directions)
	return math.floor((reverse_projected_orientation(orientation)*directions)+0.5) % directions
end
orientations3.jpg
orientations3.jpg (39.05 KiB) Viewed 1461 times
Image

Post Reply

Return to “Modding help”