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

Using two given points in 2D space to determine one of the following directions: north, south, east, or west

7 posts in this topic

I have a hard time trying to calculate a value to determine what direction an entity is moving towards at.

 

Currently, I have 2 points known at any point in time, the starting point and the ending point. The unit moves at a constant speed in a linear interpolating fashion. I'm trying to find what direction the entity is moving from the starting point to the ending point, not the other way around.

 

Example: If the starting point is at the point of origin in the Cartesian plane, and the ending point is at (10.1235, 0.81235), the direction the entity is moving towards at is east, since the angle is less than 45 degrees of northeast direction. In this case, after calculating the value (that I have a hard time trying), using conditional ifs, we can conclude that the direction east is determined.

 

Any tips I can use? I'm looking at Math.atan2(dy,dx), but maybe I may have been looking at it incorrectly. Thanks in advance.

Edited by tom_mai78101
0

Share this post


Link to post
Share on other sites

What Juliean said, but I have to object to the variable name `dist'. A distance is a number, not a vector. `direction' would have been a much better name.

 

Alternatively, you could accomplish this using atan2, as you started:

static int const direction_table[5] = {WEST, SOUTH, EAST, NORTH, WEST};
static double const Tau = std::atan(1.0)*8.0; // a.k.a. `2*Pi'
 
Vector2D direction = target - start;
double angle = std::atan2(direction.y, direction.x);
int rounded_angle = static_cast<int>(std::floor(0.5 + angle * 4.0 / Tau)) + 2;
 
return direction_table[rounded_angle];

The latter solution is easier to generalize to using 8 or 16 directions.

 

EDIT: Sorry the code is in C++, since I don't know Java. It should be easy to understand anyway.

Edited by Álvaro
1

Share this post


Link to post
Share on other sites


What Juliean said, but I have to object to the variable name `dist'. A distance is a number, not a vector. `direction' would have been a much better name.

 

Hm, I honestly didn't think about it before writing, its probably some bad translation I made from german, but doesn't distance make sense here too, viewing the vector as an aggregate of both an x and an y distance? Thats all a vector is after all, whether its two position or two distancen components, isn't it? In my own code, I'd put a prefix befor it, like vDist, which should make it a little more clear.

0

Share this post


Link to post
Share on other sites

The distance between two points is the length of the difference vector, so of course these things are related. Still, I like my variables to have names as clear as possible, and calling a vector `dist' is just not clear enough. Someone reading the code might assume the wrong type for it, given its name.

 

A vector is not "two distancen components" at all. If something can have a value of -5, it's not a distance.

0

Share this post


Link to post
Share on other sites


A vector is not "two distancen components" at all. If something can have a value of -5, it's not a distance.

 

Apologies for my poor wording. I was refering to the fact that you said "A distance is a number, not a vector", and a vector is (AFAIK) a tuple of two numbers. In this case, the distancen on the x - axis and on the y-axis. I didn't know about that a distance cannot be negative though, so thanks for clearing me up.

0

Share this post


Link to post
Share on other sites


I was refering to the fact that you said "A distance is a number, not a vector", and a vector is (AFAIK) a tuple of two numbers.

 

A [two-dimensional] vector is a pair of numbers once you have decided on a basis. But "a number" and "a pair of numbers" are not the same thing: In programming terms, they have different types.

0

Share this post


Link to post
Share on other sites

I've seen the term "signed distance" used before especially with regards to distance to plane checks (+ve means it is in front, -ve behind a plane with respect to the plane normal).

 

But what is normally meant by distance is something which satisfies the properties of a metric http://en.wikipedia.org/wiki/Metric_%28mathematics%29

 

i.e. given a function d(x, y) where x, y are members of a set and d(x, y) is a real number

 

d(x, y) >= 0 with d(x, y) = 0 if and only if x = y

d(x, y) = d(y, x)

d(x, z) <= d(x, y) + d(y, z) [triangle inequality; it's never quicker to get from x to z by going via y]

Edited by Paradigm Shifter
0

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