Javascript 按下鼠标时的矢量吸引
目前正在尝试使用createVector函数在3D立方体上创建吸引效果。设计用于在按下鼠标时触发的吸引力,但当前它表示错误:Javascript 按下鼠标时的矢量吸引,javascript,p5.js,Javascript,P5.js,目前正在尝试使用createVector函数在3D立方体上创建吸引效果。设计用于在按下鼠标时触发的吸引力,但当前它表示错误: Uncaught TypeError: Cannot read property 'copy' of undefined 守则: let cubes = []; function setup() { createCanvas(windowWidth, windowHeight, WEBGL); backCol = color(243, 243, 243);
Uncaught TypeError: Cannot read property 'copy' of undefined
守则:
let cubes = [];
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
backCol = color(243, 243, 243);
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
let xPos = map(i, 0, 9, 50, width - 50);
let yPos = map(j, 0, 9, 50, height - 50);
cubes.push(new Cubes(xPos, yPos));
}
}
}
function draw() {
background(backCol);
noFill();
for (let cube of cubes) {
cube.update();
}
attracting();
}
function attracting() {
for (let a = 0; a < cubes.length; a++) {
cubes[a].attraction(mouseX,mouseY);
}
}
class Cubes {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 30;
this.stroke = 70;
this.gap = 150;
this.shift1 = color(96);
this.shift2 = color(244);
//vector variables
this.pos = createVector(x, y);
this.vel = createVector();
this.acc = createVector();
}
update() {
this.shape();
this.test_Color();
//attraction values
this.vel.add(this.acc);
this.vel.limit(5);
this.pos.add(this.vel);
this.acc.mult(0);
}
shape() {
push();
stroke(this.stroke);
this.test_Color();
translate(this.x - width / 2, this.y - height / 2, 0);
this.test_rotation()
box(this.size);
pop();
}
test_Color() {
fill(this.shift1);
}
test_rotation() {
rotateX(frameCount / 60);
rotateY(frameCount / 60);
}
attraction(target) {
//all cubes supposed to attract towards the mouse when pressed
if (mouseIsPressed) {
let force = p5.Vector.sub(target.pos,this.pos);
let d = force.mag();
d = constrain(d, 1, 25);
var G = 50;
var strength = G / (d * d);
force.setMag(strength);
if (d < 20) {
force.mult(10);
}
this.vel.add(force);
}
}
}
let cubes=[];
函数设置(){
createCanvas(窗口宽度、窗口高度、WEBGL);
背景色=颜色(243243243243);
for(设i=0;i<10;i++){
for(设j=0;j<10;j++){
设xPos=map(i,0,9,50,宽度-50);
设yPos=map(j,0,9,50,高度-50);
push(新的多维数据集(xPos,yPos));
}
}
}
函数绘图(){
背景(背景色);
noFill();
for(让立方体为立方体){
update();
}
吸引();
}
函数吸引(){
for(设a=0;a
不确定要进一步添加什么细节。不确定要进一步添加什么细节。不确定要进一步添加什么细节。不确定要进一步添加什么细节。不确定要进一步添加什么细节。不确定要进一步添加什么细节。不确定要进一步添加哪些细节。- 您正在使用索引
作为a
的参数,该参数期望对象具有Cubes.attraction
向量场(立方体?)pos
- 您正在尝试使用
/this.x
定位多维数据集,更新不会更改这些多维数据集。改用this.y
向量this.pos
- 您可以通过只按下一次鼠标来进行优化,然后在每个立方体上以鼠标坐标作为向量参数调用
吸引力
- 在物理学中,力驱动加速度,而不是速度。我改变了,但也许是你故意的
- 您应该将类名更改为
,而不是Cube
Cubes
let cubes=[];
函数设置(){
/*这里的max(…)仅用于以片段中的最小大小进行渲染*/
createCanvas(最大(窗口宽度,800),最大(窗口高度,600),WEBGL);
背景色=颜色(243243243243);
for(设i=0;i<10;i++){
for(设j=0;j<10;j++){
设xPos=map(i,0,9,50,宽度-50);
设yPos=map(j,0,9,50,高度-50);
push(新的多维数据集(xPos,yPos));
}
}
}
函数绘图(){
背景(背景色);
noFill();
for(让立方体为立方体){
update();
}
吸引();
}
函数吸引(){
/*更改为检查所有多维数据集是否按过一次鼠标*/
如果(按下鼠标){
/*为所有立方体生成一次鼠标位置向量*/
const mousePosVect=新的p5.向量(mouseX,mouseY);
for(设a=0;a
P5立方吸引子
不清楚您希望多维数据集被什么吸引。彼此?鼠标指针?对不起,刚才注意到了吸引力(目标)
中的评论,所以鼠标指针就是*按“谢谢”解决了所有问题时吸引鼠标