I dont understand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I dont understand

Why is the blue cube immediately placed in the red cube position? I can't understand it... According to the idea, it should move smoothly, according to the variables dx, dy, which are responsible for the speed of the cubes. https://code.sololearn.com/WtGC0dkn9lLw/?ref=app

7th Apr 2021, 7:54 AM
Alexander Sokolov
Alexander Sokolov - avatar
10 Answers
+ 4
The blue cube doesn't move at all so it's hard to say what the problem is. Anyway, this is how I would implement a following mechanic: const dx = red.x - blue.x const dy = red.y - blue.y const distance = Math.sqrt(dx * dx + dy * dy) blue.x += dx/distance * speed blue.y += dy/distance * speed Where `speed` is a constant that defines how fast the blue cube is moving.
7th Apr 2021, 11:08 AM
Schindlabua
Schindlabua - avatar
+ 4
What do you mean by "strictly in the direction of"? You mean, restricted to the 4 major directions? In that case you can simply `Math.round(dx/distance)` and that should work. If the cube is moving too fast, turn down the speed. :P
7th Apr 2021, 1:43 PM
Schindlabua
Schindlabua - avatar
+ 3
First and for most your code just throws a million alerts at me when i try to run it(could be my phone configuration) and i can't see how it's actually behaving. Secondly Comments.... Please youre asking for advise at least give me an easy start with commenting on your code. Any how... You could/ maybe should use classes where you can define your behavior. Which will give your code a much better structure and maintenability. I've got a very similar project on codepen https://codepen.io/jacklanger/pen/PoGavQR Have a look for your positional adjustment/update method it might help you to figure out how to get the result you want. If you need more specific input please clean your code and comment on it, as for me it doesn't look like a piece of work I'd like to read through P. S. you need to get the angle between the red cube and the blue cube and reset the cube position along the angle by the speed/ adjustment to achive that you will have to call setInterval on a fixed framerate. and call update in there
8th Apr 2021, 11:46 AM
jack
jack - avatar
+ 2
Alexander Sokolov Why there is 300-20 in object2? Just put 20 and see effect.
7th Apr 2021, 9:50 AM
A͢J
A͢J - avatar
0
Josh Greig No, you don't seem to understand me. I want the Blue cube to follow the red one. But you have made them both move, and in an incomprehensible direction. Have you ever seen this code? If so, look again to see what I'm asking for in my task. https://code.sololearn.com/WtGC0dkn9lLw/?ref=app
7th Apr 2021, 8:46 AM
Alexander Sokolov
Alexander Sokolov - avatar
0
🅰🅹 (Challenge Accepted) This has nothing to do with my problem at all, believe me, man.
7th Apr 2021, 10:02 AM
Alexander Sokolov
Alexander Sokolov - avatar
0
Schindlabua I would like the blue cube to move strictly in the direction of the red cube I have already done the mechanics of moving the cube, but it moved very quickly.
7th Apr 2021, 11:18 AM
Alexander Sokolov
Alexander Sokolov - avatar
0
Great discussion. Loved the code-pen project too! When I click "run" to see what this code is doing, each time the movements I'm seeing are very random (in terms of direction of movement as well as speed of movement). The first time was TOO quick for my old-eyes to see at all--the two become one object quickly. The next time I ran the code, it moved slowly. The third time it randomly went to the top and moved slowly. The fourth time it randomly moved towards the left and then up slowly. . . an so on. . .
11th Apr 2021, 7:45 PM
EO4Wellness
EO4Wellness - avatar
0
What currently happens is that the blue block is moving in a 45deg while its x or y position is not the same as the red blocks to get it moving in a streight line ( strictly in the red blocks direction) you need to get the angle between the to and adjust the position acording to the angle. Hope this helps.
11th Apr 2021, 8:17 PM
jack
jack - avatar
- 3
Josh Greig if you can't solve my problem just say so. I don't will be swear at you.
7th Apr 2021, 8:31 AM
Alexander Sokolov
Alexander Sokolov - avatar