1+ import java .awt .Rectangle ;
2+ import java .awt .Shape ;
3+ import java .awt .geom .AffineTransform ;
4+ import java .awt .geom .PathIterator ;
5+ import java .awt .geom .Point2D ;
6+ import java .awt .geom .Rectangle2D ;
17import java .util .ArrayList ;
28
39import processing .core .PApplet ;
410import processing .core .PVector ;
511
6- public class Physics extends PApplet {
7- private static final float GRAV = (float ) - 9 .8 ;
8-
12+ public class Physics extends PApplet implements Shape {
13+ private static final float GRAV = (float ) 0 .8 ;
14+
915 // Vector code inspired by processing tutorial 'acceleration with vectors'
1016 private PVector location ;
1117 private PVector velocity ;
1218 private PVector acceleration ;
1319 private float topSpeed ;
14- private float radius ;
20+ private float objWidth ;
21+ private float objHeight ;
22+ private float mass ;
1523
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 ) {
24+ public Physics (float x , float y , float objWidth , float objHeight , float mass , float topSpeed ) {
2025 this .location = new PVector (x , y );
2126 this .velocity = new PVector (0 ,0 );
2227 this .acceleration = new PVector (0 , GRAV );
2328 this .topSpeed = topSpeed ;
24- this .radius = radius ;
25-
29+ this .objWidth = objWidth ;
30+ this .objHeight = objHeight ;
31+ this .mass = mass ;
2632 }
2733
2834 public PVector update (ArrayList <GameObj > objects ) {
2935 //this.acceleration.setMag(0.2);
30-
36+
3137 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 );
38+ if (this .intersects (obj .getPy ().getBounds2D ())) {
39+ if (obj .isFloor ()) {
40+ this .velocity .y = (float ) -1 ;
41+ this .acceleration .mult (0 );
42+ } else {
43+ this .velocity .mult (-1 );
44+ }
45+ break ;
3646 }
3747 }
38-
48+
3949 this .velocity .add (this .acceleration );
4050 this .velocity .limit (this .topSpeed );
4151 this .location .add (this .velocity );
42-
52+
4353 // Reset acceleration?
4454 this .acceleration = new PVector (0 , GRAV );
4555
4656 return this .location ;
4757 }
58+
59+ // From processing tutorial forces with vectors
60+ public void applyForce (PVector force ) {
61+ PVector f = PVector .div (force , this .mass );
62+ acceleration .add (f );
63+ }
4864
4965 public PVector getAcceleration () {
5066 return acceleration ;
@@ -59,7 +75,7 @@ public void setAccelerationX(float x) {
5975 }
6076
6177 public void setAccelerationY (float y ) {
62- this .acceleration .x = (y + GRAV );
78+ this .acceleration .y = (y + GRAV );
6379 }
6480
6581 public PVector getLocation () {
@@ -77,6 +93,9 @@ public PVector getVelocity() {
7793 public void setVelocity (PVector velocity ) {
7894 this .velocity = velocity ;
7995 }
96+ public void setVelocityX (float velocity ) {
97+ this .velocity .x = velocity ;
98+ }
8099
81100 public float getTopSpeed () {
82101 return topSpeed ;
@@ -86,11 +105,61 @@ public void setTopSpeed(float topSpeed) {
86105 this .topSpeed = topSpeed ;
87106 }
88107
89- public float getRadius () {
90- return radius ;
108+ @ Override
109+ public boolean contains (Point2D p ) {
110+ // TODO Auto-generated method stub
111+ return false ;
112+ }
113+
114+ @ Override
115+ public boolean contains (Rectangle2D r ) {
116+ // TODO Auto-generated method stub
117+ return false ;
118+ }
119+
120+ @ Override
121+ public boolean contains (double x , double y ) {
122+ // TODO Auto-generated method stub
123+ return false ;
124+ }
125+
126+ @ Override
127+ public boolean contains (double x , double y , double w , double h ) {
128+ // TODO Auto-generated method stub
129+ return false ;
130+ }
131+
132+ @ Override
133+ public Rectangle getBounds () {
134+ // TODO Auto-generated method stub
135+ return null ;
136+ }
137+
138+ @ Override
139+ public Rectangle2D getBounds2D () {
140+ return new Rectangle2D .Float (this .location .x , this .location .y , this .objWidth , this .objHeight );
141+ }
142+
143+ @ Override
144+ public PathIterator getPathIterator (AffineTransform at ) {
145+ // TODO Auto-generated method stub
146+ return null ;
147+ }
148+
149+ @ Override
150+ public PathIterator getPathIterator (AffineTransform at , double flatness ) {
151+ // TODO Auto-generated method stub
152+ return null ;
153+ }
154+
155+ @ Override
156+ public boolean intersects (Rectangle2D r ) {
157+ return this .getBounds2D ().intersects (r .getBounds2D ());
91158 }
92159
93- public void setRadius (float radius ) {
94- this .radius = radius ;
160+ @ Override
161+ public boolean intersects (double x , double y , double w , double h ) {
162+ // TODO Auto-generated method stub
163+ return false ;
95164 }
96165}
0 commit comments