#### Archived

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

# bouncing vectors

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

## 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 on other sites
Can it be that the formula optimisedP is not 100% accurate when it comes to small numbers like this?

zilch_

##### 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 on other sites
well you could test if it''s an accuracy problem by changing it to floats and see if it gets much worse