Hello. I am having a problem with bouncing. When the MainBall contacts the Ball, the MainBall is supposed to bounce off and the Ball is supposed to disappear. At the moment, the bounce does not occur and the only thing that happens is the ball disappearing.
Here is the code for MainBall:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MainBall extends SmoothMover
{
private static final int RADIUS = 12;
//Colliding balls and gravity. I can make a game where the ball breaks blocks.
//Fill up a water container, the paddle hits balls.
//More than one level.
//Make a pinball game.
//Round objects bouncing off round objects.
//Make powerups.
//If bullets is more than 0, I can shoot.
//REMEMBER TO ADD GRAVITY.
public MainBall()
{
int angle = Greenfoot.getRandomNumber(360);
double velocity = Greenfoot.getRandomNumber(100) / 10.0 + 10.0;
addForce(new Vector(angle, velocity));
}
public void act()
{
move();
accelerate(0.99);
bounceOffWall();
bounceOffBalls();
bounceOffPaddle();
}
public void bounceOffBalls()
{
Ball ball = (Ball)getOneIntersectingObject(Ball.class);
if(ball != null){
if(touching(ball)){
doCollision(ball);
}
}
getWorld().removeObject(ball);
}
public void bounceOffPaddle()
{
Paddle paddle = (Paddle)getOneIntersectingObject(Paddle.class);
if(paddle != null){
if(touching(paddle)){
doCollision(paddle);
}
}
}
public boolean touching(SmoothMover other)
{
double dx = other.getExactX()-this.getExactX();
double dy = other.getExactY()-this.getExactY();
return Math.sqrt(dx*dx+dy*dy)<=RADIUS*2;
}
public void doCollision(SmoothMover other)
{
double dx = other.getExactX()-this.getExactX();
double dy = other.getExactY()-this.getExactY();
int angle = (int) Math.toDegrees(Math.atan2(dy,dx));
double force = Math. abs(this.getSpeed()-other.getSpeed());
other.addForce(new Vector(angle,force));
this.addForce(new Vector(angle +180, force));
}
public void bounceOffWall()
{
if(getExactX() <= RADIUS || getExactX() >= getWorld().getWidth()-RADIUS){
getMovement().revertHorizontal();
move();
}
if(getExactY() <= RADIUS || getExactY() >= getWorld().getHeight()-RADIUS){
getMovement().revertVertical();
move();
}
}
}


