• 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.

Archived

This topic is now archived and is closed to further replies.

shalrath

Travelling along a vector

8 posts in this topic

let''s say I have a vector (2d) at ''25, 45'' and I want to move at an angle of 56 degree''s from here, 20 units each frame, how can I do this??? (also, does anyone know the C/C++ func for inverse tangent?)
0

Share this post


Link to post
Share on other sites
You must want to move "at" 56 degrees relative to some existing heading (ie, you want to turn/rotate 56 degrees first). Your direction is the unit vector:

float x_dir = cos( 56 * PI / 180 ); // must convert degrees to rad
float y_dir = sin( 56 * PI / 180 ); // ditto

To make your effective displacement (distance travelled) 20 units, multiply your unit direction vector (which has a length of 1) by 20:

x_dir *= 20;
y_dir *= 20;

Then just add x_dir and y_dir to your current position (25, 45). Wash, rinse, repeat.

P.S the C/C++ func for inverse tangent is atan() (or is it atan2()? I forget - and am too lazy to check MSDN).
0

Share this post


Link to post
Share on other sites
There are *two* functions for arctangent:

atan(x) gives the arctangent of x, result valued from -pi/2 to pi/2 (first or fourth quadrants of the plane).

atan2(x,y) gives the arctangent of y/x. The benefit here is that with two values you can actually determine where you are in any of the four quadrants of the plane. Result here is valued from -pi to pi.

Those are double-precision functions. I believe there are also floating point equivalents, atanf(x) and atan2f(x,y).

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
0

Share this post


Link to post
Share on other sites
Thanks a heap guys, just one more question though, this doesn''t seem to work, maybe there is an error in my calculations, I''m only working within the first quadrant, and here''s the code, p is the player, pos is where he is, and moveto is set by the mouse cursor as the position he is to walk towards:

plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
0

Share this post


Link to post
Share on other sites
Thanks a heap guys, just one more question though, this doesn''t seem to work, maybe there is an error in my calculations, I''m only working within the first quadrant, and here''s the code, p is the player, pos is where he is, and moveto is set by the mouse cursor as the position he is to walk towards:

plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
0

Share this post


Link to post
Share on other sites
quote:
Original post by shalrath
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector



The length of a vector is the root of the sum of the squares of its coordinates. In other words, your equations should be:
    
plen = sqrt( p.pos[0] * p.pos[0] + p.pos[1] * p.pos[1] );
mlen = sqrt( p.moveto[0] * p.moveto[0] + p.moveto[1] * p/moveto[1] );


You correctly calculate your direction, but things go awry after that. You need to loop while you increment p.pos or your player will only move once - a fraction of the distance you want to cover. Also, you can optimize away the trig operations because cos( acos( theta ) ) == theta and sin( theta ) == sqrt( 1 - cos(theta )2 ). So:
        
x_dir = dot/(plen * mlen);
y_dir = sqrt( 1 - (x_dir * x_dir ) );


**Edit: sin( acos(x) ). What was I thinking!


Edited by - Oluseyi on July 19, 2001 7:17:48 PM
0

Share this post


Link to post
Share on other sites
My next problem, the character can only move in the x-positive, y-positive direction, no other directions, I think there may be something wrong with this code now... Maybe you guys could help again

float newx, newy;
if (p.moveto[0] != p.pos[0])
{
if (p.moveto[0] > p.pos[0])
{
newx = (x_dir * p.speed);
}
else if (p.moveto[0] < p.pos[0])
{
newx = -(x_dir * p.speed);
}
}
if (p.moveto[1] != p.pos[1])
{
if (p.moveto[1] > p.pos[1])
{
newy = (y_dir * p.speed);
}
if (p.moveto[1] < p.pos[1])
{
newy = (y_dir * p.speed);
}
}
p.pos[0] += newx;
p.pos[1] += newy;
0

Share this post


Link to post
Share on other sites
If you know the start and end points of where you want to move, you don''t need to calculate the angle at all. Say you''re at p0 and you want to go to p1. Here''s the code:

  
Point dir;
float len;
dir.x = p1.x - p0.x;
dir.y = p1.y - p0.y;
.
// Normalize dir so it''s unit length.

len = sqrtf( dir.x * dir.x + dir.y * dir.y );
dir.x /= len;
dir.y /= len;
.
// move the point p0 to p1...

while( p0.x != p1.x && p0.y != p1.y )
{
p0.x += dir.x * speed;
p0.y += dir.y * speed;

// display the scene

}


I''ve expanded everything out, but if you''re using C++, you should be able to create overloaded operators for the Point class and turn it all into nice easy-to-look-at math notation.


War Worlds - A 3D Real-Time Strategy game in development.
0

Share this post


Link to post
Share on other sites