Skip to content
Game 0 Unit 11 of 15 1 hr learning time

Bumping Into Things

Objects that pass through each other aren't a game yet — a game needs consequence. Blitz answers the question 'are these two shapes touching?' with a single function, ShapesHit, that returns true the instant they overlap. That one test is the seed of every hit, catch, pickup and crash there is.

73% of Meet Blitz

Your shapes move, animate and follow the joystick — but they still drift through each other like ghosts. The piece that turns motion into a game is consequence: something has to happen when two things touch. That starts with a single question — are these two shapes overlapping right now? — and Blitz answers it with one function. ShapesHit takes two shapes and their positions and tells you, every frame, whether they're touching. Get a true back from it and you've got the seed of every hit, catch, pickup, pickup-the-key and crash-into-the-wall a game ever needed.

What you'll see by the end

A ball sweeps back and forth across a target. The banner flashes on the instant the two shapes overlap — and only then.

A ball slides left and right across a stationary target block. Watch the banner at the top: it's dark while the ball is in open space, and it lights up the moment the ball touches the target, going dark again as the ball passes clear. Nothing tells the program where the target is — it asks ShapesHit, every frame, "are these two touching?", and acts on the answer.

The whole program

; ShapesHit tells you the instant two shapes overlap.
BLITZ
  ; ball (shape 0) and a target block (shape 1), both 24x24
  BitMap 2,24,24,3 : Use BitMap 2 : Cls 0 : Circle 12,12,11,4 : GetaShape 0,0,0,24,24
  Use BitMap 2 : Cls 0 : Boxf 1,1,22,22,2 : GetaShape 1,0,0,24,24

  BitMap 0,320,256,3
  BitMap 1,320,256,3
  Slice 0,44,320,256,$fff8,3,8,8,320,320

  bx.w=20 : by.w=116
  tx.w=180 : ty.w=116
  dx.w=3
  ht.w=0
  db=0
  While Joyb(0)=0
    VWait
    Show db : db=1-db : Use BitMap db
    If ShapesHit(0,bx,by,1,tx,ty) Then ht=18
    Cls 0
    Blit 1,tx,ty
    Blit 0,bx,by
    If ht>0 Then Boxf 60,36,260,66,6
    If ht>0 Then ht=ht-1
    bx=bx+dx
    If bx>270 Then dx=-dx
    If bx<20 Then dx=-dx
  Wend

Two shapes, a sweep, and one question asked once a frame.

Two shapes to test

BitMap 2,24,24,3 : Use BitMap 2 : Cls 0 : Circle 12,12,11,4 : GetaShape 0,0,0,24,24
Use BitMap 2 : Cls 0 : Boxf 1,1,22,22,2 : GetaShape 1,0,0,24,24

Nothing new — you've grabbed shapes before. Shape 0 is the ball (a circle), shape 1 the target (a filled block). Both come off the same work bitmap, cleared between, exactly as in the earlier units.

Ask the question

If ShapesHit(0,bx,by,1,tx,ty) Then ht=18

This is the unit. ShapesHit(shape1, x1, y1, shape2, x2, y2) asks: if shape 0 sits at (bx, by) and shape 1 sits at (tx, ty), are they overlapping? It returns true the instant they do. The positions you pass are the same ones you Blit with, so the test matches exactly what's on screen. When it comes back true, we set ht — a little countdown — to 18.

ShapesHit doesn't just compare boxes; it looks at the shapes' actual pixels. The ball only "hits" the target when their solid parts genuinely overlap, not when their corners cross. That's why a round ball and a square block touch when they look like they touch.

Hold the result so you can see it

If ht>0 Then Boxf 60,36,260,66,6
If ht>0 Then ht=ht-1

Here's a practical wrinkle worth knowing. The ball is only over the target for a handful of frames, so if you drew the banner only on the exact frames ShapesHit is true, it would flash by in a fraction of a second — gone before you register it, and almost impossible to catch in a screenshot. So we don't draw straight from the test. Instead the hit sets a small timer, ht, to 18, and the banner shows while that timer counts down. One touch lights the banner for about a third of a second — long enough to see. This little timer-after-the-event trick is how games make a brief moment readable, and you'll use it constantly: a flash on damage, a coin's sparkle, a "+10" that lingers.

How ShapesHit sees a hit

It's worth being clear about what "touching" means here, because it's cleverer than it looks. The lazy way to test two objects is by their bounding boxes — treat each as a rectangle and see if the rectangles overlap. That's fast, but it's wrong at the edges: two circles would "collide" when their corners cross, well before they touch. ShapesHit does better — it checks the shapes' real pixels, so a hit means the ball's solid pixels and the target's solid pixels genuinely share space. For a round ball nosing into a square block, that's exactly the honesty a game wants: it registers when they look joined, not a moment early.

Type it and compile it

Type the program into Ted and press right-Amiga + X (recompile if Blitz asks about memory). The ball sweeps across; the banner lights as it crosses the target. Press fire — or reset — to stop.

Try this: bounce off instead

Detecting the hit is half a game; reacting is the other half. Make the ball bounce off the target: where you set ht=18, also reverse the ball with dx=-dx. Now the ball can't pass through — it strikes the block and rebounds, the difference between a ghost and a wall.

Try this: keep score

Add a counter. Before the loop, score.w=0; on a hit, score=score+1. You won't see it without printing, but you've just written the heart of every points system — a collision that changes a number. Combined with the bounce above, that's a complete little game mechanic: hit the target, score, ricochet.

If it doesn't work

  • The banner never shows. Remember it flashes only during overlap — that's why we hold it with ht. If you removed the timer and drew straight from ShapesHit, the flash is a few frames and slips past unseen. Check ht is set on the hit and counted down each frame.
  • It seems to hit too early or too late. The positions in ShapesHit must be the same bx,by / tx,ty you Blit with. Pass different numbers and the test won't match what you see.
  • It hits when they only look close. Make sure both shapes were grabbed cleanly on a Cls 0 background, so their transparent areas are truly transparent — ShapesHit tests the solid pixels, and a stray background colour becomes a solid pixel that collides.

What you've learnt

Collision is a question you ask each frame: ShapesHit(shape1,x1,y1,shape2,x2,y2) returns true the instant two shapes' pixels overlap — pixel-accurate, not just bounding boxes — using the same positions you Blit with. Act on that answer and you have consequence: a hit, a bounce, a score. And because a touch lasts only a few frames, a small timer set on the hit keeps the result on screen long enough to see and use — a trick you'll reach for again and again.

What's next

You've now built every piece of a game — a player you steer, objects that animate, and collisions that matter. The next three units turn toward the metal, where Blitz's compiled speed and direct hardware access come into their own. First — Smooth Scrolling — a world wider than the screen, sliding past one pixel at a time, the effect that made the Amiga's name.