Getting Started
Physijs allows you to write custom JavaScript directly within the editor. Every script runs in real-time once you hit the PLAY button.
Note: Scripts are attached to individual objects. If you delete an object, its script is also removed!
The Environment
The engine uses Three.js for rendering and Cannon-es for physics. You have full access to these libraries globally via window.THREE and window.CANNON.
Object Scripting
When scripting a 3D object, this refers to the Mesh instance.
Basic Movement
this.rotation.y += 0.01; // Constant spin
this.position.y += 0.05; // Move up
Materials
this.material.color.set("#FF0000"); // Change to red
this.material.opacity = 0.5; // Transparency
Physics API
Unanchored objects use a physicsBody. You must apply forces to the body, not the mesh.
Applying Forces
let body = this.userData.physicsBody;
if (body) {
// Apply upward force (x, y, z)
body.applyImpulse(new CANNON.Vec3(0, 10, 0), body.position);
}
Collision Events
Physics bodies update the visual mesh automatically every frame during Play mode.
Audio / Sound
Once you import an MP3, it is stored in the window object using its filename (spaces replaced by underscores).
Playing Sound
// If you uploaded "Jump Sound.mp3"
if (window.Jump_Sound) {
window.Jump_Sound.play();
}
Stopping Sound
window.Jump_Sound.stop();
GUI / Interface
GUI elements are HTML components. this refers to the actual DOM element (Button or Span).
Button Events
this.onclick = function() {
alert("You clicked the UI button!");
this.style.display = "none"; // Hide after click
};
Updating Text
this.innerText = "Health: 100";