6DoF Demo

Blitz3D Forums/Blitz3D Programming/6DoF Demo

Chroma(Posted 2007) [#1]
Well I figured since I couldn't get the quat rotations working then I'd try to use some built in commands. Come to find out it was exactly what I was looking for. My first big hurdle was being able to define a thrust vector (along the z-axis) and being able to apply thrust to it no matter what orientation the aircraft was at. I had a feeling TFormNormal looked promising...and it was just what the doctor ordered.

Here's my labor. Thrust applied to the z-axis (0,0,1) no matter what orientation the object is at. It's a first step towards making a decent flight sim. Test it out. I know it's not much but at least I'm getting there. With this accomplishment, I'm kicking Tandem Fighters up to 2% complete!

From here on out it should get easier...well...let me get another beer.

;6DoF Demo
;by Chroma

Graphics3D 800,600,32,2
SetBuffer BackBuffer()

camera = CreateCamera()
PositionEntity camera,0,15,-15
light = CreateLight()

;Replace with your airplane
cube = CreateCube()
RotateEntity cube,0,0,0
PointEntity camera,cube

;Vectors
vOri.Vec3 = New Vec3	;Orientation
vFor.Vec3 = New Vec3	;Total Forces acting on the object
vAcc.Vec3 = New Vec3	;Acceleration (a=f/m)
vVel.Vec3 = New Vec3	;Velocity
vPos.Vec3 = New Vec3	;Position

;Object mass
Local mass# = 10

;Gravity
g# = 9.8

;Initial thrust
Local thrust# = 10.0

;Main Loop
While Not KeyHit(1)
Cls

;Crude Controls
If KeyDown(200) TurnEntity cube,1,0,0
If KeyDown(208) TurnEntity cube,-1,0,0
If KeyDown(203) TurnEntity cube,0,0,1
If KeyDown(205) TurnEntity cube,0,0,-1

;Reset Forces to Zero
Vec3_Set( vFor )

;Set Thrust Vector Forward
TFormNormal 0,0,1,cube,0

;Get Transformed Thrust Vector
Vec3_Set( vOri, TFormedX#(), TFormedY#(), TFormedZ#() )

;Apply Thrust
Vec3_MulScalar(vFor, vOri, Thrust)

;Gravity
;vFor\y = vFor\y - g * mass

;Apply Physics
Vec3_DivScalar(vAcc, vFor, mass)
Vec3_Add(vVel,vVel,vAcc,0.016)
Vec3_Add(vPos,vPos,vVel,0.016)
PositionEntity cube,vPos\x,vPos\y,vPos\z

UpdateWorld
RenderWorld

;Debug
Text 5,5,"XRot: "  + vOri\x
Text 5,25,"YRot: " + vOri\y
Text 5,45,"ZRot: " + vOri\z

Flip
Wend
EndGraphics
End


;Bare Vector Guts
Type Vec3
	Field x#,y#,z#
End Type

Function Vec3_Set(a.Vec3, x#=0, y#=0, z#=0)
	a\x = x
	a\y = y
	a\z = z
End Function

Function Vec3_Add(res.Vec3, a.Vec3, b.Vec3, time# = 1.0)
	res\x = a\x + b\x * time
	res\y = a\y + b\y * time
	res\z = a\z + b\z * time
End Function

Function Vec3_Mul(res.Vec3, a.Vec3, b.Vec3)
	res\x = a\x * b\x
	res\y = a\y * b\y
	res\z = a\z * b\z
End Function

Function Vec3_MulScalar(res.Vec3, a.Vec3, scalar#)
	res\x = a\x * scalar
	res\y = a\y * scalar
	res\z = a\z * scalar
End Function

Function Vec3_DivScalar(res.Vec3, a.Vec3, scalar#)
	res\x = a\x / scalar
	res\y = a\y / scalar
	res\z = a\z / scalar
End Function



t3K|Mac(Posted 2007) [#2]
man i'd thought you have support of 6DoF for TrackIR with Blitz3d when i read your title ;)


OJay(Posted 2007) [#3]
stupid me thought, DOF would stay for DepthOfField...stupid, i know ;)


hm, but what exactly is this code meant to do? i see a small cube slowly flying out of view...? o.O


Chroma(Posted 2007) [#4]
Apparently my choice of title sucks lol.

Use the arrow keys to turn the cube. The thrust always emits along the cube's local z-axis no matter what the global orientation. That was what I've been trying to do with quaternions but, and I still have no clue why, I couldn't get it working.


Stevie G(Posted 2007) [#5]
Nothing wrong with the title if you ask me but I am surprised you hadn't discovered the wonders of the tform commands before now.

I can't stress enough how useful they've been in building my 2d and 3d physics engines.

Good luck with the flight sim - you'll definately need it! ;)

Stevie


Chroma(Posted 2007) [#6]
I blame everquest! But now I have nothing to do except code. :)