This site requires JavaScript, please enable it in your browser!
Greenfoot back
matt.milan
matt.milan wrote ...

2012/3/15

Mouse Movement Error

matt.milan matt.milan

2012/3/15

#
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();
    }
}
Duta Duta

2012/3/15

#
Currently getMouseInfo() returns null if the mouse hasn't moved since last time it was called, which may be causing the change in direction
davmac davmac

2012/3/15

#
Your code doesn't actually compile; 'updateImage()' method is missing, as is the definition for 'b' variable. Other than that, this code works fine. I commented out references to missing parts, and the particle doesn't change direction when the mouse stops moving.
matt.milan matt.milan

2012/3/15

#
i sort of cut it down from the original code, trying to only include the methods i thought were bugging out. guess i cut too much. if it works for you, then maybe the error's in one of those methods i didnt include. i'll take another look at it
Duta Duta

2012/3/15

#
Also atWorldEdge() could be made into just:
public boolean atWorldEdge()
{
	return getX() < img.getWidth()
		|| getY() < img.getHeight()
		|| getX() > getWorld().getWidth() - getImage().getWidth()
		|| getY() > getWorld().getHeight() - getImage().getHeight());
}
What you're effectively doing currently is saying:
if(something == true)
	return true;
else
	return false;
When instead you can just return the boolean (in my example "something").
matt.milan matt.milan

2012/3/16

#
haha yeah that's pretty redundant...thanks for the optimization tip, i'm gonna switch that now. i guess i should have seen that earlier
You need to login to post a reply.