Skip to content

Commit a4887c0

Browse files
committed
Physics init
1 parent 9b8d54c commit a4887c0

2 files changed

Lines changed: 164 additions & 9 deletions

File tree

rectangles/Physics.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import processing.core.PApplet;
2+
3+
public class Physics extends PApplet{
4+
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+
}
33+
34+
return this.yPos;
35+
}
36+
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);
50+
}
51+
52+
return this.xPos;
53+
}
54+
55+
public float getxPos() {
56+
return xPos;
57+
}
58+
59+
public void setxPos(float xPos) {
60+
this.xPos = xPos;
61+
}
62+
63+
public float getyPos() {
64+
return yPos;
65+
}
66+
67+
public void setyPos(float yPos) {
68+
this.yPos = yPos;
69+
}
70+
71+
public float getxV() {
72+
return xV;
73+
}
74+
75+
public void setxV(float xV) {
76+
this.xV = xV;
77+
}
78+
79+
public float getyV() {
80+
return yV;
81+
}
82+
83+
public void setyV(float yV) {
84+
this.yV = yV;
85+
}
86+
87+
public float getxA() {
88+
return xA;
89+
}
90+
91+
public void setxA(float xA) {
92+
this.xA = xA;
93+
}
94+
95+
public float getyA() {
96+
return yA;
97+
}
98+
99+
public void setyA(float yA) {
100+
this.yA = yA;
101+
}
102+
103+
}

rectangles/Rectangles.java

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,78 @@
33
import processing.core.PShape;
44

55
public class Rectangles extends PApplet {
6-
7-
private PShape square;
6+
7+
private ShapeObj square;
8+
private ShapeObj rectangle;
89

910
public void settings() {
10-
System.out.println(Test.testString);
11-
size(500, 500);
12-
//this.square = createShape(RECT, 0, 0, 50, 50);
13-
//this.square.setFill(color(0, 0, 255));
14-
//this.square.setStroke(false);
11+
size(640, 360);
12+
}
13+
14+
public void setup() {
15+
background(0);
16+
frameRate(60);
17+
// 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);
20+
1521
}
1622

1723
public void draw() {
18-
rect(30, 20, 25, 25);
24+
background(0);
25+
this.square.update(1/60);
26+
System.out.println("(" + this.square.py.getxPos() + ", " +
27+
this.square.py.getyPos() + ")");
28+
}
29+
30+
public void keyPressed() {
31+
if (key == CODED) {
32+
if (keyCode == LEFT) {
33+
System.out.println("LEFT pressed");
34+
this.square.py.setxA(-1);
35+
}
36+
if (keyCode == RIGHT) {
37+
System.out.println("RIGHT pressed");
38+
this.square.py.setxA(1);
39+
}
40+
}
41+
if (key == ' ') {
42+
System.out.println("SPACE pressed");
43+
this.square.py.setyA(20);
44+
}
1945
}
2046

21-
//API stuff from https://happycoding.io/tutorials/java/processing-in-java
47+
// API stuff from https://happycoding.io/tutorials/java/processing-in-java
2248

2349
public static void main(String[] args) {
2450
String[] processingArgs = {"Rectangles"};
2551
Rectangles sketch = new Rectangles();
2652
PApplet.runSketch(processingArgs, sketch);
2753
}
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+
}
2880
}

0 commit comments

Comments
 (0)