I found a work around for my first problem, not what i want as an end result but will work for the moment.
Although I have encounter a new problem and can not figure it out for the life of me.
The collisions seem to only work for one cube, if the player hits the cube will collisions working then both cubes care affected by the collision (turn black) but only one cube can trigger it. I have a feeling it may be because my second cube has a different X position. But I had to do that so I could assign a second random so that the second cube doesn't appear over the top of the first one. Does anyone know how I can get the collision to work with more then one cube?
here is the code.
- int spawnpos = (int) random(600);
- int spawnpos2 = (int) random(600);
- int playerX, playerY;
- int playerSize = 30;
- int movespeed = 2;
- int fallspeed = 4;
- int cubeX, cubeY, cubeX2;
- int cubeW = 50;
- int cubeH = 50;
- void setup() {
-
- size(600, 600);
- smooth();
- noStroke();
- rectMode(CENTER);
-
-
- playerX = spawnpos;
- playerY = 560;
-
- cubeX = spawnpos;
- cubeX2 = spawnpos2;
- cubeY = -50;
-
-
-
-
- }
- int cubecount = 0;
- void draw() {
- int spawnpos = (int) random(600);
- int spawnpos2 = (int) random(600);
-
- background(255);
-
- if (rectBall(cubeX, cubeY, cubeW, cubeH, playerX, playerY, playerSize) == true) {
- fill(0);
- }
- else {
- fill(200);
- }
-
- rect(cubeX, cubeY, cubeW, cubeH);
- rect(cubeX2, cubeY, cubeW, cubeH);
-
- cubeY = cubeY + fallspeed;
-
- if (cubeY > 650){
- cubeX2 = spawnpos2;
- cubeX = spawnpos;
- cubeY = -50;
- }
-
-
-
-
- fill(255, 0, 0);
- ellipse(playerX, playerY, playerSize, playerSize);
-
- if (keyPressed) {
- if (key == CODED) {
- if (keyCode == LEFT) {
- playerX -= movespeed;
- }
- if (keyCode == RIGHT) {
- playerX += movespeed;
- }
- if (keyCode == UP) {
- playerY -= movespeed;
- }
- if (keyCode == DOWN) {
- playerY += movespeed;
- }
- }
- }
- }
- boolean rectBall(int rx, int ry, int rw, int rh, int bx, int by, int d) {
-
- if (bx+d/2 >= rx-rw/2 && bx-d/2 <= rx+rw/2 && abs(ry-by) <= d/2) {
- return true;
- }
- else if (by+d/2 >= ry-rh/2 && by-d/2 <= ry+rh/2 && abs(rx-bx) <= d/2) {
- return true;
- }
- float xDist = (rx-rw/2) - bx;
- float yDist = (ry-rh/2) - by;
- float shortestDist = sqrt((xDist*xDist) + (yDist * yDist));
- xDist = (rx+rw/2) - bx;
- yDist = (ry-rh/2) - by;
- float distanceUR = sqrt((xDist*xDist) + (yDist * yDist));
- if (distanceUR < shortestDist) {
- shortestDist = distanceUR;
- }
- xDist = (rx+rw/2) - bx;
- yDist = (ry+rh/2) - by;
- float distanceLR = sqrt((xDist*xDist) + (yDist * yDist));
- if (distanceLR < shortestDist) {
- shortestDist = distanceLR;
- }
- xDist = (rx-rw/2) - bx;
- yDist = (ry+rh/2) - by;
- float distanceLL = sqrt((xDist*xDist) + (yDist * yDist));
- if (distanceLL < shortestDist) {
- shortestDist = distanceLL;
- }
- if (shortestDist < d/2) {
- return true;
- }
- else {
- return false;
- }
- }
Thanks.