How to make a semi-circular movement?

Viewed 233

Suppose I have a car in position P0 and I want to move it to position P1, just like these 4 examples:

enter image description here

The linear distance between P0 and P1 is d and the perpendicular maximum height the movement reaches is d/3. I want to simulate this clockwise semi-circular movement from P0 to P1.

Suppose dir = P1 - P0 (length d) and perp is the vector (of length d/3) perpendicular to dir.

Suppose t = 0 is the beginning of the semi-circular movement and t = 1 is the end, how can I measure the angle and the position of the car at t = i?

1 Answers

We have to find angle of this arc and circle center.

At first find radius.

R^2 = (d/2)^2 + (R-d/3)^2  //pythagorean
R = 13/24 * d

Now angle

half_angle = arcsin(12/13) ~ 67.4 degrees 
angle = 2 * half_angle ~ 135 degrees = 2.35 radians

Normalize perp vector

uperp = perp / len(perp)

Get circle center

M = (P0 + P1)/2   //chord middle
C = M + uperp * 5/24 * d

Starting angle

A0 = atan2(P0.Y-C.Y, P0.X-C.X)

And finally coordinates

Car.X = C.X + R * Cos(A0 + t * angle)
Car.Y = C.Y + R * Sin(A0 + t * angle)

In Unity this would look like:

Vector2 startPosition;
Vector2 endPosition;
Vector2 perp;
float t;

float d = (endPosition - startPosition).magnitude;

float radius = 13f/24f * d;
float angle = 2f * Mathf.Asin(12f/13f);

Vector2 uperp = perp.normalized;

Vector2 M = (startPosition+endPosition)*0.5f;
Vector2 C = M + uperp * 5f/24f * d;

float A0 = Mathf.Atan2(startPosition.y-C.y, startPosition.x-C.x);
float At = A0 + t * angle;

Vector2 newPos = C + radius * new Vector2(Mathf.Cos(At), Mathf.Sin(At));

enter image description here

Related