抱歉,帖子很长,但我想尽可能详细
所以我正在创建一个带有几个自定义ig.Class
实例的插件。一个是 Vector 实现:Vec2然后我有一个粒子类:
Particle = ig.Class.extend({
pos: null,
last: null,
init: function (pos) {
this.pos = new Vec2().mutableSet(pos.x, pos.y);
this.last = this.pos;
},
draw: function (ctx) {
ctx.beginPath();
ctx.arc(this.pos.x, this.pos.y, 2, 0, 2 * Math.PI);
ctx.fillStyle = "#2dad8f";
ctx.fill();
}
});
接下来我在另一个类中有这个轮胎功能:
tire: function (origin, radius, segments, spokeStiffness, treadStiffness) {
var stride = (2 * Math.PI) / segments;
var composite = new Composite();
// particles
for (var i = 0; i < segments; i++) {
var theta = i * stride;
composite.particles.push(new Particle(new Vec2(origin.x + Math.cos(theta) * radius, origin.y + Math.sin(theta) * radius)));
}
var center = new Particle(origin);
composite.particles.push(center);
// constraints
for (i = 0; i < segments; i++) {
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i + 1) % segments], treadStiffness));
composite.constraints.push(new DistanceConstraint(composite.particles[i], center, spokeStiffness))
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i + 5) % segments], treadStiffness));
}
this.composites.push(composite);
return composite;
}
最后是与轮胎函数在同一类中的更新函数:
update: function (step) {
for (var c in this.composites) {
for (var i in this.composites[c].particles) {
var particles = this.composites[c].particles;
// calculate velocity
var velocity = particles[i].pos.sub(particles[i].last).scale(this.friction);
// ground friction
if (particles[i].pos.y >= this.height - 1 && velocity.length2() > 0.000001) {
var m = velocity.length();
velocity.x /= m;
velocity.y /= m;
velocity.mutableScale(m * this.groundFriction);
}
// save last good state
particles[i].last.mutableSet(particles[i].pos);
// gravity
particles[i].pos.mutableAdd(this.gravity);
// inertia
particles[i].pos.mutableAdd(velocity);
}
}
// relax
var stepCoef = 1 / step;
for (var c in this.composites) {
var constraints = this.composites[c].constraints;
for (var i = 0; i < step; ++i) {
for (var j in constraints) {
constraints[j].relax(stepCoef);
}
}
}
// bounds checking
for (var c in this.composites) {
var particles = this.composites[c].particles;
for (var i in particles) {
this.bounds(particles[i]);
}
}
}
我得到的错误是在更新函数的这一行:var velocity = particles[i].pos.sub(particles[i].last).scale(this.friction);
特别是错误是说它不能调用sub
未定义的方法。我将上面的轮胎方法更改为如下所示,以便可以对其进行调试:
tire: function (origin, radius, segments, spokeStiffness, treadStiffness) {
var stride = (2 * Math.PI) / segments;
var composite = new Composite();
// particles
for (var i = 0; i < segments; i++) {
var theta = i * stride;
var x = origin.x + Math.cos(theta) * radius;
var y = origin.y + Math.sin(theta) * radius;
var pos = new Vec2(x, y);
console.log(pos);
var particle = new Particle(pos);
composite.particles.push(particle);
}
var center = new Particle(origin);
composite.particles.push(center);
// constraints
for (i = 0; i < segments; i++) {
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i + 1) % segments], treadStiffness));
composite.constraints.push(new DistanceConstraint(composite.particles[i], center, spokeStiffness))
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i + 5) % segments], treadStiffness));
}
this.composites.push(composite);
return composite;
}
当我记录pos
变量时,我得到正确的值输出到控制台,但是如果仍然得到错误。我唯一能想到的是,pos
在方法中创建变量的位置tire
与在Particle
构造函数中传递和分配变量之间的某个位置,它会丢失其值并导致粒子具有pos.x / pos.y
NaN 值,我可以在记录时看到varparticles
高于错误行 ( var velocity = particles[i].pos.sub(particles[i].last).scale(this.friction);
)。我真的不知道出了什么问题我尝试更改Particle
构造函数的参数pos
分配以通过多种方式分配它(通过Vec2.mutableSet()
方法和直接设置。但无论我做什么,它仍然会导致粒子的 NaN 值。
谁能看到我不是的东西?谢谢