A
A
Anatoly2015-07-29 00:38:35
Mathematics
Anatoly, 2015-07-29 00:38:35

What is the formula for rotating an object onto another object in 2D?

Hello! Please tell me the formula by which you can calculate the angle (in radians) of an object's rotation so that it looks at another object. Something can not be explained in words, so I attached a picture.
0c927b10096d465886e203bf567a4200.png
You just need to turn the blue figure to red.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TomasHuk, 2015-07-29
@TomasHuk

1 According to the formula , we are looking for the equation of a straight line that passes through two points.
2 We bring the found equation to the equation of a straight line with the angular coefficient .
3 Through the slope in the equation of a straight line (equal to the tangent) we find the angle.
4 Rotate the blue figure by an angle equal to the difference between the found angle and the current angle of the figure.

X
xmoonlight, 2015-07-29
@xmoonlight

Берем 3 точки (P) на плоскости с координатами (X,Y).
Пример кода на Pascal/Delphi:

type TPoint = record
  X: Longint;
  Y: Longint;
end;

function GetUgol(P1, P2, P3: TPoint):Integer;
var
  M1, M2, SM, CosUgol: Single; //Модули А1 и А2, их скалярное произведение и косинус угла
begin
  M1 := Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y));
  M2 := Sqrt(Sqr(P3.X - P1.X) + Sqr(P3.Y - P1.Y));
  SM := (P2.X - P1.X) * (P3.X - P1.X) + (P2.Y - P1.Y) * (P3.Y - P1.Y);
  CosUgol := SM / (M1 * M2);
  GetUgol := Round(ArcCos(CosUgol) * 180);
end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question