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

2013/10/10

Help with this Code!

tartags11 tartags11

2013/10/10

#
Trying to work with collisions and I used the chapter 10 marbles code but it wont work. This Comes Up:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to Fruit
	at Fruit.checkCollision(Fruit.java:34)
	at Fruit.act(Fruit.java:25)
	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)
Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This code describes the Fruit class.
 */


public class Fruit extends SmoothMover

{
    private static final double DIAMETER = 54.0;
    private static final int RADIUS = 27;
    
    public void act() 
    {
        if ( atWorldEdge() )
        {
            turn(17);
        }
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(90)-45);
        }
        move();
        checkCollision();
        
    }
        /**
     * Check whether we are colliding with another marble.
     */
    private void checkCollision()
    {
        // first, get all fruit whose image intersects (we don't need to check the others)
        Fruit fruit = (Fruit) getIntersectingObjects(Fruit.class);
        for (Fruit fruit : fruit) {
            if ( haveHit(fruit) ) {
                doCollision(fruit);
            }
        }
    }
    
    /**
     * We have hit another fruit. Perform the collision now (that is: compute the new vectors of 
     * movement for us and the other fruit).
     */
    private void doCollision(Fruit fruit)
    {
        double dx = this.getExactX() - fruit.getExactX();
        double dy = this.getExactY() - fruit.getExactY();
        int direction = (int) Math.toDegrees(Math.atan2(dy, dx));
        double angle = direction - getMovement().getDirection();
        
        // if the not 90 < angle < 270 then we're hit from behind and don't want to move the other ball
        if (Math.abs(angle) < 90 || Math.abs(angle) > 270) {
            return;
        }
     
        double length = Math.cos(Math.toRadians(angle)) * getMovement().getLength();
        fruit.addForce( new Vector (direction, length) );
        //fruit.setMoving(true);
        
        this.addForce (new Vector (direction + 180, length) );
        //System.out.println("dir (after): " + direction + "  " + this);
    }
   
    /**
     * Check whether we have hit the given fruit. We have hit it if its distance from us 
     * (measured at the centre points) is less then our diameter.
     */
    private boolean haveHit(Fruit fruit)
    {
        int dx = Math.abs (this.getX() - fruit.getX());
        int dy = Math.abs (this.getY() - fruit.getY());
        double distance = Math.sqrt(dx*dx+dy*dy);
        
        return distance < DIAMETER;
    }
}
SPower SPower

2013/10/10

#
Change this:
private void checkCollision()  
    {  
        // first, get all fruit whose image intersects (we don't need to check the others)  
        Fruit fruit = (Fruit) getIntersectingObjects(Fruit.class);  
        for (Fruit fruit : fruit) {  
            if ( haveHit(fruit) ) {  
                doCollision(fruit);  
            }  
        }  
    }  
into this:
private void checkCollision()  
    {  
        Fruit fruit = (Fruit) getOneIntersectingObject(Fruit.class);  
        for (Fruit fruit : fruit) {  
            if ( haveHit(fruit) ) {  
                doCollision(fruit);  
            }  
        }  
    }  
Gevater_Tod4711 Gevater_Tod4711

2013/10/10

#
The problem is in line 34. The method getIntersectingObjects(Fruit.class) returns a list of Fruit objects. Now you try to cast this list to a fruit object what is simply not possible. Therefore you get the ClassCastException. When you change line 34 to: List<Fruit> fruit = getIntersectingObjects(Fruit.class); it should work. You just need to import java.util.List to use the lists (see the first line import greenfoot.*; Just add the line 'import java.util.List;')
Gevater_Tod4711 Gevater_Tod4711

2013/10/10

#
I think there is another problem in lines 34 and 35. You can't have two variables with the same name. @SPower I don't think this code would work. The class Fruit (probably) has no Iterator and so you cant use it in a for loop like this.
SPower SPower

2013/10/10

#
Whoops, forgot to read further into the method again, sorry for any trouble! (Maybe I should take more time for helping people on this website :) )
SPower SPower

2013/10/10

#
To summarise all the stuff we were talking about, this should work:
private void checkCollision()  
    {  
        // first, get all fruit whose image intersects (we don't need to check the others)  
        List<Fruit> fruits = getIntersectingObjects(Fruit.class);  
        for (Fruit fruit : fruits) {  
            if ( haveHit(fruit) ) {  
                doCollision(fruit);  
            }  
        }  
    }  
You need to login to post a reply.