Both sides previous revisionPrevious revisionNext revision | Previous revision |
math [2019/07/04 17:43] – [AnglesToVector] korshun | math [2019/08/26 18:26] (current) – [Length] korshun |
---|
''int ipow(int x, int y)'' | ''int ipow(int x, int y)'' |
| |
Returns x^y. Uses a simple multiplication loop. | Returns x<sup>y</sup>. Uses a simple multiplication loop. |
| |
==== fpow ==== | ==== fpow ==== |
''fixed fpow(fixed x, fixed y)'' | ''fixed fpow(fixed x, fixed y)'' |
| |
returns x^y. Supports powers below ''1.0''. Uses a simple loop with ''FixedMul'' or ''FixedDiv'', depending on whether the power is below ''1.0''. | returns x<sup>y</sup>. Supports powers below ''1.0''. Uses a simple loop with ''FixedMul'' or ''FixedDiv'', depending on whether the power is below ''1.0''. |
| |
| |
===== Vector math ===== | ===== Vector math ===== |
| |
==== VectorLength3D ==== | Vector math functions come in 2D and 3D versions. All vector math functions operate only on fixed-point vectors. No integer versions are available. |
''fixed VectorLength3D(fixed x, fixed y, fixed z)'' | |
| |
A 3D version of [[zdoom>VectorLength]]. Returns the length of the 3D vector. | Vectors are passed as arguments named ''x'', ''y'', ''z''. |
| |
| If a function returns a vector, it uses [[multiple return values]], returning vector components in the same XYZ order, so ''r1'' is X, ''r2'' is Y and ''r3'' is Z. |
| |
| If a function accepts more than one vector, the first vector is passed as ''x1'', ''y1'', ''z1'', and the second one as ''x2'', ''y2'', ''z2''. |
| |
| |
| ==== Length ==== |
| |
| * 2D: [[zdoom>VectorLength]] -- already built into ZDoom. |
| * 3D: ''fixed VectorLength3D(fixed x, fixed y, fixed Z)'' |
| |
| These functions return the length of the input vector. ''VectorLength3D'' is implemented efficiently as ''VectorLength(VectorLength(x, y), z)''. |
| |
| ==== Dot product ==== |
| |
==== dot ==== | |
* 2D: ''fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)'' | * 2D: ''fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)'' |
* 3D: ''fixed dot3(fixed x1, fixed y1, fixed z1, fixed x2, fixed y2, fixed z2)'' | * 3D: ''fixed dot3(fixed x1, fixed y1, fixed z1, fixed x2, fixed y2, fixed z2)'' |
| |
Returns the [[wp>dot product]] of two vectors. Shorthand for writing out ''FixedMul(x1, x2) + FixedMul(y1, y2) + ...''. | These functions return the sum of components of a vector obtained by componentwise multiplication of the two input vectors, also known as the [[wp>dot product]]. They are shorthands for writing out ''FixedMul(x1, x2) + FixedMul(y1, y2) + ...''. |
| |
| ==== Normalization ==== |
| |
==== normalize ==== | |
* 2D: ''fixed, fixed normalize2d(fixed x, fixed y)'' | * 2D: ''fixed, fixed normalize2d(fixed x, fixed y)'' |
* 3D: ''fixed, fixed, fixed normalize3d(fixed x, fixed y, fixed z)'' | * 3D: ''fixed, fixed, fixed normalize3d(fixed x, fixed y, fixed z)'' |
| |
[[wp>Normalized vector|Normalizes]] the vector and returns the result using [[multiple return values]]. | These functions return the input vector scaled to have length ''1.0'', also known as [[wp>Normalized vector|normalizing]] the input vector, and return the result using [[multiple return values]]. |
| |
=== Example usage === | === Examples === |
<code> | <code> |
normalize3d(x, y, z); | normalize3d(x, y, z); |
Converts the vector's direction to a pair of angles (angle, pitch) and returns them using [[multiple return values]]. | Converts the vector's direction to a pair of angles (angle, pitch) and returns them using [[multiple return values]]. |
| |
<note important>Remember that ZDoom uses inverted pitch angles on actors. ACSUtils functions don't invert pitches.</note> | <note important>Remember that ZDoom uses negated pitch angles on actors. ACSUtils functions don't use negated pitches. </note> |
| |
=== Example usage === | === Example usage === |
int pitch = r2; | int pitch = r2; |
SetActorAngle(tid, angle); | SetActorAngle(tid, angle); |
SetActorPitch(tid, -pitch); // SetActorPitch expects inverted pitch. | SetActorPitch(tid, -pitch); // SetActorPitch expects negated pitch. |
} | } |
</code> | </code> |
Converts a pair of angles (angle, pitch) to a 3D vector of length ''1.0'', which points in the same direction, and returns it using [[multiple return values]]. | Converts a pair of angles (angle, pitch) to a 3D vector of length ''1.0'', which points in the same direction, and returns it using [[multiple return values]]. |
| |
<note important>Remember that ZDoom uses inverted pitches on actors. ACSUtils functions don't invert pitches.</note> | <note important>Remember that ZDoom uses negated pitch angles on actors. ACSUtils functions don't use negated pitches.</note> |
| |
=== Example usage === | === Example usage === |
| |
<code> | <code> |
AnglesToVector(GetActorAngle(0), -GetActorPitch(0)); | AnglesToVector(GetActorAngle(0), -GetActorPitch(0)); // Un-negate actor pitch |
int dirX = r1; | int dirX = r1; |
int dirY = r2; | int dirY = r2; |