// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Decorations public void newDecos() { int prob = (int) (Math.random() * 100); // randomly spawnst he decoration if (prob == 1) { if (decoList.size() < 1) { int prob2 = (int) (Math.random() * 2); if (prob2 == 1) { // right side or left side decoList.add(new Decorations(-700, (int) (backy * 0.1) % 23080, false)); } else { decoList.add(new Decorations(-700, (int) (backy * 0.1) % 23080, true)); } } } Boolean check = true; for (Decorations i : decoList) { if (i.getYTop() >= 2000) { check = false; break; } } if (check == false && decoList.size() > 0) { // theres only on in the lsit but for consitency we kept it as a list decoList.remove(0); } }
/* -------------------------------------------------------------*/ private void objectCollision(ArrayList movingObjects) { for (int i = 0; i < movingObjects.size(); i++) { // make sure not testing if collided with yourself :P if (((SpaceObject) movingObjects.get(i)).getObjCount() != objectNum) { SpaceObject object = (SpaceObject) movingObjects.get(i); // find distance vector between two objects int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // has it collided with the object? if (distance < (radius + object.getRadius())) { // has it collided with a BULLET (or MISSILE)? if (object.isBullet()) { // do nothing } // is it another SPACESHIP? (INSTANT DEATH) else if (object.isSpaceShip()) { // do nothing } // collided with anything else (e.g PLANET): (INSTANT DEATH) else { collision.play(); kill(movingObjects); // object has died } } } } // end for loop }
/* -------------------------------------------------------------*/ private void gravityEffect(ArrayList movingObjects) { // find effect of gravity on this objects from all other objects for (int i = 0; i < movingObjects.size(); i++) { // reset variables double add_vx = 0.0; double add_vy = 0.0; SpaceObject object = (SpaceObject) movingObjects.get(i); if (object.getObjCount() != objectNum && // ignore yourself (distance = 0!) !object.isBullet() && // ignore bullets !object.isSpaceShip()) // ignore spaceships { // find distance vector between planet and ball int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // find effect of planet on velocity double add_vec = (SpaceObject.G * k * object.getMass() * ballMass) / (distance * distance); add_vx = -(x / distance) * add_vec; add_vy = -(y / distance) * add_vec; // add objects speeds onto spaceship speed (clip speed if too large) x_speed += add_vx; y_speed += add_vy; } } }
public void checkEnemyCollision() { for (Enemy e : enemyList) { if (e.getOnScreen()) { // can be removed later on // goes through all the enemies and checks if they collide if (e.checkCollision(e.getPics().get(e.getCounter()), player1)) { if (player1.getInvi() == false) { // If the player is not invisble then you get spiked. if (player1.getVelocity() > 0) { // if the player hits it from the bottom player1.setSpikeVelo(); loseCoins(); } else { player1.setVelo( 50); // if the player is on top instead the player bounces off the enemy if (musicOn) { bounce.play(); } } } eRemove.add(e); // once we hit, we remove the enemy } } else { eRemove.add(e); // if the enemy goes of the screen, we remove } } for (Enemy e : eRemove) { poofList.add(new Poof(e.getX(), e.getY(), 1)); // removes all the enemies enemyList.remove(e); } eRemove = new ArrayList<Enemy>(); }
public void checkStarCollision() { for (Star s : starList) { if (s.getOnScreen()) { // if the star is on the screen we need to check if player collides if (s.checkCollision( s.getPics().get(s.getCounter()), player1)) { // if the player collides with the star we remove it then change the // velosity to the distance that the star provides sRemove.add(s); // remove star once you collide with it player1.setVelo(s.getDist()); // changes the velocity player1.setInvi(true); // sets the player invisble for a few seconds score += s.getPoints(); // points increase by the star type if (musicOn) { starSound.play(); // playthe star sound } } } else { sRemove.add(s); // remove the star if its not on the screen } } for (Star s : sRemove) { poofList.add(new Poof(s.getX(), s.getY(), s.getNum() + 3)); // make the poof effect starList.remove(s); } sRemove = new ArrayList<Star>(); }
public void trace(float x, float y) { println(x); if (frameCounter > 2 && mousePressed) { coords.add(new PVector(x, y)); frameCounter = 0; if (coordCounter > 0) { PVector tempCoord = (PVector) coords.get(coordCounter - 1); line(x, y, tempCoord.x, tempCoord.y); } coordCounter++; } // println(coordCounter); for (int i = 0; i < coords.size(); i++) { PVector tempCoordnear = (PVector) coords.get(i); float coordDist = dist(x, y, tempCoordnear.x, tempCoordnear.y); if (coordDist < distance) { stroke(255, 0, 0); line(x, y, tempCoordnear.x, tempCoordnear.y); } } frameCounter++; }
public void setup() { size(600, 400); smooth(); f = createFont("Georgia", 12, true); obstacles = new ArrayList(); boids = new ArrayList(); // A little algorithm to pick a bunch of random obstacles that don't overlap for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float r = random(50 - i / 2, 50); boolean ok = true; for (int j = 0; j < obstacles.size(); j++) { Obstacle o = (Obstacle) obstacles.get(j); if (dist(x, y, o.loc.x, o.loc.y) < o.radius + r + 20) { ok = false; } } if (ok) obstacles.add(new Obstacle(x, y, r)); } // Starting with three boids boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.2f)); boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.1f)); boids.add(new Boid(new PVector(random(width), random(height)), 2f, 0.05f)); }
// Does a deepcopy of an array list public static ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { alNew.add(((CelestialObject) al.get(i)).clone()); } return alNew; }
public ArrayList getPastObjectStates() { ArrayList alPast = new ArrayList(); for (int i = intTimeIdx; i > 0 && i > intTimeIdx - 100; i--) { alPast.add(this.alObjectStateArchive.get(i)); } return alPast; }
// Does a deepcopy of an array list public ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { PVector pv = (PVector) al.get(i); alNew.add(new PVector(pv.x, pv.y)); } return alNew; }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
public void draw() { background(255); for (int i = 0; i < balls.size(); i++) { Ball ball = (Ball) balls.get(i); ball.calc(); ball.display(); } }
public void mouseClicked() { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { println(obj.getName() + " clicked!"); break; } } }
// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Patterns public void newPattern() { int prob = (int) (Math.random() * probs.get(level - 1) + 1); Pattern p = new Pattern(0, -300, prob, (int) (backy * 0.1)); if (checkSpawn(new Rectangle(p.getX(), p.getY(), p.getWidth(), p.getHeight())) == true) { for (Coin i : p.getCoins()) { coinList.add(i); } for (Star i : p.getStars()) { starList.add(i); } for (Box i : p.getBoxes()) { boxList.add(i); } for (Jumper i : p.getJumpers()) { jumperList.add(i); } for (Enemy i : p.getEnemies()) { enemyList.add(i); } for (Spikes i : p.getSpikes()) { spikeList.add(i); } for (Powerup i : p.getPowerUps()) { pupList.add(i); } for (Rectangle i : p.getRects()) { rectList.add(i); } } }
// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Poofs public void removePoof() { // once the poof animation is the poof is removed from the list for (Poof p : poofList) { if (p.isDone()) { pRemove.add(p); } } for (Poof p : pRemove) { poofList.remove(p); } pRemove = new ArrayList<Poof>(); }
public ArrayList getFutureObjectStates() { ArrayList alFutureArchive = new ArrayList(); ArrayList alFutureObjects = cloneArrayList(alStatefulObjects); for (int i = 0; i < 100; i++) { sim.calculateForces(alFutureObjects); alFutureArchive.add(cloneArrayList(alFutureObjects)); } return alFutureArchive; }
/** Update all of the Ball's and draw them */ public void update() { if (balls.size() != 0) { for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.update(); b.attract = kelly; b.drawBall(); } } }
public void tick() { if (inGame) { level.tick( (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2); character.tick(); sky.tick(); health.tick(); for (int i = 0; i < mob.toArray().length; i++) { mob.get(i).tick(); } } }
public void draw() { background(0); fill(255); if (!paused) timeline.moveForward(); if (dragging) drawOrbits(timeline.getFutureObjectStates()); else drawOrbits(timeline.getPastObjectStates()); ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); obj.display(); } }
public void draw() { // background(255); fill(255); rect(-4, -4, width + 4, height + 4); for (int i = skaters.size() - 1; i >= 0; i--) { Skater skater = (Skater) skaters.get(i); // if (mousePressed) { skater.trace(mouseX, mouseY); // blob[i].x etc. println(i); } }
/** If a key is pressed perform the respective actions */ public void keyPressed() { // Add 'stems' to the balls if (keyCode == SHIFT) { stems = !stems; for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.STEM = stems; } } // toggle repaint background else if (key == 'b') REPAINT = !REPAINT; // Empty the ArrayList of Balls else if (key == 'x') balls.clear(); // Add a ball else if (key == 'f') addBall(); }
public int moveForward() { // println("forward!"); intTimeIdx++; // if the future values have already been calculated, just fetch them instead of calculating // them again if (alObjectStateArchive.size() > intTimeIdx) setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); else { // println("calculating..."); sim.calculateForces(alStatefulObjects); alObjectStateArchive.add(cloneArrayList(alStatefulObjects)); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public void calculateForces(ArrayList objects) { for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); ArrayList forces = obj.getForces(); float totalForceX = 0; float totalForceY = 0; for (int j = 0; j < forces.size(); j++) { totalForceX += ((PVector) forces.get(j)).x; totalForceY += ((PVector) forces.get(j)).y; } PVector newAccel = new PVector(totalForceX / obj.getMass(), totalForceY / obj.getMass()); obj.setAcceleration(newAccel); } for (int i = 0; i < objects.size(); i++) { CelestialObject obj1 = (CelestialObject) objects.get(i); float forceX = 0; float forceY = 0; obj1.clearForces(); if (obj1.getClass() == Star.class) { println(obj1.getVelocity()); continue; } for (int j = 0; j < objects.size(); j++) { CelestialObject obj2 = (CelestialObject) objects.get(j); if (i == j) continue; PVector pvDistance = PVector.sub(obj2.getPosition(), obj1.getPosition()); // println("distance: x:" + pvDistance.x + " y:" + pvDistance.y); float distance = sqrt(sq(pvDistance.y) + sq(pvDistance.x)); float angle = degrees(atan2(pvDistance.y, pvDistance.x)); float force = (G * obj1.getMass() * obj2.getMass()) / sq(distance); forceX = force * cos(radians(angle)); forceY = force * sin(radians(angle)); // println("FORCES on " + obj1.getName() + ":" + forceX + "," + forceY); obj1.addForce(new PVector(forceX, forceY)); println(); } } }
public void mouseDragged() { if (paused) { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { dragging = true; PVector pos = obj.getPosition(); pos.x = mouseX; pos.y = mouseY; timeline.reset(); timeline.setCurrentState(objects); sliderTimeline.setValue(0); break; } } } }
public void draw() { if (_pointLists.size() <= 0) return; pushStyle(); noFill(); PVector vec; PVector firstVec; PVector screenPos = new PVector(); int colorIndex = 0; // draw the hand lists Iterator<Map.Entry> itrList = _pointLists.entrySet().iterator(); while (itrList.hasNext()) { strokeWeight(2); stroke(_colorList[colorIndex % (_colorList.length - 1)]); ArrayList curList = (ArrayList) itrList.next().getValue(); // draw line firstVec = null; Iterator<PVector> itr = curList.iterator(); beginShape(); while (itr.hasNext()) { vec = itr.next(); if (firstVec == null) firstVec = vec; // calc the screen pos context.convertRealWorldToProjective(vec, screenPos); vertex(screenPos.x, screenPos.y); } endShape(); // draw current pos of the hand if (firstVec != null) { strokeWeight(8); context.convertRealWorldToProjective(firstVec, screenPos); point(screenPos.x, screenPos.y); } colorIndex++; } popStyle(); }
public void setup() { size(800, 600); smooth(); strokeWeight(1); skaters = new ArrayList(); skaters.add(new Skater()); }
public void setup() { size(320, 240); smooth(); // Start with 10 balls balls = new ArrayList(); for (int i = 0; i < 10; i++) { Ball ball = new Ball(random(width), random(height)); balls.add(ball); } }
public void checkJumperCollision() { // same as all the other collsions for (Jumper j : jumperList) { if (j.getOnScreen()) { // if (j.checkCollision(j.getImage(), player1)) { player1.setVelo(50); player1.setDown(false); if (musicOn) { bounce.play(); } } } else { jRemove.add(j); } } for (Jumper j : jRemove) { jumperList.remove(j); } jRemove = new ArrayList<Jumper>(); }
public void checkSpikeCollision() { // checks the spike collision for (Spikes s : spikeList) { if (s.getOnScreen()) { // if the spike is on the screen if (s.checkCollision(s.getImage(), player1)) { // if the user collides with the spike if (player1.getInvi() == false) { // if player is not invisiblity player1.setSpikeVelo(); // set the spike velocity loseCoins(); player1.setDown(true); } spRemove.add(s); } } else { spRemove.add(s); // remove the spike } } for (Spikes s : spRemove) { spikeList.remove(s); } spRemove = new ArrayList<Spikes>(); }
public void draw() { background(255); // Turn off highlighting for all obstalces for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.highlight(false); } // Act on all boids for (int i = 0; i < boids.size(); i++) { Boid b = (Boid) boids.get(i); b.avoid(obstacles); b.run(); } // Display the obstacles for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.display(); } // Instructions textFont(f); fill(0); text( "Hit space bar to toggle debugging lines.\nClick the mouse to generate a new boids.", 10, height - 30); }