2014-11-18 1 views
0

(소스로 this 사용) :2D 탄성 충돌 - '고정'동작? 나는 탄성 충돌을 처리하기 위해이 방법을 사용하고 있습니다

public static void handleElasticCollisions(Entity a, Entity b) { 
    // make a few variables to make equations easier to read 
    Vector2f aPos = a.getPos(); 
    Vector2f bPos = b.getPos(); 

    Vector2f aV = a.getVelocity(); 
    Vector2f bV = b.getVelocity(); 

    float aMass = a.getSize(); 
    float bMass = b.getSize(); 

    // find unit normal vector 
    Vector2f uN = normalize(Vector2f.sub(bPos, aPos)); 

    // find unit tanget vector 
    Vector2f uT = new Vector2f(-uN.y, uN.x); 

    // get normal and tangential components of both velocity vectors 
    // before the collision 
    float aVn = dot(uN, aV); 
    float aVt = dot(uT, aV); 

    float bVn = dot(uN, bV); 
    float bVt = dot(uT, bV); 

    // get normal and tangential components of both velocity vectors 
    // after the collision 
    float aVN = (aVn*(aMass - bMass) + 2*bMass*bVn)/(aMass + bMass); 
    float aVT = aVt; 

    float bVN = (bVn*(bMass - aMass) + 2*aMass*aVn)/(aMass + bMass); 
    float bVT = bVt; 

    // convert normal and tangential components into vectors 
    Vector2f avn = Vector2f.mul(uN, aVN); 
    Vector2f avt = Vector2f.mul(uT, aVT); 

    Vector2f bvn = Vector2f.mul(uN, bVN); 
    Vector2f bvt = Vector2f.mul(uT, bVT); 

    a.setVelocity(Vector2f.add(avn, avt)); 
    b.setVelocity(Vector2f.add(bvn, bvt)); 
} 

그러나, 때때로, 개체가 서로 안으로 들어가/대신 서로 떨어져 수신 거부의 서로에 충실. 여기에만 관련 코드, 내 업데이트 방법입니다 :

public boolean intersectsWithAsteroid() { 
    for (Entity e : GameScreen.getEntities()) { 
     if (e instanceof AsteroidEntity && 
       asteroidSprite.getGlobalBounds().intersection(e.getBounds()) != null && 
       !this.equals(e)) { 
      collidingAsteroid = (AsteroidEntity)e; 
      return true; 
     } 
    } 
    return false; 
} 

public void handleAsteroidIntersection() { 
    CMath.handleElasticCollisions(this, collidingAsteroid); 
} 

Hereintersection에 대한 문서입니다 :

public void show() { 

    // set delta time 
    float currentTime = clock.getElapsedTime().asSeconds(); 
    float dt = currentTime - lastTime; 


    // update entities and check for removal 
    for (Entity e : entities) { 
     e.update(dt); 
    } 

    // draw entities 
    for (Entity e : entities) { 
     e.draw(); 
    } 


    Window.getWindow().draw(pointerSprite); 
    Window.getWindow().display(); 

    lastTime = currentTime; 

} 

그리고 내 충돌 기관의 update

, 나는이 확인.

왜 이런 일이 발생하는지 이해할 수 없지만 누군가이 알고리즘을 잘못 구현하는 방법을 말해 줄 수 있습니까?

+1

코드 스 니펫을 기반으로 한 대답을 여기에서 파악하는 것은 대단히 어렵습니다. http://stackoverflow.com/help/mcve가 도움이 될지 모르지만 "디버깅"을 위해 시간을 투자해야 할 수도 있습니다 - 특히 "때때로 *"가 붙어 있다고 말하면 힘들 것 같습니다 이 상황이 발생했을 때의 정확한 조건을 지정하려면 ... – Marco13

답변

0

그래서, 나는 마침내 내 반응 함수에 코드의이 3 줄을 추가하여, 그것을 고정 :

Vector2f rv = Vector2f.sub(bV, aV); 
    Vector2f norm = Vector2f.sub(bPos, aPos); 
    float velAlongNormal = CMath.dot(rv, norm); 

    // Do not resolve if velocities are separating 
    if(velAlongNormal > 0) { 
     return; 
    } 

물체의 속도가 직접 충돌하지 않은 경우, 충돌 응답이 안 . 이것은 모든 문제를 해결합니다.