Click on the circle and toss it around. I threw in a bezier curve as this when what I will eventually be colliding against. I know there a few bugs, but this explains how collision at an angle work.
sync on
sync rate 60
set display mode 800,600,16
global vx# = 0.0 `balls x velocity
global vy# = 3.0 `balls y velocity
global tvx# = 0 `temp x velocity when dragging
global tvy# = 0
global gravity# = 0.07
global bounce# = -0.7 `the amount of bounce the ball has
global dragging = 0
global la = 55 `line angle
global lx# = 100.0 `line start x
global ly# = 100.0 `line start y
global lx2#` = lx#+cos(la)*250 (end of line)
global ly2#` = ly#+sin(la)*250
global bx# = 200.0 `ball x position
global by# = 10.0 `ball y position
global splinestep# = 0.02
global cnt = 0
`control handles of the curve
dim xpoint(3)
dim ypoint(3)
xpoint(0)=10
ypoint(0)=10
xpoint(1)=100
ypoint(1)=500
xpoint(2)=700
ypoint(2)=500
xpoint(3)=790
ypoint(3)=10
`array that holds the x / y position of each curve point
global dim sx#(50) ` 1 \ 0.02 = 50
global dim sy#(50)
`prerender the bezier curve and set up the sx and sy arrays
`major performance increase seeing how the curve never changes
drawbezier(xpoint(0),ypoint(0),xpoint(1),ypoint(1),xpoint(3),ypoint(3),xpoint(2),ypoint(2))
do
cls
`get the endpoint of the line and draw it
lx2# = lx#+cos(la)*300
ly2# = ly#+sin(la)*300
line lx#,ly#,lx2#,ly2#
for i = 0 to 50 - 1
dot sx#(i),sy#(i) `draw the curve that was already prerendered
next i
if dragging = 1
bx# = mousex()
by# = mousey()
vy# = 0 `must be zero as you will always be adding velocity, not good
vx# = 0
tvx# = mousemovex() * 0.2 `must reduce the amount of throw, or the ball flys whereever
tvy# = mousemovey() * 0.2
endif
if dragging = 0
vy# = vy# + gravity# + tvy# `add y velocity with the gravity and temp throw speed
vx# = vx# + tvx#
bx# = bx# + vx#
by# = by# + vy#
tvx# = 0 `must reset or it will think you are always throwing
tvy# = 0
endif
checkbounds() `make sure ball stays on screen
checkline() `line collision code
`start getting the distance between the mouse and ball
dx# = abs(mousex()-bx#)
dy# = abs(mousey()-by#)
dis# = sqrt(dx#*dx#+dy#*dy#)
`if the distance is less than the radius, the ball is being dragged
if dis# < 10 && mouseclick()=1
dragging = 1
else
dragging = 0
endif
circle bx#,by#,10
sync
loop
function checkline()
`code must be updated to relize that an angle can be 90 degrees.
if bx# > lx# && bx# < lx2#
angle = la
cosine# = cos(angle)
sine# = sin(angle)
x# = bx# - lx# `setting up to get the diestande between the line and ball
y# = by# - ly#
`rotate line
y1# = cosine# * y# - sine# * x#
`rotate y velocity
vy1# = cosine# * vy# - sine# * vx#
`once things are rotated. the line is virtually going left to right
`you are now texting if the ball is below the line
`if it is then the ball is moved back up
if y1# > -10 && y1# < vy1#
x1# = cosine# * x# + sine# * y# `rotate line
vx1# = cosine# * vx# + sine# * vy# `rotate velocity
`perform bounce with rotated values
y1# = -10
vy1# = vy1# * bounce#
`rotate everything back
x# = cosine# * x1# - sine# * y1#
y# = cosine# * y1# + sine# * x1#
vx# = cosine# * vx1# - sine# * vy1#
vy# = cosine# * vy1# + sine# * vx1#
`setting the ball position back to where it should be
bx# = lx# + x#
by# = ly# + y#
endif
`this is the same as above but is testing for the ball coming from the other side of the line
`this muist be here of the ball will "fall" through the line
if y1# < 10 && y1# > vy1#
x1# = cosine# * x# + sine# * y# `rotate line
vx1# = cosine# * vx# + sine# * vy# `rotate velocity
`perform bounce with rotated values
y1# = 10
vy1# = vy1# * bounce#
`rotate everything back
x# = cosine# * x1# - sine# * y1#
y# = cosine# * y1# + sine# * x1#
vx# = cosine# * vx1# - sine# * vy1#
vy# = cosine# * vy1# + sine# * vx1#
bx# = lx# + x#
by# = ly# + y#
endif
endif
endfunction
function drawbezier(x1#,y1#,vx1#,vy1#,x2#,y2#,vx2#,vy2#)
local lastx#=x1#
local lasty#=y1#
cnt = 0
for t# = 0 to 1 step 0.02 `the curve is drawn over time from 0 to 1
`actual formula for a 4 control point bezier curve
`pointx pointy are the locations of each dot over time (t#)
pointx# = x1# * (1-t#)^3 + 3*vx1#*(1-t#)^2*t# + 3*vx2#*(1-t#)*t#^2 + x2#*t#^3
pointy# = y1# * (1-t#)^3 + 3*vy1#*(1-t#)^2*t# + 3*vy2#*(1-t#)*t#^2 + y2#*t#^3
`line pointx#,pointy#,lastx#,lasty#
`set up the prerender arrays
sx#(cnt)=pointx#
sy#(cnt)=pointy#
lastx#=pointx#
lasty#=pointy#
cnt = cnt + 1
next t#
cnt = 0
endfunction
function checkbounds()
`change code around to not be so static
if bx# > 790
bx# = 790
vx# = vx# * bounce#
endif
if bx# < 10
bx# = 10
vx# = vx# * bounce#
endif
if by# > 590.0
by# = 590.0
vy# = vy# * bounce#
endif
if by# < 10
by# = 10
vy# = vy# * bounce#
endif
endfunction