zoukankan      html  css  js  c++  java
  • 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教程。

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    php CodeIgniter处理多环境错误级别配置
    bootstrap导航条在手机上默认展开二级目录,必须用setTimeout才能实现
    WordPress博客网站fonts.useso加载慢解决办法
    JS实现复制网页内容自动加入版权内容代码和原文链接
    bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件
    mysql字段varchar区分大小写utf8_bin、utf8_general_ci编码区别
    js捕捉IE窗口失去焦点事件,判断离开页面刷新或关闭的方法
    php防盗链,php ci在control里面控制除了自己站内的链接点击跳转,其他来源的都跳到站内页面
    php原子操作,文件锁flock,数据库事务
    默认只显示指定高度,出来按钮 阅读更多,加载全文,点击后显示全文的实现方式
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2378302.html
Copyright © 2011-2022 走看看