私はbox2djsで遊んでいます。私は 'onCollision()'コールバック関数を書く方法を見つけようとしていますが、ドキュメンテーションは疎であり、それを実行する明白な方法を見つけることができません。box2djsの衝突イベントハンドラについて
ありがとうございます!
私はbox2djsで遊んでいます。私は 'onCollision()'コールバック関数を書く方法を見つけようとしていますが、ドキュメンテーションは疎であり、それを実行する明白な方法を見つけることができません。box2djsの衝突イベントハンドラについて
ありがとうございます!
box2djsのコールバックは実際には衝突「フィルタ」として参照されます。これを実装する方法は次のとおりです。真のコールバックのように素敵ではない
// Called whenever a collision occurs in the world
//
var JellyCollisionCallback = function()
{
// Required function - this is the function the gets called when b2ContactManager registers a collision between two bodies
//
this.ShouldCollide = function(shape1, shape2)
{
// These are the two bodies…
//
var cBody1 = shape1.m_body;
var cBody2 = shape2.m_body;
// I'm setting userData when I create the body object
//
var jellyObject1 = cBody1.GetUserData();
var jellyObject2 = cBody2.GetUserData();
// This is the code from the default collision filter
//
if (shape1.m_groupIndex == shape2.m_groupIndex && shape1.m_groupIndex != 0)
{
return shape1.m_groupIndex > 0;
}
var collide = (shape1.m_maskBits & shape2.m_categoryBits) != 0 && (shape1.m_categoryBits & shape2.m_maskBits) != 0;
return collide;
}
return this;
}
function createWorld()
{
var world = new b2World(worldAABB, gravity, doSleep);
myCollisionCallback = new JellyCollisionCallback();
world.SetFilter(myCollisionCallback);
}
おそらく:私はまた、私は多分少し遅くなる代わりにやっているが、私の他のアプローチは、ステップの外にあるので、()私は、オブジェクトやものを破壊することができるものを紹介します私はもともとコールバックを取得しようとするのに問題があったので、私はこのアプローチを書いて、代わりにそれを保持した。私はいくつかの簡単な趣味を達成するために必要world.step()
// Find collisions between selected objects
//
// (world is the main b2World object)
//
var aContact;
for (aContact = world.m_contactList; aContact != null; aContact = aContact.m_next)
{
var cBody1 = aContact.m_shape1.m_body;
var cBody2 = aContact.m_shape2.m_body;
// I'm setting userData when I create the body object
//
var jellyObject1 = cBody1.GetUserData();
var jellyObject2 = cBody2.GetUserData();
// Not one of my controlled Objects
//
if (typeof(jellyObject1) != "object" || jellyObject1 == null)
continue;
if (typeof(jellyObject2) != "object" || jellyObject2 == null)
continue;
// Call my collision event for the colliding objects
//
jellyObject1.dink();
jellyObject2.dink();
}
ドキュメントは、私が見つけたものからほとんど利用できないを呼び出して、私のメインループでこれを行うが、私は本当にbox2djsのように、最終的にすべてのものを考え出しましたプロジェクト。オリジナルのbox2djsデモを拡張するいくつかの例がありますjellyrobotics box2djs project
この質問と私の答えをご覧ください。 http://stackoverflow.com/questions/8951392/collision-detection-using-box2dfor-android – Andrew