Table of Contents

Class AimConvergence

Namespace
FishMMO.Shared
Assembly
FishMMO.Shared.dll

Where a player's shots go: from the character's eye to the point under the crosshair.

public static class AimConvergence
Inheritance
AimConvergence
Inherited Members

Remarks

Why the aim is not simply the camera's forward. Shots start at the character's eye (CharacterAimOrigin, derived from the motor on every peer so a client cannot pick it), while the crosshair is on the camera's line, and the camera is never at the eye: in third person it sits behind and below it (the follow point is chest high), and over the shoulder it is also half a metre to the side. A shot along the camera's forward from the eye travels a line PARALLEL to the one the player aimed down, so it lands that offset away from the crosshair at every range: never on it.

The fix is to make the two lines meet. The camera's ray is traced into the world to find what the crosshair is on (the apex), and the aim is the direction from the eye to that point. The camera and the eye then form a V whose point is the thing the player is looking at, at whatever range it is.

Two guards keep the V sane. The camera's ray starts level with the eye, not at the camera, so nothing between the camera and the character (a wall behind them, their own back) can be the apex. And the apex is never closer than MinConvergenceDistance beyond that start: a crosshair resting on a post at the character's elbow would otherwise swing the aim to the side by up to ninety degrees.

The body is angled into the V too. The character faces, and walks toward, the same point, measured from where it is drawn (BodyHeading(Vector3, Vector3, Vector3)), so camera, character and shot all look at one point. The body's point is held at least BodyMinConvergenceDistance out, so the crosshair crossing from a near object to a far one turns the body by a few degrees rather than snapping it. The replicate carries the body's heading as a small yaw offset from the aim (HeadingOffset).

Computed by the owner only, in KCCPlayer.PopulateInput: the camera exists on no other peer. The result is quantised there before the owner predicts from it, like every other input (see AimDirectionCompression).

Fields

BodyMinConvergenceDistance

The nearest the point the BODY turns to (and walks toward) may be, beyond the character's level along the camera's ray.

public const float BodyMinConvergenceDistance = 6

Field Value

float

Remarks

Farther than the shot's minimum on purpose. The apex jumps whenever the crosshair crosses from something near to something far, and the body follows it with the facing's own snap; at 6 m the shoulder's half metre turns the body at most about five degrees, which reads as leaning into the aim rather than twitching. Shots are not held to this: they still converge exactly from MinConvergenceDistance.

FarDistance

How far the crosshair's ray is traced, and where the apex is put when it hits nothing (open sky, the horizon).

public const float FarDistance = 250

Field Value

float

Remarks

Far enough that the eye's remaining offset from the camera's line is a few thousandths of a radian there; the trace is once a tick on the owner only, so its length costs nothing measurable.

MaxHeadingOffsetDegrees

The largest yaw the replicate may put between the aim and the body's heading: the whole circle.

public const float MaxHeadingOffsetDegrees = 180

Field Value

float

Remarks

The offset is measured from the heading of the AIM, and the aim's heading is not always near the body's. Over the shoulder looking down at nearby ground the eye is half a metre beside a ray that converges steeply, so the aim's heading swings toward the camera's side: 23 degrees off the body at 60 degrees down, 45 at 77, 79 at 89. Looking almost straight down while running, the eye leads the drawn body by more than the aim's few centimetres of ground run, and its heading points BACKWARDS: 180 degrees.

A bound of 45 degrees clamped those cases, and the body then faced (and walked) up to 135 degrees off the view. Bending the aim to fit instead moved the shot off the crosshair by 15 to 20 degrees. Neither is needed: the body's heading is the owner's to choose (it steers the character with its own input anyway), so nothing is gained by a modified client sending any value it likes. Two bytes on the wire for an offset over 16 degrees, sent only when it changes.

MaxOriginOffset

The farthest from the eye a launch line may start, metres.

public const float MaxOriginOffset = 1

Field Value

float

Remarks

The line starts where the third-person camera's ray passes the eye: the follow point's offset from the eye plus however far the drawn body trails the simulated one. Over the shoulder the shoulder's own displacement is taken out first (KCCPlayer.ResolveOwnerAim), so the projectile leaves the character, not the view's side. A metre covers every camera this game has, and is the whole of what a modified client can gain by lying: its projectile starts within a metre of where its eye is, and the path from the eye to the line is still checked for cover (AbilityObject.ResolveSpawnPose).

MinConvergenceDistance

