Skip to content
Advertisement

Circular movement when user input left/right arrow

I am trying to achieve left/right circular movement inside my main circle. I have class Circle and Ball. Circle is responsible for drawing a “circle” where I am placing my Ball, that is moving inside.

JavaScript

For now I am inputing left right arrow press and i do want my ball to change direction based on arrow press. I was playing with

JavaScript

where * -2 should revert movement, but it is not working as I thought it will Here is codesandobx I do get that this can be a bit more complicated than I anticipated

Advertisement

Answer

Usually you make something change direction by modifying its speed. Here I change the value of this.vx to be either positive or negative.

I also changed the calculations for ball position slightly, I just used numbers like 75 and 66 that seemed to be approximately correct, but you might need to change those for your needs.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement