User Tools

Site Tools


cw4:tutorials:rotation_conversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cw4:tutorials:rotation_conversion [2021/01/09 14:38] Saniancw4:tutorials:rotation_conversion [2025/02/14 14:57] (current) – external edit 127.0.0.1
Line 1: Line 1:
-<=[[cw4:scriptingScripting]]+<=[[4rpl:start|Index]] \\ 
 +<=[[cw4:Tutorials]]
  
 ====== Converting between different kinds of rotation ====== ====== Converting between different kinds of rotation ======
Line 11: Line 12:
 ===== Radians and degrees ===== ===== Radians and degrees =====
  
-{{ :4rpl:tutorials:simple_math_rotation.png?300&nolink}}You're probably at least a little familiar with a coordinate grid as show here, with an X and Y axis. An arbitrary angle is usually defined by the distance in radians or degrees from the positive side of the  horizontal axis. You can define the same angle with both a positive or negative value, 30° is the same as -330°. (in radians: ⅙π and -1⅚π)+{{ :4rpl:tutorials:simple_math_rotation.png?300&nolink|Basic coordinate grid with an angle}}You're probably at least a little familiar with a coordinate grid with an X and Y axis, as shown here. An arbitrary angle is usually defined by the distance in radians or degrees from the positive side of the  horizontal axis. You can define the same angle with both a positive or negative value, 30° is the same as -330°. (in radians: ⅙π and -1⅚π)
  
 The trigonometry functions of 4RPL such as sin, cos, tan, atan2, etc. all use or return radian values, but other parts of the game use degrees, which is why you'll have to convert between them. The trigonometry functions of 4RPL such as sin, cos, tan, atan2, etc. all use or return radian values, but other parts of the game use degrees, which is why you'll have to convert between them.
Line 31: Line 32:
 ===== The difference between Unity rotation and mathematical rotation ===== ===== The difference between Unity rotation and mathematical rotation =====
  
-**TLDR; Rotations along the X and Y axes are inverted.**+**TLDR; In Unity, rotations along the X and Y axes are inverted.**
  
-Even though units are normally locked to the grid, the game allows us to rotate them by any amount. Lets try to give a unit a Y rotation of 30°, which should make them point slightly up and to the right just like the image of the section above.+Even though units are normally locked to the grid, the game allows us to rotate them by any amount. Lets try to give a unit a Y rotation of 30°, which should make them point slightly up and to the rightjust like the image of the section above.
  
 <code 4rpl> <code 4rpl>
Line 40: Line 41:
 </code> </code>
  
-What we see when we run this however, is that the unit faces //down //and to the right instead, we only get the result we originally expected when we use the //negative //value -30°. This means that rotations along the Y-axis is inverted. It turns out the the same is true for the X-axis. The Z axis on the other hand is fine and works as we would expect.+What we see when we run this however, is that the unit faces //down //and to the right instead, we only get the result we originally expected when we use the //negative //value -30°. This means that rotations along the Y-axis is inverted.
  
-{{:4rpl:tutorials:unit_rotation_0.png?200&nolink}} {{:4rpl:tutorials:unit_rotation_30.png?200&nolink}} {{:4rpl:tutorials:unit_rotation_-30.png?200&nolink}} Rotating along the Y-axis is inverted+{{:4rpl:tutorials:unit_rotation_0.png?200&nolink|A cannon rotated 0° on the Y-axis}} {{:4rpl:tutorials:unit_rotation_30.png?200&nolink|A cannon rotated 30° on the Y-axis}} {{:4rpl:tutorials:unit_rotation_-30.png?200&nolink|A cannon rotated -30° on the Y-axis}} Rotating along the Y-axis is inverted
  
-So, if you work out some kind of angle within the map'coordinate system and you want to to use that angle to modify unit in some way, make sure to invert the X and Y axes before doing so, or your unit will turn in exactly the opposite way. The easiest way to invert the X and Y angles of a vector is as follows: ''<-rotVector V3(-1 -1 1) * ->rotVector ''+It turns out the the same is true for the X-axis (rotating by a negative value will make units face upwards instead of downwards). The Z axis on the other hand is not inverted and works as we would expect. 
 + 
 +So, if you use conventional math to calculate a rotation within the coordinate system of map, make sure to invert the X and Y axes of the rotation before applying it to a unit, or your unit will turn in exactly the opposite way. The easiest way to invert both X and Y values of a vector is as follows: ''<-rotVector V3(-1 -1 1) * ->rotVector''. For inverting a single value the [[4rpl:commands:neg]] function can be used. 
 + 
 +This way of calculating rotations is tied to the engine, and therefore used in any and all game objects that can have some kind of rotation. That includes units, their individual component objects and notably, the camera.
  
 ---- ----
Line 78: Line 83:
 </code> </code>
  
-<=[[cw4:scriptingScripting]]+<=[[4rpl:tutorialsTutorials]]
  
  
cw4/tutorials/rotation_conversion.1610203098.txt.gz · Last modified: 2025/02/14 14:56 (external edit)