<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0,  comments - 6,  trackbacks - 0

    Collision Detection 碰撞檢測


    Lets start by saying there are better ways to implement collision detection than the method outlined here. It could be described a "brute force". In our main game loop we're going to cycle round check whether each entity has collided with every other entity.

    譯:開始之前讓我們先說明,下面給出的方法并不是實現碰撞檢測的最佳方法。它可以被描述為描述為強力碰撞檢測。我們的游戲主循環過程中要循環檢測任意實體兩兩之間是否發生碰撞。

    First, we'll need to implement a check to resolve whether two entities have in fact collided. We'll do this in the Entity class like this:

    譯:首先,我們需要實施檢測,以檢測兩個實體是否在實際上已經相撞 ,在實體類中這樣來做

    public boolean collidesWith(Entity other) {

    me.setBounds((int) x,(int) y,sprite.getWidth(),sprite.getHeight());

    him.setBounds((int) other.x,(int)other.y,

                            other.sprite.getWidth(),other.sprite.getHeight());

    return me.intersects(him);

    }

    This method checks if the entity itself collides with the other entity specified. We're going to rely on rectangular intersection regions and the AWT class Rectangle. In this case the variables "me" and "him" are instances of Rectangle that are held at the class level.

    譯:實體方法檢查自己與另外一個參數指定的實體是否發生了碰撞。我們依靠矩形相交AWT Rectangle 類來實現碰撞檢測。在這個例子中,變量“me”和“him”都是 Rectangle 類的實例。

    First we configure the two rectangles to represent the two entities. Next we use the inbuilt functionality of java.awt.Rectangle to check if the two entities intersect with each other. This isn't the smartest way to do this by any means, but for our purposes it will be good enough.

    譯:首先,我們創建兩個矩形Rectangle類的實例)來代表兩個實體。接下來我們使用java.Awt.Rectangle內置方法來檢測這兩個實體是否發生了交叉碰撞(兩個矩形是否有交叉區域)。不管怎么說,這不是最好的方法,但是對于達到我們的目的已經足夠了。

    The next thing we'll add is a way to notify entities that they have collided with another. To do this we'll add a method like this to the Entity class:

    譯:下一步我們要添加一種方法來告訴實體,它已經和其它實體發生了碰撞。為了更好的實現這項功能我們需要在 Entity 類中添加一方法如下:

    public abstract void collidedWith(Entity other);

    Its been made abstract since different implementations of the Entity class will want to respond to collisions in their own ways, e.g. Alien<->Shot, Ship<->Alien.

    譯:這個方法被定義成抽像方法,Entity 的不同實現將可以采用各自的方法來反映碰撞的結果。例如,外星人<->子彈之間碰撞的效果,飛船<->外星人之間碰撞的效果。

    ShipEntity 飛船實體
    The ship entity needs to react when it hits an entity, i.e. The player should be killed. To facilitate this we'll should do this:

    譯:當飛船的實體擊中其它實體,它需要有相應的反應,例如,玩家飛船應該被殺死。為了簡便起見我們來這樣做:

    public void collidedWith(Entity other) {

    // if its an alien, notify the game that the player

    // is dead 如果它是一個外星人,通知游戲玩家飛船死亡

    if (other instanceof AlienEntity) {

    game.notifyDeath();

    }

    }

    Again, the result of the collision is based on the game logic (covered later). If the entity that the ship collided with is an Alien then notify the game that player should die.

    譯:再次重申一下,實體間發生碰撞的結果是基于游戲邏輯(稍后介紹)定義的。如果飛船實體和一個外星人實體發生了碰撞,就說明玩家飛船死亡,游戲結束。

    ShotEntity 子彈實體
    When the shot entity hits an alien we want the alien and the shot to be destroyed. This is achieved with the following code:

    譯:當飛船發射的子彈擊中了一個外星人實體發生了碰撞,我們讓這個子彈和外星人同時消失。下面是實現這個效果的代碼:

    public void collidedWith(Entity other) {

    // if we've hit an alien, kill it! 如果我們擊中了一個外星人,殺死他

    if (other instanceof AlienEntity) {

    // remove the affected entities

    game.removeEntity(this);

    game.removeEntity(other);

    // notify the game that the alien has been killed

    game.notifyAlienKilled();

    }

    }

    If the shot hit and alien then the alien and shot are removed. In addition the game logic is notified that the an alien has been killed (covered later).

    譯:如果飛船發射的子彈擊中了外星人,外形人和子彈都應該被移除。也就是說,當外星人被殺死時,游戲邏輯會被通知,

    Game Loop Additions 游戲循環添加碰撞檢測功能
    The final step in getting the collision detection to work is to add a section to the game loop to cycle through all the entities checking whether they collide with each other. Here's the code:

    譯:完成碰撞檢測工作的最后一步是在游戲循環中添加一個代碼片段,循環每一個實體,并檢查兩兩之間是否發生了碰撞。下面是實現代碼:

    // brute force collisions, compare every entity against

    // every other entity. If any of them collide notify 

    // both entities that the collision has occured

    //強力碰撞檢測,比較每一個實體是否和其他實體發生了碰撞。如果其中任何兩個實體想撞了,通知兩個實體碰撞發生 

    for (int p=0;p<entities.size();p++) {

    for (int s=p+1;s<entities.size();s++) {

    Entity me = (Entity) entities.get(p);

    Entity him = (Entity) entities.get(s);

    if (me.collidesWith(him)) {

    me.collidedWith(him);

    him.collidedWith(me);

    }

    }

    }

    For each entity we cycle through all the other entities that we haven't compared against and check whether a collision has occured. If a collision has occured we notify both sides.

    譯:對于每個實體我們都要循環所有其它實體,檢測他們之間是否發生碰撞了,如果發生了碰撞我們就通知碰撞雙方

    A smarter way to do this might be to check for the collisions only every so often. It might also be nice to have a flag on an entity whether it should detect collisions.

    譯:一個聰明的辦法可能是每隔一段時間進行一次碰撞檢查。為實體添加一個表示是否應該檢查碰撞的標志屬性也可能是不錯的主意。

    學軟件開發,到蜂鳥科技!
    超強的師資力量 、完善的課程體系 、超低的培訓價格 、真實的企業項目。

    網址:www.ntcsoft.com 
    電話:0371-63839606 
    鄭州軟件開發興趣小組群:38236716

    posted on 2010-11-26 00:01 whistler 閱讀(392) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    留言簿(2)

    我參與的團隊

    文章檔案(22)

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 好吊妞788免费视频播放| 亚洲AV色无码乱码在线观看| AAAAA级少妇高潮大片免费看| 久久久久免费看黄A片APP| 日韩视频免费在线| 亚洲成a人片在线网站| 91高清免费国产自产拍2021| 99人中文字幕亚洲区| 日本一卡精品视频免费| 亚洲国产精品婷婷久久| 亚欧色视频在线观看免费| 亚洲最大黄色网站| 午夜寂寞在线一级观看免费| 国产精品亚洲综合网站| 亚洲精品视频免费观看| 中文字幕免费在线视频| 好先生在线观看免费播放| 亚洲欧洲av综合色无码| 亚洲AV无码成人精品区大在线| 国产成人精品免费视频大全| 国产亚洲精品无码成人| 2020因为爱你带字幕免费观看全集 | 日韩一级免费视频| 一区二区三区免费在线观看| 亚洲精品无码乱码成人| 最近高清中文字幕无吗免费看| 亚洲精品乱码久久久久蜜桃| 亚洲精品网站在线观看不卡无广告| 久久精品国产免费| 亚洲日韩一区二区一无码| 亚洲精品tv久久久久| 精品一区二区三区免费毛片爱| 亚洲日韩国产二区无码| 亚洲人成影院在线观看 | 国产免费久久精品丫丫| 亚洲最大免费视频网| 亚洲av日韩片在线观看| 亚洲高清毛片一区二区| 狠狠综合久久综合88亚洲| 一本岛高清v不卡免费一三区| 日韩精品免费一线在线观看 |