I am using a physics engine in my aframe application, and I want the camera to move when the user clicks a button.
I want to keep the physics engine properties so I am using applyImpulse as a method for motion.
Here is my example scene:
<script src="https://aframe.io/releases/0.3.0/aframe.min.js"></script>
<script src="//cdn.rawgit.com/donmccurdy/aframe-extras/v2.3.0/dist/aframe-extras.min.js"></script>
<a-scene physics>
<!-- Camera -->
<a-entity id="cameraWrapper" geometry="box" dynamic-body="mass:1" position="0 1 0" width="2" height="1" depth="2" listener>
<a-entity id="camera" camera universal-controls position="0 1 0" rotation="0 0 0">
</a-entity>
</a-entity>
<a-grid static-body position="0 0 0"></a-grid>
</a-scene>
<div>
<a id="impulseButton">Move</a>
</div>
The javascript method that is supposed to move the camera looks like this:
$(document).ready(function(){
$("#impulseButton").on("click",function(){
applyImpulse();
});
function applyImpulse(){
var x = 0;
var y = 0;
var z = 1;
var el = $('#cameraWrapper')[0];
el.body.applyImpulse(
new CANNON.Vec3(x,y,z),
new CANNON.Vec3().copy(el.body.position)
);
}
});
However, the movement seems not very smooth, and when the users uses WASD controls, the cameraWrapper entity remains at the old location. How can I move the camera with applyImpulse smoothly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…