This site requires JavaScript, please enable it in your browser!
Greenfoot back
S77
S77 wrote ...

2017/4/8

AI bouncing off walls!

S77 S77

2017/4/8

#
public void act() { move(-2); if(isAtEdge()) { setRotation(180); } } i've got this tiny piece of code here and it works but not how i want it to,every time it bounces off a wall for the 2nd time is stops, i was wondering if anyone could give me an idea or help?
Super_Hippo Super_Hippo

2017/4/8

#
Maybe you need 'turn(180)' instead of 'setRotation'.
S77 S77

2017/4/8

#
oh nice thanks!!! a lot.
danpost danpost

2017/4/8

#
Super_Hippo wrote...
Maybe you need 'turn(180)' instead of 'setRotation'.
This would have the actor bounce between two walls, hitting each at the same point every time. You can vary the angle by adding a random turn:
turn(135+Greenfoot.getRandomNumber(90));
// or
turn(150+Greenfoot.getRandomNumber(60));
You can even have it bounce like a normal bounce by adjusting the direction depending on what wall is being hit (a vertical one or a horizontal one).
S77 S77

2017/4/8

#
thanks ill try that aswell!
You need to login to post a reply.