点约束
如果添加了一个对象,那这个对象会随着指定的点产生类似大摆锤的效果,超出不了被添加时距离点的距离。
如果添加了两个对象,那这两个对象的距离超出不了添加场景时的两个物体的距离。
var constraint = new Physijs.PointConstraint(
physijs_mesh_a, // 第一个被约束的对象
physijs_mesh_b, // 限制第一个对象的物体,可以忽略,如果被忽略,第一个对象将被限制到点上
new THREE.Vector3( 0, 10, 0 ) // 被限制到的点的位置
);
scene.addConstraint( constraint );
案例查看:http://www.wjceo.com/examples/physijs/05.html
铰链约束
铰链约束限制物体的运动,使其像铰链一样起作用,例如门。与点约束一样,铰链可以用1个或2个物体创建。
var constraint = new Physijs.HingeConstraint(
physijs_mesh_a, // 第一个被约束的对象
physijs_mesh_b, // 限制第一个对象的物体,可以忽略,如果被忽略,第一个对象将被限制到点上
new THREE.Vector3( 0, 10, 0 ), // 被限制到的点的位置
new THREE.Vector3( 1, 0, 0 ) // 哪个轴向会被限制,当前默认是x轴
);
scene.addConstraint( constraint );
constraint.setLimits(
low, // 最小运动角度,以弧度表示
high, // 最大运动角度,以弧度表示
bias_factor, // 设置误差范围
relaxation_factor, // 控制极限反弹(0.0 ==不反弹)
);
constraint.enableAngularMotor( target_velocity, acceration_force ); //可以设置力的方向(正方向或者负方向),当前的加速度
constraint.disableMotor(); //关闭当前的移动
案例查看:http://www.wjceo.com/examples/physijs/06.html
滑块约束
滑块约束将约束物体与给定轴对齐的线性运动。例如推拉门上的推拉门或升降机构。像点约束和铰链约束一样,第二个对象是一个可选参数。
var constraint = new Physijs.SliderConstraint(
physijs_mesh_a, // 被约束的对象
physijs_mesh_b, // 限制第一个对象的物体,可以忽略,如果被忽略,第一个对象将被限制到点上
new THREE.Vector3( 0, 10, 0 ), // 被限制到的点的位置
new THREE.Vector3( 1, 0, 0 ) // 哪个轴向会被限制,当前默认是x轴
);
scene.addConstraint( constraint );
constraint.setLimits(
linear_lower, // 线性移动的下限,以世界单位表示
linear_upper, // 线性移动的上限,以世界单位表示
angular_lower, // 角度移动的下限,以弧度表示
angular_upper // 角度上限运动,以弧度表示
);
constraint.setRestitution(
linear, // 达到线性限制时的恢复量
angular // 达到角度限制时的恢复量
);
constraint.enableLinearMotor( target_velocity, acceration_force );//设置线性力的方向(正方向或者负方向),当前的加速度
constraint.disableLinearMotor();//关闭当前的线性移动
constraint.enableAngularMotor( target_velocity, acceration_force );//设置角旋转力的方向(正方向或者负方向),当前的加速度
constraint.disableAngularMotor();//关闭当前的旋转移动
案例查看:http://www.wjceo.com/examples/physijs/07.html
ConeTwist约束
锥形扭曲约束限制两个物体之间的运动和旋转,类似于肩膀或脚踝等人的关节。在这个约束中,两个对象是必需的。
var constraint = new Physijs.ConeTwistConstraint(
physijs_mesh_a, // 被约束的对象
physijs_mesh_b, // 限制第一个对象的物体
new THREE.Vector3( 0, 10, 0 ), // 被限制到的点的位置
);
scene.addConstraint( constraint );
constraint.setLimit( x, y, z ); // 每个轴限制的旋转限制,以弧度表示
constraint.setMotorMaxImpulse( max_impulse ); // 设置可以驱动第一个物体的力
constraint.setMotorTarget( target ); // target是约束的期望旋转,可以用THREE.Vector3,THREE.Matrix4或THREE.Quaternion
constraint.enableMotor(); //开启力
constraint.disableMotor(); //关闭力
案例查看:http://www.wjceo.com/examples/physijs/08.html
自由度约束
自由度约束条件完全控制对象应如何在线性和角度运动中受到约束
var constraint = new Physijs.DOFConstraint(
physijs_mesh_a, // 被约束的对象
physijs_mesh_b, // 限制第一个对象的物体
new THREE.Vector3( 0, 10, 0 ), // 被限制到的点的位置
);
scene.addConstraint( constraint );
constraint.setLinearLowerLimit( new THREE.Vector3( -10, -5, 0 ) ); // 设置沿x,y和z轴的线性运动的下限
constraint.setLinearUpperLimit( new THREE.Vector3( 10, 5, 0 ) ); // 设置沿x,y和z轴线性运动的上限
constraint.setAngularLowerLimit( new THREE.Vector3( 0, -Math.PI, 0 ) ); // 沿着x,y和z轴以弧度设置角运动的下限
constraint.setAngularUpperLimit( new THREE.Vector3( 0, Math.PI, 0 ) ); // 沿着x,y和z轴以弧度设置角运动的上限
constraint.configureAngularMotor(
which, // 指定马达工作的轴 0是x轴 1是y轴 2是z轴
low_limit, // 设置马达的角度的下限
high_limit, // 设置马达的角度的上限 下限设置的比上限高可以沿轴自由旋转
velocity, // 目标速度
max_force // 马达可以施加的最大力
);
constraint.enableAngularMotor( which ); // 开启哪个轴的马达0是x轴 1是y轴 2是z轴
constraint.disableAngularMotor( which ); // 关闭哪个轴的马达