IMPLEMENTATION NOTES / C++ / SDL2
Pong Game (Multi-ball)
Classic Pong in C++ and SDL2, based on Sanjay Madhav’s Game Programming in C++, with a multi-ball extension.
IMPLEMENTATION / EQUATIONS & SOURCE
Follow the inputs through the code.
Read each section as purpose → equation → actual source. Snippets are excerpts of the working implementation, not standalone programs. Follow the source link for surrounding context.
01 / Time-step integration and paddle reflection
Store balls in an array and integrate position by velocity×deltaTime, capped at 0.05 seconds. Paddle contact tests y distance and an x region, then negates x velocity. There is no positional correction, so repeated flips and high-speed tunneling remain possible.
pnext = p + vΔt
|paddley−bally| ≤ paddleHeight/2
vx ← −vx [パドル接触 / paddle contact] Ball& ball = mBalls[i];
ball.pos.x += ball.vel.x * deltaTime;
ball.pos.y += ball.vel.y * deltaTime;
// 左パドルとの衝突判定
float diff1 = fabs(mPaddlePos.y - ball.pos.y);
if (diff1 <= paddleH / 2.0f && ball.pos.x <= (25.0f + thickness) &&
ball.pos.x > 0.0f) {
ball.vel.x *= -1.0f;
}SOURCE / SCOPE
Scope of the notes.
Each source excerpt comes from the linked fixed commit. Equations describe those operations. Apart from worked examples, no new performance or quality measurements are claimed. Refer to the repository for build steps and dependencies.
Open repository ↗