• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Quaternion Interface

2 posts in this topic

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 - [url="http://en.wikipedia.org/wiki/Gimbal_lock"]http://en.wikipedia....iki/Gimbal_lock[/url]

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! - [url="http://en.wikipedia.org/wiki/Quaternion"]http://en.wikipedia....wiki/Quaternion[/url]

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:[list]
[*]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
[left][background=rgb(250, 251, 252)]Vector3D _pos, _lookVector, _upVector,[/background][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]_localVelocity, _axisVelocity;[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]Quaternion _qHeading, _qPitch;[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]float _pitch, _heading, _roll,[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]_acceleration, _friction;[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]bool _xLocked, _yLocked, _zLocked,[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]_xViewLocked, _yViewLocked, _zViewLocked,[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]_moving;[/background][/size][/font][/color][/left]


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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0