+ 4

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 https://code.sololearn.com/WrXGkNW6aSdl/?ref=app

29th Jan 2019, 3:15 PM
🇵🇰 Danish Khan 🇵🇰
🇵🇰 Danish Khan 🇵🇰 - avatar
2 Antworten
+ 3
No, I want it to stop at hitting boundaries, but still able to move away from boundaries.
30th Jan 2019, 1:11 PM
🇵🇰 Danish Khan 🇵🇰
🇵🇰 Danish Khan 🇵🇰 - avatar
0
Do you want to appear on the opposite side of the rectangle ?
30th Jan 2019, 10:31 AM
zemiak