The nearest the apex may be, beyond the eye's level along the camera's ray.

public const float MinConvergenceDistance = 2

Field Value

float

Methods

AimFrame(Vector3, out Vector3, out Vector3)

The two directions across an aim: to its right (level), and up across it. The same on every peer for the same aim.

public static void AimFrame(Vector3 aimDirection, out Vector3 right, out Vector3 up)

Parameters

aimDirection Vector3
right Vector3
up Vector3

Apex(Vector3, Vector3, Vector3, float)

The point the shot should pass through.

public static Vector3 Apex(Vector3 cameraPosition, Vector3 cameraForward, Vector3 eye, float hitDistance)

Parameters

cameraPosition Vector3

The camera.

cameraForward Vector3

The camera's forward, unit length.

eye Vector3

The aim origin.

hitDistance float

How far beyond RayStart(Vector3, Vector3, Vector3) the trace hit something; infinity for nothing.

Returns

Vector3

BodyFloorAlongRay(Vector3)

How far along the camera's ray the body's point must be for it to be BodyMinConvergenceDistance away ON THE GROUND PLANE, which is the plane the body turns in.

public static float BodyFloorAlongRay(Vector3 cameraForward)

Parameters

cameraForward Vector3

Returns

float

Remarks

The floor was a distance along the ray. Looking down, that distance covers less and less ground (by the cosine of the pitch) while the camera's sideways offset stays what it is, so with the crosshair on the ground nearby the body turned 10 degrees off the view at 60 down, 19 at 75 and 45 at 85, and walked that way. Measured on the ground the turn is the same small angle at any pitch.

BodyHeading(Vector3, Vector3, Vector3)

The heading the body turns to: from where the character is DRAWN to its BodyPoint(Vector3, Vector3, Vector3, float, Vector3), flat.

public static Vector3 BodyHeading(Vector3 drawnPosition, Vector3 bodyPoint, Vector3 cameraForward)

Parameters

drawnPosition Vector3
bodyPoint Vector3
cameraForward Vector3

Returns

Vector3

Remarks

From the drawn character, not the eye, because the view is built around the drawn one: the camera follows the smoothed body, which trails the simulated motor (and so the eye) by the owner's interpolation. Measured from the eye, a strafing character would be off the crosshair's line by that lag, turn toward it, and so steer its own strafe into a slow circle. Measured from the drawn body, the answer depends only on the camera's layout:

  • third and first person: the drawn character is on the crosshair's line, so this is the camera's heading exactly, as before;
  • over the shoulder: the character is the shoulder's offset left of that line, so this turns it in toward the crosshair, by about five degrees at the nearest and almost nothing at range.

BodyPoint(Vector3, Vector3, Vector3, float, Vector3)

The point the body turns to and walks toward: the same thing under the crosshair, held at least BodyMinConvergenceDistance beyond the character's level along the camera's ray.

public static Vector3 BodyPoint(Vector3 cameraPosition, Vector3 cameraForward, Vector3 eye, float hitDistance, Vector3 drawnPosition)

Parameters

cameraPosition Vector3

The camera.

cameraForward Vector3

The camera's forward, unit length.

eye Vector3

The aim origin, which hitDistance is measured from.

hitDistance float

How far beyond the eye's RayStart(Vector3, Vector3, Vector3) the trace hit; infinity for nothing.

drawnPosition Vector3

