the function is great you may want to pass your types as reference
function func1( a as point ) // pass by value, variable is copied
a.x = 5 // this change is local and lost at the end of the function
endfunction
function func2( a ref as point ) // pass by reference, variable is the original
a.x = 7 // this change modifies the original variable
endfunction
https://www.appgamekit.com/documentation/guides/12_array_changes.htm
It may be handy in the future if you keep track of all image ids
so with the code I supplied above you could function load_3d_object(path$, file$, scale#,mytank ref as tank[])
and call it with load_3d_object(path$, file$, scale#,tank)
this would keep track of all the ids and modify the tank array in main loop
for example making it easy if you wanted to flash or tween different images, delete them etc
rather than just have images etc and having no idea of which id they have
type _tank
ID
bulletID as integer[]
bulletTimer as float[]
imgID as integer[]
imgName as string[]
wheel_l as integer[7]
wheel_r as integer[7]
turret as integer
cannon as integer
endtype
SetErrorMode(2)
SetWindowTitle( "Tank Test" )
SetWindowSize( 1024, 768, 0 )
SetSyncRate( 60, 0 )
SetScissor( 0,0,0,0 )
tank as _tank[]
load_tank(tank)
SetObjectScalePermanent(tank[0].ID, 0.1, 0.1, 0.1)
SetObjectPosition(tank[0].ID, 0, 0, 0)
SetCameraPosition(1,0,0,-100)
SetCameraLookAt(1,0,0,0,0)
create3DPhysicsWorld()
do
print ("tank ID "+str(tank[0].ID))
for num= 1 to tank[0].imgId.length
print ("texture ID "+str(tank[0].imgID[num-1])+" name "+(tank[0].imgName[num=1]))
next num
angleX# = angleX# + 0.3
angleY# = angleY# + 0.5
if angleX# > 360.0
angleX# = 0.0
endif
if angleY# > 360.0
angleY# = 0.0
endif
SetObjectRotation(tank[0].ID, angleX#, angleY#, 0.0)
//get mouse pointer
pointer_x = GetPointerX()
pointer_y = GetPointerY()
// get the x, y and z unit vectors based on the pointer position
unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)
unit_y# = -Get3DVectorYFromScreen(pointer_x,pointer_y)
//rotate turret and cannon according to mouse position
SetObjectBoneRotation(tank[0].ID, tank[0].turret, 0.0, unit_x# * 100, 0.0)
SetObjectBoneRotation(tank[0].ID, tank[0].cannon, unit_y# * 40, 0.0, 0.0)
for i = 1 to 7
SetObjectBoneRotation(tank[0].ID, tank[0].wheel_l[i], GetMilliseconds(),0,0)
SetObjectBoneRotation(tank[0].ID, tank[0].wheel_r[i], GetMilliseconds(),0,0)
next i
if GetPointerPressed()
shootMissile( tank[0], 50.0, 100.0)
endif
//clean up bullets
for num=tank[0].bulletID.length to 0 step -1
if tank[0].bulletTimer[num] +5 <timer()
DeleteObject(tank[0].bulletID[num])
tank[0].bulletID.Remove(num)
tank[0].bulletTimer.Remove(num)
endif
next num
Step3DPhysicsWorld()
sync()
loop
function load_tank(tank ref as _tank[])
tmptank as _tank
img as String
tmptank.ID=LoadObjectWithChildren("Tank3D2.X")
For textureIndex = 1 to GetObjectNumTextures(tmptank.ID)
tmpTank.imgID.insert(textureIndex)
img=GetObjectTextureName(tmpTank.ID, textureIndex)
tmpTank.imgName.insert(img)
tmpTank.imgID[textureIndex-1]=LoadImage(img)
SetObjectMeshImage(tmpTank.ID, textureIndex, tmpTank.imgID[textureIndex-1], 0)
Next textureIndex
tmptank.turret = GetObjectBoneByName(tmptank.ID, "torret")
tmpTank.cannon = GetObjectBoneByName(tmptank.ID, "cannon")
for i = 1 to 7
tmptank.wheel_l[i] = GetObjectBoneByName(tmptank.ID,("wl"+str(i)))
tmptank.wheel_r[i] = GetObjectBoneByName(tmptank.ID,("wr"+str(i)))
next i
tank.insert(tmpTank)
endfunction
function shootMissile(tank ref as _tank,initialSpeed as float, mass as float)
gunPositionVec= CreateVector3(GetObjectboneWorldX(tank.ID, tank.cannon),GetObjectboneWorldY(tank.ID, tank.cannon),GetObjectboneWorldZ(tank.ID, tank.cannon)) //im using the camera position as the gun position
//Create a tempory projectile block to calculate movement vectors of bullets
projectileDirBox as integer
projectileDirBox = CreateObjectBox( 1.0, 1.0, 1.0 )
FixObjectToBone(projectileDirBox,tank.Id,tank.cannon)
x#=GetObjectWorldX(projectileDirBox):y#=GetObjectWorldY(projectileDirBox):z#=GetObjectWorldZ(projectileDirBox)
FixObjectToObject(projectileDirBox,0)
SetObjectPosition(projectileDirBox,x#,y#,z#)
setobjectrotation(projectiledirbox,GetObjectBoneWorldAngleX(tank.ID, tank.cannon),GetObjectBoneWorldAngleY(tank.ID, tank.cannon),GetObjectBoneWorldAngleZ(tank.ID, tank.cannon))
MoveObjectLocalZ( projectileDirBox, 1.0 )
projectileDirVec = CreateVector3( GetobjectWorldX( projectileDirBox )-GetVector3X( gunPositionVec ), GetobjectWorldY( projectileDirBox )-GetVector3Y( gunPositionVec ), GetobjectWorldZ( projectileDirBox )-GetVector3Z( gunPositionVec ) )
missile as integer
missile = CreateObjectSphere(.4,5,5)
SetObjectColor(missile, 255,0,0,255 )
SetObjectLightMode(missile,0)
SetObjectPosition(missile, GetVector3X( gunPositionVec ), GetVector3Y( gunPositionVec ), GetVector3Z( gunPositionVec ) )
SetObjectRotation(missile,GetObjectAngleX(projectileDirBox),GetObjectAngleY(projectileDirBox),GetObjectAngleZ(projectileDirBox)+180)
DeleteObject( projectileDirBox )
//3D Physics code
Create3DPhysicsDynamicBody(missile)
SetObject3DPhysicsMass(missile, mass )
SetObject3DPhysicsLinearVelocity(missile, projectileDirVec, initialSpeed )
time#=timer()
tank.bulletID.insert(missile) //the object id of the bullet
tank.bulletTimer.insert(time#) //timer is used to set there life time as bullets will die off after so long
deletevector3(projectileDirVec)
deletevector3(gunPositionVec)
endfunction missile
the missile function needs work
I thought this line in the missile function setobjectrotation(projectiledirbox,GetObjectBoneWorldAngleX(tank.ID, tank.cannon),GetObjectBoneWorldAngleY(tank.ID, tank.cannon),GetObjectBoneWorldAngleZ(tank.ID, tank.cannon))
the x angle was out so I thought would fix it
wrapvalue(360-GetObjectBoneWorldAngleX(tank.ID, tank.cannon))
function wrapvalue(num as float)
while num<0
num=num+360
endwhile
while num>360
num=num-360
endwhile
endfunction num
but i was wrong