+ 3

How to find the angle between 2 points (Javascript?)

https://code.sololearn.com/WL76d3pZqUd5/?ref=app I want the coloured circle to move towards the target point at the shortest angle (red). So I need to find the angle between them, rotate it, then move it. How do I find the closest angle? People keep mentioning something to to with acos and acos2 whatever they are.

21st Jul 2020, 12:17 PM
Clueless Coder
Clueless Coder - avatar
10 Antworten
21st Jul 2020, 1:16 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 5
Think easy and forget all of sin cos and that nerdy stuff. u know x, y of circle and target. so u know how much x to walk and how much y. u can make a vector from it and devide.
21st Jul 2020, 2:06 PM
Oma Falk
Oma Falk - avatar
+ 2
Bahha🐧 Thanks. That's a bit what I wanted. I was hoping for it to come back once it's at the center. Do mind elaborating on the vector maths method?
21st Jul 2020, 1:20 PM
Clueless Coder
Clueless Coder - avatar
+ 1
I could not fully understand what you are trying to do. you want the colored circle(s) to move toward the red circle? what kind of movement? rotational...
21st Jul 2020, 12:41 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
you could use lerp() to move toward the red circle. for example to move balls[0] you would do something like : balls[0].pos.x = lerp(balls[0].pos.x, t.pos.x, 0.05); balls[0].pos.y = lerp(balls[0].pos.y, t.pos.y, 0.05); or you can use vector subtraction to move toward a specific point.
21st Jul 2020, 1:10 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
check this out, https://m.youtube.com/watch?v=7eBLAgT0yUs instead of mouse you can choose any point such as the center of the red circle in your code. in general that youtube channel will help you a lot if you are interested in animation with p5.js :)
21st Jul 2020, 1:32 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Bahha🐧 Thanks! I love the Coding Train! Didn't know he made a video on this!
21st Jul 2020, 1:33 PM
Clueless Coder
Clueless Coder - avatar
+ 1
Here's what I got. https://code.sololearn.com/Ws6jXrRDGJeb/#js This returns the angle with x-axis
21st Jul 2020, 1:50 PM
Arnesh
Arnesh - avatar
0
Bahha🐧 Yes, I want it to move. I've done some playing around, I want to coloured circles to go along the lines. But to do that I need to angle of which to move
21st Jul 2020, 12:46 PM
Clueless Coder
Clueless Coder - avatar
0
Oma Falk I agree, I don't want all this crazy maths. That's what I wanted to achieve, (x step and y step) but I didn't know the formular. I thought rotating would be another option
21st Jul 2020, 2:09 PM
Clueless Coder
Clueless Coder - avatar