Help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

14th Apr 2021, 7:53 AM
Alexander Sokolov
Alexander Sokolov - avatar
10 Answers
+ 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.
14th Apr 2021, 7:01 PM
Schindlabua
Schindlabua - avatar
+ 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();
14th Apr 2021, 6:44 PM
Schindlabua
Schindlabua - avatar
+ 3
Schindlabua I even subscribed to you, I am very grateful to you.
14th Apr 2021, 6:45 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 2
Schindlabua thanks bro, I really thought no one would help me.
14th Apr 2021, 6:45 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 2
Schindlabua could you show me how it works, what you wrote using my code as an example ... 🙏
14th Apr 2021, 6:52 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
What... How we can support you if you dont tell us the problem..
14th Apr 2021, 10:45 AM
Mohibul Alam
Mohibul Alam - avatar
+ 1
cheers ;)
14th Apr 2021, 6:48 PM
Schindlabua
Schindlabua - avatar
+ 1
Пщс оаз спасибо
14th Apr 2021, 6:49 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
the coding train has an amazing video on polar coordinates if you want to learn about it: https://youtu.be/O5wjXoFrau4
16th Apr 2021, 5:18 AM
Bot
Bot - avatar
0
What... I did not understand you, I wrote about my problem, see my question.
14th Apr 2021, 10:46 AM
Alexander Sokolov
Alexander Sokolov - avatar