- Home /
Question by
username707 · Jul 12, 2013 at 06:36 AM ·
positiondistanceangle
How to calculate a new position having angle and distance?
How to calculate a new position having angle and distance in two-dimentional space? Thanks!
Comment
Answer by robertbu · Jul 12, 2013 at 06:43 AM
You can use polar coordinates.
var x = dist * cos(angle * Mathf.Deg2Rad);
var y = dist * sin(angle * Mathf.Deg2Rad);
var newPosition = currentPosition;
newPosition.x += x;
newPosition.y += y;
Or solve it using a Quaternion (assumes camera is looking towards positive 'Z'). Also assumes Vector3.right is the basis for the angle:
var q = Quaternion.AngleAxis(angle, Vector3.forward);
newPosition = currentPosition + q * Vector3.right * distance;
Thanks! This also works
Vector3 movement = Vector3.zero;
movement.x = 1 * $$anonymous$$athf.Cos(dragAngle * $$anonymous$$athf.PI/180)*dragDist;
movement.z = 1 * $$anonymous$$athf.Sin(dragAngle * $$anonymous$$athf.PI/180)*dragDist;
But, if you use sin/cos then your angles run counter-clockwise from East. At some point you may need to use "game programmer" angles, which run clock-wise from North.
Sure, you can convert ( -1*(a-90)?,) but sometimes it's easier just to use 0=North angles and built-in angle functions for everything.