Thanks guys.
@puzzler2018: that doesn't work in general code for axis changed.
@smallg, FixObjectPivot(id) indeed did the trick, as you can see from below code snippet, which is 3d-ification of the 2d type/array example from the AppGameKit that uses sprites.
( Original here: https://www.appgamekit.com/documentation/guides/types_003.htm )
function SetupLasers ( )
// this function will set up the lasers
// load an image for the laser
// image = LoadImage ( "laserVert.png" )
// create sprites for each laser
for i = 1 to spaceShip.lasers.length
//spaceShip.lasers [ i ].sprite = CreateSprite ( image )
spaceShip.lasers [ i ].mesh = CreateObjectCapsule(.2,.5,16 )
RotateObjectLocalX (spaceShip.lasers [ i ].mesh,90) //changes x,y,z axis also, which is undesired..
FixObjectPivot(spaceShip.lasers [ i ].mesh) // this fixes it.
ResetLaser ( i, 0 )
next i
endfunction
Full working example:
// Project: ArraysTest
// Created: 2017-10-14
// show all errors
SetErrorMode(2)
// create a space ship on screen that flies left and right
// and also fires lasers
// declare a type to store data for our lasers
type laserType
mesh as integer
speed as float
x as float
y as float
z as float
state as integer
endtype
// declare a type to store data for our ship
type shipType
mesh as integer
speed as float
direction as integer
fireTimer as float
lasers as laserType [ 10 ]
endtype
// variable for our ship, make it global so it can be
// easily accessed by all parts of the program
global spaceShip as shipType
// set a virtual resolution of 1024 x 768
SetVirtualResolution ( 1024, 768 )
// call functions to set up the ship and its lasers
SetupShip ( )
SetupLasers ( )
SetCameraPosition(1,0,0,0) : SetCameraRotation(1,0,0,0)
SetCameraRotation(1,90,0,0) // rotate cam so looking at from side
SetCameraPosition(1,0,30,20) // move above and adjust
// our main loop
do
// call functions to update the ship and its lasers
UpdateShip ( )
UpdateLasers ( )
// update the screen
sync ( )
loop
function SetupLasers ( )
// this function will set up the lasers
// load an image for the laser
// image = LoadImage ( "laserVert.png" )
// create sprites for each laser
for i = 1 to spaceShip.lasers.length
//spaceShip.lasers [ i ].sprite = CreateSprite ( image )
spaceShip.lasers [ i ].mesh = CreateObjectCapsule(.2,.5,16 )
RotateObjectLocalX (spaceShip.lasers [ i ].mesh,90)
SetObjectColor(spaceShip.lasers [ i ].mesh, 255,255,0,255)
FixObjectPivot(spaceShip.lasers [ i ].mesh)
ResetLaser ( i, 0 )
next i
endfunction
function ResetLaser ( index as integer, visible as integer )
// this function will reset the lasers, giving them default values
// state, speed, position to match the ship and visibility
spaceShip.lasers [ index ].state = 0
spaceShip.lasers [ index ].speed = Random ( 80, 100 ) / 500.0
// SetSpritePosition ( spaceShip.lasers [ index ].sprite, GetSpriteX ( spaceShip.sprite ) + GetSpriteWidth ( spaceShip.sprite ) / 2 - 8 , GetSpriteY ( spaceShip.sprite ) - 12 )
SetObjectPosition(spaceShip.lasers [ index ].mesh, GetObjectX ( spaceShip.mesh ) , GetObjectY ( spaceShip.mesh ) , GetObjectZ ( spaceShip.mesh)+2 )
// SetSpriteVisible ( spaceShip.lasers [ index ].sprite, visible )
SetObjectVisible ( spaceShip.lasers [ index ].mesh, visible )
// store the newly reseted lasers x,y,z(should be at ships location)
spaceShip.lasers [ index ].x = GetObjectX(spaceShip.lasers [ index ].mesh)
spaceShip.lasers [ index ].y = GetObjectY(spaceShip.lasers [ index ].mesh)
spaceShip.lasers [ index ].z = GetObjectZ(spaceShip.lasers [ index ].mesh)
endfunction
function UpdateLasers ( )
// this function updates the lasers
// increment the timer to control when the lasers should fire
spaceShip.fireTimer = spaceShip.fireTimer + 0.01
// run through all the lasers
for i = 1 to spaceShip.lasers.length
// if the state is 0 then this laser is available
if spaceShip.lasers [ i ].state = 0
// only fire the laser if the fire timer is over 0.25
if spaceShip.fireTimer >= 0.25
// reset the laser, set the state to 1 and reset the timer
ResetLaser ( i, 1 )
spaceShip.lasers [ i ].state = 1
spaceShip.fireTimer = 0.0
endif
endif
// when the state is 1 the laser has been fired
if spaceShip.lasers [ i ].state = 1
// move the laser up the screen
//SetSpritePosition ( spaceShip.lasers [ i ].sprite, GetSpriteX ( spaceShip.lasers [ i ].sprite ), GetSpriteY ( spaceShip.lasers [ i ].sprite ) - spaceShip.lasers [ i ].speed )
//SetObjectPosition ( spaceShip.lasers [ i ].mesh,GetObjectx(spaceShip.lasers[i].mesh), GetObjecty(spaceShip.lasers[i].mesh) , GetObjectZ(spaceShip.lasers[i].mesh)+ spaceShip.lasers [ i ].speed )
// SetObjectPosition ( spaceShip.lasers [ i ].mesh,spaceShip.lasers[i].x, spaceShip.lasers[i].y , spaceShip.lasers[i].z+ spaceShip.lasers [ i ].speed )
MoveObjectLocalZ ( spaceShip.lasers [ i ].mesh, spaceShip.lasers [ i ].speed )
// check for the laser leaving the screen
//if GetSpriteY ( spaceShip.lasers [ i ].sprite ) < 0
if GetObjectZ ( spaceShip.lasers [ i ].mesh ) > 50
//if spaceShip.lasers [ i ].z >= 50
// reset the laser and allow it to be used again
ResetLaser ( i, 0 )
endif
endif
next i
endfunction
function SetupShip ( )
// set up for our ship
// create a sprite, control how fast it moves and set its initial direction
//spaceShip.sprite = CreateSprite ( LoadImage ( "TopFighter1(turned).png" ) )
spaceShip.mesh=CreateObjectCone(2,1,16)
spaceShip.speed = .1
spaceShip.direction = 0
SetObjectColor(spaceShip.mesh, 100,100,255,255)
// position the sprite towards the centre and near the bottom of the screen
//SetSpritePosition ( spaceShip.sprite, 1024 / 2, 626 )
SetObjectPosition ( spaceShip.mesh, 0,0,0 ) : SetObjectRotation ( spaceShip.mesh, 0,0,0 )
SetObjectPosition ( spaceShip.mesh, 0,-5,10 ) : SetObjectRotation ( spaceShip.mesh, 90,0,0 )
endfunction
function UpdateShip ( )
// control the movement of the ship
// get the X coordinate of the ship
// x = GetSpriteX ( spaceShip.sprite )
x = GetObjectX ( spaceShip.mesh )
y = GetObjectY ( spaceShip.mesh )
z = GetObjectZ ( spaceShip.mesh )
// find out whether it has moved to the left or right side
// if ( spaceShip.direction = 0 and x > 976 ) or ( spaceShip.direction = 1 and x < 0 )
//if ( spaceShip.direction = 0 and x > 400 ) or ( spaceShip.direction = 1 and x < -400 )
if x > 15
// reverse the speed and direction
spaceShip.speed = -.1
// spaceShip.direction = not spaceShip.direction
elseif x < -15
spaceShip.speed = .1
endif
// add the current speed to the X position
// x = x + spaceShip.speed
// update the position of our ship
//SetSpritePosition ( spaceShip.sprite, x, GetSpriteY ( spaceShip.sprite ) )
//SetObjectPosition ( spaceShip.mesh, x, y, z )
MoveObjectLocalX ( spaceShip.mesh,spaceShip.speed )
endfunction
*I didn't clean it up, so don't look at the mess, it's just a quick typ/array test.
What is a bit silly : I actually already had used FixObjectPivot () before in another piece of code, that I forgot/ditched for I couldn't get what I want, which is clone an object with other objects linked to it.
Procedure:
1) create bullettip using: bulletip=CreateObjectCapsule(.2,.4,3)
2) create bulletshell using: bulletshell=CreateObjectCylinder(.4,.2,32)
3) tie both pieces together: FixObjectToObject(bulletshell,bulletip)
4) copy the whole thing: bullet=CloneObject(bulletip)
5) move it in desired direction: SetObjectPosition(bullet,10,5,10)
This doesn't work for somehow the FixObjectToObject command doesn't create a real child object apparently, so only the bulletip object will be copied.
Unfortunately this somehow limits the creation of examples with procedurally generated graphics.