Skip to content

Commit ab833e4

Browse files
committed
Basic physics and rendering
1 parent c9322c6 commit ab833e4

3 files changed

Lines changed: 145 additions & 39 deletions

File tree

rectangles/GameObj.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ public class GameObj extends PApplet {
1010
private PShape shape;
1111
private float objWidth;
1212
private float objHeight;
13+
private boolean isFloor;
1314

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;
15+
public GameObj(float objWidth, float objHeight, float mass, float x, float y, PShape shape,
16+
boolean isFloor) {
17+
this.isFloor = isFloor;
18+
this.py = new Physics(x, y, objWidth, objHeight, mass, 20);
19+
try {
20+
this.shape = shape;
21+
} catch (NullPointerException e) {
22+
// Do nothing, its probably screen limits
23+
}
1724
}
1825

1926
public Physics getPy() {
@@ -47,4 +54,8 @@ public float getObjHeight() {
4754
public void setObjHeight(float objHeight) {
4855
this.objHeight = objHeight;
4956
}
57+
58+
public boolean isFloor() {
59+
return this.isFloor;
60+
}
5061
}

rectangles/Physics.java

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
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;
17
import java.util.ArrayList;
28

39
import processing.core.PApplet;
410
import 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
}

rectangles/Rectangles.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
public class Rectangles extends PApplet {
99

10+
private GameObj floor;
11+
private GameObj ceiling;
12+
private GameObj leftWall;
13+
private GameObj rightWall;
1014
private GameObj square;
1115
private GameObj rectangle;
12-
16+
1317
private ArrayList<GameObj> objects = new ArrayList<GameObj>();
1418

1519
public void settings() {
@@ -19,49 +23,71 @@ public void settings() {
1923
public void setup() {
2024
background(0);
2125
frameRate(60);
26+
27+
this.floor = new GameObj(width, (float) 100, 0, 0, height, null, true);
28+
this.ceiling = new GameObj(width, (float) 100, 0, 0, -100, null, false);
29+
this.leftWall = new GameObj((float) 100, height, 0, -100, 0, null, false);
30+
this.rightWall = new GameObj((float) 100, height, width, width, 0, null, false);
31+
32+
this.objects.add(this.floor);
33+
this.objects.add(this.ceiling);
34+
this.objects.add(this.leftWall);
35+
this.objects.add(this.rightWall);
36+
2237
// Place square and rectangle in bottom corners of screen
2338
float sqrDim = 50;
2439
float rectWidth = 100;
2540
float rectHeight = 50;
2641
PShape sqr = createShape(RECT, 0, 0, sqrDim, sqrDim);
2742
sqr.setFill(color(random(255), random(255), random(255)));
2843
sqr.setStroke(false);
29-
44+
3045
PShape rect = createShape(RECT, 0, 0, rectWidth, rectHeight);
3146
rect.setFill(color(random(255), random(255), random(255)));
3247
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);
48+
49+
this.square = new GameObj(sqrDim, sqrDim, 0, height - sqrDim - 2, 1, sqr, false);
50+
this.rectangle = new GameObj(rectWidth, rectHeight, 2, width - rectWidth, height - rectHeight, rect, false);
3651

3752
// TODO: This will need to be reworked for server-client
3853
this.objects.add(this.rectangle);
39-
54+
4055
}
4156

4257
public void draw() {
4358
background(0);
59+
// Walls
60+
rect((float) this.floor.getPy().getBounds2D().getX(), (float) this.floor.getPy().getBounds2D().getY(),
61+
(float) this.floor.getPy().getBounds2D().getWidth(), (float) this.floor.getPy().getBounds2D().getHeight());
62+
rect((float) this.ceiling.getPy().getBounds2D().getX(), (float) this.ceiling.getPy().getBounds2D().getY(),
63+
(float) this.ceiling.getPy().getBounds2D().getWidth(), (float) this.ceiling.getPy().getBounds2D().getHeight());
64+
rect((float) this.leftWall.getPy().getBounds2D().getX(), (float) this.leftWall.getPy().getBounds2D().getY(),
65+
(float) this.leftWall.getPy().getBounds2D().getWidth(), (float) this.leftWall.getPy().getBounds2D().getHeight());
66+
rect((float) this.rightWall.getPy().getBounds2D().getX(), (float) this.rightWall.getPy().getBounds2D().getY(),
67+
(float) this.rightWall.getPy().getBounds2D().getWidth(), (float) this.rightWall.getPy().getBounds2D().getHeight());
68+
4469
// Update physics
45-
PVector newLoc = this.square.getPy().update(objects);
70+
this.square.getPy().update(objects);
71+
4672
// Render
47-
shape(this.square.getShape(), newLoc.x, newLoc.y);
73+
shape(this.square.getShape(), this.square.getPy().getLocation().x,
74+
this.square.getPy().getLocation().y);
75+
shape(this.rectangle.getShape(), this.rectangle.getPy().getLocation().x,
76+
this.rectangle.getPy().getLocation().y);
4877

4978
}
5079

5180
public void keyPressed() {
5281
if (key == CODED) {
5382
if (keyCode == LEFT) {
54-
System.out.println("LEFT pressed");
55-
this.square.getPy().setAccelerationX(-1);
83+
this.square.getPy().setAccelerationX(-5);
5684
}
5785
if (keyCode == RIGHT) {
58-
System.out.println("RIGHT pressed");
59-
this.square.getPy().setAccelerationX(1);
86+
this.square.getPy().setAccelerationX(5);
6087
}
6188
}
6289
if (key == ' ') {
63-
System.out.println("SPACE pressed");
64-
this.square.getPy().setAccelerationY(20);
90+
this.square.getPy().setAccelerationY(-20);
6591
}
6692
}
6793

0 commit comments

Comments
 (0)