Here is another more Tidy and improved version:
*Gives the distance between shooting spot and hitting spot
*Gives the Peek point on the trajectory
*Gives the Time of flight
However it has some bugs which i dont understand,
range and time of flight values are wrong when the shooting point is higher than 0.0 and the hit spot is lower or higher than 0.0
Formulas are all right though..
any ideas to fix it?
Rem Project: Cannon Ball
Rem Created: 02.05.2006 14:01:14
Rem ***** Main Source File *****
AUTOCAM OFF
GLOBAL gravity# As Float = 9.8
GLOBAL shootPw# As Float = 50.0
Type vec
x As Float
y As Float
z As Float
EndType
Type obj
p As vec
a As vec
EndType
GLOBAL CannonBase As obj
GLOBAL CannonMidl As obj
GLOBAL CannonEnd As obj
GLOBAL Cam As obj
GLOBAL CannonBall As Obj
GLOBAL Target As Obj
oCannonBase = freeObject()
Make Object Cube oCannonBase, 10 : Position Object oCannonBase, 0.0, 5.0, 0.0 : xRotate Object oCannonBase, 358
oCannonMidl = freeObject()
Make Object Box oCannonMidl, 10, 10, 40 : Position Object oCannonMidl, 0.0, 0.0, 0.0
oCannonEnd = freeObject()
Make Object Cube oCannonEnd, 10 : Position Object oCannonEnd, 0.0, 0.0, 0.0
Set Object WireFrame oCannonEnd, 1
oCannonBall = freeObject()
Make Object Sphere oCannonBall, 10
oTarget = freeObject()
Make Object Plain oTarget, 50, 50
xRotate Object oTarget, 90 : Fix Object Pivot oTarget
Position Object oCannonEnd, 0.0, 0.0, 0.0
Make Matrix 1, 10000, 10000, 10, 10
mmx# = MouseMoveX() : mmy# = MouseMoveY()
Sync On
Do
REM CAMERA
mmx# = MouseMoveX() : mmy# = MouseMoveY()
Cam.a.x = wrapvalue(Cam.a.x + mmy#) : Cam.a.y = WrapValue(Cam.a.y + mmx#)
Rotate Camera Cam.a.x, Cam.a.y, 0.0
If MouseClick()=1 Then Move Camera 2
If MouseClick()=2 Then Move Camera -2
CannonBase.a.x = WrapValue(CannonBase.a.x + UpKey()-DownKey())
CannonBase.a.y = WrapValue(CannonBase.a.y + RightKey()-LeftKey())
If CannonBase.a.x > 0 And CannonBase.a.x < 180 Then CannonBase.a.x = 0
If CannonBase.a.x > 180 And CannonBase.a.x < 270 Then CannonBase.a.x = 270
xRotate Object oCannonBase, CannonBase.a.x
yRotate Object oCannonBase, CannonBase.a.y
CannonBase.p.x = Object Position X(oCannonBase)
CannonBase.p.y = Object Position Y(oCannonBase)
CannonBase.p.z = Object Position Z(oCannonBase)
CannonBase.a.x = Object Angle X(oCannonBase)
CannonBase.a.y = Object Angle Y(oCannonBase)
CannonBase.a.z = Object Angle Z(oCannonBase)
CannonMidl.p.x = CannonBase.p.x + (Sin(CannonBase.a.y) * Cos(CannonBase.a.x) * 10)
CannonMidl.p.y = CannonBase.p.y - (Sin(CannonBase.a.x) * 10)
CannonMidl.p.z = CannonBase.p.z + (Cos(CannonBase.a.y) * Cos(CannonBase.a.x) * 10)
Position Object oCannonMidl, CannonMidl.p.x, CannonMidl.p.y, CannonMidl.p.z
Set Object To Object Orientation oCannonMidl, oCannonBase
CannonMidl.p.x = Object Position X(oCannonMidl)
CannonMidl.p.y = Object Position Y(oCannonMidl)
CannonMidl.p.z = Object Position Z(oCannonMidl)
CannonMidl.a.x = Object Angle X(oCannonMidl)
CannonMidl.a.y = Object Angle Y(oCannonMidl)
CannonMidl.a.z = Object Angle Z(oCannonMidl)
CannonEnd.p.x = CannonMidl.p.x + (Sin(CannonBase.a.y) * Cos(CannonBase.a.x) * 25)
CannonEnd.p.y = CannonMidl.p.y - (Sin(CannonBase.a.x) * 25)
CannonEnd.p.z = CannonMidl.p.z + (Cos(CannonBase.a.y) * Cos(CannonBase.a.x) * 25)
Position Object oCannonEnd, CannonEnd.p.x, CannonEnd.p.y, CannonEnd.p.z
Set Object To Object Orientation oCannonEnd, oCannonBase
CannonEnd.p.x = Object Position X(oCannonEnd)
CannonEnd.p.y = Object Position Y(oCannonEnd)
CannonEnd.p.z = Object Position Z(oCannonEnd)
CannonEnd.a.x = Object Angle X(oCannonEnd)
CannonEnd.a.y = Object Angle Y(oCannonEnd)
CannonEnd.a.z = Object Angle Z(oCannonEnd)
`-----------MAKE BUTTON (A)------------
If KeyState(30) > 0 And BUTTON_A = 0 Then BUTTON_A = 1
If BUTTON_A = 1
BUTTON_A = 2
REM START VALUES HERE
Dec shootPw#, 1
EndIf
If KeyState(30) = 0 Then BUTTON_A = 0
`--------------------------------------
`-----------MAKE BUTTON (B)------------
If KeyState(31) > 0 And BUTTON_B = 0 Then BUTTON_B = 1
If BUTTON_B = 1
BUTTON_B = 2
REM START VALUES HERE
Inc shootPw#, 1
EndIf
If KeyState(31) = 0 Then BUTTON_B = 0
`--------------------------------------
`---------MAKE BUTTON (SPACE)----------
If SpaceKey() > 0 and BUTTON_Spc = 0 Then BUTTON_Spc = 1
If BUTTON_Spc = 1
BUTTON_Spc = 2
REM START VALUES HERE
CannonBall.a.x = CannonBase.a.x
CannonBall.a.y = CannonBase.a.y
offsetX# = CannonEnd.p.x : offsetY# = CannonEnd.p.y : offsetZ# = CannonEnd.p.z
tS# = Timer()
v# = shootPw#
Inc STATEMENT_Spc, 1
If STATEMENT_Spc > 1 Then STATEMENT_Spc = 0
EndIf
If SpaceKey() = 0 Then BUTTON_Spc = 0
`--------------------------------------
`-------MAKE STATEMENT (SPACE)---------
If STATEMENT_Spc = 0
Position Object oCannonBall, CannonEnd.p.x, CannonEnd.p.y, CannonEnd.p.z
EndIf
If STATEMENT_Spc = 1
tE# = Timer()
t# = (tE# - tS#) / 1000
CannonBall.p.x = Sin(CannonBall.a.y) * Cos(CannonBall.a.x) * v# * t#
CannonBall.p.y = (-Sin(CannonBall.a.x) * v# * t#) - ((gravity# * t#^2) * 0.5)
CannonBall.p.z = Cos(CannonBall.a.y) * Cos(CannonBall.a.x) * v# * t#
Position Object oCannonBall, offsetX#+CannonBall.p.x, offsetY#+CannonBall.p.y, offsetZ#+CannonBall.p.z
CannonBall.p.x = Object Position X(oCannonBall)
CannonBall.p.y = Object Position Y(oCannonBall)
CannonBall.p.z = Object Position Z(oCannonBall)
`If CannonBall.p.y =< -0.1 Then STATEMENT_Spc = 0
If CannonBall.p.y =< Get Ground Height(1,CannonBall.p.x, CannonBall.p.z)+5 Then STATEMENT_Spc = 0
EndIf
`--------------------------------------
Rem Info
voy# = shootPw# * -Sin(CannonBase.a.x)
`slope# =
peak# = voy#^2 / (2.0 * gravity#)
time# = voy# * 2 / gravity#
time1# = (voy# * 2 / gravity#)
time2# = Sqrt((voy#^2 / gravity#^2) - (((CannonEnd.p.y+2.5) * 2) / gravity#))
time3# = time1# - time2#
time4# = time1# + time2#
range# = ((shootPw#^2) * (2.0 * -Sin(CannonBase.a.x)*Cos(CannonBase.a.x))) / gravity#
Target.p.x = offsetX# + Sin(CannonBase.a.y) * Cos(CannonBase.a.x) * range#
Target.p.z = offsetZ# + Cos(CannonBase.a.y) * Cos(CannonBase.a.x) * range#
Position Object oTarget, Target.p.x, 0.1, Target.p.z
Text 0, Screen Height() - Text Size(), "Shooting Power: " + str$(shootPw#)
Set Cursor 0, 0
Print "Time : ", t#
Print "YAngle: ", 360 - CannonBase.a.x
Print "ShotPw: ", v#
Print "Range : ", range#
Print "Peak : ", peak#
Print "Time : ", time#
Print "Time : ", time1#
Print "Time : ", time2#
Print "Time3 : ", time3#
Print "Time4 : ", time4#
Print CannonBall.p.y
Sync
Loop
Function freeObject()
Repeat
Inc i
Until Object Exist(i) = 0
EndFunction i
Too Sexy