get direction

Started by
5 comments, last by _WeirdCat_ 12 years, 6 months ago
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:

xspeed = cosine(Direction) * speed
yspeed = sine(Direction) * speed


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:

speed = sqaureroot(xspeed^2 + yspeed^2)

But I'm clueless as how to calculate the Direction.Thanks to anyone who can help. And Sorry If its to noobish a question.
Advertisement
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
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.

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

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.


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.


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:

direction = atan2(yspeed,xspeed) / ( PI / 180 );

Thanks

Yes, it's in radians from -pi to pi.
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
new_x = x * cos(angle) - y * sin(angle);
new_y = x * sin(angle) + y * cos(angle);


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.
do you update speed at every frame?




xspeed = cosine(angle) * speed
yspeed = sine(angle) * speed




if angle is not in radioasn then cos(angle*(pi/180.0)) * speed

etc

this is a direction

direction.x = direction.x+xspeed;

This topic is closed to new replies.

Advertisement