处理子弹互动

时间:2015-01-15 20:13:22

标签: animation 2d processing collision interaction

您好如何让子弹与处理中的对象发生碰撞? 子弹被射击并被翻译和旋转 但每当我尝试使用函数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();

谢谢

1 个答案:

答案 0 :(得分:1)

你从dist()获得0,因为translate()移动了坐标系!我认为,除了您的问题之外,您还需要重新考虑整体代码。您转换为side.x, side.y(在您调用0,0之前将为popMatrix()),然后在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,其中包含许多可能有用的示例。

相关问题