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

2013/11/22

How to getRotation() of an object

1
2
JasonZhu JasonZhu

2013/11/22

#
Alrighty, here it is:
    private void bounceOff(Actor Object)
    {       
        int angleDeflect = world.getContainer().getRotation()-90;
        int angle = angleDeflect+90;
        getMovement().setDirection(angle);
        if(getSpeed()<0.5){
            falling=false;
            getWorld().removeObject(this);
        }
    }

java.lang.NullPointerException at Drop.bounceOff(Drop.java:50) at Drop.act(Drop.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) I made the modification in the bounceOff from reading what the tutorial had suggested.
danpost danpost

2013/11/22

#
Alright, it still might be easiest for you to show the whole class. I want to see how 'world' is set and then the 'getContainer' method in the world class plus the creation code for those objects.
JasonZhu JasonZhu

2013/11/22

#
Forgive me, but I won't be able to reply any time soon as I'm away from my house. I will reply as soon as I return
JasonZhu JasonZhu

2013/11/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Drop here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Drop extends SmoothMover
{
    /**
     * Act - do whatever the Drop wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    private static final int SIZE = 5;
    private static final Vector GRAVITY = new Vector(90,0.2);
    private boolean falling; 
    private WaterWorld world;

    public Drop()
    {
        GreenfootImage img = new GreenfootImage(SIZE,SIZE);
        img.setColor(Color.BLUE);
        img.fillOval(0,0,SIZE,SIZE);
        setImage(img);
        falling = true;
    }

    public void act()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        Actor container = getOneIntersectingObject(Container.class);        
        if (paddle!=null&&getWorld()!=null){
            bounceOff(paddle);
            accelerate(0.85);            
        }
        //         if (containerEdge()==true){
        //             bounceOff(container);
        //             accelerate(0.85);            
        //         }
        if (falling){
            fall(); 
        }        
    } 

    private void bounceOff(Actor Object)
    {       
        int angleDeflect = world.getContainer().getRotation()-90;
        int angle = angleDeflect+90;
        getMovement().setDirection(angle);
        if(getSpeed()<0.5){
            falling=false;
            getWorld().removeObject(this);
        }
    }

    private void fall()
    {
        addForce(GRAVITY);
        move();
        if (atVerticalEdge()){
            getMovement().revertHorizontal();  
            accelerate(0.85);            
        }        
        if (atHorizontalEdge()){
            getMovement().revertVertical();
            accelerate(0.85);
            //            move();
            if(getSpeed()<0.5){
                falling=false;
                getWorld().removeObject(this);
            }
        }       
    }

    //     private boolean containerEdge()
    //     {
    //         return getWorld().getX()==430||getWorld.getX()==630;
    //     }    
}
This is the Drop class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class WaterWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WaterWorld extends World
{
    private int counter;
    private Container container;

    /**
     * Constructor for objects of class WaterWorld.
     * 
     */
    public WaterWorld()
    {    
        super(800, 600, 1);
        addObject(new Paddle(),100,getHeight()-100);
        container = new Container();
        addObject(container,530,460);
        int counter = 0;
    }
    
    public void act()
    {
        addDrops();
    }
    
    private void addDrops()
    {
        counter++;
        if(counter==25){
            addObject(new Drop(),100,5);
            counter = 0;
        }
    }
    
    public Container getContainer()
    {
        return container;
    }
}
This is the World class
danpost danpost

2013/11/23

#
In the Drop class, remove line 20 and change line 50 to the following:
int angleDeflect = ((WaterWorld)getWorld()).getContainer().getRotation()-90;
However, if you only have one Container object in the world, better might be this:
int angleDeflect = ((Actor)getWorld().getObjects(Container.class).get(0)).getRotation()-90;
In the latter, you are sure to get the Container object that is in the world, provided one is there. And if one is not there, it will error (probably IndexOutOfBoundsException); and all this would mean is that you are calling the method at a time when you should not be, and something else needs fixed. In the former, if the 'container' field in the WaterWorld class is 'null' or points to a Container object other than the one in the world, you will get a NullPointerException or possibly an incorrect rotational value. Wait, you already have a reference to the object:
// change the 'bounceOff' method to the following
private void bounceOff(Actor actor)
{       
    int angleDeflect = actor.getRotation()-90;
    int angle = angleDeflect+90;
    getMovement().setDirection(angle);
    if(getSpeed()<0.5)
    {
        falling=false;
        getWorld().removeObject(this);
    }
}
JasonZhu JasonZhu

2013/11/23

#
The error is fixed, but It's not deflecting properly...
    private void bounceOff(Actor Object)
    {       
        int angleDeflect = ((WaterWorld)getWorld()).getContainer().getRotation()-90;  
        int angle = angleDeflect+90;
        getMovement().setDirection(angle);
        if(getSpeed()<0.5){
            falling=false;
            getWorld().removeObject(this);
        }
    }
What I'm trying to do is make the object bounce off the paddle at the same angle it came in at. The code's math seems correct, but its not correct.
danpost danpost

2013/11/23

#
If you want it to return along the same path it approached at, add 180 (not 90).
JasonZhu JasonZhu

2013/11/23

#
My drops drop straight downwards, whereas my paddle underneath it is rotatable, I want to be able to store the angle at which the platform is relative to the drop and have it deflect off the paddle with the same angle (paddles rotation + 180 - stored angle)? I'm not too sure about this physics. Is this right?
JasonZhu JasonZhu

2013/11/23

#
Come to think of it, isn't that exactly what you suggested?
You need to login to post a reply.
1
2