mirror of
https://github.com/processing/processing4.git
synced 2026-01-28 19:01:08 +01:00
reworked Reflection2 to use PVector and OOP
This commit is contained in:
@@ -1,14 +1,78 @@
|
||||
class Orb{
|
||||
float x, y, r;
|
||||
class Orb {
|
||||
PVector position;
|
||||
PVector velocity;
|
||||
float r;
|
||||
float damping = 0.8;
|
||||
|
||||
// Default constructor
|
||||
Orb() {
|
||||
Orb(float x, float y, float r_) {
|
||||
position = new PVector(x, y);
|
||||
velocity = new PVector(.5, 0);
|
||||
r = r_;
|
||||
}
|
||||
|
||||
Orb(float x, float y, float r) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.r = r;
|
||||
void move() {
|
||||
// Move orb
|
||||
velocity.add(gravity);
|
||||
position.add(velocity);
|
||||
}
|
||||
|
||||
void display() {
|
||||
// Draw orb
|
||||
noStroke();
|
||||
fill(200);
|
||||
ellipse(position.x, position.y, r*2, r*2);
|
||||
}
|
||||
|
||||
void checkWallCollision() {
|
||||
if (position.x > width-r) {
|
||||
position.x = width-r;
|
||||
velocity.x *= -1;
|
||||
velocity.x *= damping;
|
||||
}
|
||||
else if (position.x < r) {
|
||||
position.x = r;
|
||||
velocity.x *= -1;
|
||||
velocity.x *= damping;
|
||||
}
|
||||
}
|
||||
|
||||
void checkGroundCollision(Ground groundSegment) {
|
||||
|
||||
// Get difference between orb and ground
|
||||
float deltaX = position.x - groundSegment.x;
|
||||
float deltaY = position.y - groundSegment.y;
|
||||
|
||||
// Precalculate trig values
|
||||
float cosine = cos(groundSegment.rot);
|
||||
float sine = sin(groundSegment.rot);
|
||||
|
||||
/* Rotate ground and velocity to allow
|
||||
orthogonal collision calculations */
|
||||
float groundXTemp = cosine * deltaX + sine * deltaY;
|
||||
float groundYTemp = cosine * deltaY - sine * deltaX;
|
||||
float velocityXTemp = cosine * velocity.x + sine * velocity.y;
|
||||
float velocityYTemp = cosine * velocity.y - sine * velocity.x;
|
||||
|
||||
/* Ground collision - check for surface
|
||||
collision and also that orb is within
|
||||
left/rights bounds of ground segment */
|
||||
if (groundYTemp > -r &&
|
||||
position.x > groundSegment.x1 &&
|
||||
position.x < groundSegment.x2 ) {
|
||||
// keep orb from going into ground
|
||||
groundYTemp = -r;
|
||||
// bounce and slow down orb
|
||||
velocityYTemp *= -1.0;
|
||||
velocityYTemp *= damping;
|
||||
}
|
||||
|
||||
// Reset ground, velocity and orb
|
||||
deltaX = cosine * groundXTemp - sine * groundYTemp;
|
||||
deltaY = cosine * groundYTemp + sine * groundXTemp;
|
||||
velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
|
||||
velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
|
||||
position.x = groundSegment.x + deltaX;
|
||||
position.y = groundSegment.y + deltaY;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
*/
|
||||
|
||||
Orb orb;
|
||||
PVector velocity;
|
||||
float gravity = .05, damping = 0.8;
|
||||
|
||||
PVector gravity = new PVector(0,0.05);
|
||||
int segments = 40;
|
||||
Ground[] ground = new Ground[segments];
|
||||
float[] peakHeights = new float[segments+1];
|
||||
@@ -16,7 +16,7 @@ float[] peakHeights = new float[segments+1];
|
||||
void setup(){
|
||||
size(640, 360);
|
||||
orb = new Orb(50, 50, 3);
|
||||
velocity = new PVector(.5, 0);
|
||||
|
||||
|
||||
// Calculate ground peak heights
|
||||
for (int i=0; i<peakHeights.length; i++){
|
||||
@@ -39,12 +39,11 @@ void draw(){
|
||||
noStroke();
|
||||
fill(0, 15);
|
||||
rect(0, 0, width, height);
|
||||
|
||||
// Move orb
|
||||
orb.x += velocity.x;
|
||||
velocity.y += gravity;
|
||||
orb.y += velocity.y;
|
||||
|
||||
|
||||
orb.move();
|
||||
orb.display();
|
||||
orb.checkWallCollision();
|
||||
|
||||
// Draw ground
|
||||
fill(127);
|
||||
beginShape();
|
||||
@@ -56,71 +55,15 @@ void draw(){
|
||||
vertex(ground[0].x1, height);
|
||||
endShape(CLOSE);
|
||||
|
||||
// Draw orb
|
||||
noStroke();
|
||||
fill(200);
|
||||
ellipse(orb.x, orb.y, orb.r*2, orb.r*2);
|
||||
|
||||
// Collision detection
|
||||
checkWallCollision();
|
||||
for (int i=0; i<segments; i++){
|
||||
checkGroundCollision(ground[i]);
|
||||
orb.checkGroundCollision(ground[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void checkWallCollision(){
|
||||
if (orb.x > width-orb.r){
|
||||
orb.x = width-orb.r;
|
||||
velocity.x *= -1;
|
||||
velocity.x *= damping;
|
||||
}
|
||||
else if (orb.x < orb.r){
|
||||
orb.x = orb.r;
|
||||
velocity.x *= -1;
|
||||
velocity.x *= damping;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void checkGroundCollision(Ground groundSegment) {
|
||||
|
||||
// Get difference between orb and ground
|
||||
float deltaX = orb.x - groundSegment.x;
|
||||
float deltaY = orb.y - groundSegment.y;
|
||||
|
||||
// Precalculate trig values
|
||||
float cosine = cos(groundSegment.rot);
|
||||
float sine = sin(groundSegment.rot);
|
||||
|
||||
/* Rotate ground and velocity to allow
|
||||
orthogonal collision calculations */
|
||||
float groundXTemp = cosine * deltaX + sine * deltaY;
|
||||
float groundYTemp = cosine * deltaY - sine * deltaX;
|
||||
float velocityXTemp = cosine * velocity.x + sine * velocity.y;
|
||||
float velocityYTemp = cosine * velocity.y - sine * velocity.x;
|
||||
|
||||
/* Ground collision - check for surface
|
||||
collision and also that orb is within
|
||||
left/rights bounds of ground segment */
|
||||
if (groundYTemp > -orb.r &&
|
||||
orb.x > groundSegment.x1 &&
|
||||
orb.x < groundSegment.x2 ){
|
||||
// keep orb from going into ground
|
||||
groundYTemp = -orb.r;
|
||||
// bounce and slow down orb
|
||||
velocityYTemp *= -1.0;
|
||||
velocityYTemp *= damping;
|
||||
}
|
||||
|
||||
// Reset ground, velocity and orb
|
||||
deltaX = cosine * groundXTemp - sine * groundYTemp;
|
||||
deltaY = cosine * groundYTemp + sine * groundXTemp;
|
||||
velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
|
||||
velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
|
||||
orb.x = groundSegment.x + deltaX;
|
||||
orb.y = groundSegment.y + deltaY;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user