在famo.us物理引擎中,是否可以在不改变x/y分量的情况下设置粒子速度的大小?我希望能够以类似的方式,独立于球对象与其他对象的交互来设置球对象的速度
ball.particle.setVelocity([1,1,0]);
但只影响速度的大小。
我想这个问题的一个更普遍的版本是,向量可以根据大小和角度来操纵,而不是根据x和y坐标来操纵吗?
矢量的角度和大小是根据Famo.us中的x、y和z坐标计算的。
了解Magnitude将对您有所帮助。幅值是通过使用坐标计算的向量的度量导数。幅值是矢量的长度。你可以在卡恩学院学习有关震级的知识。仅仅基于知道"幅值"是无法知道向量值的。
回到您想要使用Famo.us.实现的目标
粒子的基本解释
Famo.us中的粒子基于其速度、位置和力。因此,我们必须知道角度的矢量,才能在Famo.us中设定粒子的速度。目前还没有一种方法可以通过角度并设定速度。
在Famo.us中创建具有恒定幅值的示例
jsBin上的工作示例代码
设置原始矢量
var impulse = 0.01;
var vector = new Vector(1,1,0);
// Setup our maximum Magnitude to the magnitude of our vector
var maxMagnitude = vector.norm();
将力设置为粒子上的脉冲,以使其保持运动。将我们的速度设置为最大值,以控制我们的恒定速度。
注:Famo.us中粒子的幅值为particle.norm()
。不知道他们为什么不称之为震级。
function setForce() {
// Apply an impulse to the current vector keep it in motion
this.applyImpulse(this.velocity.cap(impulse));
// set the velocity speed to be a max in units
this.setVelocity(this.velocity.cap(maxMagnitude));
// Return the particles transform value
return this.getTransform();
}
从粒子变换进行变换
var physicsEngine = new PhysicsEngine();
var particle = new Particle({
mass: 1,
position: [100, 100, 0]
});
var modifier = new Modifier();
modifier.transformFrom(setForce.bind(particle));
var surface = new new ImageSurface({
content: 'http://code.famo.us/assets/famous_logo.svg',
size: [radius * 2, radius * 2],
properties: {
borderRadius: (radius * 2) + 'px'
}
});
physicsEngine.addBody(particle);
context.add(modifier).add(surface);
//Start the particle in motion
particle.setVelocity([vector.x, vector.y, 0]);
这将允许任意设置大小&方向(以标准方向的度为单位)。
function setMagAndDir(particle, magnitude, angle) {
angle = angle * (Math.PI / 180);
var xComp = magnitude * Math.cos(angle);
var yComp = -1 * magnitude * Math.sin(angle);
particle.setVelocity([xComp,yComp,0]);
};
唯一的缺点是两者必须同时设置。要单独设置,请传递下面的一个getter。
function readMagnitude(particle) {
return Math.sqrt( ((particle.getVelocity()[0]) * (particle.getVelocity()[0])) + ((particle.getVelocity()[1]) * (particle.getVelocity()[1])) );
};
function readDirection(particle) {
var direction = Math.atan2((-1 * particle.getVelocity()[1]),particle.getVelocity()[0]);
direction = direction * (180 / Math.PI);
if (particle.getVelocity()[1] > 0) {
direction = direction + 360;
}
return direction;
};