• 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
Henri Korpela

Utilizing vectors to move entities in 3D space

3 posts in this topic

Hi, folks and welcome to the topic!

First of all, I'd like to point out that I'm no math genius, but I really got hooked on vectors seeing their benefits in programming entity movement in virtual 2D and 3D environments.

There are several vector classes written in C/C++ to be found on the Internet. However, as I'm willing to learn more about vectors all the time, I dediced to create my own class template in C++ called [b]Vector3[/b].

As far as I know, vector consists of a direction and a magnitude. Direction can be represents with the help of other vectors or by an angle. Magnitude on the hand, is simply the line segment from one point to another. Sounds reasonably simple.

Now, I'm also willing to create a class called [b]Entity[/b]. In this class I'd like to have a [b]Vector3[/b]-class instance as a member. Via member function called "move" I'd like to use the vector to move some entity, like this:

Entity bob("Bob");
bob.move(30, 0, 1); // '30' and '0' are both angles, '1' represents velocity, which is in meters per second.

Now, if I know both the direction given in angles and the magnitude, not to mention the vector start point in 3D space, how is it possible to calculate the vector end point or do I need to calculate the vector end point in order to move the entity? For me, it makes sense that the movement of an entity happens from one point to another, but how is this movement usually implemented by vectors?

Share this post

Link to post
Share on other sites
Thanks, [b]Kyall[/b], for such a useful post! Now I see what are the benefits of using plain coordinates instead of angles to represent direction of where to go to. There's no need to recalculate direction when you can just sum it up to already existing coordinates in order to move requested entity. Simple yet satisfying.

Anyway, could somebody still prove why this is true to deepen my understanding (and possibly the other's following this topic):
[source lang="c++"]result.x = cos( direction.x ) * magnitude - sin( direction.y ) * magnitude
result.y = sin( direction.x ) * magnitude + cos( direction.y ) * magnitude[/source]

Share this post

Link to post
Share on other sites
I think Kyall got mixed up somewhere - the equations he gives look like those to rotate a 2D vector by some angle (which can be represented as another 2D vector, but with magnitude 1, i.e. a unit vector). I think what he meant was:

result.x = cos(angle) * magnitude
result.y = sin(angle) * magnitude

Which is true by the very definition of the angle and the trigonometric angles cosine and sine. Think back to the unit circle (circle of radius 1) - for any angle, the cosine of the angle gives the x-coordinate of the point at which a line starting from the origin and going at said angle would intersect the circle. Similarly, the sine will give the y-coordinate of the point of intersection. So the point at which the line intersects the circle is:

(cos(angle), sin(angle))

Since this is in relation to the origin, this point can be represented as a vector (more accurately, a position vector, which means the vector of the movement required to get from the origin to that point). Now, the length of this segment (from the origin to the point) is going to be:

sqrt(cos(angle)^2 + sin(angle)^2)

And we know that cos(angle)^2 + sin(angle)^2 = 1, so the length of the segment is sqrt(1) = 1. This could also be deduced by noting that the point of intersection of the line with the unit circle is, by definition, located on the circle, which has radius 1, so the length to the origin must be 1.

So we just need to multiply this position vector by the magnitude, which is the length of the desired vector, to obtain the final vector:

(cos(angle), sin(angle)) * magnitude

Which is the same as:

(cos(angle) * magnitude, sin(angle) * magnitude)

Which is, in cartesian coordinates:
x = cos(angle) * magnitude
y = sin(angle) * magnitude


I find it very difficult to completely and intuitively explain vectors in a single post. If you really want to get a grip on it, I suggest following some sort of linear algebra course, which is basically all about vectors and matrices. It really does help.

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