package Objekts; import java.awt.Color; import java.awt.Graphics2D; import Math.MathCalc; import Math.Vektor2D; public class Ball extends Object{ double speed = 1; double mass = 1; double radius = 1; double size = 2 * radius; Vektor2D velocity = new Vektor2D(); Vektor2D position = new Vektor2D(); Vektor2D acceleration = new Vektor2D(); public Ball(Vektor2D position, Vektor2D velocity, double speed, double mass, double radius) { this.position = position; this.velocity = velocity; this.speed = speed; this.mass = mass; this.radius = radius; size = 2 * radius; // set the position to the center this.position.setX(position.x() ); this.position.setY(position.y() ); // given is a direktion, normalize it and give it the speed this.velocity = this.velocity.unitVektor().mulWith(speed); } // get / set Methoden public double speed() { return this.speed; } public double mass() { return this.mass ; } public double radius(){ return this.radius;} public double size() { return this.size; } public Vektor2D position(){ return this.position; } public Vektor2D velocity(){ return this.velocity; } public void setVelocity(Vektor2D velocity) { this.velocity = velocity; } public void setSpeed(double speed) { this.speed = speed;} public void setMass(double mass) { this.mass = mass; } public void setRadius(double radius){ this.radius = radius; } public void setSize(double size){ this.size = size; setRadius(size/2); } // Draw and Move Methods public boolean collides(Ball b){ double dist = Math.sqrt( Math.pow( (b.position.x() - position.x()), 2 ) + Math.pow( (b.position.y() - position.y()), 2)) ; dist = dist - (b.radius + radius); if(dist <= 0) return true; else return false; } public void drawBody(Graphics2D g2d, Color color){ g2d.setColor(color); g2d.fillOval((int)position.x() - (int)radius, (int)position.y() - (int)radius, (int)size, (int)size); //this.drawCenter(g2d, Color.RED); } public void drawCenter(Graphics2D g2d, Color color){ g2d.setColor(color); g2d.drawLine((int)position.x() - 2, (int)position.y(), (int)position.x() + 2, (int)position.y()); g2d.drawLine((int)position.x(), (int)position.y() - 2, (int)position.x(), (int)position.y() + 2); } public void bounceWall(int Screen_W, int Screen_H){ if(position.x() - radius() <= 0 || position.x() + radius() >= Screen_W + 10) velocity.setX(velocity.x() * (-1)); if(position.y() - radius() <= 0 || position.y() + radius() >= Screen_H - 20) velocity.setY(velocity.y() * (-1)); } public void move(){ // add acceleration to velocity velocity.setX(velocity.x() + acceleration.x()); velocity.setY(velocity.y() + acceleration.y()); position.setX(position.x() + velocity.x()); position.setY(position.y() + velocity.y()); // maybe it is important to correct the position by using MathCalc position.setX((new MathCalc()).roundDouble(position.x(), 13)); position.setY((new MathCalc()).roundDouble(position.y(), 13)); } }