voila
type vect2
x as float
y as float
z as float
endtype
global ret as vect2
global retAngle as vect2
autocam off
make object box 1,10,.1,10
position object 1,0,0,0
make object box 2,1,2,.5
ChildPos as vect2
ChildAng as vect2
ChildPos.x=rnd(10)-5
ChildPos.y=1
ChildPos.z=rnd(10)-5
ChildAng.x=rnd(10)-5
position object 2,ChildPos.x,ChildPos.y,ChildPos.z
sync on
sync rate 30
do
rotate object 1,object angle x(1)+keystate(16)-keystate(30),object angle y(1)+keystate(17)-keystate(31),object angle z(1)+keystate(18)-keystate(32)
if upkey()
inc ChildPos.x,cos(ChildAng.y)*.1
inc ChildPos.z,sin(ChildAng.y)*.1
endif
if downkey()
inc ChildPos.x,-cos(ChildAng.y)*.1
inc ChildPos.z,-sin(ChildAng.y)*.1
endif
inc ChildAng.x,mousemovey()
inc ChildAng.y,mousemovex()
rotate object 2,ChildAng.x,ChildAng.y,ChildAng.z
ParentToChild(1,2,ChildPos)
position object 2,ret.x,ret.y,ret.z
rotate object 2,retAngle.x,retAngle.y,retAngle.z
position camera ret.x,ret.y,ret.z
rotate camera retAngle.x,retAngle.y,retAngle.z
if spacekey()
position camera 10,10,10
point camera 0,0,0
endif
sync
loop
function ParentToChild(Parent as integer, Child as integer, offset as vect2)
null = make matrix4(1)
null = make matrix4(2)
null = make matrix4(3)
null = make vector4(4)
null = make vector4(5)
ObjectRotationMatrix(Parent,1)
ObjectRotationMatrix(Child,2)
set vector4 4,offset.x,offset.y,offset.z,1
transform vector4 5,4,1
ret.x=x vector4(5) : ret.y=y vector4(5) : ret.z=z vector4(5)
multiply matrix4 3,2,1
rotMatrixToEulerAng(3)
null = delete matrix4(1)
null = delete matrix4(2)
null = delete matrix4(3)
null = delete vector4(4)
null = delete vector4(5)
endfunction
function rotMatrixToEulerAng(C as integer)
C31#=M4Element(C,3,1)
C32#=M4Element(C,3,2)
C33#=M4Element(C,3,3)
C21#=M4Element(C,2,1)
C11#=M4Element(C,1,1)
if absolut(C31#)<>1
retAngle.y=-asin(C31#)
retAngle.x=atanfull(C32#/cos(retAngle.y),C33#/cos(retAngle.y))
retAngle.z=atanfull(C21#/cos(retAngle.y),C11#/cos(retAngle.y))
else
retAngle.z=0
retAngle.y=posneg(C31#,90)
retAngle.x=atanfull(posneg(C31#,C13#),posneg(C31#,C13#))
endif
endfunction
function ObjectRotationMatrix(object as integer, mtrx as integer)
null = make matrix4(1000)
null = make matrix4(1001)
null = make matrix4(1002)
rotate x matrix4 1000,object angle x(object)*0.0174532925
rotate y matrix4 1001,object angle y(object)*0.0174532925
multiply matrix4 1002,1000,1001
rotate z matrix4 1000,object angle z(object)*0.0174532925
multiply matrix4 mtrx,1002,1000
null = delete matrix4(1000)
null = delete matrix4(1001)
null = delete matrix4(1002)
endfunction
function posneg(a#,num#)
if a#<0 then exitfunction -num#
if a#>0 then exitfunction num#
endfunction 0.0
function absolut(a#)
if a#>0 then exitfunction a#
a#=-a#
endfunction a#
function M4Element(m4,row,column)
n#=get matrix4 element(m4,(column-1)*4+row-1)
endfunction n#
press qwe/asd to change the angle of the plane, mouse to look around, and the up and down arrowkeys to move. You'll notice absolutely nothing special, but press the spacekey to view yourself standing on a plane, rotating around.
Is't life, I ask, is't even prudence, to bore thyself and bore thy students?