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

SMurf7

Converting an angle to vectors

7 posts in this topic

Hi, I''m making a rather cheap clone of another 2D game (it''s my first game, so bear with me here) and I''m trying to convert an angle in radians to x and y vectors. My code looks like this:- void Create(int x, int y, double Dir) { xpos = x; ypos = y; Fh = cos(1.5707963267949 - Dir); Fv = cos(Dir); if ((int)Fh != Fh) X[0] = Fh - -(int)Fh; if ((int)Fv != Fv) Y[0] = Fv - -(int)Fv; active = true; } xpos and ypos track the current position of the object, Fh and Fv are doubles representing the vector results (horizontal and vertical), X[0] and Y[0] are doubles, overflow variables used during the movement of the object. However, this code does not appear to produce the correct vectors. Admittedly I''m quite rubbish at this sort of math, so could somebody put me out of my misery? Please?
0

Share this post


Link to post
Share on other sites
ok tell me if I''m making any incorrect assumptions as I go

"void Create(int x, int y, double Dir)
{
xpos = x;
ypos = y;"

basically just assigning some sort of grid coordinates, Dir is the angle

"Fh = cos(1.5707963267949 - Dir);"

taking the sin of Dir, but doing it wrong. sin is dir + pi/2 or dir - pi/2. Dir shouldn''t be the negative part. Also bad naming convention. Fh? How does that remind you of the y value?

"Fv = cos(Dir);"

getting the x

"if ((int)Fh != Fh)
X[0] = Fh - -(int)Fh;"

what the hell is this? Ok let''s start with the (int)Fh. Casting to an int always rounds towards 0. Unless your value happens to be one of the very few cases where it is 1 or -1 you will always get 0. So that condition will always evaluate to true except in specific cases. Now if that is true it will assign something to X[0]. What is X[0] standing for, the x part? Remember that Fh is representing the y. Ok now what are we assigning. We start by turning FH into 0 (or -0), it always turns to 0. Now we take the opposite of it. Then we subtract it from itself. So in the end all we do is assign Fh (the y value) to X[0]. If the angle happened to be 0 or pi nothing gets assigned to X[0]

"f ((int)Fv != Fv)
Y[0] = Fv - -(int)Fv;
active = true;"

then we do the reverse here, with the x value being assigned to Y[0].
0

Share this post


Link to post
Share on other sites
Fh and Fv represent horizontal and vertical components respectively (x and y). I lifted the names from a physics textbook (F is force).

I use X[0] and Y[0] to control the overflow of the vectors, i.e if the resultant Fh should be incremented by 1.26 (impossible to have "partial pixels"), then the .26 is put here and incremented in the move code every cycle until it is greater or equal to 1 (then the Fh is incremented and this gets set back to its original value).

Erm, is that any clearer?

0

Share this post


Link to post
Share on other sites
Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)
0

Share this post


Link to post
Share on other sites
Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)
0

Share this post


Link to post
Share on other sites
Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)
0

Share this post


Link to post
Share on other sites
I''m afraid posting the same thing 3 times does not solve the fact that I am dealing with 360 degrees of movement and not 45. Subsequently, the functions used to calculate the vectors (sin, cos) would change depending on the angle (I think). I just don''t know how
0

Share this post


Link to post
Share on other sites
Ok now see what you mean with the X[0] stuff. You have two problems. The first is that you still haven''t fixed the line where you get the height. That is not the way to get a sin out of a cos. Also you need to scale both parts by the length of the vector, otherwise the overflow will do nothing and your guy will move too slowly. Chance the 3rd and 4th lines to:

Fh = (vector.length)*cos(Dir+1.5707963267949 );
Fv = (vector.length)*cos(Dir);
0

Share this post


Link to post
Share on other sites