Few bit of code put together to make a gun turret lock on and aim at static object.
Rem Project: tank1
Rem Created: 02/02/2009 11:01:05
Rem ***** Main Source File *****
sync on
sync rate 60
autocam off
position camera 0,10,-10
rem this is object one
tankobject=1
make matrix 1,100,100,50,50
position matrix 1,-50,0,-50
load object "tankunit.x",tankobject
point camera object position x(tankobject),object position y(tankobject),object position z(tankobject)
rem link gun to top body and re position - should be done in cad but not sure how yet
link limb 1,3,2
offset limb 1,3,0,-.55,0
rem this object 2
make object cube 2,1
position object 2,-10,0,-10
rem start the loop
do
rem set the cursor to put text on screen in same place.
set cursor 0,0
print "LIMBS IN OBJECT"
perform checklist for object limbs tankobject
for c=1 to checklist quantity()
print " ";checklist value a(c);" ";checklist string$(c)
next c
rem check which key is pressed
if keystate(51) :rem "<" key
dec angle#,.1
rotate limb 1,2,0,angle#,0
endif
if keystate(52) :rem ">" key
inc angle#,.1
rotate limb 1,2,0,angle#,0
endif
rem as stated
if leftkey()
dec ty#,.5
rotate object 1,0,ty#,0
endif
if rightkey()
inc ty#,.5
rotate object 1,0,ty#,0
endif
if upkey()
move object 1,.1
endif
if downkey()
move object 1,-.1
endif
rem has the l key been pressed to turn lock on off
if keystate(38) and (clock<timer())
click=1-click
clock = timer() + 250
endif
rem check whick click is on
if click=1
lock=1
mesg$="lock on"
endif
if click=0
lock=0
mesg$="lock off"
endif
rem if lock is 1
if lock=1
rem find the x and z of objects
x1# = object position x (1) : x2# = object position x (2)
z1# = object position z (1) : z2# = object position z (2)
rem find x z distance
xt# = x2# - x1#
zt# = z2# - z1#
rem find the angle
angle# = atanfull(xt#,zt#)-object angle y(1)
rem move the gun round slowly ish
newangle#=curveangle(angle#,nowvalue#,35)
rem rotate to new value
rotate limb 1,2,0,newangle#,0
rem update now value
nowvalue#=newangle#
endif
rem print lock state
print mesg$
rem Track object with camera
camx#=object position x(1)
camy#=object position y(1)
camz#=object position z(1)
camangle#=object angle y(1)
rem set to follow object
set camera to follow camx#,camy#,camz#,camangle#,10.0,10.0,10.0,0
rem update screen
sync
loop
same code but with object 2 moving in circels
Rem Project: tank1
Rem Created: 02/02/2009 11:01:05
Rem ***** Main Source File *****
sync on
sync rate 60
autocam off
position camera 0,10,-10
rem this is object one
tankobject=1
make matrix 1,100,100,50,50
position matrix 1,-50,0,-50
load object "tankunit.x",tankobject
point camera object position x(tankobject),object position y(tankobject),object position z(tankobject)
rem link gun to top body and re position - should be done in cad but not sure how yet
link limb 1,3,2
offset limb 1,3,0,-.55,0
rem this object 2
make object cube 2,1
position object 2,-10,0,-10
rem start the loop
do
rem set the cursor to put text on screen in same place.
set cursor 0,0
print "LIMBS IN OBJECT"
perform checklist for object limbs tankobject
for c=1 to checklist quantity()
print " ";checklist value a(c);" ";checklist string$(c)
next c
rem check which key is pressed
if keystate(51) :rem "<" key
dec angle#,.1
rotate limb 1,2,0,angle#,0
endif
if keystate(52) :rem ">" key
inc angle#,.1
rotate limb 1,2,0,angle#,0
endif
rem as stated
if leftkey()
dec ty#,.5
rotate object 1,0,ty#,0
endif
if rightkey()
inc ty#,.5
rotate object 1,0,ty#,0
endif
if upkey()
move object 1,.1
endif
if downkey()
move object 1,-.1
endif
rem has the l key been pressed to turn lock on off
if keystate(38) and (clock<timer())
click=1-click
clock = timer() + 250
endif
rem check whick click is on
if click=1
lock=1
mesg$="lock on"
endif
if click=0
lock=0
mesg$="lock off"
endif
rem if lock is 1
if lock=1
rem find the x and z of objects
x1# = object position x (1) : x2# = object position x (2)
z1# = object position z (1) : z2# = object position z (2)
rem find x z distance
xt# = x2# - x1#
zt# = z2# - z1#
rem find the angle
angle# = atanfull(xt#,zt#)-object angle y(1)
rem move the gun round slowly ish
newangle#=curveangle(angle#,nowvalue#,35)
rem rotate to new value
rotate limb 1,2,0,newangle#,0
rem update now value
nowvalue#=newangle#
endif
rem make object two og around in a circle
ang# = wrapvalue(ang# + .5)
newx# = (sin(ang#) * 10)
newz# = (cos(ang#) * 10)
print "X : "+str$(newx#)+" Z : "+str$(newz#)
position object 2, newx#+10, 0.0, newz#+10
rem print lock state
print mesg$
rem Track object with camera
camx#=object position x(1)
camy#=object position y(1)
camz#=object position z(1)
camangle#=object angle y(1)
rem set to follow object
set camera to follow camx#,camy#,camz#,camangle#,10.0,10.0,10.0,0
rem update screen
sync
loop
and how to do it when the object is high or low in the sky ground.
Rem Project: tank1
Rem Created: 02/02/2009 11:01:05
Rem ***** Main Source File *****
sync on
sync rate 60
autocam off
position camera 0,10,-10
rem this is object one
tankobject=1
make matrix 1,100,100,50,50
position matrix 1,-50,0,-50
load object "tankunit.x",tankobject
point camera object position x(tankobject),object position y(tankobject),object position z(tankobject)
rem link gun to top body and re position - should be done in cad but not sure how yet
link limb 1,3,2
offset limb 1,3,0,-.55,0
rem this object 2
make object cube 2,1
position object 2,-10,0,-10
rem start the loop
do
rem set the cursor to put text on screen in same place.
set cursor 0,0
print "LIMBS IN OBJECT"
perform checklist for object limbs tankobject
for c=1 to checklist quantity()
print " ";checklist value a(c);" ";checklist string$(c)
next c
rem check which key is pressed
if keystate(51) :rem "<" key
dec angle#,.1
rotate limb 1,2,0,angle#,0
endif
if keystate(52) :rem ">" key
inc angle#,.1
rotate limb 1,2,0,angle#,0
endif
rem as stated
if leftkey()
dec ty#,.5
rotate object 1,0,ty#,0
endif
if rightkey()
inc ty#,.5
rotate object 1,0,ty#,0
endif
if upkey()
move object 1,.1
endif
if downkey()
move object 1,-.1
endif
rem has the l key been pressed to turn lock on off
if keystate(38) and (clock<timer())
click=1-click
clock = timer() + 250
endif
rem check whick click is on
if click=1
lock=1
mesg$="lock on"
endif
if click=0
lock=0
mesg$="lock off"
endif
rem if lock is 1
if lock=1
rem find the x and z of objects
x1# = object position x (1) : x2# = object position x (2)
z1# = object position z (1) : z2# = object position z (2)
y1# = object position y (1) : y2# = object position y (2)
rem find x z distance
xt# = x2# - x1#
yt# = y2# - y1#
zt# = z2# - z1#
ground# = sqrt((zt# * zt#) + (xt# * xt#))
angle# = atanfull(xt#,zt#) - object angle y(1)
height# = atanfull(yt#,ground#)
rem move the gun round slowly ish
newangle#=curveangle(angle#,nowvalue#,35)
rem rotate to new value
rotate limb 1,2,360-height#,newangle#,0
rem update now value
nowvalue#=newangle#
endif
rem make object two og around in a circle
ang# = wrapvalue(ang# + .5)
yang = wrapvalue(yang# +.5)
newx# = (sin(ang#) * 10)
newz# = (cos(ang#) * 10)
newy# = (sin(ang#) *10)
print "X : "+str$(newx#)+" Y :"+str$(height#)+" Z : "+str$(newz#)
position object 2, newx#+10, newy#, newz#+10
rem print lock state
print mesg$
rem Track object with camera
camx#=object position x(1)
camy#=object position y(1)
camz#=object position z(1)
camangle#=object angle y(1)
rem set to follow object
set camera to follow camx#,camy#,camz#,camangle#,10.0,10.0,10.0,0
rem update screen
sync
loop
Dark Physics makes any hot drink go cold.