Skip to main content

Vector2f

APJS Script API reference for the Vector2f class.

TypeNameInterface Description
Variablesx: number

Function: Represents the x-coordinate in a 2-dimensional vector.

Variablesy: number

Function: Represents the y-coordinate in a 2-dimensional vector.

Functionsconstructor()

Functionsconstructor(x?: number, y?: number)

Parameters

x: - The x-coordinate of the vector (optional).

y: - The y-coordinate of the vector (optional).

Functionsadd(vec: Vector2f): this

Function: Adds the components of vec to this vector in place.

Parameters

vec: - The vector whose components will be added to this vector.

Returns This vector with updated components after addition.

FunctionsangleTo(vec: Vector2f): number

Function: Returns the angle in radians between the current vector and the specified vector vec.

Parameters

vec: - The target vector to calculate the angle against.

Returns The angle in radians between the two vectors.

FunctionsclampLength(length: number): this

Function: Clamps the length of this vector to the specified length and returns this vector.

Parameters

length: - The maximum length to clamp the vector to.

Returns This vector with its length clamped to the specified value.

Functionsclone(): Vector2f

Function: Creates and returns a new Vector2f with the same component values. Use this before calling mutating math methods when the original value must be preserved.

Returns A new Vector2f with the same x and y values as this vector.

Functionsdistance(vec: Vector2f): number

Function: Calculates and returns the Euclidean distance between the current vector and another vector vec.

Parameters

vec: - The target vector to which the distance is calculated.

Returns The distance between the two vectors.

Functionsdivide(vec: Vector2f): this

Function: Divides this vector by vec component-wise in place.

Parameters

vec: - The vector to divide by, where each component divides the matching component of this vector.

Returns This vector after performing the division.

Functionsdot(vec: Vector2f): number

Function: Returns the dot product of the vector and vec.

Parameters

vec: - The vector to compute the dot product with.

Returns The dot product of the two vectors.

Functionsequals(vec: Vector2f): boolean

Function: Determines if this vector is equal to the specified vector.

Parameters

vec: - The vector to compare with.

Returns True if the vectors are equal, false otherwise.

Functionsinverse(): this

Function: Replaces each component with its reciprocal in place.

Returns This vector with inverted x and y values.

Functionsmagnitude(): number

Function: Calculates and returns the magnitude (length) of the vector.

Returns The magnitude of the vector.

Functionsmultiply(value: number | Vector2f): this

Function: Multiplies this vector in place. If value is a number, both components are scaled by that number. If value is a Vector2f, multiplication is performed component-wise.

Parameters

value: - A number or another Vector2f to multiply with.

Returns This vector after performing the multiplication.

FunctionsmultiplyScalar(scale: number): this

Function: Multiplies both components of this vector by scale in place.

Parameters

scale: - The scalar value to multiply the vector's components by.

Returns This vector with updated components.

Functionsnormalize(): this

Function: Normalizes this vector in place so its magnitude becomes 1. If the magnitude is zero, the vector is left unchanged.

Returns This vector after normalization.

Functionsproject(vec: Vector2f): this

Function: Projects this vector onto vec in place. If vec has zero length, this vector is set to (0, 0).

Parameters

vec: - The vector to project onto.

Returns This vector after projection.

Functionsreflect(vec: Vector2f): this

Function: Reflects this vector across the plane defined by the normal vec in place.

Parameters

vec: - The normal vector defining the reflection plane.

Returns This vector after reflection.

Functionsset(x: number, y: number): this

Function: Sets the two-dimensional coordinates of this vector in place.

Parameters

x: - The x-coordinate to set.

y: - The y-coordinate to set.

Returns This vector with updated coordinates.

FunctionssqrMagnitude(): number

Function: Returns the squared length (magnitude) of the vector.

Returns The square of the vector's magnitude.

Functionssubtract(vec: Vector2f): this

Function: Subtracts the components of vec from this vector in place.

Parameters

vec: - The vector to subtract from this vector.

Returns This vector after subtraction.

FunctionstoString(): string

Function: Returns a string representation of the vector.

Returns A string in the format "Vector2f(x, y)" where x and y are fixed to 5 decimal places.

Static FunctionscompareApproximately(vec1: Vector2f, vec2: Vector2f, dist: number): boolean

Function: Approximate comparison of the two vectors by the value of each dimension with a specified distance threshold.

Parameters

vec1: - The first vector to compare.

vec2: - The second vector to compare.

dist: - The maximum allowed difference between corresponding dimensions for the vectors to be considered approximately equal.

Returns A boolean indicating whether the two vectors are approximately equal within the given distance threshold.

Static Functionslerp(vecA: Vector2f, vecB: Vector2f, t: number): Vector2f

Function: Linearly interpolates between the two vectors vecA and vecB by the factor t.

Parameters

vecA: - The starting vector.

vecB: - The ending vector.

t: - The interpolation factor, typically ranging from 0 to 1.

Returns A new Vector2f representing the interpolated position between vecA and vecB.

Static Functionsmax(vecA: Vector2f, vecB: Vector2f): Vector2f

Function: Returns a new vector containing the largest value of each component from the two input vectors.

Parameters

vecA: - The first vector to compare.

vecB: - The second vector to compare.

Returns A new Vector2f with components set to the maximum values from vecA and vecB.

Static Functionsmin(vecA: Vector2f, vecB: Vector2f): Vector2f

Function: Returns a new vector containing the smallest value of each component from the two input vectors.

Parameters

vecA: - The first vector to compare.

vecB: - The second vector to compare.

