import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { private double h0 = 1; private double h = h0; private double tStart; private double xStart; private double yStart; private double g = 9.82; private double SCALE = 3; private double v0y; private double v0x; private boolean first = true; private boolean atEdge; private int Wind = Greenfoot.getRandomNumber(100)+1; /** * v0x = v0 * cos(a) * v0y = v0 * sin(a) * x= v0 * t * cos(a) = v0x * t * y = v0 * t * sin(a) - 1/2gt^2 = v0y *t - 1/2gt^2 + h0 * */ /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Ball(int angle, int power) { v0y = power * Math.sin(Math.toRadians(angle)); v0x = power * Math.cos(Math.toRadians(angle)); setRotation(-angle); } public void act() { move(); atEdge(); } public void move() { if(first) { first = !first; tStart = System.currentTimeMillis(); xStart = getX(); yStart = getY(); } else { double t = (System.currentTimeMillis()- tStart)/ 100; h = h0 + (v0y * t) - (g*t*t)/2; int xS = (int)(xStart + SCALE * (v0x * t)-Wind); int yS = (int)(yStart - SCALE * h); setLocation(xS, yS); } } public boolean atEdge() { World ground = getWorld(); if( getX() == ground.getWidth()- 1) { ground.removeObject(this); return true; } if( getY() == ground.getHeight()- 1) { ground.removeObject(this); return true; } else { return false; } } public void addedToWorld(World world) { move(130); } }

