Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class
Binary file not shown.
Binary file modified test_Java_Project/bin/window/bouncing/ball/Shape.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

}
}
66 changes: 61 additions & 5 deletions test_Java_Project/src/window/bouncing/ball/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -26,38 +33,87 @@ public Shape(JFrame frame) {
this.x = 150;
this.y = 100;
}
mass = 1.7;
//frame = frame != null ? : ;
velocity[0] = 0;
velocity[1] = 0;

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

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;


//printStats(newX, newY, size);
setXY(newX, newY);

lastHeight = size.height;
lastWidth = size.width;
timeStartFall = System.currentTimeMillis();
}

Expand Down