I'm trying to make a game where a ball will fall with gravity, and will bounce off of the platforms in its way. I'm pretty sure I can figure out the x component with the different angles, but I'm having issues getting the ball to bounce off of the platform. I have it checking when the ball collides with the platform, but then I want it to stop, and move it in the opposite direction with less speed. Please help me anyone, especially Danpost.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class Ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends SmoothMover
{
/**
* Act - do whatever the Ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public double vspeed = 1.0;
public double accel = 0.5;
public boolean isFall = true;
public double direction = 0;
public Ball(int width, int height, Color color)
{
GreenfootImage ball = new GreenfootImage(width, height);
ball.setColor(color);
ball.fillOval(0, 0, width, height);
setImage(ball);
turn(90);
}
public void act()
{
direction = getRotation();
if(!isFall)
vspeed -= vspeed - (vspeed/2);
else if(isFall)
falling();
}
public void falling(){
vspeed = vspeed + accel;
setLocation(getX(), getY() + (int) vspeed);
}
public boolean collision(){
if(isTouching(Platform.class)){
isFall = false;
turn(180);
return true;
}
return false;
}

