+ 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.
10 Answers
+ 2
just for fun try this:
https://code.sololearn.com/WAJV9a2MyBey/?ref=app
+ 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.
+ 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?
+ 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...
+ 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.
+ 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 :)
+ 1
Bahhaš§ Thanks! I love the Coding Train! Didn't know he made a video on this!
+ 1
Here's what I got.
https://code.sololearn.com/Ws6jXrRDGJeb/#js
This returns the angle with x-axis
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
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