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

2014/1/9

Problem with deflection code (perhaps the pi range?)

JasonZhu JasonZhu

2014/1/9

#
I have wrote the deflection code as follows:
    private void pushGrease(int range)
    {
        List<GreaseDrop>list = getObjectsInRange(range,GreaseDrop.class);
        double dx,dy;
        for(GreaseDrop p: list){
            dx=p.getX()-this.getX();
            dy=p.getY()-this.getY();   
            double d = Math.sqrt(dx*dx+dy*dy);
            int angle = (int)Math.toDegrees(Math.atan2(dx,dy))+180;
            Vector v = new Vector(angle,3);
            p.addForce(v);
        }        
    }
However, I find that my GreaseDrops aren't deflecting off in the behavior that I want it to. Certain angles don't seem to be working in my favor; perhaps Greenfoot or Java has only a range of 0 - pi (0-180)?
JasonZhu JasonZhu

2014/1/10

#
I figured out the error myself, for those interested, it's simply that my atan parameters were in the wrong place. dy then dx.
You need to login to post a reply.