Vector math in ACS
int fua_Vec(operands..., int channel, int op) // 24012
Availability
ZandroX only.
fua_Vecis what zt-bcc's built-invec2andvec3types compileLength(),Unit(),dotandcrossto. A mod written in BCS never calls it by name: the compiler's-targetoption decides.
zt-bcc -target |
What a mod gets |
|---|---|
zandronum (the default) |
Plain ACS on every engine, ZandroX included |
fua |
fua_Vec calls; the mod needs ZandroX |
any |
fua_Vec on ZandroX, plain ACS on Zandronum 3.3 and older ZandroX builds |
ACC has no vec2 or vec3: with fua.acs included in place of zcommon.acs it calls fua_Vec
directly, naming each operation with the FUA_VEC_ constants
(Compiling ACS). zt-bcc's types are the better fit, as they pass
the operands, the channel and the operation for you, and fall back to plain ACS on other engines.
A mod that also runs on stock Zandronum servers builds with -target any. It asks the engine for
fua_Vec once per map, the first time it needs it, and an engine without it answers 0 and
prints nothing.
Usage
On ZandroX each operation is one engine call instead of a function of 50 to 440 ACS instructions, and its result is exact: the true value rounded to the nearest 1/65536 and saturated at ±32767.99998. The plain-ACS version is within 1/65536 of exact, so the two can differ in the lowest bit. Every machine running ZandroX gives the same bits, so a client's clientside scripts agree with the server's.
Parameters
The operation comes last, then the map variable holding the module's channel array before it; the operands come first.
op |
Operation | Operands | Returns | Writes to the channel |
|---|---|---|---|---|
| 0 | the version, 1 (the call may be fua_Vec(0) alone) |
none | the version | nothing |
| 1, 2 | length of a vec2, a vec3 | x, y[, z] | the length | nothing |
| 3, 4 | unit vector of a vec2, a vec3 | x, y[, z] | x | y[, z] in cells 0[, 1] |
| 5, 6 | dot product of two vec2s, two vec3s | a's, then b's | the product | nothing |
| 7 | cross product of two vec3s | a's, then b's | x | y, z in cells 0, 1 |
An unknown operation or the wrong number of operands returns 0 and writes nothing. A script can also call it directly, with any map array of at least two cells as the channel.
Examples
Each example is a whole library: compile it with zt-bcc, add it to LOADACS and run its scripts with
pukename. zt-bcc's vectors page
covers the types themselves.
How far an imp is, and whether the player faces it:
#library "fuaaim"
#import "zcommon.bcs"
script "Facing" ( int tid ) {
vec3 to = ActorPos( tid ) - ActorPos( 0 );
fixed far = to.Length();
// Within about 45 degrees when the two unit vectors' dot product is over 0.7.
bool ahead = ( to.Unit() dot ActorDir( 0 ) ) > 0.7;
Log( s: "imp ", d: far >> 16, s: " units away, ahead ", d: ahead );
}
Pulling the player toward a point at a steady speed, a step a tic:
#library "fuapull"
#import "zcommon.bcs"
script "PullToward" ( int tid ) {
vec3 to = ActorPos( tid ) - ActorPos( 0 );
if ( to.Length() > 64.0 ) {
vec3 push = to.Unit() * 12.0;
SetActorVelocity( 0, push.x, push.y, push.z, false, false );
Log( s: "pulling at ", d: push.Length() >> 16 );
}
}
How far the imp is and whether the player faces it, for ACC, on the floor's plane. The channel is the map variable number of an array of two or more cells, here 0 as it is the library's first map variable:
#library "accaim"
#include "fua.acs"
int channel[2]; // map variable 0: a unit vector's y lands in cell 0
script "AccFacing" (int tid)
{
int dx = GetActorX(tid) - GetActorX(0);
int dy = GetActorY(tid) - GetActorY(0);
int far = fua_Vec(dx, dy, 0, FUA_VEC_LENGTH2);
int ux = fua_Vec(dx, dy, 0, FUA_VEC_UNIT2);
int ahead = fua_Vec(ux, channel[0], Cos(GetActorAngle(0)), Sin(GetActorAngle(0)), 0, FUA_VEC_DOT2) > 0.7;
Log(s: "imp ", d: far >> 16, s: " units away, ahead ", d: ahead);
}
Best practices
- Build with
-target anywhen the mod also runs on stock Zandronum: ZandroX then answers in one engine call, and stock servers run the plain ACS. - Whole units are
x >> 16. Outside astrict namespace, as in these examples and every ACC-style file, afixedis a plain number:( int ) xkeeps its bits, so( int ) 816.5is 53,510,144, andint( x )doesn't compile. Shift right by 16 for the whole part, or print it withf:. Inside astrict namespace,int( x )converts; zt-bcc's strict mode page lists the other differences, such asfixed * fixedbeing an integer multiply outside it (useFixedMul). - Take
Unit()once and keep it when several uses need the same direction; each call is a square root. - Compare squared lengths (
v dot v) against a squared distance when you only need "nearer than": it skips the square root, and both sides stay within fixed point's range up to about 181 units' length.
Online
Every machine running ZandroX computes the same bits for each operation, so a clientside script's vectors match the server's for the same inputs.