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

2017/7/26

Intersection of objects not working correctly?

Super_Hippo Super_Hippo

2017/7/26

#
After starting to program another version of my traffic simulation, I noticed that intersection checks only work correctly when the rotation is 0. The cars are now simply 3x3 block, move two steps (horizontal or vertical, no diagonal movement), check if there is a car and move one step back if so. Then they move another step back. But the end result is that only cars moving to the right will not hit the car in front of them. I just made a new scenario doing the same thing:
import greenfoot.*;
public class MyWorld extends World
{
    private Block[] b = {new Block(false, 0), new Block(false, 1), new Block(false, 2), new Block(false, 3),
                         new Block(true,  0), new Block(true,  1), new Block(true,  2), new Block(true,  3)};
    public MyWorld()
    {
        super(25, 25, 1);
        Greenfoot.setSpeed(25);
        
        addObject(b[0], 10, 15);
        addObject(b[1], 15, 10);
        addObject(b[2], 2, 20);
        addObject(b[3], 20, 2);
        
        addObject(b[4], 2, 15);
        addObject(b[5], 15, 2);
        addObject(b[6], 10, 20);
        addObject(b[7], 20, 10);
    }
    
    public void act()
    {
        b[4].fall();
        b[5].fall();
        b[6].fall();
        b[7].fall();
    }
}
import greenfoot.*;
public class Block extends Actor
{
    public Block(boolean mov, int rot)
    {
        GreenfootImage img = new GreenfootImage(3, 3);
        img.setColor(mov?Color.RED:Color.BLUE);
        img.fill();
        setImage(img);
        setRotation(90*rot);
    }
    
    public void fall()
    {
        move(2);
        if (isTouching(Block.class)) move(-1);
        move(-1);
    }
}
Start: End: (one cell between red block which moved right, no cell between all others) Anything wrong with my code or why is the intersection not the same in each direction?
davmac davmac

2017/7/26

#
Hmm. This is surprising, but it looks like maybe a rounding issue in the collision checking code (in Greenfoot itself). I'll look into it when I get a chance.
Super_Hippo Super_Hippo

2017/8/22

#
Did you already find the issue?
davmac davmac

2017/8/24

#
I made a test case which reproduces it. I haven't had a chance to actually look for the problem yet, but it should be fixed for the next release of Greenfoot (which should be coming up in the next few months I think).
You need to login to post a reply.