Physics in TCJSgame
TCJSgame allows you to simulate realistic movement by enabling physics
on any Component
. When physics is enabled, the engine will
automatically apply gravity
and bounce
calculations to the object. This makes it easy to create platformer-like
mechanics where objects fall, land, and react to the ground.
Enabling Physics
To use physics, simply set component.physics = true
. You can
also configure the gravity
and bounce
properties:
let display = new Display();
display.start();
let ball = new Component(30, 30, "red", 50, 50, "rect");
ball.physics = true; // enable physics
ball.gravity = 0.2; // pull downwards each frame
ball.bounce = 0.6; // lose 40% of speed when bouncing
display.add(ball, 0);
function update() {
ball.hitBottom(); // prevents falling through the floor
}
Physics Properties
physics
→ Enable or disable physics (default:false
)gravity
→ Controls downward pull (default:0
)gravitySpeed
→ Internal value tracking fall speedbounce
→ Controls how much energy is preserved after a bounce (0–1)
Example: Bouncing Ball
function update() {
ball.hitBottom();
}
In this example, the ball falls under gravity and bounces when it reaches
the bottom of the canvas. You can experiment with gravity
and
bounce
values to achieve different effects, such as heavy
objects that hardly bounce, or light objects that bounce more.
physics
is disabled, the component
moves only according to its speedX
and speedY
values, without any gravity or bounce applied.