my code sets the rotation of new particles to face away from the current mouse position. this works fine when the mouse is moving. when the mouse stops moving, the particles change direction.
i am trying to make it so they continue to go in the direction facing away from the "last" mouse position.
import greenfoot.*; import java.awt.Color; public class Particle extends Actor { private GreenfootImage img; private Color myColor; private MouseInfo target; private int oldX, oldY; public Particle() { target = Greenfoot.getMouseInfo(); img = new GreenfootImage(25,25); myColor = new Color(0,0,255); img.setColor(myColor); img.drawOval(10,10,10,10); setImage(img); setRotation(Greenfoot.getRandomNumber(359)); } public void act() { updateMouseInfo(); if (target != null) setRotation(fleeMouse()); else setRotation(fleeMouseBackup()); move(5); updateImage(); if (atWorldEdge() || b < 20) getWorld().removeObject(this); } public boolean atWorldEdge() { if (getX() < img.getWidth() || getY() < img.getHeight() || getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight()) { return true; } else { return false; } } public int fleeMouse() { if(target != null) { int xDiff = target.getX() - getX(), yDiff = target.getY() - getY(); double angle = Math.toDegrees(Math.atan2(yDiff, xDiff)); int beforeReversed = (int)Math.round(angle); beforeReversed += 180; if (beforeReversed > 360) { beforeReversed -= 360; } return beforeReversed; } else return 0; } public int fleeMouseBackup() { int xDiff = oldX - getX(), yDiff = oldY - getY(); double angle = Math.toDegrees(Math.atan2(yDiff, xDiff)); int beforeReversed = (int)Math.round(angle); beforeReversed += 180; if (beforeReversed > 360) { beforeReversed -= 360; } return beforeReversed; } public void updateMouseInfo() { if (target!=null) { oldX = target.getX(); oldY = target.getY(); } target = Greenfoot.getMouseInfo(); } }