Confused sphere

Blitz3D Forums/Blitz3D Beginners Area/Confused sphere

streb2001(Posted 2005) [#1]
I am new to B3D and this 3D API business, but otherwise I am an experienced programmer. This has got me stumped however. I am just starting out on a 3D Orrery program - I want to be able to fly around the Solar System using proper newtonian Physics (like Elite Frontier). Can anyone explain what is going wrong at frame 426 onwards? (code below...)

Thanks

Const x_res = 800
Const y_res = 600

frame_number = 0

Graphics3D x_res, y_res

SetBuffer BackBuffer()

AmbientLight 0, 0, 0
WireFrame True
AntiAlias True

camera=CreateCamera()
CameraViewport camera, 0, 0, x_res, y_res

light=CreateLight(1)
LightColor light, 255, 255, 255
PositionEntity light, 10, 0, 19

planet=CreateSphere(16)
ScaleEntity planet, 20, 20, 20
PositionEntity planet, 0, 0, 23
TurnEntity planet, -40, 0.0, 0.0

PointEntity light, planet

While Not KeyHit(1)

oldTime=MilliSecs()
While MilliSecs() < oldTime + 100 : Wend

TurnEntity planet, 0.0, 0.1, 0.0
MoveEntity camera, 0, 0.1, -0.02
PointEntity camera, planet

UpdateWorld
RenderWorld

frame_number = frame_number + 1
Text 100, 100, "Frame " + frame_number
Text 100, 120, "Cam Y " + EntityY#(camera)

Flip

Wend
End


Floyd(Posted 2005) [#2]
The sphere is fine. It's the camera which is confused.

If you display the camera's pitch, yaw and roll values you will see what is happening. When the camera points almost exactly up or down it can spontaneously do a 180 degree turn. This is the dreaded 'gimbal lock' problem.

The cameras yaw value should be 0, but may become +180 or -180. In your case I think there is a quick fix. Update the camera as usual. Then check for trouble and turn it right side up, if necessary, by doing

If Abs( EntityYaw( camera ) ) > 179 Then TurnEntity camera, 0,0, 180


streb2001(Posted 2005) [#3]
Thanks Floyd. Your line works - the yaw and roll values still go mad after the second complete rotation of the camera but it still renders OK. Now I know what is happening I should be able to test and trap all eventualities. I take it putting a camera into a pitch spin is not a good idea?


streb2001(Posted 2005) [#4]
Er...quite. Quaternions I think. And crampons as I see a steep learning curve coming up.