/** * Mix the colors of the two bodies, and weight the colors. For Example: If a big white body hits * a small red body the resultant body should be pinkish * */ private static Color combineColor(Body m1, Body m2) { double mult; if (m1.getMass() > m2.getMass()) mult = m1.getMass() / m2.getMass(); else mult = m2.getMass() / m1.getMass(); int red = (int) ((m1.getColor().getRed() + m2.getColor().getRed()) / 2 * mult); int blue = (int) ((m1.getColor().getBlue() + m2.getColor().getBlue()) / 2 * mult); int green = (int) ((m1.getColor().getGreen() + m2.getColor().getGreen()) / 2 * mult); if (red > 255) red = 255; if (blue > 255) blue = 255; if (green > 255) green = 255; return new Color(red, blue, green); }
/** 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; }