【问题标题】:Android Rect CollisionAndroid 矩形碰撞
【发布时间】:2014-03-18 04:54:47
【问题描述】:

我想为安卓设备制作一个小游戏。 一切正常……除了与玩家和物体的碰撞。我做错什么了吗?我尝试了许多检查碰撞的方法。例如。 intersect, intersects, contains and a function with self-made碰撞测试。

编辑:我的问题是什么都没发生 :)

    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    private Bitmap player, enemy;
    private int speedrun=1, x= 0, y = height - height / 5, i=1, yg=height - height / 5 + 40, xe= 920, xp =width / 2 - width / 4;
    private Rect p, e;
    public static int speed=0;

//other code

@Override
    protected void onDraw(Canvas c)
    {
        c.drawColor(Color.CYAN);
        handelPlayer();
        p = new Rect(xp, y, 0, 0);
        wayrect = new Rect(x, yg, 0, 0);
        wayrect2 = new Rect(x + width, yg, 0, 0);
        e = new Rect(xe, yg - 250, 0, 0);
        c.drawBitmap(enemy, e.left, e.top, null);
        c.drawBitmap(player, p.left, p.top, null);
    }

    public void handelPlayer()
    {
        x -= speed*speedrun;
        xe -= speed*speedrun;
        if (x + width < 0)
            x = 0;
        if (xe < -100)
            xe = 920;
        if (MainActivity.touch == 1)
        {
            y -= 100;//jump
            MainActivity.touch = 0;
            i = 1;
        }
        if (y <= height - height / 5)
            y += 3 * i / 10; //gravity
        i++;

        if (p.intersect(e)) //collosision
            speedrun = 0;
    }

【问题讨论】:

  • 尝试将p.intersect(e) 更改为Rect.intersects(p,e)intersect 会将矩形p 更改为交叉点,而Rect.intersects(Rect a, Rect b) 只是一个测试,我认为这就是您要寻找的。​​span>
  • 如果你觉得有用,记得给你接受的答案投票;)

标签: java android canvas collision


【解决方案1】:

首先,您的矩形从播放器位图的顶部到设备的左上角 (0,0)。我想象你的意思是:p = new Rect(xp, y, xp + player.getWidth(), y + player.getHeight());e 相同,请参见下面的代码。

其次,p.intersect(e) 将矩形 p 更改为交叉点,如果它们相交,则应使用 Rect.intersects(p, e) 代替。

第三,您正在检查旧位置值的碰撞,因为您在更改位置后没有更新矩形。

一个快速的解决方法可能是将交叉测试移动到handelPlayer 的顶部(小注:handlePlayer 是正确的拼写方式),如下所示:

protected void onDraw(Canvas c)
{
    c.drawColor(Color.CYAN);
    handelPlayer();
    p = new Rect(xp, y, xp + player.getWidth(), y + player.getHeight());;
    wayrect = new Rect(x, yg, 0, 0);   // These rectangles also has their right bottom corner at (0,0), which might cause problems
    wayrect2 = new Rect(x + width, yg, 0, 0);
    e = new Rect(xe, yg - 250, xe + enemy.getWidth(), yg - 250 + enemy.getHeight()) ;
    c.drawBitmap(enemy, e.left, e.top, null);
    c.drawBitmap(player, p.left, p.top, null);
}


public void handelPlayer()
{
    if (Rect.intersects(p, e)){ //collision
        speedrun = 0;
    }

    x -= speed*speedrun;
    xe -= speed*speedrun;
    if (x + width < 0)
        x = 0;
    if (xe < -100)
        xe = 920;
    if (MainActivity.touch == 1)
    {
        y -= 100;//jump
        MainActivity.touch = 0;
        i = 1;
    }
    if (y <= height - height / 5)
        y += 3 * i / 10; //gravity
    i++;


}

但可能还有另一个问题,因为您还没有描述到底发生了什么。

【讨论】:

  • 我尝试了固定版本,但没有修复它!我的问题是,什么都没有发生……所以碰撞测试总是返回flase!
  • 有什么问题?发生了什么/没有发生什么?
  • 什么都没有发生 :) 如果他碰撞另一个纹理,玩家不会停止!
  • 好的,我用我发现的另一个问题更新了我的答案。看看现在能不能用。问题是当你定义你的矩形时,你只正确设置了左上角,而右下角设置为(0,0),这没有意义,会干扰交集计算。
  • 不客气 :) 你应该考虑命名 pe playerRectenemyRect 或类似的名称,因为它会使代码更具可读性。另请注意,您对 wayrect 和 wayrect2 犯了同样的错误。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多