Skip to content

Commit 7332070

Browse files
committed
Make physics use vectors/Add game object
1 parent a4887c0 commit 7332070

3 files changed

Lines changed: 144 additions & 106 deletions

File tree

rectangles/GameObj.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import processing.core.PShape;
2+
3+
import java.util.ArrayList;
4+
5+
import processing.core.PApplet;
6+
import processing.core.PVector;
7+
8+
public class GameObj extends PApplet {
9+
private Physics py;
10+
private PShape shape;
11+
private float objWidth;
12+
private float objHeight;
13+
14+
public GameObj(float objWidth, float objHeight, float x, float y, PShape shape) {
15+
this.py = new Physics(x, y, (objWidth + objHeight)/2, 5);
16+
this.shape = shape;
17+
}
18+
19+
public Physics getPy() {
20+
return py;
21+
}
22+
23+
public void setPy(Physics py) {
24+
this.py = py;
25+
}
26+
27+
public PShape getShape() {
28+
return shape;
29+
}
30+
31+
public void setShape(PShape shape) {
32+
this.shape = shape;
33+
}
34+
35+
public float getObjWidth() {
36+
return objWidth;
37+
}
38+
39+
public void setObjWidth(float objWidth) {
40+
this.objWidth = objWidth;
41+
}
42+
43+
public float getObjHeight() {
44+
return objHeight;
45+
}
46+
47+
public void setObjHeight(float objHeight) {
48+
this.objHeight = objHeight;
49+
}
50+
}

rectangles/Physics.java

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,96 @@
1+
import java.util.ArrayList;
2+
13
import processing.core.PApplet;
4+
import processing.core.PVector;
25

36
public class Physics extends PApplet{
47
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;
3325

34-
return this.yPos;
3526
}
3627

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+
}
5037
}
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;
5347
}
5448

55-
public float getxPos() {
56-
return xPos;
49+
public PVector getAcceleration() {
50+
return acceleration;
5751
}
5852

59-
public void setxPos(float xPos) {
60-
this.xPos = xPos;
53+
public void setAcceleration(PVector acceleration) {
54+
this.acceleration = acceleration;
6155
}
6256

63-
public float getyPos() {
64-
return yPos;
57+
public void setAccelerationX(float x) {
58+
this.acceleration.x = x;
6559
}
66-
67-
public void setyPos(float yPos) {
68-
this.yPos = yPos;
60+
61+
public void setAccelerationY(float y) {
62+
this.acceleration.x = (y + GRAV);
6963
}
7064

71-
public float getxV() {
72-
return xV;
65+
public PVector getLocation() {
66+
return location;
7367
}
7468

75-
public void setxV(float xV) {
76-
this.xV = xV;
69+
public void setLocation(PVector location) {
70+
this.location = location;
7771
}
7872

79-
public float getyV() {
80-
return yV;
73+
public PVector getVelocity() {
74+
return velocity;
8175
}
8276

83-
public void setyV(float yV) {
84-
this.yV = yV;
77+
public void setVelocity(PVector velocity) {
78+
this.velocity = velocity;
8579
}
8680

87-
public float getxA() {
88-
return xA;
81+
public float getTopSpeed() {
82+
return topSpeed;
8983
}
9084

91-
public void setxA(float xA) {
92-
this.xA = xA;
85+
public void setTopSpeed(float topSpeed) {
86+
this.topSpeed = topSpeed;
9387
}
9488

95-
public float getyA() {
96-
return yA;
89+
public float getRadius() {
90+
return radius;
9791
}
9892

99-
public void setyA(float yA) {
100-
this.yA = yA;
93+
public void setRadius(float radius) {
94+
this.radius = radius;
10195
}
102-
10396
}

rectangles/Rectangles.java

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11

2+
import java.util.ArrayList;
3+
24
import processing.core.PApplet;
35
import processing.core.PShape;
6+
import processing.core.PVector;
47

58
public class Rectangles extends PApplet {
69

7-
private ShapeObj square;
8-
private ShapeObj rectangle;
10+
private GameObj square;
11+
private GameObj rectangle;
12+
13+
private ArrayList<GameObj> objects = new ArrayList<GameObj>();
914

1015
public void settings() {
1116
size(640, 360);
@@ -15,32 +20,48 @@ public void setup() {
1520
background(0);
1621
frameRate(60);
1722
// Place square and rectangle in bottom corners of screen
18-
this.square = new ShapeObj(50, 50, 0, height - 50);
19-
this.rectangle = new ShapeObj(100, 50, this.width - 100, this.height - 50);
23+
float sqrDim = 50;
24+
float rectWidth = 100;
25+
float rectHeight = 50;
26+
PShape sqr = createShape(RECT, 0, 0, sqrDim, sqrDim);
27+
sqr.setFill(color(random(255), random(255), random(255)));
28+
sqr.setStroke(false);
29+
30+
PShape rect = createShape(RECT, 0, 0, rectWidth, rectHeight);
31+
rect.setFill(color(random(255), random(255), random(255)));
32+
rect.setStroke(false);
33+
34+
this.square = new GameObj(sqrDim, sqrDim, 0, height - sqrDim, sqr);
35+
this.rectangle = new GameObj(100, 50, width - rectWidth, height - rectHeight, rect);
2036

37+
// TODO: This will need to be reworked for server-client
38+
this.objects.add(this.rectangle);
39+
2140
}
2241

2342
public void draw() {
2443
background(0);
25-
this.square.update(1/60);
26-
System.out.println("(" + this.square.py.getxPos() + ", " +
27-
this.square.py.getyPos() + ")");
44+
// Update physics
45+
PVector newLoc = this.square.getPy().update(objects);
46+
// Render
47+
shape(this.square.getShape(), newLoc.x, newLoc.y);
48+
2849
}
2950

3051
public void keyPressed() {
3152
if (key == CODED) {
3253
if (keyCode == LEFT) {
3354
System.out.println("LEFT pressed");
34-
this.square.py.setxA(-1);
55+
this.square.getPy().setAccelerationX(-1);
3556
}
3657
if (keyCode == RIGHT) {
3758
System.out.println("RIGHT pressed");
38-
this.square.py.setxA(1);
59+
this.square.getPy().setAccelerationX(1);
3960
}
4061
}
4162
if (key == ' ') {
4263
System.out.println("SPACE pressed");
43-
this.square.py.setyA(20);
64+
this.square.getPy().setAccelerationY(20);
4465
}
4566
}
4667

@@ -51,30 +72,4 @@ public static void main(String[] args) {
5172
Rectangles sketch = new Rectangles();
5273
PApplet.runSketch(processingArgs, sketch);
5374
}
54-
55-
private class ShapeObj {
56-
public Physics py;
57-
public PShape shape;
58-
59-
public ShapeObj(float width, float height, float x, float y) {
60-
this.py = new Physics(x, y, width, height);
61-
this.shape = createShape(RECT, 0, 0, width, height);
62-
this.shape.setFill(color(random(255), random(255), random(255)));
63-
this.shape.setStroke(false);
64-
shape(this.shape, x, y);
65-
}
66-
67-
public void update(float t) {
68-
this.py.updateX(t);
69-
this.py.updateY(t);
70-
shape(this.shape, this.py.getxPos(), this.py.getyPos());
71-
}
72-
73-
public void setPos(float x, float y) {
74-
this.py.setxPos(x);
75-
this.py.setyPos(y);
76-
shape(this.shape, x, y);
77-
}
78-
79-
}
8075
}

0 commit comments

Comments
 (0)