2011-11-18 35 views
0

我已经发布了一个关于此主题的问题..但没有得到答案。我正在做一个游戏。在那个应用程序中,我必须检测画布中两个对象的碰撞。对象类粘贴在这里。Android中两个对象的碰撞检测

 public class Droid { 

private Bitmap bitmap; // the actual bitmap 
private int x;   // the X coordinate 
private int y;   // the Y coordinate 
private boolean touched; // if droid is touched/picked up 
private Speed speed; // the speed with its directions 

public Droid(Bitmap bitmap, int x, int y) { 
    this.bitmap = bitmap; 
    this.x = x; 
    this.y = y; 
    this.speed = new Speed(); 
} 

public Bitmap getBitmap() { 
    return bitmap; 
} 
public void setBitmap(Bitmap bitmap) { 
    this.bitmap = bitmap; 
} 
public int getX() { 
    return x; 
} 
public void setX(int x) { 
    this.x = x; 
} 
public int getY() { 
    return y; 
} 
public void setY(int y) { 
    this.y = y; 
} 

public boolean isTouched() { 
    return touched; 
} 

public void setTouched(boolean touched) { 
    this.touched = touched; 
} 

public Speed getSpeed() { 
    return speed; 
} 

public void setSpeed(Speed speed) { 
    this.speed = speed; 
} 


public void draw(Canvas canvas) { 
    canvas.drawBitmap(bitmap, x - (bitmap.getWidth()/2), y - (bitmap.getHeight()/2), null); 
} 

/** 
* Method which updates the droid's internal state every tick 
*/ 
public void update() { 
    if (!touched) { 
     x += (speed.getXv() * speed.getxDirection()); 
     y += (speed.getYv() * speed.getyDirection()); 
    } 
} 


/** 
* Handles the {@link MotionEvent.ACTION_DOWN} event. If the event happens on the 
* bitmap surface then the touched state is set to <code>true</code> otherwise to <code>false</code> 
* @param eventX - the event's X coordinate 
* @param eventY - the event's Y coordinate 
*/ 
public void handleActionDown(int eventX, int eventY) { 
    if (eventX >= (x - bitmap.getWidth()/2) && (eventX <= (x + bitmap.getWidth()/2))) { 
     if (eventY >= (y - bitmap.getHeight()/2) && (y <= (y + bitmap.getHeight()/2))) { 
      // droid touched 
      setTouched(true); 
     } else { 
      setTouched(false); 
     } 
    } else { 
     setTouched(false); 
    } 

} 
} 

我正在创建5个droid对象。其中4人不断在画布上移动,1人由用户控制。我想要检测这4个移动物体与用户控制对象发生碰撞的事件。我已经阅读了许多教程,但还没有得到解决方案。请帮助我的人...

+0

没有人为此问题提供答案?????? :( –

+0

使用AndEngine进行游戏会更好。 –

回答

0

您应该使用一个库,提供检测精灵碰撞的功能。尝试谷歌搜索“android精灵碰撞”。

1

在一般情况下,通过使用不精确表示简化碰撞检测通常是一个好主意,因为它是易于使用和实施,以及速度一般很好。对于这样的解决方案,使用轴对齐边界框或可能的圆圈进行碰撞检测应该可以正常工作。对于更复杂的解决方案,基于多边形的解决方案是可用的,但它们更难以使用。

如果您需要更精确的碰撞检测,例如检测两幅图像之间的碰撞,则需要像素完美的碰撞检测。这通常很难有效地进行,因此建议使用现有的库。

由于您使用Java,并且您似乎使用位图,AndEngine作为Reno建议或PoxelColl应该做的伎俩。如果你需要旋转和缩放等基本转换,我推荐PoxelColl。