Hi
I had some spare time, so I coded this short collision tutorial. It's fully commented (the hardest thing to do

), so I hope you have no problems understanding it - but if you do please post.
` Project: Platform collision
` Created: 19/12/2004 14:48:45
`
` Author : pizzaman - Hope you enjoy this short program
`
` ***** Main Source File *****
`
`Note : You'll notice that commands like "sprite y()" and "sprite height()" are used
` alot, these would be cached to variables to make your code faster. For example
` sprite width(10) would be cached to iPlayerWidth, therefore outside the main loop
` you would have code saying "iPlayerWidth = sprite width(10)", then you can use
` the variable in your program.
`clear anything left in video memory
flush video memory
`set up display size
set display mode 800,600,32
`cap the sync rate, so the program doesn't run too fast
sync on
sync rate 100
`create variables
iX as integer
iY as integer
iOldX as integer
iOldY as integer
iStartJump as integer
iSpriteNum as integer
bJump as byte
bDirY as byte
boGravity as boolean
`set some starting values for the player, and turn on gravity
iX = 375
iY = 500
boGravity = 1
`create some boxes for media
box 0,0,800,600,rgb(255,0,0),rgb(200,0,0),rgb(200,200,0),rgb(0,255,0)
get image 1,0,0,800,600,1
get image 2,0,0,100,50,1
box 0,0,40,40,rgb(255,255,0),rgb(0,200,0),rgb(200,200,0),rgb(0,100,0)
get image 3,0,0,40,40,1
box 0,0,100,50,rgb(0,255,255),rgb(255,0,255),rgb(200,200,0),rgb(0,100,255)
get image 4,0,0,100,50,1
`make the sprites and set their positions
sprite 1,0,550,1
sprite 2,50,400,2
sprite 3,650,400,2
sprite 4,350,150,2
sprite 5,200,275,2
sprite 6,500,275,2
sprite 7,350,400,4
`make the players sprite
sprite 10,iX,iY,3
`this is here to turn off the backsave for all sprites - gets rid of the blue screen,
`but also means you have to update the screen manually
set sprite 1,0,1
`main loop
do
`clear the screen
cls rgb(0,190,240)
`exit the main loop if "q" is pressed - which leads to the end of the program
if keystate(16) = 1 then exit
`store last frames co-ordinates in the iOldX and iOldY variables
iOldX = iX
iOldY = iY
`detect left and right arrow keys, and alter players co-ordinates respectively
if keystate(203) = 1 then iX = iX - 1
if keystate(205) = 1 then iX = iX + 1
`detect space bar and if the player hasn't jumped and there is a sprite collision
`with the player; jump. When on any platform the palyer is always in contact with
`another sprite, even if there range or co-ordinates don't intersect (ie they look
`like they are touching, but not crossing each other).
if keystate(57) = 1 and bJump = 0 and sprite collision(10,0) > 0
`check to see the player's bottom edge is near the top edge of the platform
`before allowing the player to jump - it stops a bug where if you fall off a
`a platform then collide with it quickly and jump, you will jump
if iY + sprite height(10) <= sprite y(sprite collision(10,0)) + 4
bJump = 1 `make player start to jump
iStartJump = iY `store the players current y-ordinate in iStartJumpY
boGravity = 0 `turn off gravity
endif
endif
`if player has jumped...
if bJump = 1
`...if the player is higher than 200 pixels than their starting y-ordinate
`of their jump, start the second phase of the jump (ie the fall); otherwise
`increase the height of the jump.
if iY <= (iStartJump - 200) `change the 200 to alter the jump height
bJump = 2
else
iY = iY - 2 `change the 2 to effect the speed of the jump
endif
endif
`if the second phase of the jump is active then turn gravity back on
if bJump = 2 then boGravity = 1
`if gravity is active decrease the player's y-ordinate
if boGravity = 1 then iY = iY + 2
`stop the player from going off the edges of the screen, by resetting their
`x-ordinate if they go off the screen
if iX < 0 then iX = 0
if iX > 760 then iX = 760
`cycle through all the platform sprites
for iSpriteNum = 1 to 6
`since we only want the player to be on a platform if they have not jumped
`or are falling and collide with it, we detect this...
if sprite collision(10,iSpriteNum) = 1 and (bJump = 0 or bJump = 2)
`...we must then make sure that the bottom edge of the player is greater than
`the top edge of the platform, or you get a weird jumpy effect - however we
`also want a little liegh-way. In other words we'll give the player a few
`pixels spare, in case they're bottom edge is just below the to edge of the
`platform (2 in our case, as you've got to consider that we are already
`colliding with the platform, so we are already inside of it by 2 pixels)
if (iY + (sprite height(10) - 4)) <= sprite y(iSpriteNum)
`place the player just above the platform by reseting the player's
`y-ordinate to last frmae's position
iY = iOldY
bJump = 0 `reset the jump, to not jumping
endif
endif
next iSpriteNum
`for this platform (multi-coloured one) the collision will be on all sides
if sprite collision(10,7) = 1
`if the player is in the upward part of their jump and if their y-ordinate is
`just above the bottom edge of the platform; reset the players y-ordinate to
`last frame's positionand set the player to fall
if bJump = 1 and iY >= sprite y(7) + sprite height(7) - 4
iY = iOldY
bJump = 2
endif
`if the player is in the downward part of their jump or falling, and if their
`y-ordinate is just below the top edge of the platform; reset the players
`y-ordinate to last frame's position and set the player to not jumping
if (bJump = 2 or bJump = 0) and iY + sprite height(10) <= sprite y(7) + 4
iY = iOldY
bJump = 0
endif
`if the player's x-ordinate is just in the right side of the platform and the
`player's bottom edge is below the platform's top edge; reset the players
`x-ordinate to last frame's position
if iX >= sprite x(7) + sprite width(7) - 4 and iY + sprite height(10) > sprite y(7)
iX = iOldX
endif
`if the player's right side is just in the left side of the platform and the
`player's bottom edge is below the platform's top edge; reset the players
`x-ordinate to last frame's position
if iX + sprite width(10) <= sprite x(7) + 4 and iY + sprite height(10) > sprite y(7)
iX = iOldX
endif
endif
`place sprite at new co-ordinates
sprite 10,iX,iY,3
`place some helpful text
text 0, 0,"Press the arrow keys to move and space bar to jump"
text 0,20,"Press Q to quit"
text 0,40,"Note that the multicolouerd platform has different collision"
text 740,0,"FPS " + str$(screen fps())
`update screen
sync
loop
`delete sprites that were created, but see if they exist first before deleting to
`avoid errors
for iSpriteNum = 1 to 10
if sprite exist(iSpriteNum) = 1 then delete sprite iSpriteNum
next iSpriteNum
`delete images that were created - note that we are reusing the iSpriteNum variable.
`This can sometimes confuse so its better to use generic names for for-next loops
`like Temp1, Index1 etc
for iSpriteNum = 1 to 4
if image exist(iSpriteNum) = 1 then delete image iSpriteNum
next iSpriteNum
`clear anything left in video memory
flush video memory
`...and finally end!!!
end
pizzaman