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

2013/1/3

Platform collision

MindlessRicky MindlessRicky

2013/1/3

#
For my platform game I use this when there is a collision going on with a platform and the player:
public boolean intersect5()
    {
        Actor player = getOneIntersectingObject(Platform.class);
            if (player == null){
                return false;
            }else{
                return true;
            }
    }
    public void checkCollision()
    {
        int originalX = getX();
        int originalY = getY();
        if(intersect5()){
            setLocation(originalX, originalY-7);
        } 
    }
Right now it just pushes the player upward when they intersect but the problem is that the player will also be pushed up when it intersects the side of the platform.. So is there another way to do this? Something that will keep the player out of the platform from all the sides?
Gevater_Tod4711 Gevater_Tod4711

2013/1/3

#
You could change the setLocation method so that you only move to a position if there is no platform. The method should look like this:
public void setLocation(int x, int y) {
    if (noPlatformAt(x, y)) {//only pseudocode; you first have to writ such a method;
        super.setLocation(x, y);
    }
}
danpost danpost

2013/1/3

#
If you create local variables to hold the amount of movement horizontally and vertically, you can revert backward one pixel at a time until the intersection is not longer. The code would depend on how you are moving your actor (using int or doubles; using move(int) or setLocation(int, int); range of speed; possible directions of move; etc.).
You need to login to post a reply.