弾を撃つ
init:function () {//初期化
//トリガーを押した時(両手)
this.el.addEventListener('triggerdown', function (e) {
});
//Aボタンを押した時(右手のみ)
this.el.addEventListener('abuttondown', function (e) {
});
//Bボタンを押した時(右手のみ)
this.el.addEventListener('bbuttondown', function (e) {
});
//Xボタンを押した時(左手のみ)
this.el.addEventListener('xbuttondown', function (e) {
});
//Xボタンを離した時(左手)
this.el.addEventListener('xbuttonup', function (e) {
});
} ,
ここを編集
68.
弾を撃つ
this.el.addEventListener('triggerdown', function (e){
//コントローラの3次元的な位置を取得
var point = this.object3D.getWorldPosition();
//ボールを生成
var ball = document.createElement('a-sphere');
ball.setAttribute('position', point);
ball.setAttribute('scale', '0.2 0.2 0.2');
ball.setAttribute('class', 'ball');
//物理演算を適用
ball.setAttribute('dynamic-body', 'shape: sphere; sphereRadius:0.2; ');
//a-sceneにアクセス
var scene = document.querySelector('a-scene');
//VR空間に弾を登場させる
scene.appendChild(ball);
});
このあとここに弾を発射するコードを追加
69.
弾を撃つ
//コントローラのレイキャスターを取得
var dir =this.getAttribute("raycaster").direction;
//コントローラに対するレイの向きと力を設定
var force = new THREE.Vector3(dir.x, dir.y, dir.z);
force.multiplyScalar(2500);
//ボールにforceというプロパティを宣言して代入
ball.force = this.object3D.localToWorld(force);
//弾の準備ができたら上記で設定した力を加える
ball.addEventListener('body-loaded', function (e) {
var p = this.object3D.position;
var f = this.force;
this.body.applyForce(
new CANNON.Vec3(f.x, f.y, f.z),
new CANNON.Vec3(p.x, p.y, p.z)
);
});
Lesson14