// Update heading direction public void updateMovement(Pointt destination) { this.destination = destination; finalAngle = Geometry.arcTan( destination.getY() - position.getY(), destination.getX() - position.getX(), destination.getX() - position.getX()); }
@Override public void setPosition(Pointt position) { super.setPosition(position); generatePaint(); double startAngle = Maths.randomAngle(); GeneralPath tempRep = new GeneralPath(); Pointt next = new Pointt(0, 0); for (int i = 0; i < vertices.length; i++) { vertices[i] = next.getArcPointt( radius() * Geometry.DISPLAY_REAL_RATIO, startAngle + i * 2 * (Math.PI / NUMBER_OF_VERTICES)); } tempRep.moveTo(vertices[0].getX(), vertices[0].getY()); for (int i = 0; i < vertices.length; i++) { next = vertices[(i + 1) % vertices.length]; tempRep.quadTo( vertices[i].midPoint(next).getX() + Maths.randomNegative(radius() * Geometry.DISPLAY_REAL_RATIO / 2), vertices[i].midPoint(next).getY() + Maths.randomNegative(radius() * Geometry.DISPLAY_REAL_RATIO / 2), next.getX(), next.getY()); } tempRep.closePath(); rep = new Area(tempRep); for (int i = 0; i < vertices.length; i++) { vertices[i].translate(position); } }
private double tangentialAngle(Units testUnit) { double output; double side = (destination.getY() - position.getY()) * (position.getX() - testUnit.position.getX()) - (position.getY() - testUnit.position.getY()) * (destination.getX() - position.getX()); // Change angle to the tangential angle output = Geometry.arcTan( position.getY() - testUnit.position.getY(), position.getX() - testUnit.position.getX(), position.getX() - testUnit.position.getX()) - Math.PI / 2; if (side >= 0) { output += Math.PI; } return output; }
@Override public void plot(Graphics2D a, AffineTransform transform, Pointt focus) { if (activate()) { a.setTransform(transform); Pointt display = displayPosition(focus); a.translate(display.getX(), display.getY()); if (ELAPSE_TIME - elapsedTime() < DISAPPEAR_TIME) { if (ELAPSE_TIME - elapsedTime() > 0) { a.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, (ELAPSE_TIME - elapsedTime()) / (float) DISAPPEAR_TIME)); } else { return; } } else { a.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f)); } a.setPaint(paint); a.fill(getRep()); a.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); if (Math.random() < 0.5) { synchronized (game().visualEffects()) { game() .visualEffects() .add( new UniversalEffect( position().randomRange(25), Sarcophagidae.repInstances[0], Sarcophagidae.standardColors, APPEAR_TIME_VISUAL / 100, APPEAR_SPEED_VISUAL, Maths.randomAngle(), UniversalEffect.STAND_STILL, UniversalEffect.FADING, UniversalEffect.FIX_SCALE)); } } } }
/** Movement given no collision */ public synchronized void moveNoCollision(double time) { if (time <= 0) { throw new InvalidParameterException("Cannot move the unit, time <= 0!"); } if (Double.isNaN(time)) { throw new RuntimeException( "time is NaN. speed is " + speed + " moving angle is " + movingAngle); } // Check if the unit is facing the required direction if (movingAngle == finalAngle) { // Already facing that direction if (this.destination != null) { if (speed * time >= position.distance(destination)) { xVelocity = 0; yVelocity = 0; position = destination; return; } } xVelocity = speed * Math.cos(movingAngle); yVelocity = speed * Math.sin(movingAngle); position.setX(Geometry.fixX(position.getX() + xVelocity * time)); position.setY(Geometry.fixY(position.getY() + yVelocity * time)); } else { // Need to turn to that direction double distance = finalAngle - movingAngle; double alternative = -(2 * Math.PI - finalAngle + movingAngle); if (distance < 0) { while (alternative <= -Math.PI) { alternative += 2 * Math.PI; } } else { while (alternative >= Math.PI) { alternative -= 2 * Math.PI; } } double angularDisplacement; if (Math.abs(distance) < Math.abs(alternative)) { angularDisplacement = distance; } else { angularDisplacement = alternative; } if (angularSpeed * time < Math.abs(angularDisplacement)) { // Does not finish turning movingAngle += ((angularDisplacement) / Math.abs(angularDisplacement)) * angularSpeed * time; xVelocity = 0; yVelocity = 0; } else { // Finish turning with extra or no time left movingAngle = finalAngle; if (Double.isNaN(angularSpeed)) { throw new RuntimeException( "Angular speed is NaN. angulardisplacement is " + angularDisplacement); } if (Double.isInfinite(angularSpeed)) { throw new RuntimeException( "Angular speed is isInfinite. angulardisplacement is " + angularDisplacement); } if (Double.isNaN(angularDisplacement)) { throw new RuntimeException("angularDisplacement is NaN. angularSpeed is " + angularSpeed); } if (Double.isNaN(time - Math.abs(angularDisplacement) / angularSpeed)) { throw new RuntimeException( "Next input is NaN. angularSpeed is " + angularSpeed + " angularDisplacement is " + angularDisplacement + "time is " + time); } moveNoCollision(time - Math.abs(angularDisplacement) / angularSpeed); } } }