简体   繁体   English

在P5.js中的网格内旋转对象

[英]Rotating objects within a grid in P5.js

I'm trying to use decent OOP approach to getting objects to rotate individually within the cells of a grid. 我正在尝试使用体面的OOP方法来使对象在网格的单元格内单独旋转。 My current result is rotating all objects around the 0, 0 reference point instead of each within it's own cell. 我当前的结果是围绕0、0参考点旋转所有对象,而不是围绕其自己的像元旋转每个对象。

Of course, what's needed is a correct translate function, but when I try to apply this in the innerSquare function translate(x, y); 当然,需要一个正确的转换函数,但是当我尝试在innerSquare函数中应用它时,translate(x,y); - this leads to even more bizarre behavior. -这会导致更奇怪的行为。

I'm still at early stage learning - any help would be appreciated! 我仍处于早期学习阶段,我们将不胜感激!

The code can be viewed here: https://editor.p5js.org/knectar/sketches/BJpI5_BG4 可以在这里查看代码: https : //editor.p5js.org/knectar/sketches/BJpI5_BG4

Or directly: 或直接:

 var cols, rows; var w = 50; var grid = []; function setup() { createCanvas(400, 400); // load the col / row vars with values that dynamically read from the canvas. cols = floor(width/w); rows = floor(height/w); // load the the array with generic row and column values for (var j = 0; j < rows; j++){ for (var i = 0; i < cols; i++){ // And for each, create an object instance based on the Shape class. var shape = new Shape(i,j); grid.push(shape); } } } function draw() { background(51); frameRate(2); // draw grid (outer squares) for (var i = 0; i < grid.length; i++) { grid[i].outerGrid(); } // draw inner squares for (var i = 0; i < grid.length; i++) { grid[i].innerSquare(); } } function Shape(i, j) { this.i = i; this.j = j; var x = this.i*w; var y = this.j*w; this.outerGrid = function () { push(); stroke(200, 0, 255); noFill(); rect(x, y, w, w); translate(x, y); pop(); } this.innerSquare = function () { // translate(x, y); noFill(); stroke(150, 0, 255); rect(x+10, y+10, w-20, w-20); rotate(radians(frameCount)); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script> 

If you want to rotate a rectangle around a pivot, then you have draw the in this way, that the center of the rectangle is at position (0, 0): 如果要围绕枢轴旋转矩形,则可以通过这种方式绘制,矩形的中心位于位置(0,0):

let rect_w = (w-20);
rect(-rect_w/2, -rect_w/2, rect_w, rect_w);

Rotate ( rotate() ) the rectangle around (0, 0), which is the center of the rectangle, too: 也围绕(0,0 rotate()旋转( rotate() )矩形,矩形也是矩形的中心:

rotate(angle);

Translate ( translate() ) the rectangle to its final position: 将矩形平移( translate() )到其最终位置:

let tx = x+10 + rect_w / 2;
let ty = y+10 + rect_w / 2;

translate(tx, ty);

The matrix stack has to be saved ( push() ) before this operations and restored ( pop() ) after. 矩阵堆栈必须在此操作之前保存( push() ),之后要保存( pop() )。 The manipulations to the current model matrix have to be performed in reverse order: 当前模型矩阵的操作必须以相反的顺序执行:

eg 例如

this.innerSquare = function () {
    noFill();
    stroke(150, 0, 255);
    let ts = millis()/1000.0;
    let angle = radians(ts*2.0*Math.PI*5.0); // or "frameCount"
    let rect_w = (w-20);
    let tx = x+10 + rect_w / 2;
    let ty = y+10 + rect_w / 2;
    push();
        translate(tx, ty);
        rotate(angle);
        rect(-rect_w/2, -rect_w/2, rect_w, rect_w);
    pop();
}

See the example, where I applied the suggested changes to your original code: 请参见示例,其中我将建议的更改应用于原始代码:

 var cols, rows; var w = 50; var grid = []; function setup() { createCanvas(400, 400); // load the col / row vars with values that dynamically read from the canvas. cols = floor(width/w); rows = floor(height/w); // load the the array with generic row and column values for (var j = 0; j < rows; j++){ for (var i = 0; i < cols; i++){ // And for each, create an object instance based on the Shape class. var shape = new Shape(i,j); grid.push(shape); } } } function draw() { background(51); //frameRate(2); // draw grid (outer squares) for (var i = 0; i < grid.length; i++) { grid[i].outerGrid(); } // draw inner squares for (var i = 0; i < grid.length; i++) { grid[i].innerSquare(); } } function Shape(i, j) { this.i = i; this.j = j; var x = this.i*w; var y = this.j*w; this.outerGrid = function () { push(); stroke(200, 0, 255); noFill(); rect(x, y, w, w); translate(x, y); pop(); } this.innerSquare = function () { noFill(); stroke(150, 0, 255); let ts = millis()/1000.0; let angle = radians(ts*2.0*Math.PI*5.0); let rect_w = (w-20); let tx = x+10 + rect_w / 2; let ty = y+10 + rect_w / 2; push(); translate(tx, ty); rotate(angle); rect(-rect_w/2, -rect_w/2, rect_w, rect_w); pop(); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script> 

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

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