Collider
Base class for 3D collision shapes.
| Type | Name | Interface Description |
|---|---|---|
| Variables | center: Vector3f | • Function: Gets or sets the collider's local center offset relative to the SceneObject. Use this to shift the collision shape without moving the visual object itself, such as placing a hitbox slightly in front of a character. |
| Variables | emitCollisionEvent: boolean | • Function: Gets or sets whether APJS should emit collision callbacks for this collider. When enabled, APJS checks this collider's contacts on each update and emits , , and . Disabling it clears the cached collision state. |
| Variables | eulerAngles: Vector3f | • Function: Same local collider rotation as , expressed as Euler angles in degrees. Use this when you want to rotate the collision shape relative to the object. This is not the current world-space rotation of the physics body. |
| Variables | interactable: boolean | • Function: Gets or sets whether this collider is marked as interactable for the Physics Interactor path. When enabled, APJS adds a dedicated interaction bit to the collider's category mask so Physics Interactor Script Graph Node can pick and drag it. This does not change normal collision behavior. |
| Variables | isTangible: boolean | • Function: Gets or sets whether the collider is tangible. Default is true. When true, objects collide with it normally. When false, it becomes a trigger — objects can pass through it, but collision events can still be detected. |
| Variables | physicsMaterial: PhysicsMaterial | null | • Function: Gets or sets the physics material used by this collider. The assigned material provides dynamic friction, static friction, and bounciness values for contact response. |
| Variables | rotation: Quaternionf | • Function: Local rotation offset of the collider shape, stored as a quaternion. This rotates the collision shape relative to the SceneObject or attached RigidBody, without rotating the visual object itself. It is a collider configuration value, not the live world-space rotation currently simulated by physics. |
| Functions | constructor() |
Examples
constructor()
let obj = new APJS.Collider();
Use Case
Example 1 — Play a sound effect when a 2D physics object collides — uses CollisionEvent2D.Enter on collider's object emitter
@component()
export class CollisionSoundEffect extends APJS.BasicScriptComponent {
@serializeProperty
sfxPlayer!: APJS.SceneObject;
private sfxAudio!: APJS.AudioComponent;
private collider!: APJS.BoxCollider2D;
private initialized = false;
private collisionCallback!: (event: APJS.IEvent) => void;
onUpdate(dt: number): void {
if (this.initialized) return;
if (!this.sfxPlayer) return;
this.sfxAudio = this.sfxPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
// Get any 2D collider on this object (BoxCollider2D or CircleCollider2D)
this.collider = this.getSceneObject().getComponent("BoxCollider2D") as APJS.BoxCollider2D;
if (!this.sfxAudio || !this.collider) return;
// CRITICAL: enable collision events before registering listener
this.collider.emitCollisionEvent = true;
this.collisionCallback = (event: APJS.IEvent) => {
// Play SFX on each collision
this.sfxAudio.stop();
this.sfxAudio.loopCount = 1;
this.sfxAudio.play();
};
// Use object emitter on the COLLIDER (not global emitter)
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.on(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
this.initialized = true;
}
onDestroy(): void {
if (this.collider && this.collisionCallback) {
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.off(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
}
}
}
Example 2 — 3D physics collision event handler. Listens on BoxCollider/SphereCollider for Enter/Stay/Exit events.
@component()
export class CollisionHandler3D extends APJS.BasicScriptComponent {
private collider: APJS.Collider;
private inited = false;
private hitCount = 0;
private onEnter = (event: APJS.IEvent) => {
this.hitCount++;
const infos = event.args[0] as APJS.CollisionInfo[];
for (const info of infos) {
if (info.otherObject) {
console.log("[Collision] Enter #" + this.hitCount + ": " + info.otherObject.name);
console.log(" point: " + info.point.x.toFixed(2) + ", " + info.point.y.toFixed(2) + ", " + info.point.z.toFixed(2));
console.log(" normal: " + info.normal.x.toFixed(2) + ", " + info.normal.y.toFixed(2) + ", " + info.normal.z.toFixed(2));
}
}
};
private onExit = (event: APJS.IEvent) => {
console.log("[Collision] Exit");
};
// RecordStart: only the script-owned hitCount accumulator needs reset; this example
// does not move any RigidBody, so no Physics3D reset block is needed. See GameState SKILL.
private onRecordStart = (_event: APJS.IEvent) => {
this.hitCount = 0;
};
onStart(): void {
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.RecordStart, this.onRecordStart);
}
onUpdate(dt: number): void {
if (!this.inited) {
const obj = this.getSceneObject();
if (!obj) return;
// Try BoxCollider first, then SphereCollider
this.collider = obj.getComponent("BoxCollider") as APJS.BoxCollider;
if (!this.collider) {
this.collider = obj.getComponent("SphereCollider") as APJS.SphereCollider;
}
if (!this.collider) return;
this.inited = true;
this.collider.emitCollisionEvent = true;
// CRITICAL: pass collider, NOT sceneObject
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.on(APJS.CollisionEvent.Enter, this.onEnter, this);
emitter.on(APJS.CollisionEvent.Exit, this.onExit, this);
}
}
onDestroy(): void {
if (this.collider) {
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.off(APJS.CollisionEvent.Enter, this.onEnter, this);
emitter.off(APJS.CollisionEvent.Exit, this.onExit, this);
}
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.RecordStart, this.onRecordStart);
}
}