Here's a quick example of what I was talking about, little bit of a rush job so naming of things won't be amazing but it has the affect. You'll notice that players position and rotation is local to the ship, as is the same for the ships entities and this is how you should treat them.
What the code produces...
[Requires IanM's Matrix Plugin]
// declares
gosub declare_math
gosub declare_player
gosub declare_ship
gosub declare_entities
global _mvx as float
global _mvy as float
// set up display
set text font "Tahoma"
set text size 13
sync on : sync rate 60
autocam off : hide mouse
color backdrop 0 : set camera range 1,12000
// set up player
math_vector3( 0, 0, 0 ) : _player.pos = _vector3
math_vector3( 0, 0, 0 ) : _player.ang = _vector3
math_vector3( 0, 16, 0 ) : _player.eyePos = _vector3
_player.speed = 0.5
// set up ship
math_vector3( 0, 0, 0 ) : _ship.pos = _vector3
math_vector3( 0, 0, 0 ) : _ship.ang = _vector3
// create ship assests
make object box 1, 128, 2, 128 // floor
set object diffuse 1, rgb(75, 73, 86)
shipFloor = entity_create( 1 )
make object box 2, 8, 8, 8 // random block
set object diffuse 2, rgb(146, 14, 97)
shipConsole = entity_create( 2 )
entity_setPosition( shipConsole, 48, 4, 48 )
make object box 3, 2, 2, 2 // random block
set object emissive 3, rgb(0, 255, 255)
shipConsoleCube = entity_create( 3 )
entity_setPosition( shipConsoleCube, 48, 10, 48 )
// space stuff
for n = 1 to 1000
o = find free object()
make object cube o,1+rnd(2)
position object o, rnd(2048)-1024, rnd(2048)-1024, rnd(2048)-256
next
o = find free object()
make object sphere o, 9000,16,16
position object o, 6000,256,9000
do
// update input
_mvx = mousemovex()
_mvy = mousemovey()
_left = keystate(30) || leftkey()
_right = keystate(32) || rightkey()
_forward = keystate(17) || upkey()
_backward = keystate(31) || downkey()
// update ship - setting the speed really high is quite funny to look ati
`_ship.ang.x = wrapvalue(_ship.ang.x + 0.1)
_ship.ang.z = wrapvalue(_ship.ang.z + 0.1)
inc _ship.pos.z, 0.4
// update cube thing
_entity(shipConsoleCube).ang.x = wrapvalue(_entity(shipConsoleCube).ang.x + 1)
_entity(shipConsoleCube).ang.y = wrapvalue(_entity(shipConsoleCube).ang.y + 1)
// build ship world matrix
rotate xyz matrix4 _rotMat, degToRad(_ship.ang.x), degToRad(_ship.ang.y), degToRad(_ship.ang.z)
translate matrix4 _transMat, _ship.pos.x, _ship.pos.y, _ship.pos.z
multiply matrix4 _worldMat, _rotMat, _transMat
// update player
`camera angle
_player.ang.x = clamp(_player.ang.x + (_mvy * .25),-90.0,90.0)
_player.ang.y = wrapvalue(_player.ang.y + (_mvx * .25))
_player.ang.z = 0
rotate xyz matrix4 _rotMat2, degToRad(_player.ang.x), degToRad(_player.ang.y), degToRad(_player.ang.z)
multiply matrix4 _rotMat2, _rotMat2, _rotMat // THE MULTIPLICATION ORDER IS IMPORTANT
set vector3 to matrix4 rotation _matVec3, _rotMat2
rotate camera to vector3 0, _matVec3
// create a rotation matrix for the player to move from (Look/Right)
rotate xyz matrix4 _rotMat2, 0.0, degToRad(_player.ang.y), 0.0
look = new vector3( get matrix4 element(_rotMat2, 8),get matrix4 element(_rotMat2, 9),get matrix4 element(_rotMat2, 10) )
right = new vector3( get matrix4 element(_rotMat2, 0),get matrix4 element(_rotMat2, 1),get matrix4 element(_rotMat2, 2) )
set vector3 _matVec3, _player.pos.x, _player.pos.y, _player.pos.z
scale vector3 look, look, _player.speed * (_backward-_forward)
subtract vector3 _matVec3, _matVec3, look
scale vector3 right, right, _player.speed * (_right-_left)
add vector3 _matVec3, _matVec3, right
_player.pos.x = x vector3(_matVec3)
_player.pos.y = y vector3(_matVec3)
_player.pos.z = z vector3(_matVec3)
set vector3 _matVec3, _player.pos.x, _player.pos.y+_player.eyePos.y, _player.pos.z
transform coords vector3 _matVec3, _matVec3, _worldMat
position camera at vector3 0, _matVec3
// update entities
for n = 0 to array count(_entity())
set vector3 _matVec3, _entity(n).pos.x, _entity(n).pos.y, _entity(n).pos.z
transform coords vector3 _matVec3, _matVec3, _worldMat
position object at vector3 _entity(n).id, _matVec3
rotate xyz matrix4 _rotMat2, degToRad(_entity(n).ang.x), degToRad(_entity(n).ang.y), degToRad(_entity(n).ang.z)
multiply matrix4 _rotMat2, _rotMat2, _rotMat
set vector3 to matrix4 rotation _matVec3, _rotMat2
rotate object to vector3 _entity(n).id, _matVec3
next
text 4,4, "WASD to move, Mouse to change angle"
text 4,4+(text size()*2),"[Ship Info]"
text 4,4+(text size()*3), "Pos: "+str$(_ship.pos.x,2)+","+str$(_ship.pos.y,2)+","+str$(_ship.pos.z,2)
text 4,4+(text size()*4), "Ang: "+str$(_ship.ang.x,2)+","+str$(_ship.ang.y,2)+","+str$(_ship.ang.z,2)
text 4,4+(text size()*6),"[Player Info]"
text 4,4+(text size()*7), "Pos: "+str$(_player.pos.x,2)+","+str$(_player.pos.y,2)+","+str$(_player.pos.z,2)
text 4,4+(text size()*8), "Ang: "+str$(_player.ang.x,2)+","+str$(_player.ang.y,2)+","+str$(_player.ang.z,2)
sync
loop
// math class
type vector3
x as float
y as float
z as float
endtype
declare_math:
global _vector3 as vector3
global _rotMat, _rotMat2, _transMat, _worldMat, _matVec3
_rotMat = new matrix4() // ship
_rotMat2 = new matrix4() // anything on the ship
_transMat = new matrix4() // translation (position)
_worldMat = new matrix4()
_matVec3 = new vector3() // temp vector
return
function math_vector3( x as float, y as float, z as float )
_vector3.x = x : _vector3.y = y : _vector3.z = z
endfunction
// player class
type player
pos as vector3
ang as vector3
speed as float
eyePos as vector3
endtype
declare_player:
global _player as player
return
// ship class
type ship
pos as vector3
ang as vector3
endtype
declare_ship:
global _ship as ship
return
// entities class
type entity
id
pos as vector3
ang as vector3
endtype
declare_entities:
global dim _entity() as entity
return
function entity_create( id )
array insert at bottom _entity()
out = array count(_entity())
_entity().id = id
endfunction out
function entity_setPosition( id, x as float, y as float, z as float )
_entity(id).pos.x = x : _entity(id).pos.y = y : _entity(id).pos.z = z
endfunction
function entity_setAngle( id, x as float, y as float, z as float )
_entity(id).ang.x = x : _entity(id).ang.y = y : _entity(id).ang.z = z
endfunction
Quote: "I do not have that much experience with vector or rotation matrices"
They are quite useful so definitely take up using them. They're not that complicated. An easy way to understand them is just recreate the vector/matrix commands in code and you can see how they work (or read up on them). This is how I learnt them. Now I use the commands cause they are so much quicker than doing the math.
"Get in the Van!" - Van B