topical media & game development
mobile-query-three-plugins-cannonjs-vendor-cannon.js-demos-callbacks.htm / htm
<DOCTYPE html>
<html>
<head>
<title>cannon.js - callbacks demo</title>
<meta charset="utf-8">
<style>* {margin:0;padding:0}</style>
</head>
<body>
<script src="../build/cannon.js"></script>
<script src="../build/cannon.demo.js"></script>
<script src="../libs/dat.gui.js"></script>
<script src="../libs/Three.js"></script>
<script src="../libs/Detector.js"></script>
<script src="../libs/Stats.js"></script>
<script>
Callback related demos
var demo = new CANNON.Demo();
// Using the preStep callback to add a force
demo.addScene("Moon",function(){
var world = demo.getWorld();
var mass = 5;
var moonShape = new CANNON.Sphere(0.5);
var planetShape = new CANNON.Sphere(3.5);
var moon = new CANNON.RigidBody(mass,moonShape);
var planet = new CANNON.RigidBody(0,planetShape);
moon.position.set(5,0,0);
moon.velocity.set(0,0,8);
moon.linearDamping = 0.0;
// Use the preStep callback to apply the gravity force on the moon.
// This callback is evoked each timestep
moon.preStep = function(){
// Get the vector pointing from the moon to the planet center
var moon_to_planet = new CANNON.Vec3();
this.position.negate(moon_to_planet);
// Get distance from planet to moon
var distance = moon_to_planet.norm();
// Now apply force on moon
// Fore is pointing in the moon-planet direction
moon_to_planet.normalize();
moon_to_planet.mult(1500/Math.pow(distance,2),this.force);
}
world.add(moon);
world.add(planet);
demo.addVisual(moon);
demo.addVisual(planet);
});
demo.start();
</script>
</body>
</html>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.