The difference with this code and previous ones that I and others have posted, is that here you can aim the projectile at a specific target, and move the target in three dimensions. Would be useful for games with balls, bullets, etc.
Arrow-keys move target, a and z change the ball speed, space-bar launches projectile.
Rem Project: projectile physics by Ric
Rem Created: 18/01/2005 02:58:42
Rem ***** Main Source File *****
sync on
sync rate 100
autocam off
position camera 0,5,0
color backdrop rgb(200,200,255)
fog on
fog distance 2500
gravity#=0.01
forwardspeedstart#=2
forwardspeed#=forwardspeedstart#
xstart#=0
ystart#=20
zstart#=0
x#=xstart#
y#=ystart#
z#=zstart#
yang#=-90
for x=1 to 10
for y=1 to 10
ink rgb(rnd(100),rnd(155)+100,0),0
dot x,y
next y
next x
get image 1000,1,1,10,10
make object plain 1000,1000,1000
xrotate object 1000,-90
texture object 1000,1000
scale object texture 1000,10,10
set object texture 1000,2,0
`cricket ball
make object sphere 1,1
color object 1,rgb(255,0,0)
`dummy object for aim projection
make object sphere 2,1
hide object 2
`marker for aim
make object cone 3,10
color object 3,rgb(0,0,255)
`arrow for aim
make object cone 4,1
scale object 4,100,1000,100
set object cull 4,0
color object 4,rgb(0,255,0)
ghost object on 4
xrotate object 4,90
fix object pivot 4
ink rgb(0,0,255),0
set text font "arial"
set text size 20
do
gosub aim
gosub change_speed
gosub launch
gosub update_ball
gosub camera
gosub display_text
sync
loop
aim:
if launch=0
if rightkey()=1 then inc yang#,0.3
if leftkey()=1 then dec yang#,0.3
if upkey()=1 and ystartspeed#<1 then inc ystartspeed#,0.01
if downkey()=1 and ystartspeed#>-3 then dec ystartspeed#,0.01
yspeed#=ystartspeed#
yrotate object 1,yang#
repeat
inc dummyx#,forwardspeed#*sin(yang#)
inc dummyz#,forwardspeed#*cos(yang#)
dec dummyyspeed#,gravity#
inc dummyy#,dummyyspeed#
position object 2,dummyx#,dummyy#,dummyz#
until dummyy#<0.2
position object 3,object position x(2),object position y(2),object position z(2)
dummyx#=x#
dummyy#=y#
dummyz#=z#
dummyyspeed#=ystartspeed#
trajectory#=asin(ystartspeed#/forwardspeedstart#)
scale#=forwardspeedstart#*300
position object 4,xstart#,ystart#,zstart#
rotate object 4,0,0,0
zrotate object 4,-trajectory#
yrotate object 4,yang#
`move object 4,10
scale object 4,100,scale#,100
endif
return
launch:
if spacekey()=1 then launch=1
if launch=1
inc z#,forwardspeed#*cos(yang#)
inc x#,forwardspeed#*sin(yang#)
dec yspeed#,gravity#
inc y#,yspeed#
if y#<0.2 and bounce<4
y#=0.2
yspeed#=yspeed#*-0.5
forwardspeed#=forwardspeed#*0.5
inc bounce
endif
if y#<0.2 and bounce=>4
x#=xstart#
y#=ystart#
z#=zstart#
yspeed#=ystartspeed#
launch=0
bounce=0
forwardspeed#=forwardspeedstart#
endif
endif
return
update_ball:
position object 1,x#,y#,z#
return
camera:
if launch=0
position camera x#+4,y#+4,z#
set camera to object orientation 1
move camera -10
endif
return
change_speed:
if launch=0
if keystate(30)=1 then inc forwardspeedstart#,0.05
if keystate(44)=1 and forwardspeedstart#>0.6 then dec forwardspeedstart#,0.1
forwardspeed#=forwardspeedstart#
endif
return
display_text:
text 0,0,"Ball speed ("a"/"z"): "+str$(forwardspeedstart#)
text 0,20,"Angle (upkey/downkey): "+str$(trajectory#)
text 0,40,"Spacebar to launch"
return