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.
// 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);
}
// 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);
}
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;
}
}
}
// 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;
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;
}
}
Does anyone know how i set Pshader as my background as im using the nebula sample as my background image and i want my polygon shapes to appear on top of the nebula, i can get either the nebula effect to appear inside of my polygon shapes or i just get the nebula but no polygon shapes ill show u the code and hopfully some one can help this newbie out.
kind regards
carl reeves
/**
* PolygonPShapeOOP.
*
* Wrapping a PShape inside a custom class
* and demonstrating how we can have a multiple objects each
* using the same PShape.
*/
PShader nebula;
// 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() {
background(255);
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);
// Display and move them all
for (Polygon poly : polygons) {
poly.display();
poly.move();
}
}