From cf584835e63d45a268a458a272f59d1019f93c9d Mon Sep 17 00:00:00 2001 From: MrWalium Date: Wed, 7 Aug 2024 18:08:49 -0700 Subject: [PATCH] Add files via upload --- .../bouncing/ball/BouncingBallWindow.class | Bin 1529 -> 1861 bytes .../bin/window/bouncing/ball/Shape.class | Bin 3677 -> 4812 bytes .../bouncing/ball/BouncingBallWindow.java | 12 +++- .../src/window/bouncing/ball/Shape.java | 66 ++++++++++++++++-- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class b/test_Java_Project/bin/window/bouncing/ball/BouncingBallWindow.class index 074d2861c569cbfc3b3811697a38d0d9fc326220..9551f9e7a2731598fce260d144999c4d6e9a444f 100644 GIT binary patch delta 772 zcmZvaO-~bH5XXN{yJclrc@t;|;2Y7j8sLH=6jT(^0wU6;2&h=f+Aj13+m!+bN%UZ1 zxR`zdy^wfQu_hXg9=sU8f*-_-I135U#B83;%ztM7GxL9*Usd0#jqkrdd9 z77VI6V{(=rMOD&r#&U_cW9PDp`ap1OF}@P-iWi-(iJYBvLY4Fq(&ykd4(=oMC^5G*z~s! zLw3f>7DOM>VuAZQl2E^|#eUQz>26<xKn`987wM=eB zX#D?`B^Z1A!+StgZs3y-zLb7y;f+(y&~CHbMyY7`~ekG3QHnvPOR7mXR!nXPD- K>QxDQSo;Ixk*2u- delta 443 zcmXAlOHUJF6o#K~W}2bH0HWm@DQqxB&~D1LfTDtFDfd#O3OJI1luJuG4X`6&>vHyP zSu18tAR#Vv!yoZCxB<_^&38HPd7tk+-|xo1_15j()eSJk$FJ71rkf1>&6ilv5C{b z6ZwYcQcv@M3=gI65u3!s*)HA=ygZ$p@R&1NDTux2e>B#3C%09eu})E*@&x;y4`^&i YJ%)ZS49!HDG9vV1R(s!v)mnSm%N~Ey>v_M~O*S?5$L2TlUGIH;@8p$# zPdp2t8vh8uqhZmIm5A9x6BHtC`CAcvNVzDr#i|pOM`z%G!@1xbObR=a4K}X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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(); }