diff --git a/InputEvent.pde b/InputEvent.pde index 7933cc3..c63df84 100644 --- a/InputEvent.pde +++ b/InputEvent.pde @@ -9,8 +9,11 @@ void userMovement(char k, float hfo) { finalAngle += HALF_PI; break; case 's': - finalAngle += PI; + finalAngle += PI; + break; } + if (finalAngle == hfo && k != 'w') + return; PVector t = PVector.fromAngle(finalAngle); t.setMag(USER_ACCELERATION_MAGNITUDE); userVelocity.add(t); diff --git a/Logic.pde b/Logic.pde index 451291e..a7d960a 100644 --- a/Logic.pde +++ b/Logic.pde @@ -1,3 +1,4 @@ +// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line_segment PVector intersect(PVector A, PVector B, PVector C, PVector D) { final float d = ((A.x-B.x)*(C.y-D.y)-(A.y-B.y)*(C.x-D.x)); final float t = ((A.x-C.x)*(C.y-D.y)-(A.y-C.y)*(C.x-D.x))/d; @@ -8,6 +9,7 @@ PVector intersect(PVector A, PVector B, PVector C, PVector D) { return output; } +// https://math.stackexchange.com/questions/65503/point-reflection-over-a-line boolean reflect(Projectile p, Wall w) { // Position -> Next : A -> B // Wall.start -> Wall.end : C -> D diff --git a/reflectingProjectiles.pde b/reflectingProjectiles.pde index 325ed7c..9426dba 100644 --- a/reflectingProjectiles.pde +++ b/reflectingProjectiles.pde @@ -1,4 +1,4 @@ -final int SHOOTING_COOLDOWN = 2; +final int SHOOTING_COOLDOWN = 1; final float PROJECTILE_RADIUS = 10; final float VELOCITY_MAGNITUDE = 2; final float HEADING_LINE_LENGTH = 50; @@ -16,123 +16,136 @@ boolean dragType = false; PVector userPosition, userVelocity; void setup() { - fullScreen(); + fullScreen(); - mouse = new PVector(width, height/2); + mouse = new PVector(width, height/2); - userPosition = new PVector(width/2, height/2); - userVelocity = new PVector(0, 0); + userPosition = new PVector(width/2, height/2); + userVelocity = new PVector(0, 0); - // walls.add(new Wall( - // new PVector(width/2, 0), - // new PVector(width, height/2) - // )); - // walls.add(new Wall( - // new PVector(width, height/2), - // new PVector(width/2, height) - // )); - // walls.add(new Wall( - // new PVector(width/2, height), - // new PVector(0, height/2) - // )); - // walls.add(new Wall( - // new PVector(0, height/2), - // new PVector(width/2, 0) - // )); + // walls.add(new Wall( + // new PVector(width/2, 0), + // new PVector(width, height/2) + // )); + // walls.add(new Wall( + // new PVector(width, height/2), + // new PVector(width/2, height) + // )); + // walls.add(new Wall( + // new PVector(width/2, height), + // new PVector(0, height/2) + // )); + // walls.add(new Wall( + // new PVector(0, height/2), + // new PVector(width/2, 0) + // )); - walls.add(new Wall( - new PVector(100, 100), - new PVector(width-100, 100) - )); - walls.add(new Wall( - new PVector(width-100, 100), - new PVector(width-100, height-100) - )); - walls.add(new Wall( - new PVector(width-100, height-100), - new PVector(100, height-100) - )); - walls.add(new Wall( - new PVector(100, height-100), - new PVector(100, 100) + walls.add(new Wall( + new PVector(100, 100), + new PVector(width-100, 100) + )); + walls.add(new Wall( + new PVector(width-100, 100), + new PVector(width-100, height-100) + )); + walls.add(new Wall( + new PVector(width-100, height-100), + new PVector(100, height-100) + )); + walls.add(new Wall( + new PVector(100, height-100), + new PVector(100, 100) + )); + + final PVector center = new PVector(width/2, height/2); + for (float a = 0; a < TWO_PI; a+=0.01) { + PVector v = new PVector( + cos(a) * HEADING_LINE_LENGTH, + sin(a) * HEADING_LINE_LENGTH + ); + projectiles.add(new Projectile( + PVector.add(center, v), + PVector.mult(PVector.div(v, v.mag()), VELOCITY_MAGNITUDE) )); + } } void draw() { - background(0); + background(0); - for (Wall w : walls) - w.show(); + for (Wall w : walls) + w.show(); - for (int i = projectiles.size()-1; i >= 0; i--) { - Projectile p = projectiles.get(i); - p.show(); - p.move(); - } + for (int i = projectiles.size()-1; i >= 0; i--) { + Projectile p = projectiles.get(i); + p.show(); + p.move(); + } - mouse = new PVector(mouseX, mouseY); - stroke(255,0,0); - fill(255,0,0); - ellipse(userPosition.x, userPosition.y, PROJECTILE_RADIUS*2, PROJECTILE_RADIUS*2); + mouse = new PVector(width, height / 2); + stroke(255,0,0); + fill(255,0,0); + ellipse(userPosition.x, userPosition.y, PROJECTILE_RADIUS*2, PROJECTILE_RADIUS*2); - // Handle 'mouseDrag' when pressing LMB - if (dragIndex != -1) { - Wall w = walls.get(dragIndex); - if (dragType) - w.end = mouse.copy(); - else - w.start = mouse.copy(); - } + // Handle 'mouseDrag' when pressing LMB + if (dragIndex != -1) { + Wall w = walls.get(dragIndex); + if (dragType) + w.end = mouse.copy(); + else + w.start = mouse.copy(); + } - // Handle 'shooting' when pressing RMB - if (mousePressed && mouseButton == RIGHT && frameCount % SHOOTING_COOLDOWN == 0 && !mouse.equals(userPosition)) { - PVector baseVelocity = PVector.sub(mouse, userPosition); - baseVelocity.setMag(VELOCITY_MAGNITUDE); - baseVelocity.add(userVelocity); - projectiles.add(new Projectile( - userPosition, - baseVelocity - )); - } + // Handle 'shooting' when pressing RMB + if (mousePressed && mouseButton == RIGHT && frameCount % SHOOTING_COOLDOWN == 0 && !mouse.equals(userPosition)) { + PVector baseVelocity = PVector.sub(mouse, userPosition); + baseVelocity.setMag(VELOCITY_MAGNITUDE); + baseVelocity.add(userVelocity); + projectiles.add(new Projectile( + userPosition, + baseVelocity + )); + } - // Display user heading - PVector userHeading = PVector.sub(mouse, userPosition); - userHeading.setMag(HEADING_LINE_LENGTH); - PVector temp = PVector.add(userPosition, userHeading); - float headingFromOrigin = userHeading.heading(); - PVector arrowLeft = PVector.fromAngle(headingFromOrigin-(4*PI)/5); - PVector arrowRight = PVector.fromAngle(headingFromOrigin+(4*PI)/5); - arrowLeft.setMag(HEADING_LINE_LENGTH / 3); - arrowRight.setMag(HEADING_LINE_LENGTH / 3); - arrowLeft.add(temp); - arrowRight.add(temp); - if (!mouse.equals(userPosition)) { - stroke(255); - noFill(); - line(userPosition.x, userPosition.y, temp.x, temp.y); - line(temp.x, temp.y, arrowLeft.x, arrowLeft.y); - line(temp.x, temp.y, arrowRight.x, arrowRight.y); - } + // Display user heading + PVector userHeading = PVector.sub(mouse, userPosition); + userHeading.setMag(HEADING_LINE_LENGTH); + PVector temp = PVector.add(userPosition, userHeading); + float headingFromOrigin = userHeading.heading(); + PVector arrowLeft = PVector.fromAngle(headingFromOrigin-(4*PI)/5); + PVector arrowRight = PVector.fromAngle(headingFromOrigin+(4*PI)/5); + arrowLeft.setMag(HEADING_LINE_LENGTH / 3); + arrowRight.setMag(HEADING_LINE_LENGTH / 3); + arrowLeft.add(temp); + arrowRight.add(temp); + if (!mouse.equals(userPosition)) { + stroke(255); + noFill(); + line(userPosition.x, userPosition.y, temp.x, temp.y); + line(temp.x, temp.y, arrowLeft.x, arrowLeft.y); + line(temp.x, temp.y, arrowRight.x, arrowRight.y); + } - // Handle user movement - float decel = userVelocity.mag()-USER_DECELERATION_MAGNITUDE; - if (decel < 0) - decel = 0; - userVelocity.setMag(decel); - PVector nPos = PVector.add(userPosition, userVelocity); - boolean xZero = nPos.x < 0; - if (xZero || nPos.x > width) { - nPos.x = (xZero) ? 0 : width; - userVelocity.x = 0; - } - boolean yZero = nPos.y < 0; - if (yZero || nPos.y > height) { - nPos.y = (yZero) ? 0 : height; - userVelocity.y = 0; - } - userPosition = nPos.copy(); + // Handle user movement + float decel = userVelocity.mag()-USER_DECELERATION_MAGNITUDE; + if (decel < 0) + decel = 0; + userVelocity.setMag(decel); + PVector nPos = PVector.add(userPosition, userVelocity); + boolean xZero = nPos.x < 0; + if (xZero || nPos.x > width) { + nPos.x = (xZero) ? 0 : width; + userVelocity.x = 0; + } + boolean yZero = nPos.y < 0; + if (yZero || nPos.y > height) { + nPos.y = (yZero) ? 0 : height; + userVelocity.y = 0; + } + userPosition = nPos.copy(); - if (keyPressed) { - userMovement(key, headingFromOrigin); - } + if (keyPressed) + userMovement(key, headingFromOrigin); + + // saveFrame("Y:\\Stash\\ProcessingAnimations\\Reflections\\frame-#####.png"); }