I do actually get around to some of these suggestions eventually.
Here's a bonus:
` Some named id's for the vectors
#constant VEC_ORIGIN 1
#constant VEC_SHIP_VELOCITY 2
#constant VEC_SHIP_VELOCITY_ADJ 3
#constant VEC_SHIP_VELOCITY_MAT 4
#constant VEC_CAMERA_POSITION 5
#constant VEC_CAMERA_OFFSET 6
#constant VEC_HEADING 7
#constant VEC_ASTEROID_TEMP 8
` Settings for the control system
#constant SHIP_TURN_RATE 3
#constant SHIP_ACCELERATION 0.01
#constant SHIP_DECELERATION 0.005
#constant SHIP_MAX_SPEED 1.0
` Settings for the asteroid creation
#constant AST_MAX_DISTANCE 300
#constant AST_MIN_OBJECT 100
#constant AST_MAX_OBJECT 1500
sync on
sync rate 60
autocam off
make vector3 VEC_ORIGIN ` Always a zero vector
make vector3 VEC_SHIP_VELOCITY ` Used for the current velocity of the ship
make vector3 VEC_SHIP_VELOCITY_ADJ ` Used for the change in velocity
make matrix4 VEC_SHIP_VELOCITY_MAT ` Used to calculate the change in velocity
make vector3 VEC_CAMERA_POSITION ` Used for camera position
make vector3 VEC_CAMERA_OFFSET ` Used to convert object position to camera position
make vector3 VEC_HEADING ` Heading angle
make vector3 VEC_ASTEROID_TEMP ` Used to reposition asteroids
set vector3 VEC_ORIGIN, 0, 0, 0
set vector3 VEC_SHIP_VELOCITY, 0, 0, 0
set vector3 VEC_CAMERA_OFFSET, 0.0, 50.0, 0.0
` Make a 'ship' to control
make object cone 1, 2.0
xrotate object 1, 90.0
fix object pivot 1
` Make 'asteroids'
for i = AST_MIN_OBJECT to AST_MAX_OBJECT
make object cube i, (rnd(9) + 1) / 2.0
rotate object i, rnd( 359 ), rnd( 359 ), rnd( 359 )
repeat
set vector3 VEC_ASTEROID_TEMP, rnd( AST_MAX_DISTANCE * 2 ) - AST_MAX_DISTANCE, 0.0, rnd( AST_MAX_DISTANCE * 2 ) - AST_MAX_DISTANCE
until length vector3( VEC_ASTEROID_TEMP ) < AST_MAX_DISTANCE
position object at vector3 i, VEC_ASTEROID_TEMP
next
` Orient the camera to point down the y axis
position camera 0.0, 50.0, 0.0
point camera 0.0, 0.0, 0.0
Direction = 0
do
` *** Handle controls
if leftkey() then Direction = wrapvalue( Direction - SHIP_TURN_RATE )
if rightkey() then Direction = wrapvalue( Direction + SHIP_TURN_RATE )
if upkey()
` Set a vector to 'straight forward' at the rate of acceleration
set vector3 VEC_SHIP_VELOCITY_ADJ, 0.0, 0.0, SHIP_ACCELERATION
` Transform the forward vector to the correct angle of travel (ie, where the engine is pointing the ship to)
rotate y matrix4 VEC_SHIP_VELOCITY_MAT, DegToRad( Direction )
transform coords vector3 VEC_SHIP_VELOCITY_ADJ, VEC_SHIP_VELOCITY_ADJ, VEC_SHIP_VELOCITY_MAT
` Update the current velocity by the resulting vector
add vector3 VEC_SHIP_VELOCITY, VEC_SHIP_VELOCITY, VEC_SHIP_VELOCITY_ADJ
` If we're going 'too fast', then limit the velocity
if length vector3( VEC_SHIP_VELOCITY ) > SHIP_MAX_SPEED
normalize vector3 VEC_SHIP_VELOCITY, VEC_SHIP_VELOCITY
multiply vector3 VEC_SHIP_VELOCITY, SHIP_MAX_SPEED
endif
endif
` *** Update objects
` Update the object to match the ships direction
yrotate object 1, Direction
` Update the asteroids positions by adding the velocity
for i = AST_MIN_OBJECT to AST_MAX_OBJECT
` Tumble the asteroid
zrotate object i, wrapvalue( object angle z(i) + (4.0 - object size(i)) )
` Move the asteroid in the opposite direction to the ship travel
set vector3 to object position VEC_ASTEROID_TEMP, i
subtract vector3 VEC_ASTEROID_TEMP, VEC_ASTEROID_TEMP, VEC_SHIP_VELOCITY
` If the asteroid is too far away from the ship, bring it back in range
` from the opposite direction
if abs(x vector3(VEC_ASTEROID_TEMP) > AST_MAX_DISTANCE) or abs(z vector3(VEC_ASTEROID_TEMP)) > AST_MAX_DISTANCE
set vector3 to object position VEC_ASTEROID_TEMP, i
multiply vector3 VEC_ASTEROID_TEMP, -1.0
subtract vector3 VEC_ASTEROID_TEMP, VEC_ASTEROID_TEMP, VEC_SHIP_VELOCITY
endif
` Finally position the asteroid
position object at vector3 i, VEC_ASTEROID_TEMP
next
` Calculate the heading (direction of travel)
` I use 'Direction' to mean the direction the ship is facing, and 'Heading' to mean the direction the ship is travelling
set vector3 to angles between vectors VEC_HEADING, VEC_ORIGIN, VEC_SHIP_VELOCITY
Heading = y vector3( VEC_HEADING )
` Display some numbers
set cursor 0, 0
print "Direction = "; Direction
print " Speed = "; str$( length vector3( VEC_SHIP_VELOCITY ), 2 )
print " Heading = "; Heading
sync
loop
@Jeff,
I'll look into the problem.