|
| 1 | +import java.util.ArrayList; |
| 2 | + |
1 | 3 | import processing.core.PApplet; |
| 4 | +import processing.core.PVector; |
2 | 5 |
|
3 | 6 | public class Physics extends PApplet{ |
4 | 7 | private static final float GRAV = (float) -9.8; |
5 | | - private float xPos; |
6 | | - private float yPos; |
7 | | - private float objWidth; |
8 | | - private float objHeight; |
9 | | - private float xV = 0; |
10 | | - private float yV = 0; |
11 | | - private float xA = 0; |
12 | | - private float yA = 0; |
13 | | - |
14 | | - public Physics(float xPos, float yPos, float objWidth, float objHeight) { |
15 | | - this.xPos = xPos; |
16 | | - this.yPos = yPos; |
17 | | - this.objWidth = objWidth; |
18 | | - this.objHeight = objHeight; |
19 | | - } |
20 | | - |
21 | | - public float updateY(float t) { |
22 | | - float accl = this.yA + GRAV; |
23 | | - this.yPos = (this.yPos + this.yV * t + (1/2) * accl * (t * t)); |
24 | | - this.yV = this.yV + accl * t; |
25 | | - |
26 | | - // Did I hit the floor/ceiling |
27 | | - if ((this.yPos + this.objHeight) > height) { |
28 | | - this.yPos = height - this.objHeight; |
29 | | - } |
30 | | - if (this.yPos > height) { |
31 | | - this.yPos = height; |
32 | | - } |
| 8 | + |
| 9 | + // Vector code inspired by processing tutorial 'acceleration with vectors' |
| 10 | + private PVector location; |
| 11 | + private PVector velocity; |
| 12 | + private PVector acceleration; |
| 13 | + private float topSpeed; |
| 14 | + private float radius; |
| 15 | + |
| 16 | + // Collision based on circle around center of object |
| 17 | + // inspired from https://happycoding.io/tutorials/processing/collision-detection |
| 18 | + |
| 19 | + public Physics(float x, float y, float radius, float topSpeed) { |
| 20 | + this.location = new PVector(x, y); |
| 21 | + this.velocity = new PVector(0,0); |
| 22 | + this.acceleration = new PVector(0, GRAV); |
| 23 | + this.topSpeed = topSpeed; |
| 24 | + this.radius = radius; |
33 | 25 |
|
34 | | - return this.yPos; |
35 | 26 | } |
36 | 27 |
|
37 | | - public float updateX(float t) { |
38 | | - // TODO: Friction? |
39 | | - this.xPos = (this.xPos + this.xV * t + (1/2) * this.xA * (t * t)); |
40 | | - this.xV = this.xV + this.xA * t; |
41 | | - |
42 | | - // Did I hit the walls |
43 | | - if (this.xPos < 0) { |
44 | | - this.xPos = 0; |
45 | | - this.xA = this.xA * (-1); |
46 | | - } |
47 | | - if ((this.xPos + this.objWidth) > width) { |
48 | | - this.xPos = (width - this.objWidth); |
49 | | - this.yA = this.yA * (-1); |
| 28 | + public PVector update(ArrayList<GameObj> objects) { |
| 29 | + //this.acceleration.setMag(0.2); |
| 30 | + |
| 31 | + for (GameObj obj : objects) { |
| 32 | + if (dist(this.location.x, this.location.y, obj.getPy().getLocation().x, |
| 33 | + obj.getPy().getLocation().y) < (this.radius + obj.getPy().getRadius())) { |
| 34 | + this.velocity.mult(0); |
| 35 | + this.acceleration.mult(0); |
| 36 | + } |
50 | 37 | } |
51 | | - |
52 | | - return this.xPos; |
| 38 | + |
| 39 | + this.velocity.add(this.acceleration); |
| 40 | + this.velocity.limit(this.topSpeed); |
| 41 | + this.location.add(this.velocity); |
| 42 | + |
| 43 | + // Reset acceleration? |
| 44 | + this.acceleration = new PVector(0, GRAV); |
| 45 | + |
| 46 | + return this.location; |
53 | 47 | } |
54 | 48 |
|
55 | | - public float getxPos() { |
56 | | - return xPos; |
| 49 | + public PVector getAcceleration() { |
| 50 | + return acceleration; |
57 | 51 | } |
58 | 52 |
|
59 | | - public void setxPos(float xPos) { |
60 | | - this.xPos = xPos; |
| 53 | + public void setAcceleration(PVector acceleration) { |
| 54 | + this.acceleration = acceleration; |
61 | 55 | } |
62 | 56 |
|
63 | | - public float getyPos() { |
64 | | - return yPos; |
| 57 | + public void setAccelerationX(float x) { |
| 58 | + this.acceleration.x = x; |
65 | 59 | } |
66 | | - |
67 | | - public void setyPos(float yPos) { |
68 | | - this.yPos = yPos; |
| 60 | + |
| 61 | + public void setAccelerationY(float y) { |
| 62 | + this.acceleration.x = (y + GRAV); |
69 | 63 | } |
70 | 64 |
|
71 | | - public float getxV() { |
72 | | - return xV; |
| 65 | + public PVector getLocation() { |
| 66 | + return location; |
73 | 67 | } |
74 | 68 |
|
75 | | - public void setxV(float xV) { |
76 | | - this.xV = xV; |
| 69 | + public void setLocation(PVector location) { |
| 70 | + this.location = location; |
77 | 71 | } |
78 | 72 |
|
79 | | - public float getyV() { |
80 | | - return yV; |
| 73 | + public PVector getVelocity() { |
| 74 | + return velocity; |
81 | 75 | } |
82 | 76 |
|
83 | | - public void setyV(float yV) { |
84 | | - this.yV = yV; |
| 77 | + public void setVelocity(PVector velocity) { |
| 78 | + this.velocity = velocity; |
85 | 79 | } |
86 | 80 |
|
87 | | - public float getxA() { |
88 | | - return xA; |
| 81 | + public float getTopSpeed() { |
| 82 | + return topSpeed; |
89 | 83 | } |
90 | 84 |
|
91 | | - public void setxA(float xA) { |
92 | | - this.xA = xA; |
| 85 | + public void setTopSpeed(float topSpeed) { |
| 86 | + this.topSpeed = topSpeed; |
93 | 87 | } |
94 | 88 |
|
95 | | - public float getyA() { |
96 | | - return yA; |
| 89 | + public float getRadius() { |
| 90 | + return radius; |
97 | 91 | } |
98 | 92 |
|
99 | | - public void setyA(float yA) { |
100 | | - this.yA = yA; |
| 93 | + public void setRadius(float radius) { |
| 94 | + this.radius = radius; |
101 | 95 | } |
102 | | - |
103 | 96 | } |
0 commit comments