class Landscape { ArrayList walls; // lunar PVector[] wallVectors1 = { new PVector(0, 240), new PVector(170, 240), new PVector(170, 240), new PVector(200, 420), new PVector(200, 420), new PVector(260, 420), new PVector(260, 440), new PVector(500, 440), // safezone new PVector(500, 420), new PVector(560, 420), new PVector(560, 420), new PVector(800, 350), }; // apine PVector[] wallVectors2 = { new PVector(0, 240), new PVector(190, 240), new PVector(190, 240), new PVector(250, 370), new PVector(250, 370), new PVector(260, 350), new PVector(260, 350), new PVector(500, 350), // safezone new PVector(500, 350), new PVector(540, 250), new PVector(540, 250), new PVector(800, 300), }; PVector[][] gameWallVectors = {null, wallVectors1, wallVectors2}; Boolean[] wallTypes = { true, true, true, false, // safezone true, true, }; PImage bg; Landscape(){ walls = new ArrayList(); bg = loadImage("images/background"+iGameSkin+".jpg"); /* create standard moon lander surfaces */ for (int n = 0; n < gameWallVectors[iGameSkin].length; n+=2) { createWall( gameWallVectors[iGameSkin][n], gameWallVectors[iGameSkin][n+1], wallTypes[n/2]); } } // create, add new wall to the array void createWall(PVector a, PVector b, boolean danger) { walls.add(new Wall(a, b, danger)); } /* Draws and collision detects the ball */ void draw() { background(bg); for (int n = 0; n < walls.size(); n++) { Wall w = (Wall)walls.get(n); //don't show walls - they're for detection only : w.display(127); // check if one of the line points has been click // w.startPointButton.update(mouseX, mouseY); // w.endPointButton.update(mouseX, mouseY); // collision detection call if(circleWallCollision(lander.position, w.getStartPoint(), w.getSlope())){ // normal bounce //resolveFixedCollision(lander.velocity, wallNormal); // lander: check what type of wall if(w.isDangerous()){ // lose lander.Crash("You crashed into something!"); } else { // win! lander.Land(); } } } } /** * WALL COLLISION DECTECTION STUFF * this function takes 3 parameters, and uses the below other 2 fuctions * 1st position vector * 2nd vector starting point * 3rd vector slope */ boolean circleWallCollision(PVector position, PVector wallStartPoint, PVector wall) { boolean bResult = false; // variables to check if position vector is inside slope bounds float d1x = position.x - wallStartPoint.x; float d1y = position.y - wallStartPoint.y; float d2x = position.x - (wallStartPoint.x + wall.x); float d2y = position.y - (wallStartPoint.y + wall.y); // check if the position of the ball is inside the boundaries of the wall (slope) // if so do the collision stuff if((sqrt(d1x * d1x + d1y * d1y) < wall.mag()) && (sqrt(d2x * d2x + d2y * d2y) < wall.mag())){ PVector wallNormal = new PVector(-wall.y, wall.x); wallNormal.normalize(); // penetration factor float a = PVector.sub(wallStartPoint, position).dot(wallNormal); // check for collision bResult = (abs(a) < lander.radius); } return bResult; } void resolveFixedCollision(PVector vector, PVector directionVector) { PVector c = PVector.mult(componentVector(vector, directionVector), 2); PVector v = PVector.sub(vector, c); vector.x = v.x; vector.y = v.y; } PVector componentVector(PVector vector, PVector directionVector) { PVector v = new PVector(directionVector.x, directionVector.y); v.normalize(); v.mult(vector.dot(v)); return v; } }