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

2020/11/8

Edge acts like a mirror

Rottlo Rottlo

2020/11/8

#
how do i let the edge act like a mirror? like if the actor hits it with 30° it turns 30° and so on?
Super_Hippo Super_Hippo

2020/11/8

#
Split the movement of your actor in the horizontal and vertical axes. Then, when checking for an intersection with a wall, you also check both directions separately. When it happens while moving horizontally, you reverse the horizontal movement (multiplying by -1) and when it happens while moving vertically, you reverse the vertical movement.
Rottlo Rottlo

2020/11/9

#
thank you for your answer. i am very new to greenfoot, could you sent a screen of the code? would be awesome :3
danpost danpost

2020/11/9

#
Rottlo wrote...
thank you for your answer. i am very new to greenfoot, could you sent a screen of the code? would be awesome :3
The code would highly depend on how your actor moves. Show current code of actor you want to bounce off edges. If you are not too worried about the code itself and just want to get that behavior working, I have a QActor class ("Q" for "quality") you can extend that would do the trick. If you move by rotation, you would use something like the following:
// in constructor
setBoundedAction(QActor.BOUNCE, -25);
setQRotation(Greenfoot.getRandomNumber(360*QVAL));

// in act
move(6*QVAL);
-- if the actor is to remain upright (no rotating of actor), then something like this:
// in constructor
setBoundedAction(QActor.BOUNCE, -25);
addForce(5*QVAL, Greenfoot.getRandomNumber(360*QVAL));

// in act
move();
The QActor class can be found in my Asteroids w/Improved QActor class scenario.
Rottlo Rottlo

2020/11/10

#
really really thak you! I was pretty lost but you helped me out. THANKS!
You need to login to post a reply.