简体   繁体   English

碰撞检测java给出意想不到的结果

[英]Collision detection java giving unexpected results

My code for collision detection works but as soon as I set my players & monsters velocity to more than 1 I get weird results how can I solve this? 我的碰撞检测代码有效,但是一旦我将玩家和怪物的速度设置为1以上,我就会得到奇怪的结果,我该如何解决这个问题呢?

with velocity of 1 速度为1

速度为1

with velocity of 4 速度为4

速度为4

My collision detection: 我的碰撞检测:

static boolean collisionDown(Entity e)
{
    for(Block i : Game.blocks)
    {
        Rectangle player = new Rectangle(e.getX(), e.getY() + e.getVelocity(), e.getWidth(), e.getHeight());
        Rectangle block = new Rectangle(i.getX(), i.getY(), size, size );
        if (player.intersects(block))
        {
            //e.goUp(1);
            return true;
        }
    }
    return false;
}

static boolean collisionUp(Entity e)
{
    for(Block i : Game.blocks)
    {
        Rectangle player = new Rectangle(e.getX(), e.getY()  - e.getVelocity(), e.getWidth(), e.getHeight());
        Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
        if (player.intersects(block))
        {
            return true;
        }
    }
    return false;
}

static boolean collisionRight(Entity e)
{
    for(Block i : Game.blocks)
    {
        Rectangle player = new Rectangle(e.getX() + e.getVelocity(), e.getY(), e.getWidth(), e.getHeight());
        Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
        if (player.intersects(block))
        {
            return true;
        }
    }
    return false;
}

static boolean collisionLeft(Entity e)
{
    for(Block i : Game.blocks)
    {
        Rectangle player = new Rectangle(e.getX() - e.getVelocity(), e.getY(), e.getWidth(), e.getHeight());
        Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
        if (player.intersects(block))
        {
            return true;
        }
    }
    return false;
}

Where i use the collision detection 我在哪里使用碰撞检测

void goUp(int v)
{
    if(!Block.collisionUp(this))
        y -= v;
}

void goDown(int v)
{
    if(!Block.collisionDown(this))
        y += v;
}

void goRight(int v)
{
    if(!Block.collisionRight(this))
        x += v;
}

void goLeft(int v)
{
    if(!Block.collisionLeft(this))
        x -= v;
}

Thank you 谢谢

Your collision methods look just fine to me, assuming you are evaluating where the character will be at the next iteration of the game. 您的碰撞方法蛮好看我,假设你正在评估在角色在游戏的下一次迭代。 I'm willing to bet though, that when there will be a collision, you move your character at an offset to move it out of the collision box and back into a valid area to compensate. 我愿意打赌,当发生碰撞时,你将你的角色移动到一个偏移处,将它移出碰撞盒并返回有效区域进行补偿。 When the velocity is higher, it is magnifying this little side effect. 当速度较高时,它会放大这种小的副作用。 Double check all the places that call these methods and follow the logic that follows when these methods return true (principally those in the Y direction). 仔细检查调用这些方法的所有位置,并在这些方法返回true时遵循后面的逻辑(主要是在Y方向上的那些方法)。

An additional thing to try, increase the velocity of the character to something even higher, or something in between. 另外要尝试的是,将角色的速度提高到更高的值,或介于两者之间。 If your character moves farther way and closer as you manipulate the value, this is very likely what is happening. 如果你操纵值时你的角色走得越来越近,这很可能发生了什么。

On a side note, note this line in all of your methods 在旁注中,请注意所有方法中的这一行

Rectangle player = new Rectangle(e.getX(), e.getY()  - e.getVelocity(), e.getWidth(), e.getHeight());

This rectangle will not change (assuming you have not implemented threading into your game) during the duration of the method call, so recreating it every iteration of the for loop is just taking up time. 在方法调用期间,此矩形不会更改(假设您没有在游戏中实现线程),因此在for循环的每次迭代中重新创建它只是占用时间。 Consider modifying the methods to this: 考虑修改方法:

static boolean collisionDown(Entity e)
{
    Rectangle player = new Rectangle(e.getX(), e.getY() + e.getVelocity(), e.getWidth(), e.getHeight());
    for(Block i : Game.blocks)
    {
        Rectangle block = new Rectangle(i.getX(), i.getY(), size, size );
        if (player.intersects(block))
        {
            //e.goUp(1);
            return true;
        }
    }
    return false;
}

What I did for collision detection is develop a class called CollissionDetector . 我为碰撞检测所做的是开发一个名为CollissionDetector的类。 It has some logic within it for such methods as the following: 对于以下方法,它有一些逻辑:

public boolean isColliding();
public boolean collided(Rectangular r1, Rectangular r2);

Rectangular is an interface with one method: Rectangular是一个具有一种方法的接口:

public Rectangle getRectangle();

So my CollisionDetector class worked with anything that could implement the interface called Rectangular and return a Rectangle . 因此我的CollisionDetector类可以处理任何可以实现名为Rectangular的接口并返回Rectangle

The problem with some code that is supposed to detect collissions is that it reports the same collision more than once. 一些应该检测到碰撞的代码的问题是它不止一次地报告相同的碰撞。

With my collision detector, the collission detector object would keep track of when the collision starting, whether the two objects were still colliding, and when the collision ended. 使用我的碰撞探测器,碰撞探测器对象将跟踪碰撞开始的时间,两个物体是否仍在碰撞,以及碰撞何时结束。

It seems like I also had a CollissionListener . 好像我也有一个CollissionListener I know that this is all very complex, but it did work. 我知道这一切都非常复杂,但确实有效。 The CollisionListener had methods such as: CollisionListener具有以下方法:

public void CollissionStarted();
public void CollissionEnded();

I wish that I could give you an SSCCE: http://sscce.org but I don't have the code anymore. 我希望我能给你一个SSCCE: http ://sscce.org但我不再有代码了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM