diff --git a/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class b/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class index 074d286..9551f9e 100644 Binary files a/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class and b/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class differ diff --git a/test_Java_Project/bin/window/bouncing/ball/Shape.class b/test_Java_Project/bin/window/bouncing/ball/Shape.class index 610cf46..0edcbf6 100644 Binary files a/test_Java_Project/bin/window/bouncing/ball/Shape.class and b/test_Java_Project/bin/window/bouncing/ball/Shape.class differ diff --git a/test_Java_Project/src/window/bouncing/ball/BouncingBallWindow.java b/test_Java_Project/src/window/bouncing/ball/BouncingBallWindow.java index 3707249..5876809 100644 --- a/test_Java_Project/src/window/bouncing/ball/BouncingBallWindow.java +++ b/test_Java_Project/src/window/bouncing/ball/BouncingBallWindow.java @@ -7,6 +7,7 @@ public class BouncingBallWindow { private JFrame frame; private ImageIcon img; + //private double frameHeightChangeSpeed; public BouncingBallWindow() { frame = new JFrame("Bouncing Ball"); frame.setSize(400, 300); @@ -15,18 +16,23 @@ public BouncingBallWindow() { img = new ImageIcon("C:\\Users\\wmari\\Downloads\\red_ball_image.png"); frame.setIconImage(img.getImage()); - Shape panel = new Shape(frame); + int x = frame.getLocation().x; + int y = frame.getLocation().y; + Shape panel = new Shape(frame, x, y); frame.add(panel); frame.setVisible(true); while(true) { try { - TimeUnit.MILLISECONDS.sleep(2); + TimeUnit.MILLISECONDS.sleep(16); } catch (InterruptedException e) { e.printStackTrace(); } + //System.out.println(frame.getSize().height); + if(frame.getSize().height < 130) { + frame.setSize(frame.getSize().width, 130); + } panel.move(frame); } - } } diff --git a/test_Java_Project/src/window/bouncing/ball/Shape.java b/test_Java_Project/src/window/bouncing/ball/Shape.java index 81db245..1efa240 100644 --- a/test_Java_Project/src/window/bouncing/ball/Shape.java +++ b/test_Java_Project/src/window/bouncing/ball/Shape.java @@ -11,12 +11,19 @@ public class Shape extends JPanel { //private boolean fall; private long timeStartFall; private double inelasticCollisionPercent; + private int lastHeight; + private int lastWidth; + private double mass; + private int centeredX; + private int centeredY; + private int currentX; + private int currentY; //private int width; //private int height; //private Color c; - public Shape(JFrame frame) { + public Shape(JFrame frame, int x, int y) { super(); if(frame != null) { Dimension size = frame.getSize(); @@ -26,6 +33,7 @@ public Shape(JFrame frame) { this.x = 150; this.y = 100; } + mass = 1.7; //frame = frame != null ? : ; velocity[0] = 0; velocity[1] = 0; @@ -33,6 +41,10 @@ public Shape(JFrame frame) { timeStartFall = System.currentTimeMillis(); //this.fall = true; this.inelasticCollisionPercent = 0.8; + centeredX = x; + centeredY = y; + currentX = x; + currentY = y; //this.width = width; //this.height = height; //this.c = c; @@ -40,17 +52,59 @@ public Shape(JFrame frame) { public void move(JFrame frame) { Dimension size = frame.getSize(); - - velocity[1] += (System.currentTimeMillis() - timeStartFall)/1000.0 * 9.8; + if(frame.getLocation().x != centeredX && frame.getLocation().x != currentX) { + x += currentX - frame.getLocation().x; + currentX = frame.getLocation().x; + } else if (frame.getLocation().y != centeredY && frame.getLocation().y != currentY) { + y += currentY - frame.getLocation().y; + currentY = frame.getLocation().y; + } + + /* + * double variable drag + * density of air affected by temperature, pressure, and specific gas constant (287.26 joules per kilogram per kelvin for air); It can + * also be effected by water vapor + * density could be 1.225 for 15 C and pressure unknown, 1.293 for 273 K (-0.15 C) at pressure 101.325 + * drag = 1/2 * density (of air) * v squared * drag coefficient (greater = more drag, less = less drag) * area + */ + double dragX = 0.5 * 1.225 * Math.pow(velocity[0] * ((System.currentTimeMillis() - timeStartFall)/1000.0), 2) * 0.47; + double dragY = 0.5 * 1.225 * Math.pow(velocity[1] * ((System.currentTimeMillis() - timeStartFall)/1000.0), 2) * 0.47; + + + //System.out.println(dragX + " " + velocity[0]); + if(velocity[0] <= 0) { + velocity[0] -= dragX; + } else { + velocity[0] += dragX; + } + velocity[1] += ((System.currentTimeMillis() - timeStartFall)/1000.0 * 9.8 * mass - dragY)/mass; + //System.out.println("Drag: " + drag); + //System.out.println("Gravity: " + (((System.currentTimeMillis() - timeStartFall)/1000.0 * 9.8 * mass - drag)/mass)); if(y+velocity[1] > size.height-124) { - velocity[1] *= -inelasticCollisionPercent; + int speed = (int) ((Math.abs(lastHeight-size.height) / (System.currentTimeMillis() - timeStartFall)) * (1000.0 / (System.currentTimeMillis() - timeStartFall))); + //System.out.println(speed); + velocity[1] = velocity[1] * -inelasticCollisionPercent - speed; y = size.height-124; } else if(y+velocity[1] < 0) { - velocity[1] *= -inelasticCollisionPercent; + int speed = (int) ((Math.abs(lastHeight-size.height) / (System.currentTimeMillis() - timeStartFall)) * (1000.0 / (System.currentTimeMillis() - timeStartFall))); + velocity[1] *= -inelasticCollisionPercent - speed; y = 0; } + if(x+velocity[0] > size.width-100) { + int speed = (int) ((Math.abs(lastWidth-size.width) / (System.currentTimeMillis() - timeStartFall)) * (1000.0 / (System.currentTimeMillis() - timeStartFall))); + //System.out.println(speed); + //System.out.println(lastWidth-size.width); + velocity[0] = velocity[0] * -inelasticCollisionPercent - speed; + x = size.width-100; + } else if(x+velocity[0] < 0) { + //int speed = (int) ((Math.abs(lastWidth-size.width) / (System.currentTimeMillis() - timeStartFall)) * (1000.0 / (System.currentTimeMillis() - timeStartFall))); + //System.out.println(speed); + velocity[0] = velocity[0] * -inelasticCollisionPercent;// - speed; + x = 0; + } + int newX = (int)Math.round(velocity[0]) + x; int newY = (int)Math.round(velocity[1]) + y; @@ -58,6 +112,8 @@ public void move(JFrame frame) { //printStats(newX, newY, size); setXY(newX, newY); + lastHeight = size.height; + lastWidth = size.width; timeStartFall = System.currentTimeMillis(); }