Jump to content
  • Advertisement
Sign in to follow this  

Quaternion Interface

This topic is 2333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

One of the first tasks to accomplish in creating your own 3D engine is creating a way to move around in your potential world. While most will start with a simple untextured cube, its not much fun to stare at it from the same angle and position. We need to create a way to move in this beautiful world we are about to create!

While it may be tempting to run to your Input Handling class and start hard coding in a bunch of Trig to achieve a sort of pseudo movement - STOP! There is a better way. First I will explain why trig will fail:

GIMBAL LOCK - http://en.wikipedia....iki/Gimbal_lock

Long story short gimbal lock, as it applies to issues in gaming, will cause a freaky math problem when you attempt to look passed straight up or down - causing axes to flip and become oriented in a way that is difficult to deal with. While it may be possible to use a plethora of if statements to avoid the issue, it is not going to be fun if you need to ever allow your camera to be controlled while upside down. The solution?

Quaternions! - http://en.wikipedia....wiki/Quaternion

Quaternions provide a vector based solution to 3D calculations and movement that when properly coded the first time will provide you with a beautiful way to interact with your world objects. The math behind quaternions is definitely more advanced than trig, but a deep understanding is not necessary for their implementation. The goal of this article is the implementation not the explanation of quats.

The most effective way to implement quaternions in your code is to create an Interface (not a class) that has all the variables and functions that define being movable, and derive any new classes that you want movable from that interface. Logically this makes sense. Creating a Interface called IMovable allows you to think "well my camera is moveable, so i will derive it from the interface moveable" so you would use something like:

Class Camera : Public IMovable

When you get to more advanced stages of your engine this will be essential. You will most likely use some sort of scene graph and make nodes movable instead of individual objects. And you will use pointers so that you can call update on any object/node in the graph without needing to know what it is and blah blah blah - trust me once you get there its ease will make sense ;)

I've included the interface files and code for a vector and quaternion. You will need your own code for handling matrix math. PM me for mine if you'd like. Unfortunately working my code into your engine wont be as easy as drag and drop but it is very clean and easy to figure out.

Heres the header

class IMovable


void moveTo( float x, float y, float z );
void moveTo(Vector3D &v);
void moveTo(Vector3D *v); // Warps the object to a given vertex
void setX(double x); // Set the x value of the position
void setY(double y); // Set the y value of the position
void setZ(double z); // Set the z value of the position

void accelerateForward( float scaleFactor = 1 );
void accelerateBack( float scaleFactor = 1 );
void accelerateLeft( float scaleFactor = 1 );
void accelerateRight( float scaleFactor = 1 );
void accelerateUp( float scaleFactor = 1 );
void accelerateDown( float scaleFactor = 1 );
void accelerateXAxis( float scaleFactor = 1 );
void accelerateYAxis( float scaleFactor = 1 );
void accelerateZAxis( float scaleFactor = 1 );

void zeroAxisVelocity( bool x = true, bool y = true, bool z = true );
void zeroLocalVelocity( bool x = true, bool y = true, bool z = true );

void changePitch(float degrees);
void changeHeading(float degrees);
void setPitch( float degrees );
void setHeading( float degrees );

void setAcceleration( float acceleration );
void setFriction( float friction );

virtual void update();

Vector3D* getPosition();
Vector3D* getLookAngles();
Vector3D* getUpVector();


void calculateQuats();

Vector3D _pos, _lookVector, _upVector,
_localVelocity, _axisVelocity;

Quaternion _qHeading, _qPitch;

float _pitch, _heading, _roll,
_acceleration, _friction;

bool _xLocked, _yLocked, _zLocked,
_xViewLocked, _yViewLocked, _zViewLocked,

So any object you want to be movable derive from this interface and it will have all these functions and variables accessible. The update function that is in the Imovable class is the bare minimum needed as far as math calculations to include and must be called every iteration of your engine to achieve fluid movement. It is a virtual function because most likely every object will have a different need for how it updates. The interface doesnt provide any drawing either, because not all movable objects are drawn (e.g. waypoints, cameras, etc ). So you will have to do that yourself.

At the very least the accelerate forward, up, and left code aaaand retrieving and up vector is useful and where most people have math problems, so feel free to use the code in your own. There is a list of variables that must be set at initiation. They are all listed in the header file. Happy programming!!!


Share this post

Link to post
Share on other sites
In a generic interface for a reusable library, I'd expect:

  • Methods to set velocity (not only position and acceleration)
  • Methods to set acceleration and velocity to a certain Vector3D
  • Methods to increment position, velocity and acceleration (not only setting them)
  • Symmetrical API for pitch, roll and yaw (not two out of three)
  • Methods to set and increment orientation, angular velocity and angular acceleration around the current pitch, roll and yaw axes or around an arbitrary Vector3D
  • A sound definition of "friction". Remove it if you cannot do a good job.

Share this post

Link to post
Share on other sites

[background=rgb(250, 251, 252)]Vector3D _pos, _lookVector, _upVector,[/background]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]_localVelocity, _axisVelocity;[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Quaternion _qHeading, _qPitch;[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]float _pitch, _heading, _roll,[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]_acceleration, _friction;[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]bool _xLocked, _yLocked, _zLocked,[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]_xViewLocked, _yViewLocked, _zViewLocked,[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]_moving;[/background][/font]


That looks like way too much state. In order to describe where an object is, all you need is a position (a 3D vector) and an attitude (a quaternion). I can see an argument for having an angular velocity as well (another 3D vector) if you want to use Physics on your objects. Everything else is redundant, unclear or both, and should go. Edited by alvaro

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!