Archived

This topic is now archived and is closed to further replies.

bouncing vectors

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i found an artickle on gamasutra: http://www.gamasutra.com/features/20020118/vandenhuevel_01.htm which has a nice algirithm in the end calculating the resulting vectors after a collision. I implemented this and it looks great. However when i looked at it more carefully (by mistake?) i realised that the sum of the lengths of the vectors before the collision was not the same as the sum of the length of the vectors after, even when the masses of the objects are the same - in most cases. When one object stood still and another collided with this object STRAIGHT (0 degrees) it worked. Can anyone explain what does this? is it my code?
  

        double totalLength1 = m_velocity.length() + enemy.m_velocity.length();
    
        Vector n;
        n.set(m_x - enemy.m_x, m_y - enemy.m_y);
        n.normalize();
        
        double a1 = m_velocity.dot(n);
        double a2 = enemy.m_velocity.dot(n);
        
        double optimizedP = (2.0 * (a1 - a2)) / (m_mass + enemy.m_mass);

        Vector v1_ = m_velocity - optimizedP * enemy.m_mass * n;

        Vector v2_ = enemy.m_velocity + optimizedP * m_mass * n;
        
        m_velocity = v1_;
        enemy.m_velocity = v2_;    

        double totalLength2 = m_velocity.length() + enemy.m_velocity.length();

        printf("%f, %f\n", totalLength1, totalLength2);

  
thanks for any help zilch_

Share this post


Link to post
Share on other sites
Forgot to mention that it differs by about 1% ... and i use very small vectors... but still; I use doubles... and small numbers shouldn''t be a problem

well well

sorry if i "spam"

zilch_

Share this post


Link to post
Share on other sites