当我有特定的坐标来检测球和砖之间的碰撞时,为什么球对象与不可见的墙碰撞?

问题描述 投票:0回答:1

我想让球在任何给定的一侧单独与砖块碰撞,并且为此我设置了一个类来创建砖块并包括一个检测球和砖之间碰撞的功能,但它最终创建了隐形我设定的边界之外的对手。什么导致了隐形碰撞器?

我已经尝试更改碰撞参数的变量和坐标,以考虑每个砖块的中心和左上角,以防它是坐标的问题,但这只会使它变得更糟。我也尝试改变每块砖之间的距离,但也没有成功。我也尝试在不同的计算机上运行它无济于事,所以我不认为它与内部软件问题或bug有关。

int Posx = 150;
int Posy = 150;
int Velx = 5;
int Vely = 5;
int value = 0;
int BouncePosX = 800;
int BouncePosY = 900;
int BounceLeft = -35;
int BounceRight = 35;
int lifeCount = 3;
int points = 0;
int numBricks = 5;
Bricks[] bricks = new Bricks[numBricks];

//声明将跟踪屏幕周围形状的位置和速度的变量//

void setup() {
  size(1600,1000);
  frameRate(60);
  for (int i = 0; i<numBricks; i++){
    float x = 10 +(i*70);
    float y = 100;
    bricks[i] = new Bricks(x,y,50.0,30.0);


void draw(){
  background(0,255,0);
  fill(255,0,255);
  rect(BouncePosX,BouncePosY, 100, 5);
  fill(255,0,0);
  arc(Posx,Posy,20,20,0,TWO_PI);
  for (int i = 0; i<numBricks; i++){
    bricks[i].display();
    bricks[i].collide();


class Bricks {
  float x,y;
  float posBrickX,posBrickY;
  Bricks[] others;

  Bricks(float xin, float yin, float posBrickXin, float posBrickYin) {
    x = xin;
    y = yin;
    posBrickX = posBrickXin;
    posBrickY = posBrickYin;


}
  void collide() {



        if ((Posx <= x+25) && (Posy <= y+15) && (Posy >= y-15) || (Posx 
>= x-25) && (Posy <= y+15) && (Posy >=y-15)) {
          Velx= Velx*-1;
          points = points++;
        }
        if ((Posy <= y+15) && (Posx <= x+25) && (Posx >= x-25) || (Posy 
>= y-15) && (Posx <= x+25) && (Posx >= x-25)){
          Vely = Vely*-1;
          points = points++;
        }  

 void display() {
   rect(x,y,posBrickX,posBrickY);
}

我希望碰撞检测最终会通过使用坐标并将区域确定为碰撞来检测每个砖块的每一侧,但到目前为止,它只是在整个屏幕上创建了随机碰撞区域。

java processing collision-detection
1个回答
0
投票

我会尝试使用像Box2d这样的东西,因为碰撞检测和物理并不那么容易。如果想要实现弹性碰撞,仅仅检测一个点是否在另一个点内是不够的。您必须在时间n和n + 1为每个砖生成路径/线,并在此线上找到碰撞点。现在所有砖块都在同时移动,所以看起来它们正在“跳跃”在碰撞上。

Here is a simple visualization

© www.soinside.com 2019 - 2024. All rights reserved.