collision detection between polygons
in
Programming Questions
•
8 months ago
HI im currently working on a simple web app i have used code from examples given so far i can get the rocks to disapear when my polygon ship (as they are images loaded in)is over them and i press the mouse button but how do i go about doing the same for when i go over the other polygon shapped stars that ive got falling down the screen(was going to change my ship into a black hole so it can suck up the stars).
if anyone could help i would really appreciate it.
kind regards
carl
String filename = "rock.jpg";
PImage rock_image;
PShape star;
PShape ship;
PImage rock_image;
PShape star;
PShape ship;
int MAX_rockZ = 1000;
float[] xpos = new float[MAX_rockZ];
float[] ypos = new float[MAX_rockZ];
float dx = 0;
float dy = 15;
float[] ypos = new float[MAX_rockZ];
float dx = 0;
float dy = 15;
int GREY = 100;
int WHITE = 255;
int WIDTH = 320;
int HEIGHT = 400;
int num_rockz = 1;
int score = 0;
int frame = 0;
ArrayList<Polygon> polygons;
int WHITE = 255;
int WIDTH = 320;
int HEIGHT = 400;
int num_rockz = 1;
int score = 0;
int frame = 0;
ArrayList<Polygon> polygons;
PShader nebula;
void setup() {
rock_image = loadImage(filename);
size(320, 400, P2D);
noStroke();
frameRate(5); // A bit faster than before...
textFont(createFont("Georgia",24));
textAlign(LEFT, CENTER);
nebula = loadShader("nebula.glsl");
nebula.set("resolution", float(width), float(height));
ship = createShape();
ship.fill(102);
ship.stroke(255);
ship.strokeWeight(2);
// Here, we are hardcoding a series of vertices
ship.stroke(255);
ship.vertex(0, -50);
ship.vertex(14, -20);
ship.vertex(23, 7);
ship.vertex(29, 40);
ship.vertex(0, 25);
ship.vertex(-29, 40);
ship.vertex(-23, 7);
ship.vertex(-14, -20);
ship.end(CLOSE);
PShape star = createShape();
star.fill(255);
star.stroke(0);
star.vertex(0, -50);
star.vertex(14, -20);
star.vertex(47, -15);
star.vertex(23, 7);
star.vertex(29, 40);
star.vertex(0, 25);
star.vertex(-29, 40);
star.vertex(-23, 7);
star.vertex(-47, -15);
star.vertex(-14, -20);
star.end(CLOSE);
// Make an ArrayList
polygons = new ArrayList<Polygon>();
// Add a bunch of objects to the ArrayList
// Pass in reference to the PShape
// We coud make polygons with different PShapes
for (int i = 0; i < 25; i++) {
polygons.add(new Polygon(star));
}
}
rock_image = loadImage(filename);
size(320, 400, P2D);
noStroke();
frameRate(5); // A bit faster than before...
textFont(createFont("Georgia",24));
textAlign(LEFT, CENTER);
nebula = loadShader("nebula.glsl");
nebula.set("resolution", float(width), float(height));
ship = createShape();
ship.fill(102);
ship.stroke(255);
ship.strokeWeight(2);
// Here, we are hardcoding a series of vertices
ship.stroke(255);
ship.vertex(0, -50);
ship.vertex(14, -20);
ship.vertex(23, 7);
ship.vertex(29, 40);
ship.vertex(0, 25);
ship.vertex(-29, 40);
ship.vertex(-23, 7);
ship.vertex(-14, -20);
ship.end(CLOSE);
PShape star = createShape();
star.fill(255);
star.stroke(0);
star.vertex(0, -50);
star.vertex(14, -20);
star.vertex(47, -15);
star.vertex(23, 7);
star.vertex(29, 40);
star.vertex(0, 25);
star.vertex(-29, 40);
star.vertex(-23, 7);
star.vertex(-47, -15);
star.vertex(-14, -20);
star.end(CLOSE);
// Make an ArrayList
polygons = new ArrayList<Polygon>();
// Add a bunch of objects to the ArrayList
// Pass in reference to the PShape
// We coud make polygons with different PShapes
for (int i = 0; i < 25; i++) {
polygons.add(new Polygon(star));
}
}
void draw() {
nebula.set("time", millis() / 500.0);
shader(nebula);
// This kind of raymarching effects are entirely implemented in the
// fragment shader, they only need a quad covering the entire view
// area so every pixel is pushed through the shader.
rect(0, 0, width, height);
resetShader();
// Display and move them all
for (Polygon poly : polygons) {
poly.display();
poly.move();
}
//Draw the score.
fill(WHITE);
text(str(score), WIDTH - 50, 20);
fill(GREY);
// Add a new rock every 100 frames.
if (frame % 100 == 0) {
num_rockz = num_rockz + 1;
}
// Draw over the old rocks.
for (int i = 0; i < num_rockz; i++) {
rect(xpos[i], ypos[i], rock_image.width + 4, rock_image.height + 4);
}
nebula.set("time", millis() / 500.0);
shader(nebula);
// This kind of raymarching effects are entirely implemented in the
// fragment shader, they only need a quad covering the entire view
// area so every pixel is pushed through the shader.
rect(0, 0, width, height);
resetShader();
// Display and move them all
for (Polygon poly : polygons) {
poly.display();
poly.move();
}
//Draw the score.
fill(WHITE);
text(str(score), WIDTH - 50, 20);
fill(GREY);
// Add a new rock every 100 frames.
if (frame % 100 == 0) {
num_rockz = num_rockz + 1;
}
// Draw over the old rocks.
for (int i = 0; i < num_rockz; i++) {
rect(xpos[i], ypos[i], rock_image.width + 4, rock_image.height + 4);
}
// Draw all the rocks in their new positions.
for (int i = 0; i < num_rockz; i++) {
if (xpos[i] < 0 || xpos[i] > WIDTH || ypos[i] < 0 || ypos[i] > HEIGHT) {
xpos[i] = random(width);
ypos[i] = random(-100, -100);
}
else {
// rocks move diagonally down the screen.
xpos[i] = xpos[i] + dx;
ypos[i] = ypos[i] + dy;
}
image(rock_image, xpos[i], ypos[i]);
}
// Increase the frame number.
frame = frame + 1;
ship.stroke(0);
ship.strokeWeight(4);
ship.fill(map(mouseX, 0, width, 0, 255));
// We can use translate to move the PShape
translate(mouseX, mouseY);
// Drawing the PShape
shape(ship);
}
for (int i = 0; i < num_rockz; i++) {
if (xpos[i] < 0 || xpos[i] > WIDTH || ypos[i] < 0 || ypos[i] > HEIGHT) {
xpos[i] = random(width);
ypos[i] = random(-100, -100);
}
else {
// rocks move diagonally down the screen.
xpos[i] = xpos[i] + dx;
ypos[i] = ypos[i] + dy;
}
image(rock_image, xpos[i], ypos[i]);
}
// Increase the frame number.
frame = frame + 1;
ship.stroke(0);
ship.strokeWeight(4);
ship.fill(map(mouseX, 0, width, 0, 255));
// We can use translate to move the PShape
translate(mouseX, mouseY);
// Drawing the PShape
shape(ship);
}
void mousePressed() {
for (int i = 0; i < num_rockz; i++) {
if (mouseX < (xpos[i] + rock_image.width) && mouseX > xpos[i] &&
mouseY > ypos[i] && mouseY < (ypos[i] + rock_image.height)) {
rect(xpos[i], ypos[i], rock_image.width + 4, rock_image.height + 4);
// Ensure rock moves to top of screen.
xpos[i] = -1;
ypos[i] = -1;
score = score + 1;
}
}
}
for (int i = 0; i < num_rockz; i++) {
if (mouseX < (xpos[i] + rock_image.width) && mouseX > xpos[i] &&
mouseY > ypos[i] && mouseY < (ypos[i] + rock_image.height)) {
rect(xpos[i], ypos[i], rock_image.width + 4, rock_image.height + 4);
// Ensure rock moves to top of screen.
xpos[i] = -1;
ypos[i] = -1;
score = score + 1;
}
}
}
// A class to describe a Polygon (with a PShape)
class Polygon {
// The PShape object
PShape s;
// The location where we will draw the shape
float x, y;
// Variable for simple motion
float speed;
// The PShape object
PShape s;
// The location where we will draw the shape
float x, y;
// Variable for simple motion
float speed;
Polygon(PShape s_) {
x = random(width);
y = random(-500, -100);
s = s_;
speed = random(2, 6);
}
// Simple motion
void move() {
y+=speed;
if (y > height+100) {
y = -100;
}
}
// Draw the object
void display() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}
x = random(width);
y = random(-500, -100);
s = s_;
speed = random(2, 6);
}
// Simple motion
void move() {
y+=speed;
if (y > height+100) {
y = -100;
}
}
// Draw the object
void display() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}
1