Jump to content
  • Advertisement
Sign in to follow this  
wriordan

get direction

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:

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.

Share this post


Link to post
Share on other sites
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

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

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

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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!