Jump to content
  • Advertisement
Sign in to follow this  
RojanShrestha

Making a player change direction!

This topic is 2529 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

I have a simple problem... I am working on the movement of player for a game(top down 2d view)... Basically the player is a sprite filled with other physics such as mass and velocity. Now, I can move the player all over the screen but I want to use angles to make the sprite face in the direction the player is moving. I am having problem on using the concepts of angles for this purpose... Can anyone help??

Share this post


Link to post
Share on other sites
Advertisement
I assume you can already rotate the player? Im not sure if either of these will work, but:

1. Use atan2() to get the angle from the direction the player is heading. Takes quadrants into account, so probably the best option
2. Use dot(default player direction,direction moving) to get the cosine of the angle between them, then use acos on that to get an angle, and rotate the sprite that amount

Share this post


Link to post
Share on other sites
That's not very much information, but your right, all in all, that is not a difficult problem. You'll want to use sine or cosine for this. If your using C++, you will be using cos(a) or sin(a), where a is an angle in radians, 6.28 being 360 degrees, and sine or cosine will return a value between 0 and 1 I believe (i drank too much, so it could possibly return a value between -1 and 1, but i'm pretty sure it returns a value between 0 and 1...)

so if the sprite is facing north, or up on the screen, and you press the right key, and while they have that right key pressed, you want the sprite to rotate slightly clockwise, you could pass cos(0.15) or something every frame (You will most definitely change this value, it was only an example). Could you possibly give more information about the api your using, like what the function that your using to actually rotate the sprite is? maybe even the language your using so we could help out with the actual commands.

Share this post


Link to post
Share on other sites
What library are you using? Most libraries now support rotation, scaling, and transformation. You can look it up in your library documentation. For bonus learning, here's the WIKI page of what's going on under the hood.

Share this post


Link to post
Share on other sites
Ok!!! So what I really want to happen is:
Let's say a player is in a position (x,y), now when one click on some part of the screen, I want the player to face in that direction and move to it. How would I do that?? I am using C++ with SFML library so yes I can already rotate the player.


2. Use dot(default player direction,direction moving) to get the cosine of the angle between them, then use acos on that to get an angle, and rotate the sprite that amount


Could you elaborate what default player condition is in this case

Share this post


Link to post
Share on other sites
differenceX = targetPosition.x - currentPosition.x;
differenceY = targetPosition.y - currentPosition.y;

differenceAngle = Math.atan2(differenceY, differenceX); // in radians

velocity.x = Math.cos(differenceAngle) * 100; // 100 is the speed you want to move towards the target point
velocity.y = Math.sin(differenceAngle) * 100;

Share this post


Link to post
Share on other sites

When you don't rotate the sprite, what direction is it facing?


By default my sprite is facing north!!


[color=#1C2837][size=2][color=#000000]differenceX [color=#666600]=[color=#000000] targetPosition[color=#666600].[color=#000000]x [color=#666600]-[color=#000000] currentPosition[color=#666600].[color=#000000]x[color=#666600];[color=#000000]
differenceY [color=#666600]=[color=#000000] targetPosition[color=#666600].[color=#000000]y [color=#666600]-[color=#000000] currentPosition[color=#666600].[color=#000000]y[color=#666600];[color=#000000]

differenceAngle [color=#666600]=[color=#000000] [color=#660066]Math[color=#666600].[color=#000000]atan2[color=#666600]([color=#000000]differenceY[color=#666600],[color=#000000] differenceX[color=#666600]);[color=#000000] [color=#880000]// in radians[color=#000000]

velocity[color=#666600].[color=#000000]x [color=#666600]=[color=#000000] [color=#660066]Math[color=#666600].[color=#000000]cos[color=#666600]([color=#000000]differenceAngle[color=#666600])[color=#000000] [color=#666600]*[color=#000000] [color=#006666]100[color=#666600];[color=#000000] [color=#880000]// 100 is the speed you want to move towards the target point[color=#000000]
velocity[color=#666600].[color=#000000]y [color=#666600]=[color=#000000] [color=#660066]Math[color=#666600].[color=#000000]sin[color=#666600]([color=#000000]differenceAngle[color=#666600])[color=#000000] [color=#666600]*[color=#000000] [color=#006666]100
[/quote]

How will this rotate the player so that if faces the direction??

Share this post


Link to post
Share on other sites
Ok, so north is the default direction. Whatever your velocity is when you go north is your default direction. Probably either 0,-1 or 0,1, depending on your coordinate system

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!