- Home /
How do I convert 2d polar coordinates to a Vector2?
I'm working on a top-down movement script, I have the desired heading as a float in absolute degrees (0.0f should always go towards the top of the screen, 180.0f should go straight down), and likewise I have the desired speed as a float.
Is there anything in the API that would translate my heading in degrees to a Vector2, so I could apply both settings by calling something like RigidBody2.AddForce(headingAsVector * speed)?
Hmm, some screwing around with trigonometry produced what I thought was a good solution:
Vector2 DegreeToVector2(float degree)
{
return new Vector2($$anonymous$$athf.Cos(degree), $$anonymous$$athf.Sin(degree));
}
However, I think I messed something up. because this produces really weird results: 0.0f moves straight right, and 270.0f moves in nearly the identical direction. Am I missing a conversion somewhere that would establish the top of the screen as 0, or is my trig just totally off?
The trig functions use radians, so that should be $$anonymous$$athf.Cos(degree * $$anonymous$$athf.Deg2Rad)
etc.
Hmm, that gets rid of the weird behavior, but it's orienting in a strange way: I'm trying to align my degree measurements to a compass rose, so 0 is always due north (straight up on the screen), 90 is east (right), 180 is south, and 270 is west. Using radians, it's offset: 0 degrees creates a vector that moves due east (right), 90 is north, 180 is west, and 270 is south. Is there an additional step I'm missing to make sure the conversion occurs relative to the y axis?
Answer by Bonfire-Boy · Oct 01, 2016 at 03:33 PM
You were close.
First, the formula you used is for when the angle is measured anti-clockwise from the positive direction of the x axis. For your way of measuring the angle, you'd need to swap the trig functions over.
Second, the trig functions need the angle in radians.
I always like my function names to be fully descriptive so I don't accidentally misuse them. So I've also renamed it...
Vector2 DegreeToUnitVector2(float degrees)
{
float radians = degrees * Mathf.Deg2Rad;
return new Vector2(Mathf.Sin(radians), Mathf.Cos(radians));
}
Answer by Vyra · Oct 01, 2016 at 04:28 PM
Try the following:
void Update() {
// Clamp it to prevent any potential errors
heading = Mathf.Clamp(heading, 0f, 180f);
// Offset it by 90 degrees and invert it to go from 0d...180d heading to 90d...-90d angle
headingRadians = (heading - 90f) * -1 * Mathf.Deg2Rad;
headingToVector = new Vector2 (Mathf.Cos (headingRadians),
Mathf.Sin (headingRadians));
myRigidBody2D.AddForce (headingToVector * speed);
}
Your answer
Follow this Question
Related Questions
world to screen coordinates conversion 1 Answer
Convert screenspace coordinates (top-left origin) to worldspace coordinates. Read before you react! 1 Answer
Convert Canvas Space point to local space of a rotated rect? 2 Answers
Find cardinal direction of collision based on 2D Cartesian coordinates 1 Answer
Converting direction to Vector2 2 Answers