LessonPhysics-Based Movement in Games
Lesson: Physics-Based Movement in Games
Objective
Students will learn how to implement physics-based movement in a game by:
- Defining a player object with position and velocity.
- Making the player face toward a point.
- Moving the player using angles, velocity, and friction.
- Handling collisions with knockback.
1. The Player Object
Explanation
The player object stores all the important information about the player.
export const player = {
x: 0, // Player's X position
y: 0, // Player's Y position
xv: 0, // X velocity
yv: 0, // Y velocity
speed: 0.4,// Base movement speed
dir: 0, // Direction facing
health: 100,
coins: 0,
};
Think of this like a backpack. It holds where the player is, how fast they’re moving, and basic stats.
Quick Check
- Which properties control where the player is on the screen?
- What does
dirstore?
2. Facing an Object (pointAt())
Explanation
We often want the player to face toward something, like a wall or enemy.
export function pointAt(x, y) {
const dx = x - player.x;
const dy = y - player.y;
player.dir = Math.atan2(dy, dx) * (180 / Math.PI);
};
Steps:
- Find the difference (
dx,dy) between player and target. - Use
Math.atan2()to calculate the angle. - Convert from radians to degrees.
Now the player turns to face (x, y).
Activity
Change the code so the player always faces the mouse pointer.
3. Moving the Player (move())
Explanation
Once the player knows their direction, we can move them forward or backward.
export function move(speed) {
const angle = player.dir * (Math.PI / 180);
player.xv += Math.cos(angle) * speed;
player.yv += Math.sin(angle) * speed;
};
Math.cos(angle)→ movement along X.Math.sin(angle)→ movement along Y.speeddecides if they go forward (+) or backward (-).
Example
move(1)→ step forward.move(-1)→ knockback effect.
Quick Check
What happens if you call move(2) instead of move(1)?
4. Smoother Movement with Friction
Explanation
Without friction, the player would slide forever. We reduce velocity a little each frame:
player.xv *= 0.95;
player.yv *= 0.95;
player.x += player.xv;
player.y += player.yv;
This makes movement smooth and natural.
Activity
Try changing 0.95 to 0.5. What happens?
5. Collisions and Knockback
Explanation
When the player hits an object, we combine everything:
if (updCollide(player, t, 20)) {
player.health -= 15;
pointAt(t.x, t.y);
move(-1);
}
Effects:
- Player loses health.
- Turns to face the object.
- Gets pushed backward.
This creates a realistic “bump.”
Quick Check
Why do we call move(-1) instead of move(1) here?
6. Wrap-Up
By now, students should understand that:
- The player object stores movement data.
pointAt()makes the player face things.move()changes velocity based on direction.- Friction makes motion realistic.
- Collisions + knockback combine it all.
7. Extension Challenge
- Increase
player.speedfor a faster player. - Use
move(2)to create a dash ability. - Try making enemies use the same functions!