/** * Updates the effect. * * @param g The Graphics component the particles are being drawn with. * @return the new screenX and screenY shift values */ public static int[] update() { double screenXShift = currentLevel.getScreenXShift(); double screenYShift = currentLevel.getScreenYShift(); long elapsedTime = elapsedTime(); screenXShift += (shakeValues[0] * Math.sin(elapsedTime / shakeValues[1]) * Math.exp(shakeValues[2] * elapsedTime)); screenYShift += (shakeValues[3] * Math.sin(elapsedTime / shakeValues[4]) * Math.exp(shakeValues[5] * elapsedTime)); for (int i = 0; i < particles.size(); i++) { Particle p = particles.get(i); for (Body bod : currentLevel.getBodies()) { if (bod.intersects(p) || bod.getCenter().distanceSquared(p.getCenter()) <= (bod.getRadius() * .5) * (bod.getRadius() * .5) && i != 0) { particles.remove(i); i--; } } for (Blockage blockage : currentLevel.getBlockages()) { if (blockage.intersects(p.getCenter()) && i != 0) { particles.remove(i); i--; } } for (GoalPost gp : currentLevel.getGoalPosts()) { if (p.intersects(gp) && i != 0) { particles.remove(i); i--; } } p.setVelocity(p.getVelocity().multiply(0.99)); p.move(); } xShift = (int) screenXShift; yShift = (int) screenYShift; return new int[] {xShift, yShift}; }
/** Internal routine for generating the List of particles on collision. */ public static ArrayList<Particle> generateParticles(Ball ball, Body body) { double centerX = ball.getCenter().x(); double centerY = ball.getCenter().y(); double speed = ball.getVelocity().magnitude() * ((body != null) ? (body.getRadius() / 100.0) : 1); if (speed < .50) speed = .50; if (speed > 2.50) speed = 2.50; if (body != null) { double max = Math.pow(body.getRadius() + ball.getRadius(), 2); while (body.getCenter().distanceSquared(new Point2d(centerX, centerY)) < max) { centerX -= ball.getVelocity().xComponent(); centerY -= ball.getVelocity().yComponent(); } } ArrayList<Particle> particles = new ArrayList<Particle>(); int num = (int) (300 * speed); if (num < 150) num = 150; else if (num > 400) num = 400; for (int i = 0; i < num; i++) { double angle = (Math.random() * 2 * Math.PI); double xSpeed = 0.75 * (Math.random() * speed * Math.cos(angle)); double ySpeed = 0.75 * (Math.random() * speed * Math.sin(angle)); Color c; double rand = Math.random(); if (body == null) c = CalcHelp.getShiftedColor(ball.getColor(), 100); else if (rand < 0.3) c = CalcHelp.getShiftedColor(body.getColor(), 100); else if (rand < 0.40) c = CalcHelp.getShiftedColor(new Color(230, 130, 70), 100); else c = CalcHelp.getShiftedColor(ball.getColor(), 100); Particle newParticle = new Particle(centerX, centerY, xSpeed, ySpeed, c); particles.add(newParticle); } return particles; }