As promised here is the next part of my collision test code
This collision test is particularilly usefull for checking for collisions between objects travelling at great speed ie greater than their own radius.
This version of my previous snippet is more precise than the previous version as it tests from the back of the balls starting position to the front of the balls resting position rather than the middle of each position.
My next tutorial will show how to detect the exact position of the ball at the precise moment the ball hits the wall.
Rem * Title : simple line collision
Rem * Author : trager
Rem * Date : 9-feb-2003
set text opaque
rem draw a wall to test collision against
wall_start_x# = 100
wall_start_y# = 100
wall_end_y# = 350
wall_end_x# = 350
line wall_start_x#, wall_start_y#, wall_end_x#, wall_end_y#
rem stage 0 asks for start position
rem stage 1 ask for end position
rem stage 2 calculates the point of collision
text 0,0,"Click the start position of the balls path"
stage = 0
ink rgb(255,0,0), rgb(0,0,0)
do
rem the user defines the start and end postion of balls path using_
rem mouse clicks in the desired location
if stage = 0 and mouseclick() =1
rem get the ball start position when user clicks the mouse for the first time
ball_start_x# = mousex()
ball_start_y# = mousey()
dot ball_start_x#, ball_start_y#
stage = 1
text 0,0," "
text 0,0,"Click the end position of the balls path"
wait 500
else
if stage = 1 and mouseclick() = 1
rem get the ball end position when the user clicks the mouse for the secound time
ball_end_x# = mousex()
ball_end_y# = mousey()
dot ball_end_x#, ball_end_y#
line ball_start_x#, ball_start_y#, ball_end_x#, ball_end_y#
circle ball_start_x#, ball_start_y#, 8
circle ball_end_x#, ball_end_y#, 8
rem get the speed of the ball along the x and y axis
ball_xv# = ball_end_x# - ball_start_x#
ball_yv# = ball_end_y# - ball_start_y#
remstart
this part of the code increase the length of the line by adding 8 to the end of each line
this ensures that the collision is tested to the edge of the ball
rather than just to the centre of the balls resting position
remend
if ball_xv# 0 and ball_yv# 0
rem we can predict the values of yR# and xR# acurately if the ball fails to
rem move along one of the axis.
R# = sqrt(ball_xv#*ball_xv# + ball_yv#*ball_yv#)
yR# = ball_yv#/R#
xR# = ball_xv#/R#
else
yR# = 0
xR# = 1
if ball_xv# = 0 and ball_yv# 0
rem if the ball has vertically but not horizontally
yR# = 1
xR# = 0
endif
endif
rem once we calculated how much to add on to each end of the line we do so
ball_end_y# = ball_end_y# + yR# * 8.0
ball_end_x# = ball_end_x# + xR# * 8.0
ball_start_y# = ball_start_y# - yR# * 8.0
ball_start_x# = ball_start_x# - xR# * 8.0
rem and then add a nice green line to show the line we will be testing
rem notice the green line goes from the back of the start ball to
rem the front of the finish ball
ink rgb(0,255,0),rgb(0,0,0)
line ball_start_x#, ball_start_y#, ball_end_x#, ball_end_y#
rem get the new speed of the ball along the x and y axis
ball_xv# = ball_end_x# - ball_start_x#
ball_yv# = ball_end_y# - ball_start_y#
stage = 2
text 0,0," "
text 0,0,"Click left mouse button to get location of collision"
wait 500
else
if stage = 2 and mouseclick() = 1
rem on the third mouse click the program calculates the position of a collision
gosub show_collision
wait 500
endif
endif
endif
sync
loop
show_collision:
remstart
this part of the code finds the collision point between the wall
and the balls path and then draws a circle around it
remend
rem get the x and y difference from start to end of wall
rem the x length and y length if you like
wall_xv# = wall_end_x# - wall_start_x#
wall_yv# = wall_end_y# - wall_start_y#
rem now understanding how this next part works is not important
rem just know that this is the collision test basically it calcs s and t
rem if s and t are between 0 and 1 then there has been a collision
s# = ((0-ball_yv#) * (ball_start_x#-wall_start_x#) + ball_xv#*(ball_start_y#-wall_start_y#)) / ((0-wall_xv#)*ball_yv# + ball_xv#*wall_yv#)
t# = (wall_xv# * (ball_start_y#-wall_start_y#) - wall_yv# * (ball_start_x#-wall_start_x#)) / ((0-wall_xv#)*ball_yv# + ball_xv#*wall_yv#)
if (s# >=0 and s# = 0 and t#