This will calculate the angle based on the above. You can choose whether you want a lob shot or a direct shot. This does not use trial and error, but instead uses complex mathematics which you might not understand, and I didn't understand until I made this. It's very useful for a targeting system.
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 mode
just copy+paste
No media
Up/Down to change velocity.
Left/Right to select Lob or Direct.
Space to shoot.
Bullet will hit mouse coordinates
If angle goes to infinity, it means that the target is out of range, so you must increase the velocity.
I hope you like it, and find it useful. I certainly have
There are three types of people, those that can count and those that can't.