PitchTo

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.


Actor

clearscope double PitchTo(Actor target, double zOfs = 0, double targZOfs = 0, bool absolute = false) const

Usage

Returns the absolute pitch from the caller to the target actor.

Parameters

  • Actor target
The actor to calculate the pitch to.
  • double zOfs
The vertical offset to the callers' position to use for the check. By default, the pitch calculation will be done from the feet of the caller. So you can pass the callers' Height to get the top of their hitbox. Or their height divided by 2 to get the middle.
  • double targZOfs
Ditto, but for the target actor instead.
  • bool absolute
If true, the function will not account for static portals, default is false.

Return value

  • double — The absolute pitch between the caller and target actor.

Example

This simple function can be used to make a monster ONLY shoot if it will hit its' target. It uses AngleTo() and PitchTo() to give an angle and pitch to LineTrace that fires the raycast at the targets' direction.

Notice that the pitch in the PitchTo() call is calculated from the middle of the callers' and targets' hitboxes. Instead of their feet, which is where the origins of actors are located at.

	bool A_OnlyHitTarget()
	{
		if (!Target) return false;
		FLineTraceData LOF;
		
		double AimPitch = PitchTo (Target,Height/2,Target.Height/2);
		
		LineTrace (AngleTo (Target),INT.MAX,AimPitch,TRF_SOLIDACTORS,Height/2,data:LOF);
		
		if (LOF.HitActor == Target)
			return true;
		
		return false;
	}

ZScript definition

Note: The ZScript definition below is for reference and may be different in the current version of GZDoom.The most up-to-date version of this code can be found on GZDoom GitHub.
	clearscope double PitchTo(Actor target, double zOfs = 0, double targZOfs = 0, bool absolute = false) const
	{
		Vector3 origin = (pos.xy, pos.z - floorClip + zOfs);
		Vector3 dest = (target.pos.xy, target.pos.z - target.floorClip + targZOfs);

		Vector3 diff;
		if (!absolute)
			diff = level.Vec3Diff(origin, dest);
		else
			diff = dest - origin;

		return -atan2(diff.z, diff.xy.Length());
	}

See also