p5 向量减法 'sub' 返回错误
p5 vector subraction 'sub' returning error
我一直在尝试将 p5 草图上传到 React 构建中。使用 react-p5-wrapper 我能够在屏幕上成功呈现我的 canvas 但是,一些向量函数导致错误。
var distance = this.position.dist(ball.position)
var minDistance = this.radius + ball.radius
if (ball === this || distance > minDistance) {
return
}
// balls colliding. First, move them apart.
console.log(this.position)
console.log(ball.position)
let angle = p5.Vector.sub(this.position, ball.position)// <= RIGHT HERE[![Error Message][1]][1]
// return
在这部分我的 console.logs 两个 return 有效的 3D 矢量但是当我使用 p5.Vector.sub(v1, v2) 我 returned 一个错误说“类型错误:无法读取未定义的 属性 'sub'”。我的子功能可能与其他功能混淆了吗?我附上了我的错误输出的图片。当我在 console.log 中获取它们的值时,我不确定为什么程序将它们读取为未定义。感谢您的任何建议
根据 react-p5-wrapper
的作者在 this issue
the p5 that gets passed into the module isn't the same as the global p5 you traditionally get with a script tag. To access certain things with this package (due to the p5 node module itself) you need to use p5.constructor.[something]
在你的情况下基本上使用 p5.constructor.Vector.sub(this.position, ball.position)
。
我一直在尝试将 p5 草图上传到 React 构建中。使用 react-p5-wrapper 我能够在屏幕上成功呈现我的 canvas 但是,一些向量函数导致错误。
var distance = this.position.dist(ball.position)
var minDistance = this.radius + ball.radius
if (ball === this || distance > minDistance) {
return
}
// balls colliding. First, move them apart.
console.log(this.position)
console.log(ball.position)
let angle = p5.Vector.sub(this.position, ball.position)// <= RIGHT HERE[![Error Message][1]][1]
// return
在这部分我的 console.logs 两个 return 有效的 3D 矢量但是当我使用 p5.Vector.sub(v1, v2) 我 returned 一个错误说“类型错误:无法读取未定义的 属性 'sub'”。我的子功能可能与其他功能混淆了吗?我附上了我的错误输出的图片。当我在 console.log 中获取它们的值时,我不确定为什么程序将它们读取为未定义。感谢您的任何建议
根据 react-p5-wrapper
的作者在 this issue
the p5 that gets passed into the module isn't the same as the global p5 you traditionally get with a script tag. To access certain things with this package (due to the p5 node module itself) you need to use p5.constructor.[something]
在你的情况下基本上使用 p5.constructor.Vector.sub(this.position, ball.position)
。