Hi, I’m new to the world of game development with Processing and I am struggling to get my code to work. I have a good portion of it working but I cannot figure out collision detection.
Here’s the main loop:
//Create 16 spaceship objects
Spaceships[] ships = new Spaceships[16];
//Create the player object
Player p;
Bullet aBullet;
//float spacing = 75;
float x = random(50, width);
float y = 100;
float spacing = 75;
int n;
int px = width/2;
int py = 650;
boolean hasCollided = false;
//boolean isShooting = false;
float shipX;
float shipY;
void setup(){
size(680, 680);
//Initialize ships
for(int i = 0; i < ships.length; i++){
ships[i] = new Spaceships(x, y, 50, 1, color(150)); //X coordinate, Y coordinate, size, speed, color
//Change spacing of ships
x += spacing;
//If x is greater than or equal to width, add y spacing to make new line
if(x >= width - 50 && y >= 100){
y += spacing;
x = 50;
}
}
p = new Player(px, py, 2, 25, color(random(0, 255), random(0, 255), random(0, 255))); //X, Y, Speed, Size, Color
aBullet = new Bullet();
}
void draw(){
background(50, 100, 100);
//float factor = constrain(mouseX/10, 0, 5);
for(int i = 0; i < ships.length; i++){
ships[i].display();
ships[i].move();
ships[i].hitBox();
//p.keyPressed();
}
p.display();
p.move();
// aBullet.move();
aBullet.update();
aBullet.bulletCollision();
//aBullet.bulletCollision();
//aBullet.crash();
//aBullet.display();
}
void keyPressed(){
if(key == 'h'){
aBullet = new Bullet();
aBullet.update();
}
}
Here’s the bullet class:
class Bullet{
int x;
int y;
int xi;
int yi;
boolean isShooting = false;
//float speed = -10;
//float speed;
Bullet(){
this.x = p.x;
this.y = p.y;
//y = constrain(yi, 0, 0);
//this.speed = -10;
//speed = tempSpeed;
}
void display(){
rectMode(CENTER);
stroke(255);
fill(255);
rect(p.x, y - 15, 5, 5);
//y = y - speed;
// y += -speed;
}
void move(){
y = y - 10;
}
void update(){
move();
display();
//crash();
//bulletCollision();
}
void bulletCollision(){
if(x == shipX && y == shipY){
println("COLLISION");
}
}
/*
boolean crash(){
color detectedColor;
for (int i = y; i <= y; i++)
{
detectedColor = get(y, i);
if (detectedColor == color(0))
//println("COLLISION HAPPENED");
{
println("COLLISION AT " + y);
return true;
}
}
//println("NO COLLISION DETECTED");
return false;
}
*/
/*
public void simulate(){
p.x += v.x;
p.y += v.y;
bulletCollision();
}
*/
} //End Bullet class
Here’s the player class:
class Player{
//Player variables
//Go here
int x;
int y;
int xSpeed;
float pSize;
color c;
float bx;
float by;
float bSpeed = 1;
Player(int tempX, int tempY, int xSpeed_, float pSize_, color c_){
x = tempX;
y = tempY;
xSpeed = xSpeed_;
pSize = pSize_;
c = c_;
bx = tempX;
by = tempY - 15;
}
void move(){
//x = x + xSpeed;
if(keyPressed){
if(key == 'd'){
x = x + xSpeed;
if(x >= width){x = width;}
}else if(key == 'a'){
x = x - xSpeed;
if(x <= 0){x = 0;}
}//else if(key == 'h'){
// shoot();
//}
}
}// End move()
void display(){
rectMode(CENTER);
noStroke();
fill(c);
rect(x, y, pSize, pSize); //draw the player
rect(x, y - 15, 5, 10); //player's weapon
}
/*
void keyPressed(){
if(key == 'd'){
x += xSpeed;
}else if (key == 'a'){
x = -xSpeed;
}
} */
}
Here’s the spaceship class:
// Spaceships class
class Spaceships{
float x;
float y;
float xSpeed;
//float ySpeed;
float shipSize;
color c;
Spaceships(float tempX, float tempY, float tempShipSize, float tempXSpeed, color c_){
x = tempX;
y = tempY;
xSpeed = tempXSpeed;
//xSpacing = tempXSpacing;
shipSize = tempShipSize;
c = c_;
}
void move(){
x = x + xSpeed;
shipX = x;
shipY = y;
if(x > width){
xSpeed = -xSpeed;
y += 100;
}else if(x < 0){
xSpeed = -xSpeed;
y += 100;
}else if(y > height - 100){
y = height - 100;
}
/*
if(x >= width){
xSpeed *= -0.95;
y += 100;
}else if(x <= 0){
xSpeed *= -0.95;
y += 100;
}
if(y >= height - 100){
y = height - 100;
} */
}
void display(){
float offset = shipSize / 4;
ellipseMode(CENTER);
rectMode(CENTER);
strokeWeight(2);
stroke(0);
fill(c);
ellipse(x, y - 10, shipSize / 2, shipSize / 2);
ellipse(x, y, shipSize, shipSize / 2);
stroke(0);
fill(50, 100, 100);
ellipse(x, y, offset, offset);
ellipse(x + offset + 5, y, offset, offset);
ellipse(x - offset - 5, y, shipSize /4, shipSize / 4);
}
public void hitBox(){
//Ship hitboxes
noStroke();
fill(0, 0, 0, 0);
rect(x, y, shipSize, shipSize - 25); //Ship hitboxes
}
}
I know I’m doing something wrong but I have been hours trying to fix this. Any help is greatly appreciated!
Thank you,
-Mellie