sources, input bug fix and pre-projectiles

This commit is contained in:
Baipyrus 2022-06-17 23:06:58 +02:00
parent 33a9f46bb9
commit 402fe7e3bc
3 changed files with 123 additions and 105 deletions

View File

@ -9,8 +9,11 @@ void userMovement(char k, float hfo) {
finalAngle += HALF_PI; finalAngle += HALF_PI;
break; break;
case 's': case 's':
finalAngle += PI; finalAngle += PI;
break;
} }
if (finalAngle == hfo && k != 'w')
return;
PVector t = PVector.fromAngle(finalAngle); PVector t = PVector.fromAngle(finalAngle);
t.setMag(USER_ACCELERATION_MAGNITUDE); t.setMag(USER_ACCELERATION_MAGNITUDE);
userVelocity.add(t); userVelocity.add(t);

View File

@ -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) { 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 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; 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; return output;
} }
// https://math.stackexchange.com/questions/65503/point-reflection-over-a-line
boolean reflect(Projectile p, Wall w) { boolean reflect(Projectile p, Wall w) {
// Position -> Next : A -> B // Position -> Next : A -> B
// Wall.start -> Wall.end : C -> D // Wall.start -> Wall.end : C -> D

View File

@ -1,4 +1,4 @@
final int SHOOTING_COOLDOWN = 2; final int SHOOTING_COOLDOWN = 1;
final float PROJECTILE_RADIUS = 10; final float PROJECTILE_RADIUS = 10;
final float VELOCITY_MAGNITUDE = 2; final float VELOCITY_MAGNITUDE = 2;
final float HEADING_LINE_LENGTH = 50; final float HEADING_LINE_LENGTH = 50;
@ -16,123 +16,136 @@ boolean dragType = false;
PVector userPosition, userVelocity; PVector userPosition, userVelocity;
void setup() { void setup() {
fullScreen(); fullScreen();
mouse = new PVector(width, height/2); mouse = new PVector(width, height/2);
userPosition = new PVector(width/2, height/2); userPosition = new PVector(width/2, height/2);
userVelocity = new PVector(0, 0); userVelocity = new PVector(0, 0);
// walls.add(new Wall( // walls.add(new Wall(
// new PVector(width/2, 0), // new PVector(width/2, 0),
// new PVector(width, height/2) // new PVector(width, height/2)
// )); // ));
// walls.add(new Wall( // walls.add(new Wall(
// new PVector(width, height/2), // new PVector(width, height/2),
// new PVector(width/2, height) // new PVector(width/2, height)
// )); // ));
// walls.add(new Wall( // walls.add(new Wall(
// new PVector(width/2, height), // new PVector(width/2, height),
// new PVector(0, height/2) // new PVector(0, height/2)
// )); // ));
// walls.add(new Wall( // walls.add(new Wall(
// new PVector(0, height/2), // new PVector(0, height/2),
// new PVector(width/2, 0) // new PVector(width/2, 0)
// )); // ));
walls.add(new Wall( walls.add(new Wall(
new PVector(100, 100), new PVector(100, 100),
new PVector(width-100, 100) new PVector(width-100, 100)
)); ));
walls.add(new Wall( walls.add(new Wall(
new PVector(width-100, 100), new PVector(width-100, 100),
new PVector(width-100, height-100) new PVector(width-100, height-100)
)); ));
walls.add(new Wall( walls.add(new Wall(
new PVector(width-100, height-100), new PVector(width-100, height-100),
new PVector(100, height-100) new PVector(100, height-100)
)); ));
walls.add(new Wall( walls.add(new Wall(
new PVector(100, height-100), new PVector(100, height-100),
new PVector(100, 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() { void draw() {
background(0); background(0);
for (Wall w : walls) for (Wall w : walls)
w.show(); w.show();
for (int i = projectiles.size()-1; i >= 0; i--) { for (int i = projectiles.size()-1; i >= 0; i--) {
Projectile p = projectiles.get(i); Projectile p = projectiles.get(i);
p.show(); p.show();
p.move(); p.move();
} }
mouse = new PVector(mouseX, mouseY); mouse = new PVector(width, height / 2);
stroke(255,0,0); stroke(255,0,0);
fill(255,0,0); fill(255,0,0);
ellipse(userPosition.x, userPosition.y, PROJECTILE_RADIUS*2, PROJECTILE_RADIUS*2); ellipse(userPosition.x, userPosition.y, PROJECTILE_RADIUS*2, PROJECTILE_RADIUS*2);
// Handle 'mouseDrag' when pressing LMB // Handle 'mouseDrag' when pressing LMB
if (dragIndex != -1) { if (dragIndex != -1) {
Wall w = walls.get(dragIndex); Wall w = walls.get(dragIndex);
if (dragType) if (dragType)
w.end = mouse.copy(); w.end = mouse.copy();
else else
w.start = mouse.copy(); w.start = mouse.copy();
} }
// Handle 'shooting' when pressing RMB // Handle 'shooting' when pressing RMB
if (mousePressed && mouseButton == RIGHT && frameCount % SHOOTING_COOLDOWN == 0 && !mouse.equals(userPosition)) { if (mousePressed && mouseButton == RIGHT && frameCount % SHOOTING_COOLDOWN == 0 && !mouse.equals(userPosition)) {
PVector baseVelocity = PVector.sub(mouse, userPosition); PVector baseVelocity = PVector.sub(mouse, userPosition);
baseVelocity.setMag(VELOCITY_MAGNITUDE); baseVelocity.setMag(VELOCITY_MAGNITUDE);
baseVelocity.add(userVelocity); baseVelocity.add(userVelocity);
projectiles.add(new Projectile( projectiles.add(new Projectile(
userPosition, userPosition,
baseVelocity baseVelocity
)); ));
} }
// Display user heading // Display user heading
PVector userHeading = PVector.sub(mouse, userPosition); PVector userHeading = PVector.sub(mouse, userPosition);
userHeading.setMag(HEADING_LINE_LENGTH); userHeading.setMag(HEADING_LINE_LENGTH);
PVector temp = PVector.add(userPosition, userHeading); PVector temp = PVector.add(userPosition, userHeading);
float headingFromOrigin = userHeading.heading(); float headingFromOrigin = userHeading.heading();
PVector arrowLeft = PVector.fromAngle(headingFromOrigin-(4*PI)/5); PVector arrowLeft = PVector.fromAngle(headingFromOrigin-(4*PI)/5);
PVector arrowRight = PVector.fromAngle(headingFromOrigin+(4*PI)/5); PVector arrowRight = PVector.fromAngle(headingFromOrigin+(4*PI)/5);
arrowLeft.setMag(HEADING_LINE_LENGTH / 3); arrowLeft.setMag(HEADING_LINE_LENGTH / 3);
arrowRight.setMag(HEADING_LINE_LENGTH / 3); arrowRight.setMag(HEADING_LINE_LENGTH / 3);
arrowLeft.add(temp); arrowLeft.add(temp);
arrowRight.add(temp); arrowRight.add(temp);
if (!mouse.equals(userPosition)) { if (!mouse.equals(userPosition)) {
stroke(255); stroke(255);
noFill(); noFill();
line(userPosition.x, userPosition.y, temp.x, temp.y); line(userPosition.x, userPosition.y, temp.x, temp.y);
line(temp.x, temp.y, arrowLeft.x, arrowLeft.y); line(temp.x, temp.y, arrowLeft.x, arrowLeft.y);
line(temp.x, temp.y, arrowRight.x, arrowRight.y); line(temp.x, temp.y, arrowRight.x, arrowRight.y);
} }
// Handle user movement // Handle user movement
float decel = userVelocity.mag()-USER_DECELERATION_MAGNITUDE; float decel = userVelocity.mag()-USER_DECELERATION_MAGNITUDE;
if (decel < 0) if (decel < 0)
decel = 0; decel = 0;
userVelocity.setMag(decel); userVelocity.setMag(decel);
PVector nPos = PVector.add(userPosition, userVelocity); PVector nPos = PVector.add(userPosition, userVelocity);
boolean xZero = nPos.x < 0; boolean xZero = nPos.x < 0;
if (xZero || nPos.x > width) { if (xZero || nPos.x > width) {
nPos.x = (xZero) ? 0 : width; nPos.x = (xZero) ? 0 : width;
userVelocity.x = 0; userVelocity.x = 0;
} }
boolean yZero = nPos.y < 0; boolean yZero = nPos.y < 0;
if (yZero || nPos.y > height) { if (yZero || nPos.y > height) {
nPos.y = (yZero) ? 0 : height; nPos.y = (yZero) ? 0 : height;
userVelocity.y = 0; userVelocity.y = 0;
} }
userPosition = nPos.copy(); userPosition = nPos.copy();
if (keyPressed) { if (keyPressed)
userMovement(key, headingFromOrigin); userMovement(key, headingFromOrigin);
}
// saveFrame("Y:\\Stash\\ProcessingAnimations\\Reflections\\frame-#####.png");
} }