Quaternion: A User's Guide


Tweet blitz3d tutorials
(Posted 9 months ago) RonTek

If you're having problems with rotating objects using pitch, yaw and roll (i.e. Euler angles), the following article might help.

Author: dscho.co.uk

In Blitz 3d, the parameters of the RotateEntity and TurnEntity commands are Euler angles. There are problems with Euler angles, and I'll tell you a way round them using quaternions. I'll explain a little more about what quaternions are and what they do, but I won't be going into the maths behind them. For one thing, it's unnecessary if all you need to do is use them (rather than understand them). And for another, I've only the vaguest idea about the maths myself, and there are many people more qualified than I to tell you about them. Look at the references at the end for some excellent background reading.

More Reading...

Interpolation:
It is very difficult indeed to interpolate between two rotations using Euler angles alone. If you don't believe me, try it... Here's an exercise you can do to prove it to yourself. Copy and paste the following code into Blitz (or download it here), and run it:

; Quickly set up a scene
Graphics3D 800,600
SetBuffer BackBuffer()
WireFrame True
Global cube = CreateCube()
Global cam = CreateCamera()
TranslateEntity cam, -5, -2, -5
PointEntity cam, cube
CameraViewport cam, 0, 0, GraphicsWidth (), GraphicsHeight ()

; We're going to rotate a cube between these two angles: (sp = start pitch, er = end roll, etc)
Global sp# = 90, sy# =  0, sr# =  0 ; The start rotation
Global ep# =  0, ey# =-90, er# = 90 ; The end rotation

; Start off by rotating the cube to it's initial rotation
RotateEntity cube, sp, sy, sr, 1
CaptureWorld : RenderWorld

Text 10, 10, "Press escape for ideal rotation" : Flip 1 : Repeat : Until KeyHit(1)

; Show the rotation we want to achieve, by using Blitz's tweening
RotateEntity cube, ep, ey, er, 1
For t = 0 To 200
    RenderWorld Float(t)/200
    Flip 1
Next
Text 10, 10, "Press escape for our own rotation" : Flip 1 : Repeat : Until KeyHit(1)

; Try to emulate the rotation
For t = 0 To 200
    RotateCube(Float(t)/200)
    CaptureWorld : RenderWorld
    Flip 1
Next

Text 10, 10, "Press escape to end" : Flip 1 : Repeat : Until KeyHit(1)
End

Function RotateCube(tween#)
    ; Delete this code, replace it with something that works!
    RotateEntity cube, (ep - sp)/tween, (ey - sy)/tween, (er - sr)/tween, 1
End Function

Reply To Topic (minimum 10 characters)

Please log in to reply