-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finalize "Update vector processing" (#5061) #6438
Conversation
Changes from old `defaultunits.lua` file (before rebase) to new `StructureUnit.lua` file: Remove old quaternion import from `StructureUnit.lua` and `defaultunits.lua` Use quotes instead of colons for import string Use quat multiplication in structure rotation to fit terrain
They have some unexpected behavior not preserving roll/pitch
Fix checking unit being in the same army Use PosXYZ to avoid table allocation Fix being able to call `EntityCategoryContains(nil, unit)` Move category check before distance calculation
saves 3 table calls
lua/sim/units/StructureUnit.lua
Outdated
-- "q′ = q2 * q1 in which q′ corresponds to the rotation q1 followed by the rotation q2" (wikipedia) | ||
-- the unit's orientation comes first, and then is rotated by the terrain angle | ||
-- roll is negated because ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what reason is the roll negated?
Also I'm not sure why it is important that here we do the terrain quaternion * orientation
when other parts in this PR do orientation * quaternion
just fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is probably best asked to @Hdt80bro , I do not recall myself. I think I negated it because otherwise it would not orientate properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did, and I tried to extract the original mathematics that would interpret roll correctly without negation, but got stuck. Similarly, since I didn't quite figure it out, sometimes it needed to be multiplied on the left to rotate, others multiplied on the right (it is sensitive to the side its multiplied on). It's something I wasn't happy about and why this PR got stuck in limbo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent some time figuring it out, the reason roll is negated is because the terrain calculation's roll is given with respect to the -z
axis, and units use +z
.
This is the x component of the basic form of the calculation for small structures (only 1 edge is calculated).
The number we get is atan2(dy, lx)
, where dy
is the terrain height in the middle of the -x
side edge. This number is the trigonometric representation (local perspective) in the bottom right.
But notice how in the diagram of the real world situation (global perspective) in the bottom left the triangle is flipped horizontally, and that when using right-handedness, the angle we get is on the -z
rotation axis.
Averaging is done correctly by negating the second component, which is measuring the other side's angle which is flipping the axis of rotation 180 degrees.
Lines 24 to 25 in 81a95af
local rightX = MathAtan2(GetTerrainHeight(posX + lenX, posZ) - posY, lenX) | |
angleX = (angleX - rightX) * 0.5 |
equivalent to avg = (angle1 + -angle2) / 2
In comparison to Pitch, Roll is done incorrectly because it starts with pos - lengthX/2
towards the -x
side edge which gives an angle around -z
, but Pitch is correct because it also starts with pos - lengthZ/2
, and that goes to the -z
side edge, but that angle rotates around +x
, which is the same as units.
Also I must've made a mistake in |
The utility tests fails because of Line 885 in b93c634
but this line works in-game, since Vector2 is a Core global. |
This is great @lL1l1 , it would be great to finalize this and get rid of all the magic mathematics that is in some of the Lua files 👍 |
Make it return `angleX` aka roll on the +Z axis instead of the -Z axis Also add comments explaining the negations of angles on the negative axes
It doesn't matter since it's a 2d rotation, but in case anyone is inspired by the function to use a quaternion for 3d rotation, the order should be correct.
Matches original manual quaternion multiplication's rotation order
use `SUB` instead of `UNM` and `ADD`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed the behavior in-game and all appears to be good. The relevant code changes where the new logic is applied look tremendously better.
An absolute high-quality contribution. Nothing but exceptional. This is the type of in-depth analysis at an almost professional level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't trace every single operation, but that everything in the game works correctly gives me confidence that the math is correct too.
The files are much more readable now, very good work!
If I am just out of the loop about which functions are supposed to be deprecated, then you can merge this
A problem with the __add metamethod is that it has quite a few safety checks that slow down the operation. It can be faster than engine functions and even lua since it has the vector metatable cached for use with setmetatable (faster than |
All vector metatables can be expanded via bin patches and done with performance priority |
I'm afraid I neither understand the question, nor your accompanying explanation... |
Currently Vector, Vector2, and Quaternion use the same |
It does sound like a good idea, but I suggest to do that in a separate pull request. This one is already quite big and I'd like to have it merged. It's already a viable pull request. |
I agree, those changes would be worth while in my opinion. But let's do that in a separate, new pull request. That way we can merge this in. Other pull requests such as #6477 rely on it. I'll wait for a response and then I'll merge it in. |
Description of the proposed changes
Testing done on the proposed changes
Testing structure orientation:
Put this command into the console then spawn units by pasting from the clipboard like normal.
Results (map is "Project Albus"):
The raw numbers when logged are very slightly different but I don't see a visual difference.
Generate a bunch of wreckages using ScenarioUtilities
Seems to still create random orientations. Unfortunately due to the
TryCopyPose
inUnit:CreateWreckageProp
this doesn't work on units not in the air layer (land, navy, landed air units), so it must be tested with an air unit + 1 tick delay to get into the air layer (spawns on land layer).Reviewed the
utilities.lua
andTerrainUtils.lua
changes to make sure the logic stayed the same.Unit rotation functions review:
Just spawned a tank unit (walkers always stay vertical) on a slope and saw the commands do what the annotations say:
Ran a small m27AI skirmish with no errors.
Checklist