简体   繁体   English

p5.j​​s中两个不同形状的物体碰撞检测

[英]Two different shaped objects collision detection in p5.js

I'm trying to have my Particle object collide and reflect off my Slate object. 我试图让我的粒子对象碰撞并反射出我的Slate对象。

If I wanted to use an ellipse, it would be simple because I could just create a radius variable - can't do that with a rectangle. 如果我想使用椭圆,那会很简单,因为我可以创建一个radius变量-不能用矩形做到这一点。

It's something to do with the distance variable, I just can't figure it out. 这与距离变量有关,我只是想不通。

var div;
var movers;


function setup() {
  createCanvas(windowWidth,windowHeight);
  background("#FDEDEB");
  div = new Slate();
  movers = new Particle();
}

function draw() {

  background("#FDEDEB");

  div.display();

  movers.display();
  movers.update();
  movers.move();

  if (movers.hits(div)) {
    console.log("hit");
  }

}


function Slate() {

  this.x = 30;
  this.y = height/2;

  this.display = function() {
  noStroke();
  fill("#DF4655");
  rect(this.x,this.y, 700, 200);

  }
}

function Particle() {
  this.pos = createVector(10,0);
  this.vel = createVector(0,0);
  this.acc = createVector(0.01,0.01);
  this.history = [];

  this.display = function() {
    fill("#DF4655");
    point(this.pos.x,this.pos.y);

    //beginShape();
    for(var j = 0; j < this.history.length; j++) {
      var pos = this.history[j];
      ellipse(pos.x,pos.y, 5, 3);
    }
    //endShape();

  }

  this.update = function() {

    var v = createVector(this.pos.x,this.pos.y);
    this.history.push(v);
    if (this.history.length > 10) {
      this.history.splice(0,1);
    }
  }

  this.hits = function(div) {
        // BOUNCE OFF SLATE
      var d = dist(this.pos.x,this.pos.y,div.x,div.y);
      if (d < 0) {
        console.log('hits');
      }
  }


  this.move = function() {

    this.pos.add(this.vel);
    this.vel.add(this.acc);
    this.vel.limit(10);

    for (var i = 0; i < this.history.length; i++) {
      this.history[i].x += random(-2,2);
      this.history[i].y += random(-2,2);
    }

  }
}

If the particle is a point (or can be represented as a point), you need to use point-rectangle collision detection. 如果粒子是点(或可以表示为点),则需要使用点矩形碰撞检测。 Basically you would need to check whether the point is between the left and right edges and the top and bottom edges of the rectangle. 基本上,您需要检查该点是否在矩形的左右边缘以及上下边缘之间。

if(pointX > rectX && pointX < rectX + rectWidth && pointY > rectY && pointY < rectY + rectHeight){
  //the point is inside the rectangle
}

If the particle is an ellipse and you need to factor in the radius of that ellipse, then you're better off representing the particle as a rectangle, just for collision purposes. 如果粒子是椭圆形,并且您需要考虑该椭圆的半径,那么最好将粒子表示为矩形,仅用于碰撞目的。 Then you can use rectangle-rectangle collision detection. 然后,您可以使用矩形-矩形碰撞检测。 This is also called a bounding box, and it's one of the most common ways to handle collision detection. 这也称为边界框,它是处理碰撞检测的最常用方法之一。

if(rectOneRight > rectTwoLeft && rectOneLeft < rectTwoRight && rectOneBottom > rectTwoTop && rectOneTop < rectTwoBottom){
  //the rectangles are colliding
}

Shameless self-promotion: I wrote a tutorial on collision detection available here . 无耻的自我促进:我在此处编写了有关碰撞检测的教程。 It's for Processing, but everything is the same for P5.js. 它用于处理,但P5.js的所有内容都相同。

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

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