Jump to content

  • Log In with Google      Sign In   
  • Create Account


Quaternion Interface


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Matthew Wood   Members   -  Reputation: 114

Like
0Likes
Like

Posted 30 April 2012 - 03:58 AM

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
{
public:
Camera();
~Camera();
...

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

public:

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();

protected:

void calculateQuats();

Vector3D _pos, _lookVector, _upVector,
_localVelocity, _axisVelocity;

Quaternion _qHeading, _qPitch;

float _pitch, _heading, _roll,
_acceleration, _friction;

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



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!!!

Attached File  IMovable.zip   6.86KB   29 downloads

Sponsor:

#2 LorenzoGatti   Crossbones+   -  Reputation: 2315

Like
0Likes
Like

Posted 01 May 2012 - 03:58 AM

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.

Produci, consuma, crepa

#3 Álvaro   Crossbones+   -  Reputation: 10832

Like
0Likes
Like

Posted 01 May 2012 - 06:10 AM

Vector3D _pos, _lookVector, _upVector,


_localVelocity, _axisVelocity;



Quaternion _qHeading, _qPitch;



float _pitch, _heading, _roll,


_acceleration, _friction;



bool _xLocked, _yLocked, _zLocked,


_xViewLocked, _yViewLocked, _zViewLocked,


_moving;



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, 01 May 2012 - 06:12 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS