捻りシミュレーター(ブラウザ版)を改良して、捻る時の腕の捌き方を選択できるようにしました。このコードを書いてるとき、
PhysijsのSliderConstraintで一日悩んだので、書いておきます。
SliderConstraintの説明には、
var constraint = new Physijs.SliderConstraint(
physijs_mesh_a, // First object to be constrained
physijs_mesh_b, // OPTIONAL second object - if omitted then physijs_mesh_1 will be constrained to the scene
new THREE.Vector3( 0, 10, 0 ), // point in the scene to apply the constraint
new THREE.Vector3( 1, 0, 0 ) // Axis along which the hinge lies - in this case it is the X axis
);
とあり、X軸に沿ってスライドするようにしたければ、最後のパラメーターに、X軸の方向ベクトル ( 1, 0, 0 ) を渡せばよいと書かれていますが、これが大嘘です。
最後のパラメーターは、なんと、X軸を向いたベクトル ( 1, 0, 0 )を回転させるオイラー角です。従って、X軸に沿ってスライドさせたければ、
var constraint = new Physijs.SliderConstraint(
physijs_mesh_a, // First object to be constrained
physijs_mesh_b, // OPTIONAL second object - if omitted then physijs_mesh_1 will be constrained to the scene
new THREE.Vector3( 0, 10, 0 ), // point in the scene to apply the constraint
new THREE.Vector3( 0, 0, 0 ) // こうしないといけない
);
です。
オイラー角の順番に関する定義もゴタゴタしてるので、色々試しながらパラメーターを決めないといけません。そのとき、デバッグ用に
scene.addConstraint( constraint, true );
と言う風に、addConstraintの第二引数に true を付けてやると、指定した軸が見えるので便利です。