Vector2f
APJS Script API reference for the Vector2f class.
| Type | Name | Interface Description |
|---|---|---|
| Variables | x: number | • Function: Represents the x-coordinate in a 2-dimensional vector. |
| Variables | y: number | • Function: Represents the y-coordinate in a 2-dimensional vector. |
| Functions | constructor() | |
| Functions | constructor(x?: number, y?: number) | Parameters • • |
| Functions | add(vec: Vector2f): this | • Function: Adds the components of Parameters • Returns This vector with updated components after addition. |
| Functions | angleTo(vec: Vector2f): number | • Function: Returns the angle in radians between the current vector and the specified vector Parameters • Returns The angle in radians between the two vectors. |
| Functions | clampLength(length: number): this | • Function: Clamps the length of this vector to the specified Parameters • Returns This vector with its length clamped to the specified value. |
| Functions | clone(): 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. |
| Functions | distance(vec: Vector2f): number | • Function: Calculates and returns the Euclidean distance between the current vector and another vector Parameters • Returns The distance between the two vectors. |
| Functions | divide(vec: Vector2f): this | • Function: Divides this vector by Parameters • Returns This vector after performing the division. |
| Functions | dot(vec: Vector2f): number | • Function: Returns the dot product of the vector and Parameters • Returns The dot product of the two vectors. |
| Functions | equals(vec: Vector2f): boolean | • Function: Determines if this vector is equal to the specified vector. Parameters • Returns True if the vectors are equal, false otherwise. |
| Functions | inverse(): this | • Function: Replaces each component with its reciprocal in place. Returns This vector with inverted x and y values. |
| Functions | magnitude(): number | • Function: Calculates and returns the magnitude (length) of the vector. Returns The magnitude of the vector. |
| Functions | multiply(value: number | Vector2f): this | • Function: Multiplies this vector in place. If Parameters • Returns This vector after performing the multiplication. |
| Functions | multiplyScalar(scale: number): this | • Function: Multiplies both components of this vector by Parameters • Returns This vector with updated components. |
| Functions | normalize(): 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. |
| Functions | project(vec: Vector2f): this | • Function: Projects this vector onto Parameters • Returns This vector after projection. |
| Functions | reflect(vec: Vector2f): this | • Function: Reflects this vector across the plane defined by the normal Parameters • Returns This vector after reflection. |
| Functions | set(x: number, y: number): this | • Function: Sets the two-dimensional coordinates of this vector in place. Parameters • • Returns This vector with updated coordinates. |
| Functions | sqrMagnitude(): number | • Function: Returns the squared length (magnitude) of the vector. Returns The square of the vector's magnitude. |
| Functions | subtract(vec: Vector2f): this | • Function: Subtracts the components of Parameters • Returns This vector after subtraction. |
| Functions | toString(): 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 Functions | compareApproximately(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 • • • Returns A boolean indicating whether the two vectors are approximately equal within the given distance threshold. |
| Static Functions | lerp(vecA: Vector2f, vecB: Vector2f, t: number): Vector2f | • Function: Linearly interpolates between the two vectors Parameters • • • Returns A new Vector2f representing the interpolated position between |
| Static Functions | max(vecA: Vector2f, vecB: Vector2f): Vector2f | • Function: Returns a new vector containing the largest value of each component from the two input vectors. Parameters • • Returns A new Vector2f with components set to the maximum values from vecA and vecB. |
| Static Functions | min(vecA: Vector2f, vecB: Vector2f): Vector2f | • Function: Returns a new vector containing the smallest value of each component from the two input vectors. Parameters • • 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);
}
}