posts - 0,  comments - 6,  trackbacks - 0

          Collision Detection 碰撞檢測(cè)


          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.

          譯:開始之前讓我們先說明,下面給出的方法并不是實(shí)現(xiàn)碰撞檢測(cè)的最佳方法。它可以被描述為描述為強(qiáng)力碰撞檢測(cè)。我們的游戲主循環(huán)過程中要循環(huán)檢測(cè)任意實(shí)體兩兩之間是否發(fā)生碰撞。

          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:

          譯:首先,我們需要實(shí)施檢測(cè),以檢測(cè)兩個(gè)實(shí)體是否在實(shí)際上已經(jīng)相撞 ,在實(shí)體類中這樣來做

          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.

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

          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.

          譯:首先,我們創(chuàng)建兩個(gè)矩形Rectangle類的實(shí)例)來代表兩個(gè)實(shí)體。接下來我們使用java.Awt.Rectangle內(nèi)置方法來檢測(cè)這兩個(gè)實(shí)體是否發(fā)生了交叉碰撞(兩個(gè)矩形是否有交叉區(qū)域)。不管怎么說,這不是最好的方法,但是對(duì)于達(dá)到我們的目的已經(jīng)足夠了

          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:

          譯:下一步我們要添加一種方法來告訴實(shí)體,它已經(jīng)和其它實(shí)體發(fā)生了碰撞。為了更好的實(shí)現(xiàn)這項(xiàng)功能我們需要在 Entity 類中添加一個(gè)方法如下:

          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.

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

          ShipEntity 飛船實(shí)體
          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:

          譯:當(dāng)飛船的實(shí)體擊中其它實(shí)體,它需要有相應(yīng)的反應(yīng)例如,玩家飛船應(yīng)該被殺死。為了簡(jiǎn)便起見我們來這樣做:

          public void collidedWith(Entity other) {

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

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

          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.

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

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

          譯:當(dāng)飛船發(fā)射的子彈擊中了一個(gè)外星人實(shí)體發(fā)生了碰撞,我們讓這個(gè)子彈和外星人同時(shí)消失。下面是實(shí)現(xiàn)這個(gè)效果的代碼:

          public void collidedWith(Entity other) {

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

          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).

          譯:如果飛船發(fā)射的子彈擊中了外星人,外形人和子彈都應(yīng)該被移除也就是說當(dāng)外星人被殺死時(shí),游戲邏輯會(huì)被通知,

          Game Loop Additions 游戲循環(huán)添加碰撞檢測(cè)功能
          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:

          譯:完成碰撞檢測(cè)工作的最后一步是在游戲循環(huán)中添加一個(gè)代碼片段,循環(huán)每一個(gè)實(shí)體,并檢查兩兩之間是否發(fā)生了碰撞。下面是實(shí)現(xiàn)代碼:

          // brute force collisions, compare every entity against

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

          // both entities that the collision has occured

          //強(qiáng)力碰撞檢測(cè),比較每一個(gè)實(shí)體是否和其他實(shí)體發(fā)生了碰撞。如果其中任何兩個(gè)實(shí)體想撞了通知兩個(gè)實(shí)體碰撞發(fā)生 

          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.

          譯:對(duì)于每個(gè)實(shí)體我們都要循環(huán)所有其它實(shí)體,檢測(cè)他們之間是否發(fā)生碰撞了,如果發(fā)生了碰撞我們就通知碰撞雙方

          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.

          譯:一個(gè)聰明的辦法可能是每隔一段時(shí)間進(jìn)行一次碰撞檢查。為實(shí)體添加一個(gè)表示是否應(yīng)該檢查碰撞的標(biāo)志屬性也可能是個(gè)不錯(cuò)的主意。

          學(xué)軟件開發(fā),到蜂鳥科技!
          超強(qiáng)的師資力量 、完善的課程體系 、超低的培訓(xùn)價(jià)格 、真實(shí)的企業(yè)項(xiàng)目。

          網(wǎng)址:www.ntcsoft.com 
          電話:0371-63839606 
          鄭州軟件開發(fā)興趣小組群:38236716

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(2)

          我參與的團(tuán)隊(duì)

          文章檔案(22)

          搜索

          •  

          最新評(píng)論

          主站蜘蛛池模板: 铁岭市| 全州县| 固始县| 绵竹市| 射阳县| 子长县| 常德市| 京山县| 简阳市| 安宁市| 南川市| 隆化县| 武陟县| 广昌县| 海林市| 泊头市| 佛学| 前郭尔| 青海省| 陈巴尔虎旗| 芦山县| 大埔区| 礼泉县| 英德市| 白山市| 麻阳| 西峡县| 甘泉县| 江山市| 子长县| 繁峙县| 遂川县| 绵阳市| 定边县| 孟连| 邵武市| 通许县| 石首市| 肥城市| 岑溪市| 五莲县|