简体   繁体   English

数组中的 p5.js 对象碰撞检测

[英]p5.js objects in an array collision detection

So, this issue is similar but is not the same as this question so don't flag as a duplicate question.所以,这个问题是相似的,但不一样的这个问题,所以不要标志作为一个重复的问题。 If I create two separate rectangles like this:如果我像这样创建两个单独的矩形:

//rec is an object I made for the rectangles
rect1 = new rec(random(width-40), random(height-40), 40, 40);
rect2 = new rec(random(width-40), random(height-40), 40, 40);

my code works fine.我的代码工作正常。

but if i create an array of rectangles like this:但是如果我创建一个这样的矩形数组:

//r is an array I made and rec is an object I made for the rectangles
for(var i = 0;i < 10;i++) {
    r[i] = new rec(random(width-40), random(height-40), 40, 40); 
}

I can't call any variables inside the object like this:我不能像这样调用对象内的任何变量:

console.log(r[1].x); //returns: Uncaught TypeError: Cannot read property 'x' of undefined

So, here is the code i wrote for object collision in p5.js:所以,这是我在 p5.js 中为对象碰撞编写的代码:

var rect1;
var r;
var num;
function setup(){
    r = [];
    num = 2;
    createCanvas(windowWidth,windowHeight- 4);
    for(var i = 0;i < num; i++){

        r = new rec(random(width-40),random(height-40),40,40);

    }

}

function draw(){
    background(40);
    r.show();
    console.log(r[1].x);
    for(var i = 0;i < num; i++)    {
        for(var j = 0;j<num; j++){


            if(r[i].x + r[i].width+r[i].xa >= r[j].x && r[i].y +r[i].height >=r[j].y && r[i].y<=r[j].y +r[j].height && r[i].x + r[i].xa <= r[j].x + r[j].width){
                r[i].xa *= -1;
                r[j].xa *= -1;

            }
            if(r[i].y + r[i].height + r[i].ya>= r[j].y && r[i].x +r[i].width>=r[j].x && r[i].x<=r[j].x + r[j].height && r[i].y + r[i].ya <= r[j].y + r[j].height){
                r[i].ya *= -1;
                r[j].ya *= -1;
            }
        }    
    }


}
function rec(x,y,wid,hei){
    this.x = x;
    console.log(this.x);
    this.y = y;
    this.width = wid;
    this.height= hei;
    this.xa = random(2,5);
    this.ya = random(2,5);
    this.show = function(){
        this.x;
        this.y;
        fill(255);
        noStroke();
        rect(this.x,this.y,this.width,this.height);
        this.x += this.xa;
        this.y += this.ya;
        if(this.x > width-this.width||this.x <0){
            this.xa *= -1;
            if(this.x > width/2){
                this.x = width-this.width;
            }else{
                this.x = 0;
            }
        }
        if(this.y > height-this.height||this.y <0){
            this.ya *= -1;
            if(this.y > height/2){
                this.y = height-this.height;
            }else{
                this.y = 0;
            }

        }

    }
}
function windowResized(){
    createCanvas(windowWidth,windowHeight- 4);
}

In your code, you write:在你的代码中,你写:

for(var i = 0;i < num; i++){

    r = new rec(random(width-40),random(height-40),40,40);

}

but you should have:但你应该有:

for(var i = 0;i < num; i++){

    r[i] = new rec(random(width-40),random(height-40),40,40);

}

Also, in draw() :此外,在draw()

  r.show();

needs to be:需要是:

  r[1].show();

or similar.或类似。

Add:添加:

r[0].show();

and you can see your code working as it should!你可以看到你的代码正常工作! Just need to add the rest of the rectangles...只需要添加其余的矩形...

Here is the demo:这是演示:

 var rect1; var r; var num; function setup() { r = []; num = 2; createCanvas(windowWidth, windowHeight - 4); for (var i = 0; i < num; i++) { r[i] = new rec(random(width - 40), random(height - 40), 40, 40); } } function draw() { background(40); r[0].show(); r[1].show(); for (var i = 0; i < num; i++) { for (var j = 0; j < num; j++) { if (r[i].x + r[i].width + r[i].xa >= r[j].x && r[i].y + r[i].height >= r[j].y && r[i].y <= r[j].y + r[j].height && r[i].x + r[i].xa <= r[j].x + r[j].width) { r[i].xa *= -1; r[j].xa *= -1; } if (r[i].y + r[i].height + r[i].ya >= r[j].y && r[i].x + r[i].width >= r[j].x && r[i].x <= r[j].x + r[j].height && r[i].y + r[i].ya <= r[j].y + r[j].height) { r[i].ya *= -1; r[j].ya *= -1; } } } } function rec(x, y, wid, hei) { this.x = x; this.y = y; this.width = wid; this.height = hei; this.xa = random(2, 5); this.ya = random(2, 5); this.show = function() { this.x; this.y; fill(255); noStroke(); rect(this.x, this.y, this.width, this.height); this.x += this.xa; this.y += this.ya; if (this.x > width - this.width || this.x < 0) { this.xa *= -1; if (this.x > width / 2) { this.x = width - this.width; } else { this.x = 0; } } if (this.y > height - this.height || this.y < 0) { this.ya *= -1; if (this.y > height / 2) { this.y = height - this.height; } else { this.y = 0; } } } } function windowResized() { createCanvas(windowWidth, windowHeight - 4); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.22/p5.min.js"></script>

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

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