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

2017/6/7

Ball bouncing on platform help!

EthanLettuce EthanLettuce

2017/6/7

#
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;
    }
danpost danpost

2017/6/7

#
The rotation of a ball has nothing to do with the direction the ball travels. So turning upon collision or setting its initial rotation should not influence the movement of the ball. Your collision method has yet to be executed as it still needs to be called from somewhere. That being said, it is no wonder that you are having trouble with the bouncing as the value of 'isFall' is never changed (nowhere is it being set back to true and, since the collision method is not currently executing, it is never set to false). All your actor will do is fall.
You need to login to post a reply.