Returns A new Vector2f with components set to the minimum values from vecA and vecB.

Examples

constructor()

let obj = new APJS.Vector2f();

constructor(x?: number, y?: number)

let obj = new APJS.Vector2f();

compareApproximately(vec1: Vector2f, vec2: Vector2f, dist: number): boolean

let a = new Vector2f(0.0000001, 0.0000001);
let b = new Vector2f(0.0000000, 0.0000000);
Vector2f.compareApproximately(a, b, 0.0001); // true

Use Case

Example 1 — Detect 2D collision and reverse velocity to bounce off obstacles

@component()
export class CollisionBounce extends APJS.BasicScriptComponent {
private collisionCallback: (event: APJS.IEvent) => void;
private collider: APJS.BoxCollider2D;
private velocity: APJS.Vector2f = new APJS.Vector2f(100, 100);
private startVelocity: APJS.Vector2f = new APJS.Vector2f(100, 100);
private startPos!: APJS.Vector2f;
private startGravityOn = true;
private rb!: APJS.RigidBody2D;
private inited = false;

// RecordStart: reset bouncing ball per Physics2D §"RecordStart Reset for 2D Physics"
// (single-body block): velocity → script accumulators (this.velocity) → anchoredPosition
// → useGravity. Listener wiring per GameState §"RecordStart / RecordEnd Lifecycle".
private onRecordStart = (_event: APJS.IEvent) => {
if (!this.inited) return;
this.rb.velocity = new APJS.Vector2f(0, 0);
this.velocity = new APJS.Vector2f(this.startVelocity.x, this.startVelocity.y);
const obj = this.getSceneObject();
const st = obj.getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st && this.startPos) {
st.anchoredPosition = new APJS.Vector2f(this.startPos.x, this.startPos.y);
}
this.rb.useGravity = this.startGravityOn;
};

onStart(): void {
this.collider = this.getSceneObject().getComponent("BoxCollider2D") as APJS.BoxCollider2D;
if (!this.collider) return;

this.collider.emitCollisionEvent = true;

this.collisionCallback = (event: APJS.IEvent) => {
const infos = event.args[0] as APJS.CollisionInfo2D[];
for (const info of infos) {
// Reflect velocity based on collision normal
const normal = info.normal;
const dot = this.velocity.x * normal.x + this.velocity.y * normal.y;
this.velocity = new APJS.Vector2f(
this.velocity.x - 2 * dot * normal.x,
this.velocity.y - 2 * dot * normal.y
);
}
};

const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.on(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.RecordStart, this.onRecordStart);
}

onUpdate(deltaTime: number): void {
const rb = this.getSceneObject().getComponent("RigidBody2D") as APJS.RigidBody2D;
if (!rb) return;
if (!this.inited) {
this.rb = rb;
const st = this.getSceneObject().getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st) this.startPos = new APJS.Vector2f(st.anchoredPosition.x, st.anchoredPosition.y);
this.startGravityOn = rb.useGravity;
this.inited = true;
}
const pos = rb.position;
rb.position = new APJS.Vector2f(
pos.x + this.velocity.x * deltaTime,
pos.y + this.velocity.y * deltaTime
);
}

onDestroy(): void {
if (this.collisionCallback && this.collider) {
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.off(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
}
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.RecordStart, this.onRecordStart);
}
}

Example 2 — Move a 2D physics object using RigidBody2D addForce with ForceMode2D.Impulse for jump.

@component()
export class PhysicsForceMovement extends APJS.BasicScriptComponent {
@serializeProperty
private moveSpeed: number = 300;

@serializeProperty
private jumpForce: number = 800;

private rb: APJS.RigidBody2D;
private isGrounded: boolean = true;
private startPos!: APJS.Vector2f;
private startGravityOn = true;
private inited = false;

private onTouchEvent = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began && this.isGrounded) {
// Apply upward impulse for jump (one-shot, mass-dependent)
this.rb.addForce(new APJS.Vector2f(0, this.jumpForce), APJS.ForceMode2D.Impulse);
this.isGrounded = false;
}
};

// RecordStart: reset per Physics2D §"RecordStart Reset for 2D Physics" — velocity →
// anchoredPosition → useGravity → script accumulators (isGrounded). See GameState SKILL.
private onRecordStart = (_event: APJS.IEvent) => {
if (!this.inited) return;
this.rb.velocity = new APJS.Vector2f(0, 0);
const st = this.getSceneObject().getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st && this.startPos) {
st.anchoredPosition = new APJS.Vector2f(this.startPos.x, this.startPos.y);
}
this.rb.useGravity = this.startGravityOn;
this.isGrounded = true;
};

onStart(): void {
this.rb = this.getSceneObject().getComponent("RigidBody2D") as APJS.RigidBody2D;
this.rb.useGravity = true;
const st = this.getSceneObject().getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st) this.startPos = new APJS.Vector2f(st.anchoredPosition.x, st.anchoredPosition.y);
this.startGravityOn = this.rb.useGravity;
this.inited = true;
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.onTouchEvent);
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.RecordStart, this.onRecordStart);
}

onUpdate(deltaTime: number): void {
if (!this.rb) return;

// Horizontal movement
const pos = this.rb.position;
this.rb.position = new APJS.Vector2f(
pos.x + this.moveSpeed * deltaTime,
pos.y
);

// Simple ground detection (clamp Y)
if (pos.y <= 0) {
this.rb.position = new APJS.Vector2f(pos.x, 0);
this.isGrounded = true;
}
}

onDestroy(): void {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.onTouchEvent);
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.RecordStart, this.onRecordStart);
}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies