移动物体卡在角落里

发布于 2024-10-02 05:01:19 字数 1889 浏览 3 评论 0原文

我正在开发一个模拟物体在田野中移动的程序。该场的边界为 1024x1024。该对象的 x,y 坐标不能低于 0,也不能高于 1024。我为每个对象提供了一个名为“move()”的方法,该方法以当前速度沿当前方向移动对象。如果物体接近边界,它就会以新的方向和相同的速度转动。

我遇到的问题是,当我的一个物体靠近 x 和 y 边界(场地的角落)时,它就会卡在角落里。就好像它试图离开角落,但随后又转回来。它一定很喜欢那个角落。我查看了我的代码,对我来说,我的逻辑似乎是正确的。我检查以确保新方向不是负数或超过 359。我检查以确保新方向的新 x,y 坐标也在范围内。我什至有一个方法来设定新的方向。

我尝试过用不同的逻辑重新实现这个方法,但没有成功。如果有人能发现我的编程中的缺陷或指出可能导致该缺陷的原因,那么我们将不胜感激。

我尝试调试并单步执行我的程序,发现当它到达角落时,它会改变方向转身,移动大约 3 个空格,然后回到角落。一定是一个美妙的角落。

move方法的代码如下:

public void move(){

  localX = super.getX();
  localY = super.getY();

  float newX=0, newY=0;
  float testX, testY;
  boolean acceptX = false, acceptY = false;

  testX = (float) (Math.cos(direction)*10) + localX;
  testY = (float) (Math.sin(direction)*10) + localY;
  int testDirection;

  while(!acceptX){
   if(testX >= 0 && testX <= bound){
    newX = testX;
    acceptX = true;
   }//end if statement
   else{
    if(direction+180 > 359){
     setDirection(direction-180);
     testX = (float) (Math.cos(Math.toRadians(direction))*speed) + localX;
    }
    else{
     setDirection(direction+180);
     testX = (float) (Math.cos(Math.toRadians(direction))*speed) + localX;
    }
   }//end else
  }//end while that checks for X value

  while(!acceptY){
   if(testY >= 0 && testY <= bound){
    newY = testY;
    acceptY = true;
   }//end if statement
   else{
    if(direction+180 > 359){
     setDirection(direction-180);
     testY = (float) (Math.sin(Math.toRadians(direction))*speed) + localY;
    }
    else{
     setDirection(direction+180);
     testY = (float) (Math.sin(Math.toRadians(direction))*speed) + localY;
    }
   }//end else
  }//end while that checks for Y value

  super.setX(newX);
  super.setY(newY);

 }

这是setDirection的代码

public void setDirection(int d) {
        direction = d;
    }

I am working on a program which simulates objects moving in a field. The field has a boundary of 1024x1024. The object cannot go below 0 in terms of x,y coordinate and it cannot go above 1024. I have a method for each object called "move()" which moves the object in its current direction at its current speed. If the object approaches the boundary, it then turns around with a new direction and same speed.

The problem I am having is that when one of my objects gets close to both the x and y bound (corner of the field), it gets stuck in the corner. It is almost as if it is trying to move away from the corner, but then it turns back. It must love that corner. I looked over my code and to me, my logic seems correct. I check to make sure the new direction is not negative or over 359. I check to make sure the new x,y coordinate with the new direction is within the bounds too. I even have a method to set a new direction.

I have tried re-implementing this method with different logic, but no luck. If anyone could possibly find a flaw in my programming or point out what may be causing it, then that would be much appreciated.

I have tried to debug and step through my program and I see that when it gets to the corner, it changes direction to turn around, moves about 3 spaces, then goes back to the corner. Must be a wonderful corner.

Code for move method is below:

public void move(){

  localX = super.getX();
  localY = super.getY();

  float newX=0, newY=0;
  float testX, testY;
  boolean acceptX = false, acceptY = false;

  testX = (float) (Math.cos(direction)*10) + localX;
  testY = (float) (Math.sin(direction)*10) + localY;
  int testDirection;

  while(!acceptX){
   if(testX >= 0 && testX <= bound){
    newX = testX;
    acceptX = true;
   }//end if statement
   else{
    if(direction+180 > 359){
     setDirection(direction-180);
     testX = (float) (Math.cos(Math.toRadians(direction))*speed) + localX;
    }
    else{
     setDirection(direction+180);
     testX = (float) (Math.cos(Math.toRadians(direction))*speed) + localX;
    }
   }//end else
  }//end while that checks for X value

  while(!acceptY){
   if(testY >= 0 && testY <= bound){
    newY = testY;
    acceptY = true;
   }//end if statement
   else{
    if(direction+180 > 359){
     setDirection(direction-180);
     testY = (float) (Math.sin(Math.toRadians(direction))*speed) + localY;
    }
    else{
     setDirection(direction+180);
     testY = (float) (Math.sin(Math.toRadians(direction))*speed) + localY;
    }
   }//end else
  }//end while that checks for Y value

  super.setX(newX);
  super.setY(newY);

 }

and here is the code for setDirection

public void setDirection(int d) {
        direction = d;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿萝 2024-10-09 05:01:19

假设左上角有一个向上的物体。你的第一个测试扭转了局面,所以它下降了。然后是你的第二张支票,这将再次扭转价格……再次上涨。

您的代码还可以使用更多的可读性。我注意到的第一件事是,您正在使用 >359 检查来规范化新的进入方向。然而,所有情况也都包含移动代码。我会做类似的事情:

setDirection(direction + 180);          //turn around
if (direction >= 360) direction -= 360; //normalize
testY = ...;                            //move

将运动代码移出检查 if/else 块的方向。 360 也是一个更好用的幻数; 359度没有任何意义。正如所建议的,您最终应该使用矢量库,从而放弃大部分数学运算。

Say you have an object in the upper left corner, going up. Your first test turns it around so it goes down. Then comes your second check, which turns it around again to go up... again.

Your code could also use some more readability. The very first thing I noticed is that you're using the >359 checks to normalize the new direction to go in. Yet all cases include the movement code as well. I would do something like:

setDirection(direction + 180);          //turn around
if (direction >= 360) direction -= 360; //normalize
testY = ...;                            //move

to move the movement code out of the direction checking if/else blocks. 360 is also a better magic number to use; 359 degrees means nothing. As has been suggested, you should ultimately use a vector library and thus throw away most of the math.

茶花眉 2024-10-09 05:01:19

我真的建议将方向存储为向量 (x, y),而不是从标量计算该向量;我认为这会对您的代码有很大帮助。

I'd really recommend storing your direction as a vector (x, y) instead of calculating that vector from a scalar; I think that would help you immensely with your code.

再浓的妆也掩不了殇 2024-10-09 05:01:19

问题:当您的物体碰到边缘时,您将其旋转 180 度。如果它碰到两个边缘,它就会原地旋转,并且测试坐标将始终位于错误的位置。

当你的一个物体碰到边缘时,它需要反弹,而不是关于脸!入射角==折射角,或类似的角度。换句话说,如果您检查 x 坐标并且它反弹,请否定 x 速度,而不是同时否定 x 和 x 速度。 y。

Issue: When your object hits an edge, you turn it 180 degrees. If it hits both edges, it'll spin in place, and the test coordinates will always be in the wrong spot.

When one of your objects hits an edge, it needs to bounce, not About Face! Angle of incidence == angle of refraction, or some such. In other words, if you're checking the x coordinate and it bounces, negate the x velocity, not both x & y.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文