+ 3
Help me
how to make a blue cube rotate exactly in the direction in which it moves https://code.sololearn.com/WV1Egi6ryQ3B/?ref=app
10 ответов
+ 6
https://code.sololearn.com/W4B0tfQeA8kp/?ref=app
Just saying there are other things in your code that should probably be fixed but this does what you want :).
I don't know how good your maths is, but you might remember the term "polar coordinates" - we are translating from cartesian to polar and using the polar angle to orient the cube.
+ 3
You mean, such that one side is always facing the mouse pointer/finger?
let [vx, vy] = the cube's speed
then
const phi = Math.atan2(vy, vx);
Any you want to rotate your cube by phi. Now depending on how you have oriented your y axis you may need to flip the sign of vy. (vy = -vy)
You also want to set the "rotation point" at the center of the cube.
let [px, py] = the cube's position
Then, to draw the cube:
ctx.save();
ctx.translate(px, py);
ctx.rotate(phi);
ctx.fillRect(-width/2, -height/2, width, height);
ctx.restore();
+ 3
Schindlabua I even subscribed to you, I am very grateful to you.
+ 2
Schindlabua thanks bro, I really thought no one would help me.
+ 2
Schindlabua could you show me how it works, what you wrote using my code as an example ... 🙏
+ 1
What... How we can support you if you dont tell us the problem..
+ 1
cheers ;)
+ 1
Пщс оаз спасибо
+ 1
the coding train has an amazing video on polar coordinates if you want to learn about it:
https://youtu.be/O5wjXoFrau4
0
What... I did not understand you, I wrote about my problem, see my question.