A
A
Andrey Vasilev2020-07-30 20:43:28
JavaScript
Andrey Vasilev, 2020-07-30 20:43:28

How can you create a vector from the main object?

Hello everyone, I have this thread:


How it is possible to start up a vector from the main object which chases the cursor? Or do something like a shot?
How can such a thing be implemented? How can you calculate the degree relative to an object?
I would be grateful for any input.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-07-30
@Nolis

It’s better to do it right away and on the canvas, the angle can be obtained using the following function in radians

//Получить угол между двумя точками
function getAngle(dx, dy, dx1, dy1) {
  return Math.atan2(dy - dy1, dx - dx1) + Math.PI;
}

The distance between two points is
///Получить растояние между двумя точками
function getDist(x, y, x1, y1) {
  return Math.hypot(x - x1, y - y1);
}

Here is a small use case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question