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:
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:
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?
Mapping entity orientation to direction
- Deadlock989
- Smart Inserter
- Posts: 2529
- Joined: Fri Nov 06, 2015 7:41 pm
Re: Small documentation improvement requests
There is some info here:Deadlock989 wrote: ↑Sun Aug 01, 2021 1:27 pm So what is the formula in the source code for mapping orientation to direction?
viewtopic.php?f=23&t=30074
- Deadlock989
- Smart Inserter
- Posts: 2529
- Joined: Fri Nov 06, 2015 7:41 pm
Re: Small documentation improvement requests
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:Klonan wrote: ↑Sun Aug 01, 2021 2:45 pmThere is some info here:Deadlock989 wrote: ↑Sun Aug 01, 2021 1:27 pm So what is the formula in the source code for mapping orientation to direction?
viewtopic.php?f=23&t=30074
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