/* * Ball.java * * Created on July 9, 2004, 11:38 AM */ /** * * @author levenick */ import java.awt.*; public class Molecule extends FilledCircle { private int HT=MoleculeFrame.HT; private int WIDTH=MoleculeFrame.WIDTH; private double ELASTICITY=1.0; public static int GRAVITY=1; private int vx, vy; //private int nuX=100, nuY=100; /** Creates a new instance of Ball */ public Molecule() { } public Molecule(int x, int y, int r, Color c) { super(x,y,r,c); } public Molecule(int x, int y, int r, Color c, int vx, int vy) { this(x,y,r,c); this.vx = vx; this.vy = vy; } private boolean willHitWall() { return willHitSide() || willHitTopOrBottom(); } private boolean willHitTopOrBottom() { if (vy > 0) { return vy >= distanceToBottom(); } else { return -vy >= distanceToTop(); } } private int distanceToBottom() { return HT -y -radius; } private int distanceToTop() { return y-radius; } private int distanceToRightWall() { return WIDTH - (x+radius); } private int distanceToLeftWall() { return x-radius; } private boolean willHitSide() { if (vx > 0) { return vx >= distanceToRightWall(); } else { return -vx >= distanceToLeftWall(); } } public void step() { //System.out.println("step: y=" + y + " vy=" + vy + " r=" + radius); if (!willHitWall()) { x += vx; y += vy; if (distanceToBottom() > 0) vy += GRAVITY; } else bounce(); } private void bounce() { if (willHitTopOrBottom()) handleYBounce(); if (willHitSide()) handleXBounce(); } private void handleYBounce() { if (vy >= distanceToBottom()) { y = HT - (vy - distanceToBottom()) - radius; // the y-coord after this step vy = (int)(- (vy-1)*ELASTICITY); } else { y = -vy -(distanceToTop() - radius) + radius; vy = (int)(-vy * ELASTICITY); } } private void handleXBounce() { if (vx > 0) x = WIDTH - (vx - (WIDTH - x - radius)) - radius; else x = -vx - (x-radius) + radius; vx = (int)(-vx * ELASTICITY); } }