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

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 on other sites
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 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 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 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 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 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 on other sites
Here is an idea Find The Angle Between Two Vectors
Consider your player is looking at Q point and you want him to look at M point , this will give you the Angle between the two vectors , so you can rotate the player to face M

• Game Developer Survey We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12