How do i reverse the trajectory of the 2d box inside the canvas using JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i reverse the trajectory of the 2d box inside the canvas using JavaScript?

Im trying to make it so when collision is detected it reverses direction. I try to google it and found that most of the tutorials are doing something completely different (using different methods that dont work for me). https://code.sololearn.com/W3V5evcI8Eux/?ref=app

2nd Jun 2022, 6:19 PM
pHANTOM|sTEELE
1 Answer
+ 1
3 things on the collision detector: 1. It only checks if x or y is the exact value to hit the border. But their increments are usually higher than 1, so it's x and y hardly ever have this value. Prefer to check if it hits or passes. 2. It only checks bottom and right borders. It should check all of them. 3. When it detects collision, there's no code to change direction. You need to store x and y directions, then use them to control motion and change them in collisions.
2nd Jun 2022, 10:55 PM
Emerson Prado
Emerson Prado - avatar