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 阅读(358) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿(2)

我参与的团队

文章档案(22)

搜索

  •  

最新评论