Hey, I got a task from the university and I don’t know, why I cant detect the collision between my avatar with the 5 ghosts. some lines under voidDraw. Does anyone see the problem?
Thanks
Here is my Code:
// AVATAR
float positionX;
float positionY;
float speed;
int radius;
int hue = 0;
//COINS
int coinsSize = 15;
int coinsNum = 16;
// 05.03 CHANGE DEFINITION
boolean[] coinsActive;
// 05.02 ADD VARIABLES FOR THE TILE SIZE
int tileWidth;
int tileHeight;
//GHOST
int numGhost = 5;
float[] positionX1 = new float[numGhost];
float[] positionY1 = new float[numGhost];
float[] stepX = new float[numGhost];
float[] stepY = new float[numGhost];
float[] hue1 = new float[numGhost];
float[] radius1 = new float[numGhost];
boolean[] ghostActive;
void setup()
{
size(512, 512);
colorMode(HSB, 360, 100, 100);
background(0, 0, 0);
fill(200, 100, 100);
noStroke();
positionX = 0.5 * width;
positionY = 0.5 * height;
radius = 15;
speed = 5;
initCoins();
//GHOST
for(int i = 0; i < numGhost; i++)
{
positionX1[i] = width/2;
positionY1[i] = height/2;
stepX[i] = random(-1, 1);
stepY[i] = random(-1, 1);
hue1[i] = 360;
radius1[i] = 25;
}
}
void draw()
{
background(0, 0, 0);
updateCoins();
updateAvatar();
//Ghost Avatar Collusion
if (dist(positionX, positionY, positionX1[i], positionY1[i] < radius + radius1[i])
{
print("Pacman Dead");
}
//GHOST
for(int i = 0; i < numGhost; i++)
{
if(positionX1[i] < 0 || positionX1[i] >= width)
{
stepX[i] *= -1;
}
if(positionY1[i] < 0 || positionY1[i] >= height)
{
stepY[i] *= -1;
}
positionX1[i] += stepX[i];
positionY1[i] += stepY[i];
fill(hue1[i], 100, 100);
ellipse(positionX1[i], positionY1[i], radius1[i], radius1[i]);
}
}
void updateAvatar()
{
// AVATAR
hue++;
if(hue >= 360)
{
hue = 0;
}
fill(hue, 100, 100);
ellipse(positionX, positionY, 2*radius, 2*radius);
}
void initCoins()
{
// 05.02 COMPUTE THE TILE WIDTH AND HEIGHT
tileWidth = width / coinsNum;
tileHeight = height / coinsNum;
// 05.03 GIVING THE ARRAY THE CORRECT SIZE
coinsActive = new boolean[coinsNum * coinsNum];
// 05.03 ITERATING WITH THE CORRECT SIZE
for(int i = 0; i < coinsNum * coinsNum; i++)
{
//REMOVE RANDOM POSITIONS
coinsActive[i] = true;
}
}
void updateCoins()
{
// 05.03 NEW COIN COUNTER VARIABLE
int coinCounter = 0;
// 05.02 ADD 2D LOOP FOR PLACING COINS EVENLY
for (int y = 0; y < height; y += tileHeight)
{
for (int x = 0; x < width; x += tileWidth)
{
// 05.03 CHECK FOR COIN-AVATAR COLLISION
if(checkCollisionAvatarCoin(x, y))
{
coinsActive[coinCounter] = false;
}
else
{
if(coinsActive[coinCounter])
{
fill(40, 100, 100);
ellipse(x + tileWidth *0.5, y + tileHeight *0.5, coinsSize, coinsSize);
}
}
// 05.03 INCREASE COIN COUNTER
coinCounter++;
}
}
}
// 05.03 CHECK FOR COLLISION BETWEEN AVATAR AND X and Y
boolean checkCollisionAvatarCoin(int coinX, int coinY)
{
if(positionX >= coinX - coinsSize && positionX <= coinX + coinsSize &&
positionY >= coinY - coinsSize && positionY <= coinY + coinsSize)
{
return true;
}
else
{
return false;
}
//Ghost Avatar Collusion
if (dist(positionX, positionY, positionX1[i], positionY1[i] < 15 + 25)
{
println("Pacman Dead");
}
}
// Called if a key was
// pressed
void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP)
{
if(positionY >= radius)
{
positionY -= speed;
}
}
else if (keyCode == DOWN)
{
if(positionY <= (height - radius))
{
positionY += speed;
}
}
else if (keyCode == LEFT)
{
if(positionX >= radius)
{
positionX -= speed;
}
}
else if (keyCode == RIGHT)
{
if(positionX <= (width - radius))
{
positionX += speed;
}
}
}
else
{
// Clearing the screen
if(key == 'c')
{
background(0, 0, 100);
}
}
}