This code is a physics model for 3-D projectile motion. Suitable for anything which involves launching an object through the air, like a cannon ball, a golf-sim, a tennis game etc.......
You can alter pretty much every aspect of flight: - headwind/air-resistance/trailing wind, crosswind/hook/slice, lift due to backspin of the ball (like hitting a golf ball with a 9-iron), gravity, trajectory, initial velocity, and hardness of ground for affecting the amount of bounce.
Use the up and down arrow keys to alter trajectory, and the space key to launch the projectile. All other variables can be altered easily in the code.
`Projectile simulation by Ric.
Rem Created: 31/07/2004 04:10:01
sync on
hide mouse
`make ball
make object sphere 1,3
`make launcher
make object box 2,24,4,4
position camera 0,200,140,-300
`restart point
restart:
undim xs(0)
undim ys(0)
n=0
`define variables
`number of bounces
bounce=0 `leave this alone
`position of ball x,y,z
x#=0
y#=0
z#=0
`resultant velocity of ball
v#=1.2 `set this to whatever you like, and
`adjust camera position accordingly.
`velocity x,y,z - leave these alone
vx#=0
vy#=0
vz#=0
`acceleration x,y,z
ax#=0.0 `(deceleration due to air resistance - LEAVE AT ZERO)
ay#=0.004 `(deceleration due to gravity)
az#=0.0 `(acceleration due to cross wind/hook/slice.
` Set - for right, + for left, between 0 and about 0.01)
`lift amount
lift#=0.02 `set bigger for more backspin,
`lower for less backspin - use small values (like 0.02)
`air resistance
ar#=0.003 `(set between 0 and about 0.003 - too much and weird things happen!)
`headwind/trailing wind
hw#=0.0 `(0.001 would be a pretty strong headwind. Negative values for trailing wind)
`intitial trajectory
traj#=25 `leave this alone - control this using arrow keys in the program
`set initial positions
position object 1,0,0,0
position object 2,0,0,0
`main loop
do
`set trajectory of ball using arrow keys
if upkey()=1 then traj#=traj#+1
if downkey()=1 then traj#=traj#-1
zrotate object 2, traj#+10
`apply trajectory to initial velocity
vx#=v#*cos(traj#) `(horizontal velocity)
vy#=v#*sin(traj#) `(vertical velocity)
`check for ball being launched
if spacekey()=1 then gosub hitball
sync
loop
hitball:
do
x#=x#+vx# `increase x position by velocity amount
ax#=vx#*ar# `deceleration is proportional to air resistance
vx#=vx#-ax# `decrease velocity by deceleration amount due to air res.
vx#=vx#-hw# `and decrease it by deceleration amount due to headwind.
y#=y#+vy# `increase y position by velocity amount
vy#=vy#-ay# `decrease velocity by deceleration due to gravity
vy#=vy#+lift# `increase vertical velocity by lift amount
lift#=lift#*0.95 `decrease lift exponentially
z#=z#+vz# `change z position by velocity amount
vz#=vz#+az# `increase velocity by acceleration due to crosswind/slice/hook
`move ball
position object 1,x#,y#,z#
`leave trail of dots
dim xs(n)
dim ys(n)
xs(n)=(object screen X(1))
ys(n)=(object screen Y(1))
for d=0 to n
dx=(xs(d))
dy=(ys(d))
dot dx,dy
next d
n=n+1
`bounce when ball hits the ground
if y#<0
y#=1
vy#=vy#*-0.4 `increase for harder surface, decrease for soft surface
bounce=bounce+1
if bounce>5 then goto restart
endif
sync
loop