Skip to content

Commit

Permalink
Add ZScript method LevelLocals.SphericalCoords.
Browse files Browse the repository at this point in the history
It computes spherical coordinates from one point in the world to another. Useful for checking whether one actor is inside another actor's view cone.
  • Loading branch information
argv-minus-one authored and coelckers committed Aug 21, 2018
1 parent 03fa1a1 commit 1d930b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/g_level.cpp
Expand Up @@ -2043,6 +2043,30 @@ DEFINE_ACTION_FUNCTION(FLevelLocals, Vec3Diff)
ACTION_RETURN_VEC3(VecDiff(DVector3(x1, y1, z1), DVector3(x2, y2, z2)));
}

DEFINE_ACTION_FUNCTION(FLevelLocals, SphericalCoords)
{
PARAM_PROLOGUE;
PARAM_FLOAT(viewpointX);
PARAM_FLOAT(viewpointY);
PARAM_FLOAT(viewpointZ);
PARAM_FLOAT(targetX);
PARAM_FLOAT(targetY);
PARAM_FLOAT(targetZ);
PARAM_ANGLE_DEF(viewYaw);
PARAM_ANGLE_DEF(viewPitch);
PARAM_BOOL_DEF(absolute);

DVector3 viewpoint(viewpointX, viewpointY, viewpointZ);
DVector3 target(targetX, targetY, targetZ);
auto vecTo = absolute ? target - viewpoint : VecDiff(viewpoint, target);

ACTION_RETURN_VEC3(DVector3(
deltaangle(vecTo.Angle(), viewYaw).Degrees,
deltaangle(-vecTo.Pitch(), viewPitch).Degrees,
vecTo.Length()
));
}

DEFINE_ACTION_FUNCTION(FLevelLocals, Vec2Offset)
{
PARAM_PROLOGUE;
Expand Down
1 change: 1 addition & 0 deletions wadsrc/static/zscript/base.txt
Expand Up @@ -668,6 +668,7 @@ struct LevelLocals native

native static clearscope vector2 Vec2Diff(vector2 v1, vector2 v2);
native static clearscope vector3 Vec3Diff(vector3 v1, vector3 v2);
native static clearscope vector3 SphericalCoords(vector3 viewpoint, vector3 targetPos, vector2 viewAngles = (0, 0), bool absolute = false);

native static clearscope vector2 Vec2Offset(vector2 pos, vector2 dir, bool absolute = false);
native static clearscope vector3 Vec2OffsetZ(vector2 pos, vector2 dir, double atz, bool absolute = false);
Expand Down

0 comments on commit 1d930b4

Please sign in to comment.