【问题标题】:Processing Bullets interaction处理子弹交互
【发布时间】:2015-03-14 08:43:02
【问题描述】:

嗨,我如何使子弹与处理中的对象发生碰撞? 子弹被发射并被平移和旋转 但是每当我尝试使用函数 dist() 它总是给我 0 作为向量的位置 如果我希望子弹使用距离与物体碰撞并使另一个物体消失,我如何获得正确的矢量位置?

这是代码

void move(){ 
   passed  = passed + time;
   if (passed > bulletLife) {
      alive = false;
   }

   forward.x = sin(theta);
   forward.y = -cos(theta);
   float speed = 15.0f;
   velocity = PVector.mult(forward, speed);
   side.add(forward);

   void display(){ 
      pushMatrix();
      translate(side.x, side.y);
      rotate(theta);
      stroke(255);
      ellipse(side.x, side.y, 30, 30);
      popMatrix();

谢谢

【问题讨论】:

  • 这段代码你在哪里使用 dist() 函数?可以发MCVE吗?
  • 这是我计算距离的方法 PVector kool2 = coordinates();浮动 d = kool2.dist(位置) / 10; }//结束显示() PVector坐标() { PVector kool; kool = new PVector(side.x + cos(theta) * x2, side.y + sin(theta) * y2);返回库尔; }//结束坐标

标签: animation 2d processing collision interaction


【解决方案1】:

您从 dist() 得到 0,因为 translate() 移动坐标系!我认为,除了您的问题之外,您还需要重新考虑您的代码。您转换为side.x, side.y(在您调用popMatrix() 之前将是0,0),然后您在side.x, side.y 处绘制椭圆,该椭圆偏离其实际位置。

换句话说:如果位置是100,200,你实际上是在200,400处绘制对象!

如果您跳过translate() 部分,您可以使用它来绘制您的对象:

void display() {
  stroke(255);
  ellipse(side.x, side.y, 30,30);
}

这是检查碰撞:

if (dist(side.x, side.y, bullet.x, bullet.y) == 0) {
  collision = true;
}
else {
  collision = false;
}

您还可以查看我的collision-detection functions for Processing,其中有很多示例可能会有所帮助。

【讨论】:

  • 感谢所有这些,但是当我跳过翻译时,子弹只会出现在随机位置,或者至少不会出现在船舶位置
  • 不过,这不是您的问题所在。也许把它带到处理论坛来寻求帮助?您可能还想考虑添加PVectors,例如使用一个作为方向/速度。
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多