很抱歉发了这么长的帖子,但我想尽可能的详细。
因此,我正在创建一个带有两个自定义ig.Class实例的插件。一个是向量实现: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]);
}
}
}我得到的错误是在update函数中的这一行: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变量时,我得到了输出到控制台的正确的值,但是如果仍然得到错误的话。我能想到的唯一一件事是,在tire方法中创建pos变量的地方,以及在Particle构造函数中传递和赋值它时,它会丢失它的值,并导致粒子的pos.x / pos.y值为NaN,当我将变量particles记录在错误线(var velocity = particles[i].pos.sub(particles[i].last).scale(this.friction);)之上时,我可以看到这一点。我真的不知道哪里出了问题,我试着改变Particle构造函数对参数pos的赋值,使其以多种方式赋值(通过Vec2.mutableSet()方法和直接设置)。但无论我做什么,它仍然会导致粒子的NaN值。
有人能看到我不是的东西吗?谢谢
发布于 2013-05-30 01:42:09
弄清楚了,有一个javascript文件正在被加载,它覆盖了Particle类。删除了javascript文件的script标记,一切正常
https://stackoverflow.com/questions/16817480
复制相似问题