15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CANNON.jsについてのメモ

Last updated at Posted at 2013-08-28

CANNON.jsとは?

CANNON.jsは、JavaScript製の軽量な物理エンジンです。
Three.jsにインスパイアされていて、Three.jsの構文に非常に似ています。
物理演算結果は、以下のように簡単にThree.jsのオブジェクトにコピーすることができるようになっています。

cannonObj.position.copy(threeObj.position);
cannonObj.quaternion.copy(threeObj.quaternion);

Events

collide event

なにかの物体にぶつかった際に発生するイベント

cannonObj.addEventListener("collide", function(e) {
	alert("The object just collided with the ground!");
	console.log("Collided with body:", e.with);
	console.log("Contact between bodies:", e.contact);
});

sleepy event

オブジェクトの運動が止まった際に発生するイベント

cannonObj.addEventListener("sleepy",function(event){
	console.log("The object is sleepy!");
});

sleep event

オブジェクトの運動が停止し、規定の時間過ぎた場合に発生するイベント

cannonObj.addEventListener("sleep",function(event){
	console.log("The object sleep!");
});

wakeup event

オブジェクトのsleepが溶けたときに発生するイベント?

cannonObj.addEventListener("wakeup",function(event){
	console.log("The sphere woke up!");
});

ちなみに、これらの判定を行うには以下の設定を合わせて行う必要がありそう。

world.allowSleep = true;
cannonObj.allowSleep = true;
cannonObj.sleepSpeedLimit = 0.1; //velocityの強さ(norm)
cannonObj.sleepTimeLimit = 1; //停止1秒後にsleepイベントが発火するように

wakeUpメソッド

sleepを有効にすると、外部から撃力を与えたりしてもsleep状態が保持されるので、wakeUp()メソッドを叩いてsleepを解除する必要があることに注意。

15
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?