- Home /
Find distance around a circle, from center point and another point
I am having trouble creating a circle in 3d space, and fining a point along it. So if I have a circle with center point C, and a point on the circle R, would it be possible to find a point a specific distance along the circle from R? I don't necessarily need the code to do it, but just assistance on the math.
Answer by robertbu · Nov 10, 2013 at 06:26 PM
There are an infinite number of circles that have a center and a point in common, so you need to specify one specific circle. The code below uses the axis (normal) to the circle. The polarity of the axis will determine which direction to move along the circle. It solves your problem by creating a vector between the center and the point. It then calculates the angle by using the fraction of the circumference the distance travels. An AngleAxis() rotation generates the point. The code is untested:
function FindPoint(center : Vector3, r : Vector3, dist : float, axis : Vector3) : Vector3 {
var v3 = r - center;
var circumference = 2.0 * Mathf.PI * v3.magnitude;
var angle = dist / circumference * 360.0;
v3 = Quaternion.AngleAxis(angle, axis) * v3;
return v3 + center;
}
Answer by shieldgenerator7 · Dec 06, 2020 at 10:08 AM
Here's a modified version of @robertbu 's answer:
public static Vector2 travelAlongCircle(this Vector2 pos, Vector2 center, float distance)
{
Vector3 axis = Vector3.back;
Vector2 dir = pos - center;
float circumference = 2.0f * Mathf.PI * dir.magnitude;
float angle = distance / circumference * 360.0f;
dir = Quaternion.AngleAxis(angle, axis) * dir;
return dir + center;
}
You can call it like this:
Vector2 position = transform.position;
Vector2 center = Vector2.zero;
float distance = 10;
transform.position = position.travelAlongCircle(center, distance);
This version is handy because it is an extension method of the Vector2
class.
Your answer
Follow this Question
Related Questions
Maths: Given a point on a circle and a fixed length line. Find the next closest point on the circle. 2 Answers
How would you convert circular joystick input to a square overlay? 1 Answer
Wheel rotation, what am I doing wrong? 1 Answer
Limit Mouse Look to Circle 1 Answer
object follow mouse in radius 1 Answer