Thanks David T, you're much better than my maths teacher
Based on your equation and your teachings
, I have come up with this code. It now completely works and does not use trial and error. Also, you can choose if you want a direct shot or a lob. You can choose the velocity of the shell, and the shell will always hit the mouse if it is in range
REM Project: ballistic stuff
REM Created: 7/17/06 9:46:28 AM
REM
REM ***** Main Source File *****
REM
Rem ***** Main Source File *****
sync on : sync rate 0
global toggle as boolean
x as float
y as float
v as float
o as float
g as float
g = 0.02
do
x = mousex()
y = (240-mousey())
if upkey() = 1 then inc v,0.01
if downkey() = 1 then dec v,0.01
if rightkey() = 1 then toggle = 1
if leftkey() = 1 then toggle = 0
if spacekey() = 1
xmo# = sin(o)*v
ymo# = cos(o)*v
xpos# = 0
ypos# = 240
while (xpos# < 640 and ypos# < 480)
circle xpos#,ypos#,1
inc xpos#,xmo#
inc ypos#,ymo#
inc ymo#,g
sync
endwhile
endif
o = calc_angle(x, y, v)
line 0,240,sin(o)*20,(cos(o)*20)+240
print "Velocity: ",v
if toggle
print "Mode: Direct"
else
print "Mode: Lob"
endif
print "Angle: ",o-90
sync
cls
loop
function calc_angle(x as float, y as float, v as float)
a as float
b as float
c as float
g as float
o as float
g = 0.02
a = ((g*(x^2)) / (2*(v^2)))
b = 0-x
c = (((g*(x^2))/(2*(v^2))) + y)
o = calc_quadratic(a, b, c)
o = atan(o)+90
endfunction o
function calc_quadratic(a as float, b as float, c as float)
o as float
if toggle
o = ((0-b)-sqrt((b^2) - (4*(a*c))))/(2*a)
else
o = ((0-b)+sqrt((b^2) - (4*(a*c))))/(2*a)
endif
endfunction o
^
run in 640*480
If you knew my age, you'd realise why I didn't understand that at first.
Thanks again for explaining it.
There are three types of people, those that can count and those that can't.