Math Script API: Difference between revisions

From DF21 Wiki
No edit summary
No edit summary
Line 62: Line 62:


float3 v = m0[1]; // v = (0, 1, 0)
float3 v = m0[1]; // v = (0, 1, 0)
float2x2 rot2x2 = math.rotationMatrix2x2(angle);
float3x3 rot3x3 = math.rotationMatrix3x3(pitch, yaw, roll);
// Transform a 2D point.
float2 point;
float2 pivot; // center point of rotation.
float2 rotatedPoint = rot2x2 * (point - pivot) + pivot;
</pre>
</pre>


Line 81: Line 88:


== Math Constants ==
== Math Constants ==
{| class="wikitable"
|+
!Constant
!Description
|-
|float pi
|value of π
|-
|float twoPi
|value of 2*π
|-
|float e
|value of the natural number e
|}

Revision as of 16:44, 14 December 2024

Core math API used by the script system. It can be accessed using the math interface and consists of a number of constants, functions, and built-in vector and matrix types.

Basic Types

Basic math types available to the script system include:

  • int - 32-bit signed integer
  • uint - 32-bit unsigned integer
  • float - 32-bit floating point value

Vector and Matrix Types

  • float2, float3, float4 - vector types of the indicated size. A float3, for example, consists of 3 floats in vector format. Components can be accessed as x, y, z, w or by index (see the examples below). GLSL style swizzling is supported, as well as array syntax. Swizzling can be used to copy out smaller vectors as well.
  • float2x2, float3x3, float4x4 - matrix types of the indicated size (2x2, 3x3, or 4x4 matrix). Rows can be accessed using the array syntax and are of the associated vector type (float2x2 = 2 x float2).

Vector Operations

Vector types (float, float2, float3, float4) can be referenced using array and component syntax. Swizzling is supported.

Example Swizzles:

float2 v(1, 2);
system.print("{}", v);    // (1, 2)
system.print("{}", v.yx); // (2, 1)
system.print("{}", v.xx); // (1, 1)
float a = v.y;
system.print("{}", a); // 2

float3 v3(1, 2, 3);
float2 v2 = v3.xz;

Example of Array Access:

float2 v2(2,3);
system.print("{}", v2[0]);  // 2
system.print("{}", v2[1]);  // 3

float4 v4(1, 2, 3, 4);
system.print("{}", v4[1]); // 2
system.print("{}", v4[3]); // 4

Example of mathematical operations, basic operations are supported:

float2 a(1,2);
float2 b(2,3);
float4 a4(1,2,3,4);
float2 c = a + b;   // c = (1,2) + (2,3) = (3,5)
float2 d = a * b;   // d = (1,2) * (2,3) = (2,6)
float2 e = a * 2.0; // e = (1,2) * 2 = (2,4)
float3 f = a4.xyz * a.y; // f = (1,2,3) * (2) = (2,4,6)

Matrix Operations

Matrices are stored as a series of vectors, which can be accessed using array syntax. Matrix types support matrix/matrix and matrix/vector multiplication, as well as matrix/float scaling. The following types are supported: float2x2, float3x3, float4x4.

float2x2 m2x2(1, 0, 0, 1); // 2x2 matrix: [1 0]
                                          [0 1]
float3x3 m0(float3(1, 0, 0), float3(0, 1, 0), float3(0, 0, 1)); // 3x3 identity.
float3x3 m1;

float3x3 m2 = m0 * m1; // concatenate matrices m0 and m1.
float3 pos;
float3 r = m2 * pos;  // multiply 'pos' by matrix m2.

float3 v = m0[1]; // v = (0, 1, 0)

float2x2 rot2x2 = math.rotationMatrix2x2(angle);
float3x3 rot3x3 = math.rotationMatrix3x3(pitch, yaw, roll);
// Transform a 2D point.
float2 point;
float2 pivot; // center point of rotation.
float2 rotatedPoint = rot2x2 * (point - pivot) + pivot;

Math Examples

float2 s(2, 3);
float2 t(4, 5);
float2 q(3, 4);
float3 r(4, 5, 6);
float3 color0(1.0, 0.5, 0.5);
float3 color1(0.25, 0.25, 1.0);
float dist = math.distance(s, q);  // distance between (2,3) and (3,4).
float blend = math.smoothstep(0, 5, dist);  // produce a blend factor between [0,1]
float2 value = math.mix(s, t, blend);
float2 sum = r.xy + r.yz;
float3 unit = math.normalize(r);
float3 color = math.mix(color0, color1, blend);

Math Constants

Constant Description
float pi value of π
float twoPi value of 2*π
float e value of the natural number e