Lesson 8: Making a Game; Simple Object Collision


Now let's have more meaningful collision with...dum dum dum...ANOTHER OBJECT! Well, how do you do that? Instead of trying to check the exact shape of each object, you cheat--you just make a box roughly their size, and see if those boxes intersect. This is a really quick check (only four comparisons), and is close enough to work, unless you are doing physics-based collisions, like a pool game. (Actually, you might still use this kind of collision, then do another check to see if they _really_ collided...)

All this file does is print a message when the Pong ball is inside each of the paddles. Try to work on your own after these lessons to make the ball react different if it is in different parts of the paddle!

/*#############################################################################
DEFINES
#############################################################################*/

#define $Lesson8 "2"
#define $HitTheWalls "1"
#define $Before "2"
#define $During "3"
#define $After "4"
#define $PaddleCollision "5"
#define $CheckTopPaddle "6"
#define $CheckBottomPaddle "7"
#define $CheckBallHit "8"

/*-------------------------------------------------------
CONSTANTS
-------------------------------------------------------*/
#define $TOPSIDE "1"
#define $LEFTSIDE "1"
#define $RIGHTSIDE "232"
#define $BOTTOMSIDE "232"

#define %PADDLEWIDTH "24" size of paddle
#define %PADDLEHEIGHT "8"

/*#############################################################################
CODE
#############################################################################*/
#window $Lesson8:$HitTheWalls
startswitch $Lesson8:$Before
thinkswitch $Lesson8:$During
finishswitch $Lesson8:$After

width 256
height 256
flags noscroll // Don't scroll the text onto the screen, like dialog.
body " "
if (colliding == 0) body "Nope"
if (colliding == 1) body "TOP!"
if (colliding == 2) body "BOTTOM!"
body "\n "
body "Top Collisions %d", collisioncount1
body "\n "
body "Bottom Collisions %d", collisioncount2
font "Boost12"
image "zong\zongball.pcx" zongX,zongY
image "zong\tpaddle.pcx" tpaddleX,tpaddleY
image "zong\bpaddle.pcx" bpaddleX,bpaddleY

/*=======================================================
Before
Initialize stuff!
=======================================================*/
#switch $Lesson8:$Before
zongX = 200 //ball
zongY = 128
bpaddleX = 112 // bottom paddle
bpaddleY = 224
tpaddleX = 112 // top paddle
tpaddleY = 16
deltaX = 2.1
deltaY = .43
return

/*=======================================================
During
Actively update appropriate variables
=======================================================*/
#switch $Lesson8:$During
zongX = zongX + deltaX // add change to X location
zongY = zongY + deltaY // add change to Y location
if (zongX > $RIGHTSIDE) deltaX = -1 * deltaX // if on right side, go left
if (zongX < $LEFTSIDE) deltaX = -1 * deltaX // if on left side, go right
if (zongY > $BOTTOMSIDE) deltaY = -1 * deltaY //if at bottom side, go up
if (zongY < $TOPSIDE) deltaY = -1 * deltaY //if at top side, go down
gosub $Lesson8:$PaddleCollision
return

/*=======================================================
PaddleCollision
Where we check each if ball hit either paddle.
=======================================================*/
#switch $Lesson8:$PaddleCollision
unset colliding
gosub $Lesson8:$CheckTopPaddle
gosub $Lesson8:$CheckBottomPaddle
return

/*=======================================================
CheckTopPaddle
See if collided with top paddle, and alter ball trajectory.
=======================================================*/
#switch $Lesson8:$CheckTopPaddle
checkX = tpaddleX
checky = tpaddleY
checkWidth = %PADDLEWIDTH
checkHeight = %PADDLEHEIGHT
gosub $Lesson8:$CheckBallHit
if (colliding) set collisioncount1 = collisioncount1 + 1
return

/*=======================================================
CheckBottomPaddle
See if collided with bottom paddle, and alter ball trajectory.
=======================================================*/
#switch $Lesson8:$CheckBottomPaddle
if (colliding) return
checkX = bpaddleX
checky = bpaddleY
checkWidth = %PADDLEWIDTH
checkHeight = %PADDLEHEIGHT
gosub $Lesson8:$CheckBallHit
if (colliding)
{
colliding = 2 // bottom paddle collision!
set collisioncount2 = collisioncount2 + 1
}
return

/*=======================================================
CheckBallHit
Check if ball hit something
=======================================================*/
#switch $Lesson8:$CheckBallHit
set box1Left = checkX // whatever's bounding box
set box1Right = checkX + checkWidth
set box1Top = checkY
set box1Bottom = checkY + checkHeight
set box2Left = zongX //ball is 8 x 7
set box2Right = zongX + 7
set box2Top = zongY
set box2Bottom = zongY + 6

unset colliding // only set if inside each other

if (box1Top > box2Bottom) RETURN // check if boxes to any of four sides
if (box2Top > box1Bottom) RETURN
if (box1Left > box2Right) RETURN
if (box2Left > box1Right) RETURN

set colliding = 1 // if not to any of four sides, collision!
RETURN

/*=======================================================
After
Leave the place like we found it
=======================================================*/
#switch $Lesson8:$After
unset zongX
unset zongY
unset deltaX
unset deltaY
unset tpaddleX // top paddle
unset tpaddleY
unset bpaddleX // bottom paddle
unset bpaddleY
unset collisioncount1
unset collisioncount2
return



Save this as Lesson8.txt, and check it out.
INVOKE 2:1

Next, we'll need to know how to make our games run the same on different computers, which all run at different speeds! Move on to Lesson 9!