2012-10-21 15 views
7

Ich möchte erkennen, wenn man (Ball) ein anderes Objekt (Ziel) berührt und ich möchte den Impuls dieses Kontakts wissen.BulletPhysic: Kontakte Kraft/Impuls

Ich weiß, drei Möglichkeiten, Kontakte zu erkennen

gContactAddedCallback 

oder

int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds(); 
    for (int i=0;i<numManifolds;i++) 
    { 
     btRigidBody* obA = static_cast<btRigidBody*>(contactManifold->getBody0()); 
     btRigidBody* obB = static_cast<btRigidBody*>(contactManifold->getBody1()); 
     // May be there is contact obA and obB 

     btPersistentManifold* contactManifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i); 
     int numContacts = contactManifold->getNumContacts(); 
     for (int j=0;j<numContacts;j++) 
     { 
      btManifoldPoint& pt = contactManifold->getContactPoint(j); 
      if (pt.getDistance()<0.f) 
      { 
       // One contact point is inside of another object 
       // But some contacts are ignored 
      } 
     } 
    } 

oder

Überprüfen Sie die Längen- und Winkelgeschwindigkeitsänderung. (Nicht klar, ob gab es Kontakt und welche Aufgabe gemacht, die Geschwindigkeit zu ändern, war es das Objekt oder Dämpfung, die Schwerkraft oder ein Kraftfeld.


Ich wünsche Kontaktinformationen zu haben, um Kontakte Impuls zu umfassen. ich, dass einige bemerkt Kontakt aufgelöst in 1 Rahmen Simulation andere nehmen 2 Frames und Impuls ist zweimal niedriger. (Ich habe es Debugging-Code.) Ich wäre perfekt, 1 Kontakt Benachrichtigung mit Gesamtimpuls.

Keine der Methoden, die ich aufgeführt geben mir vollständige Information für den Kontakt Irgendwann wird es ausgelöst, wenn der Ball in der Nähe des Ziels fliegt und es sogar nicht berührt

Was ist ein vorweggenommener Weg?

Diese Informationen könnten verwendet werden, um Trittschall zu erzeugen oder eine Animation zu starten, wenn die Energie der Kontakte hoch ist.

+1

pt.getAppliedImpulse() – rraallvv

+0

pt.getAppliedImpulse() Punkt - ich dieses verpasst! füge die Antwort hinzu und ich werde sie als solche markieren. Danke – Max

Antwort

3

Dieser Code sollten Sie in auf posible Richtung

// some global constants needed 

enum collisiontypes { 
    NOTHING   = 0, // things that don't collide 
    BALL_BODY  = 1<<2, // is ball 
    TARGET_BODY  = 1<<3 // is target 
}; 

int ballBodyCollidesWith = TARGET_BODY | BALL_BODY; // balls collide with targets and other balls 
int targetBodyCollidesWith = BALL_BODY; // targets collide with balls 

// ... 
// bodies creation 

dynamicsWorld->addRigidBody(ballBody, BALL_BODY, ballBodyCollidesWith); 

dynamicsWorld->addRigidBody(targetBody, TARGET_BODY, targetBodyCollidesWith); 

//... 
// find out whether a ball collides with a target 

int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds(); 
for (int i=0;i<numManifolds;i++) 
{ 
    btRigidBody* obA = static_cast<btRigidBody*>(contactManifold->getBody0()); 
    btRigidBody* obB = static_cast<btRigidBody*>(contactManifold->getBody1()); 
    // May be there is contact obA and obB 

    // ignore bodies that are not balls or targets 
    if (
     (!(obA->getCollisionFlags() | BALL_TYPE) && !(obB->getCollisionFlags() | BALL_TYPE)) // there is no BALL_TYPE colliding 
     || 
     (!(obA->getCollisionFlags() | TARGET_TYPE) && !(obB->getCollisionFlags() | TARGET_TYPE)) // there is no TARGET_TYPE colliding 
     ) 
     continue; // no more searching needed 

    btPersistentManifold* contactManifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i); 
    int numContacts = contactManifold->getNumContacts(); 
    for (int j=0;j<numContacts;j++) 
    { 
     btManifoldPoint& pt = contactManifold->getContactPoint(j); 

     printf("%f\n", pt.getAppliedImpulse()); // log to see the variation range of getAppliedImpulse and to chose the appropriate impulseThreshold 
     if (pt.getAppliedImpulse() > impulseThreshold) 
     { 
       // increase score or something 
       break; // no more searching needed 
     } 
    } 
} 
+2

Es tut mir leid. Ich kann es nicht als beantwortet markieren. Ich gebe nur +1. Ich habe pt.getAppliedImpulse() getestet und es funktioniert nicht. Es ist oft 0 Gesamtimpuls, auch wenn ich sehe, dass es in der Simulation passiert ist und meine Geschwindigkeiten sich signifikant ändern. oder numContacts ist Null. – Max