MarteEngine tutorial:Basic collision

在完成Keyboard and mouse input后,现在是时候让两个entities交互了。

在屏幕上有一些基本的东西,你可以移动你的英雄的图像... 但是当两个entities互相接触时会发生什么?这被称为碰撞,MarteEngine能使得它处理起来比你想象的更容易。

让我们从创建两个Entities:Player和Wall 开始:

public class Player extends Entity {

    /**
     * 
@param x, x coordinate on screen where Player starts
     * 
@param y, y coordinate on screen where Player starts
     
*/
    public Player(float x, float y) {
        super(x, y);
        // load Image from disk and associate it as player image
        Image img = ResourceManager.getImage("player");
        setGraphic(img);
        
        // define basic collision info
        
// hitbox is a rectangle around entity, in this case it is exactly the size of the player image
        setHitBox(0, 0, img.getWidth(), img.getHeight());
        // declare type of this entity
        addType("PLAYER");
    }
    
}
public class Wall extends Entity {

    public Wall(float x, float y) {
        super(x, y);
        
        // load Image from disk and associate it as wall image
        Image img = ResourceManager.getImage("wall");
        setGraphic(img);
        
        // define basic collision info
        
// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
        setHitBox(0, 0, img.getWidth(), img.getHeight());
        // declare type of this entity
        addType(SOLID);        
    }
}

 我们加入的那几行代码有什么用呢?对于每个entity如果想让它对碰撞有所反应的话,你需要声明两件事情:

1.hitbox: hitbox指定一个entity的边界。当MarteEngine检查碰撞时,它查看每个entity的边界是否与其他entity的hitbox重叠。

2.type: 为加速不同entity组的碰撞检测,你可以定义entity的类型。每个entity可以有多种类型。在我们的例子中,Player 属于“PLAYER”类型,而Wall属于内置的基本类型“SOLID”.

有了这两个基本概念,你可以做你想做的任何事,例如在玩家更新它的位置时使用碰撞检测方法,是有可能检查和避免与其他有内置"SOLID"类型entities发生碰撞的。

public class Player extends Entity {

    /**
     * 
@param x
     *            , x coordinate on screen where Player starts
     * 
@param y
     *            , y coordinate on screen where Player starts
     
*/
    public Player(float x, float y) {
        super(x, y);
        // load Image from disk and associate it as player image
        Image img = ResourceManager.getImage("player");
        setGraphic(img);

        // define basic collision info
        
// hitbox is a rectangle around entity, in this case it is exactly the size of the
        
// player image
        setHitBox(0, 0, img.getWidth(), img.getHeight());
        // declare type of this entity
        addType("PLAYER");
    }

    @Override
    public void update(GameContainer container, int delta)
            throws SlickException {
        super.update(container, delta);

        // check if a key is down
        if (check("RIGHT")) {
            // do anything you like, for example: only move right if we don't collide with some SOLID entity
            if (collide(SOLID, x + 10, y)==null) {
                x = x + 10;
            }
        }
    }

}

 在update方法里,在玩家移动之前(按下键盘上的右箭头之后),可能要检查玩家与另一个类型为SOLID的entity是否发生碰撞(在我们的例子中是Wall),如果没有发生碰撞,则玩家可以移动。

你也可以使用工具方法(“callback”) 以墙的视角来处理碰撞:

public class Wall extends Entity {

    public Wall(float x, float y) {
        super(x, y);
        
        // load Image from disk and associate it as wall image
        Image img = ResourceManager.getImage("wall");
        setGraphic(img);
        
        // define basic collision info
        
// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
        setHitBox(0, 0, img.getWidth(), img.getHeight());
        // declare type of this entity
        addType(SOLID);        
    }

    @Override
    public void collisionResponse(Entity other) {
        // called when colliding with another entity
    }
    
}

 当玩家与墙发生碰撞时,墙应该通过覆写基类Entity中的collisionReponse方法执行一些代码,例如墙可能要下降20个点——假设玩家是一个笨重的人。


 现在准备开始Animate sprite教程。

——————————————————————————————————
傲轩游戏网
原文地址:https://www.cnblogs.com/cuizhf/p/2378302.html