From 74259746514ee65a6062e8e0f92ee7a1852cadf4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Wed, 12 Oct 2022 14:25:38 +0200
Subject: [PATCH] Correct bullet simulation

---
 src/main/java/lab/BulletAnimated.java | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java
index d98b7f0..342be6f 100644
--- a/src/main/java/lab/BulletAnimated.java
+++ b/src/main/java/lab/BulletAnimated.java
@@ -4,7 +4,7 @@ import javafx.geometry.Point2D;
 import javafx.scene.canvas.GraphicsContext;
 import javafx.scene.image.Image;
 
-public class BulletAnimated {
+public class BulletAnimated  {
 
 	private Point2D position;
 	private Point2D start;
@@ -12,10 +12,13 @@ public class BulletAnimated {
 	private Point2D initialSpeed;
 	private double size;
 	private double mass = 2;
-	private double strenghtOfCannon = 80;
+	private double strenghtOfCannon = 50;
 	private double cannonLength = 100;
 	private boolean accelerate = true;
 	private boolean hitToGround = false;
+
+	private double crossSectionalArea;
+	private double dragCoefficient = 0.47;
 	
 	private Image image;
 	private World world;
@@ -44,19 +47,23 @@ public class BulletAnimated {
 		gc.restore();
 	}
 
-	public void simulate(double timeStep) {
+	public void simulate(double deltaT) {
 		if (accelerate && start.distance(position) < cannonLength) {
 			double cannonAngle = cannon.getAngle(); 
 			speed = speed
 					.add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon)
-							.multiply(1 / mass * timeStep * 10));
+							.multiply(1 / mass * deltaT));
 		} else if (!hitToGround) {
 			accelerate = false;
-			Point2D acceleration = new Point2D(0, -Constants.GRAVITATIONAL_ACCELERATION*50 * mass * timeStep * 10);
-			speed = speed.add(acceleration.multiply(timeStep));
+			Point2D airResistanceforce = new Point2D(
+					-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * dragCoefficient * Math.pow(speed.getX(), 2),
+					-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * 0.47 * Math.pow(speed.getY(), 2));
+			Point2D acceleration = new Point2D(-airResistanceforce.getX() * mass,
+					-Constants.GRAVITATIONAL_ACCELERATION + airResistanceforce.getY() * mass);
+			speed = speed.add(acceleration.multiply(deltaT));
 		}
 		if (!hitToGround) {
-			position = position.add(speed.multiply(timeStep));
+			position = position.add(speed);
 			if (!accelerate && position.getY() <= size / 2) {
 				hitToGround = true;
 				position = new Point2D(position.getX(), size / 2);
-- 
GitLab