Where the character is drawn (the camera's follow point).

Returns

Vector3

DecodeDistance(byte)

The distance a convergence code stands for, in metres; 0 for the code 0 ("not given").

public static float DecodeDistance(byte code)

Parameters

code byte

Returns

float

DecodeHeadingOffset(short)

The heading offset a wire value stands for, held to the bound: the server reads whatever a client sent.

public static float DecodeHeadingOffset(short encoded)

Parameters

encoded short

Returns

float

Direction(Vector3, Vector3, Vector3)

The unit direction from the eye to the apex, or fallback if they coincide.

public static Vector3 Direction(Vector3 eye, Vector3 apex, Vector3 fallback)

Parameters

eye Vector3
apex Vector3
fallback Vector3

Returns

Vector3

EncodeDistance(float)

The one-byte code for a distance along the aim (ConvergenceDistance): 1 to 255 on a logarithmic scale, never 0, which means "not given".

public static byte EncodeDistance(float metres)

Parameters

metres float

Returns

byte

Remarks

Logarithmic because what the distance is used for is an ANGLE (the turn from the hand's line onto the eye's), and the angle's error goes with the distance's relative error: 1.25% at worst here, which puts a projectile from a hand 0.85 m off the eye's line within about a centimetre of the point aimed at, at any range.

EncodeHeadingOffset(float)

The wire value of a heading offset: quarter degrees, held to the bound.

public static short EncodeHeadingOffset(float degrees)

Parameters

degrees float

Returns

short

EncodeLaunch(Vector3, Vector3, Vector3, float)

The launch line the owner states for a camera whose crosshair line passes the eye at launchPoint, with the point aimed at apexDistance along the aim.

public static AimConvergence.AimLaunch EncodeLaunch(Vector3 eye, Vector3 launchPoint, Vector3 aimDirection, float apexDistance)

Parameters

eye Vector3
launchPoint Vector3
aimDirection Vector3
apexDistance float

Returns

AimConvergence.AimLaunch

HeadingOffsetDegrees(Vector3, Vector3)

The yaw, in degrees about world up, that turns the aim's heading onto the body's. Zero when either looks straight up or down, where neither has a heading.

public static float HeadingOffsetDegrees(Vector3 aimDirection, Vector3 bodyHeading)

Parameters

aimDirection Vector3
bodyHeading Vector3

Returns

float

LaunchLine(Vector3, Vector3, AimLaunch, out Vector3, out Vector3)

The line a projectile is simulated on: where it starts and which way it runs.

public static void LaunchLine(Vector3 aimOrigin, Vector3 aimDirection, AimConvergence.AimLaunch launch, out Vector3 origin, out Vector3 direction)

Parameters

aimOrigin Vector3
aimDirection Vector3
launch AimConvergence.AimLaunch
origin Vector3
direction Vector3

Remarks

With a launch line given, from the point beside the eye it names to the point its distance along the aim: the camera's ray through the crosshair, rebuilt. Without one, from the eye along the aim.

A pure function of the replicated input and the eye every peer derives, so the owner and the server simulate the same projectile to the last bit, and no trace is involved. The offset is held to MaxOriginOffset whatever was sent.

MovementRotation(Quaternion, short)

The rotation movement and facing are taken from: the aim, turned about world up by the heading offset onto the body's heading (BodyHeading(Vector3, Vector3, Vector3)). Exactly the aim when the offset is zero.

public static Quaternion MovementRotation(Quaternion aimRotation, short headingOffset)

Parameters

aimRotation Quaternion
headingOffset short

Returns

Quaternion

Remarks

Turning about world up moves a direction's heading by exactly the angle and leaves its pitch alone, so the movement basis (the heading) is the body's, and swimming's dive (the pitch) stays the aim's.

RayStart(Vector3, Vector3, Vector3)

Distance along the camera's ray at which the trace starts: level with the eye, or the camera itself when the eye is behind it (first person, where the two are the same point).

public static float RayStart(Vector3 cameraPosition, Vector3 cameraForward, Vector3 eye)

Parameters

cameraPosition Vector3
cameraForward Vector3
eye Vector3

Returns

float

Resolve(PhysicsScene, Vector3, Vector3, Vector3, Vector3, int, IList<Collider>, ref RaycastHit[], out Vector3, out float, out Vector3)

Traces the crosshair into the world and returns the aim from the eye to what it is on, and the heading the body turns to (BodyHeading(Vector3, Vector3, Vector3)).

public static Vector3 Resolve(PhysicsScene physicsScene, Vector3 cameraPosition, Vector3 cameraForward, Vector3 eye, Vector3 drawnPosition, int layerMask, IList<Collider> ignored, ref RaycastHit[] buffer, out Vector3 bodyHeading, out float apexDistance, out Vector3 launchPoint)

Parameters

physicsScene PhysicsScene

The physics scene the character lives in.

cameraPosition Vector3

The camera.

cameraForward Vector3

The camera's forward.

eye Vector3

The aim origin (CharacterAimOrigin).

drawnPosition Vector3

Where the character is drawn: the camera's follow point.

layerMask int

What the crosshair can rest on: the layers the cast-time target trace sees.

ignored IList<Collider>

The character's own colliders.

buffer RaycastHit[]

Hit buffer, grown as needed.

bodyHeading Vector3

The flat direction the body faces and walks in.

apexDistance float

How far from the eye the point aimed at is, along the returned direction.

launchPoint Vector3

The point on the crosshair's line level with the eye: where the launch line starts.

Returns

Vector3

The unit aim direction from the eye.