Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Canvas 2D · 10 Boundary Collisions and Integration

Series contents · Technology map

Stage: Time, Coordinates, and Input · Editable example · Storage ID: canvas-2d-step-10

Goals and learning outcomes

Prerequisites

Prerequisite concepts: Loop Ownership, Long Time Gaps. Prepare the result files and observation records from Unit 9.

Key concepts and explanations

Radius-Aware Boundaries

Checking only the ball's center lets it pass through a wall by its radius. Constrain the center between the radius and width minus radius.

Overshoot

A long dt can carry a ball far beyond a boundary. Correct its position as well as reversing velocity to avoid sticking to a wall.

Practice setup

First check the execution scope guidance on the example screen. The following describes preparation for extending the assignment in a separate project. Implement this yourself in an HTML and ES module project on a separate local HTTP development server. Record the browser version, viewport, and DPR.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. Prepare day-10.js and index.html in the external practice project. Update the ball's x,y and vx,vy, then reflect it at all four walls while accounting for its radius.
  2. Create a single-step button that takes dt as input to inspect collisions with large time intervals.
  3. Save the output with the input condition 0.016 seconds, then run again with 0.2 seconds. Expected: Larger overshoot in discrete collisions reveals the need for correction. Compare this with the actual screen and logs, and record the actual results and reasons for differences in day-10-notes.md.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
dt0.016 seconds0.2 secondsLarger overshoot in discrete collisions reveals the need for correction

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Why does simply reversing velocity cause jitter?
  2. How would you handle crossing several walls in one step?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

This is an editable browser experiment. Measurements vary with the current device and browser.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Stopping and Resuming · Next: Converting Pointer Coordinates

Open code, practice, and notes →