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.
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;
}
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.