06.04 Collision Detection
06.04 Collision Detection
04 Collision Detection
When a user sees shapes moving on a screen, and the shapes touch, they user usually expects
some interaction between the shapes. Collision detection refers to programmatically determine
whether one drawn shape interacts or collides with another. It does not matter whether the
movement is driven programmatically (animated), or by keyboard interactions.
For collision detection, the program needs to know 3 things:
1) the direction the shapes are moving, establishing the “leading edges” of the shapes
2) the location of these leading edges
3) when these leading edges meet
The programmer then will have coded some reactive behaviour to result from this collision.
Consider two shapes A and B - the program needs to know the direction A is moving, so it
knows which is A’s leading pixel’s coordinates (ie (x, y)). Then, the program needs to compare
these coordinate(s) with the leading pixel of shape B, closest to A (eg (a,b)). If the coordinates
equal or “cross”, then the shapes have collided.
A B
A is moving right
Clearly, the easiest calculations are for left/right, or
up/down movements. If the shapes are moving
left/right, the program compares x-coordinates only.
If the shapes are moving up/down, the y-coordinates leading edge
are compared.
A B
For example, the 06.03_keyboard_interaction.py can be modified with just the following code
so that the ball will stop at the right edge of the window. You can think of the right side of the
window as the leading edge of an object on the right, moving leftwards.
Lines 1 – 2: These measurement will be used several times, so they should be coded as
constants
If the ball has not reached the edge (ie is less than), then do
CIRCLE_WIDTH
nothing new, and allow the code to work as before. But if the
event means the ball has reached the edge (ie. greater than), upper_left_x lower_right_x
then set the x-coordinates so the ball is drawn just touching
the window’s edge.
Advanced Note:
As noted in class before, there often is more than 1 way to code a particular algorithm. The
code in “move_right” could have been written to use an “else” branch, and move the old
statements into that else branch.
06.04.01
a) Expand the 06.04_colllision_example.py program to move left, and to stop at
the left wall.
b) Modify your program that allows up/down movement (a) so that it stops at the
top edge or bottom edge of the window.
06.04.02
Write a program which starts with 2 shapes drawn (your choice of shapes), shape A and
shape B. A is drawn at the left edge of the window; B at the right. They are at the same
height.
The program starts animating the shapes to move toward each other, A moving right, B
moving left.
When the two shapes collide, they stop moving.
Make one circle the “predator” – it is the larger of the two circles.
Make the other circle the “prey” – it moves a little faster.
If the circles overlap (collide), then the “predator” has captured the “prey”. If the “prey”
can avoid capture for x number of moves, then it has escaped. You decide the number
of moves.
Hint: Use concepts from grade 10 math, and the library “math” and function “sqrt()”.
FYI. Although it may appear complicated, as a rule of thumb, my program for this is
about 150 lines.
06.04.05 Hurdler
In the screen shot to the right the red shape is moving from right
to left, heading to the yellow shape.
Write a program to allow the user to make the yellow shape jump
up (like Mario!) – make the jump follow the laws of gravity so the
yellow shape moves up, reaches a maximum height, the lands
back where it started from. Ie the jump is straight up then down.
If the yellow shape clears the red, print a congratulatory
message. If the yellow shape does not, then the two shapes
have collided so print a sad message ☹