Sign in to follow this  
wriordan

get direction

Recommended Posts

Hi all I'm trying to overcome my lack of maths by building simple 2d Game Engine,
But I've run into a small problem i want the game engine to allow you to set speed either based on direction and speed or by just using xspeed and yspeed. I've done this by calculating the x and y speeds whenever the speed or direction of an object changes like so:
[code]
xspeed = cosine(Direction) * speed
yspeed = sine(Direction) * speed
[/code]

But even though my Engine only uses the x and y speeds to update the objects position I still need to update the speed and direction in case a games code uses it in there code otherwise it would cause logic errors/unexpected results I calculate the speed as follows:
[code]
speed = sqaureroot(xspeed^2 + yspeed^2)
[/code]
But I'm clueless as how to calculate the Direction.Thanks to anyone who can help. And Sorry If its to noobish a question.

Share this post


Link to post
Share on other sites
Most programming languages have an atan2() function or something named very similarly that gives you the angle from the x and y coordinates. Remember to check the parameters before using it. C and C++'s atan2(), for example, expects the arguments to be (y, x), not the (x, y) many people expect.

Share this post


Link to post
Share on other sites
[quote name='jameszhao00' timestamp='1318792794' post='4873190']
For keyboard control for example...

Initial:

direction = 0

In the game loop:
if left is pressed: direction = direction + 0.1
if right is pressed: direction = direction - 0.1
[/quote]
Thanks James but I honestly don't think I'd be attempting to write an engine if this was the problem I was having think you misread the question.

[quote name='SiCrane' timestamp='1318792972' post='4873191']
Most programming languages have an atan2() function or something named very similarly that gives you the angle from the x and y coordinates. Remember to check the parameters before using it. C and C++'s atan2(), for example, expects the arguments to be (y, x), not the (x, y) many people expect.
[/quote]

Just what I was looking for SiCrane I'm using c++ so I assume it gives radians and I should use as follows to get a direction in degrees:
[code]
direction = atan2(yspeed,xspeed) / ( PI / 180 );
[/code]
Thanks

Share this post


Link to post
Share on other sites
As I have done in the past, I'll take this opportunity to advocate against using angles. What you really need is the vector (x,y). If you need to rotate it, using atan2, adding something and going back to coordinates using cosine and sine is not the most reasonable way to do it: You can rotate a vector with
[code] new_x = x * cos(angle) - y * sin(angle);
new_y = x * sin(angle) + y * cos(angle);[/code]

Even easier, you can think of the vector (x,y) as the complex number x+iy and then rotation is just multiplication by cos(angle)+i*sin(angle).

When you stop thinking in angles the code gets shorter, cleaner and easier to get right.

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