简体   繁体   English

Java游戏碰撞检测,(侧面碰撞)与矩形

[英]Java Gaming collision detection, (side collision) with rectangles

I have been writing a Game Engine and I have a problem with my actor class. 我一直在写游戏引擎,我的演员类有问题。 I want to determine the collision with a rectangle (above) and a side of a rectangle. 我想确定与矩形(上方)和矩形边的碰撞。 I had written two methods for them. 我为他们写了两种方法。

public boolean isLeftCollision(Actor actor) {
    boolean bool = false;
    Rectangle LeftBounds = new Rectangle(x, y, x-velocity, image.getHeight(null));
    bool = LeftBounds.intersects(actor.getBounds());
    return bool;
}

public boolean isRightCollision(Actor actor) {
    boolean bool = false;
    Rectangle RightBounds = new Rectangle(x+image.getWidth(null), y, image.getWidth(null)+velocity, image.getHeight(null));
    bool = RightBounds.intersects(actor.getBounds());
    return bool;
}

Here velocity is the movement for next step. 速度是下一步的运动。

But they both give me error (ie., false judgements). 但他们都给我错误(即错判断)。 How can I solve this in the actor class. 我怎样才能在actor类中解决这个问题。

I admit I can hardly read your code and I'm sorry if my answer isn't helpful. 我承认我很难读你的代码,如果我的答案没有用,我很抱歉。 My guess is that the velocity in the collision produces the errors. 我的猜测是碰撞中的速度会产生误差。 Depending on how often you check and what values velocity holds you might register collisions that haven't occured yet... 根据您检查的频率和速度保持的值,您可以注册尚未发生的碰撞......

I'd do the collision detection in two steps. 我将分两步进行碰撞检测。

  1. Test for a collision 测试碰撞
  2. Determine if it's above or to one side. 确定它是在上方还是在一侧。

here's some pseudocode: 这是一些伪代码:

Rectangle self_shape=this.getBounds();
Rectangle other_shape=actor.getBounds();
bool collision = self_shape.intersects(other_shape);
if(collision){
    //create two new variables self_centerx and self_centery
    //and two new variables other_centerx and other_centery
    //let them point to the coordinates of the center of the
    //corresponding rectangle

    bool left=self_centerx - other_centerx<0
    bool up=self_centery - other_centery<0
}

That way you can look where the other actor is positioned relative to you. 这样你就可以看到另一个演员相对于你的位置。 If it's above or to one side. 如果它在上方或一侧。

Remember that the third parameter to Rectangle is the width, not the x of the other side. 请记住,Rectangle的第三个参数是宽度,而不是另一侧的x。 So, what you really want is probably like this: 所以,你真正想要的可能是这样的:

public boolean isLeftCollision(Actor actor) {
   return new Rectangle(x - velocity, y, velocity, image.getHeight(null))
         .intersects(actor.getBounds());
}

public boolean isRightCollision(Actor actor) {
   return new Rectangle(x + image.getWidth(null), y, velocity, image.getHeight(null))
         .intersects(actor.getBounds());
}

(Assuming velocity is the (positive) distance to move left or right, and only the method for the direction you are moving will be called) (假设速度是向左或向右移动的(正)距离,并且只调用您正在移动的方向的方法)

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

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