Move within boundaries? | SoloLearn: Learn to code for FREE!


Move within boundaries?

If I have a box of 100x100 steps Top left = (0,0) Bottom right = (100,100) Each step = 10 Direction = 0 to 2*PI When ever some one commands movement dx = step * sin(Direction) dy = step * cos(Direction) if(x>0 && x<100 && y>0 && y<100){ if(forward){ x+=dx; y+=dy; }else{ x-=dx; y-=dy; } } PROBLEM: Logic works ok for movement as per present direction until hitting boundaries, but then gets stuck at boundaries, any one with some solution? Here is how I am using it

1/29/2019 3:15:46 PM

🇵🇰 Danish Khan 🇵🇰

2 Answers

New Answer


No, I want it to stop at hitting boundaries, but still able to move away from boundaries.


Do you want to appear on the opposite side of the rectangle ?