building a certain class
in
Programming Questions
•
1 year ago
I am building a game in processing where a user has to use a light, pointed at a webcam, to guide a ship to a lighthouse while dodging buoys and whales. I need help setting up the ship class to follow the brightest pixel on the screen.
My issue is, It works without the ship being called as an instance, but I need it as an instance of the ship class to calculate the collision detection I need for the buoys and whales whenever they come in contact with the ship.
Whenever I try to pass through arguments I need through my ship class, The instance of the ship only runs once and does not continue to track the brightest pixel on the screen.
I'm stuck because I don't know how to set it up, do I need to pass values through the constructor or leave the function of brightness tracking inside the draw function? Here is my code, and I appreciate the time and effort to help me. (the commented parts are where I tried to implement the ship as a class, but if they're commented out it will work just fine)
//main code
import processing.video.*;
PShape s;
Capture video;
Lighthouse myLighthouse;
Whale [] whaleCollection = new Whale[7];
Buoy [] buoyCollection = new Buoy[23];
//Ship myShip;
void setup()
{
size(800, 600);
video = new Capture(this, width, height, 60);
noStroke();
smooth();
s = loadShape("ship2.svg");
//myShip = new Ship (brightestX, brightestY);
myLighthouse = new Lighthouse(500,400);
for( int i = 0; i < whaleCollection.length; i++){
whaleCollection[i] = new Whale(random(0, width),random(0, height));
}
for( int i = 0; i < buoyCollection.length; i++){
buoyCollection[i] = new Buoy(random(0, width),random(0, 400));
}
}
void draw()
{
if(video.available())
{
video.read();
//video.loadPixels();
background(23,194,219);
//myShip.run();
myLighthouse.run();
for( int i = 0; i < whaleCollection.length; i++){
whaleCollection[i].run();
}
for( int i = 0; i < buoyCollection.length; i++){
buoyCollection[i].run();
}
int brightestX = 0;
int brightestY = 0;
float brightestValue = 0;
video.loadPixels();
int index = 0;
for(int y = 0; y < video.height; y++)
{
for(int x = 0; x < video.width; x++)
{
int pixelValue = video.pixels[index];
float pixelBrightness = brightness(pixelValue);
if(pixelBrightness > brightestValue)
{
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
}
index++;
//myShip.run();
}
}
//fill(255, 204, 0, 128);
//ellipse(brightestX, brightestY, 50, 50);
shape(s,brightestX, brightestY, 100, 100);
//myShip.run();
}
}
//Ship Class
class Ship{
float x = brightestX;
float y = brightestY;
//float brightestX = 0;
//float brightestY = 0;
//float brightestValue = 0;
PShape s;
Ship(float _x, float _y){
x = _x;
y = _y;
s = loadShape("ship2.svg");
}
void run(){
//brightnessFunction();
display();
}
void display(){
shape(s,x,y, 80, 80);
}
1