0
$\begingroup$

For a project I need to make a good simulation of balls moving around in a space. The project will be without any air drag and without losing energy in bounces. Since there is no way to lose energy the energy should be the same before and after a bounce. However, this is not the case.

The formula I'm using to calculate the new speed after a bounce are the bottom 4 on this page: http://williamecraver.wix.com/elastic-equations

The problem right now is that the balls lose quite a bit of speed whenever they bounce. However there is some similarity. Every single time they do bounce the energy down in a half parabola looking like this: http://imgur.com/Rlj3yni (the white line on the right is the total energy). I was wondering what I did wrong and I think it may be the contact angle. How would you calculate the right contact angle? And if I'm not doing that wrong, what is wrong then?

The code I'm using:

double dbotsx = Math.abs(b2.x - x);
double dbotsy = Math.abs(b2.y - y);

double difdir = Math.atan2(dbotsy, dbotsx);
//difdir = Math.toDegrees(difdir);

double dir1 = Math.atan2(dy, dx);
//dir1 = Math.toDegrees(dir1);
double dir2 = Math.atan2(b2.dy, b2.dx);
//dir2 = Math.toDegrees(dir2);

double v1 = Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2));
double v2 = Math.sqrt(Math.pow(b2.vx, 2) + Math.pow(b2.vy, 2));

double m2 = b2.m;

double vxe1 = ((((v1 * Math.cos(dir1 - difdir)) * (m - m2) + 2 * m2 * v2 * Math.cos(dir2 - difdir))) / (m + m2)) * Math.cos(difdir) + (v1 * Math.sin(dir1 - difdir) * Math.cos(difdir + (Math.PI / 2)));
double vye1 = ((((v1 * Math.cos(dir1 - difdir)) * (m - m2) + 2 * m2 * v2 * Math.cos(dir2 - difdir))) / (m + m2)) * Math.sin(difdir) + (v1 * Math.sin(dir1 - difdir) * Math.sin(difdir + (Math.PI / 2)));

double vxe2 = ((((v2 * Math.cos(dir2 - difdir)) * (m2 - m) + 2 * m * v1 * Math.cos(dir1 - difdir))) / (m + m2)) * Math.cos(difdir) + (v2 * Math.sin(dir2 - difdir) * Math.cos(difdir + (Math.PI / 2)));
double vye2 = ((((v2 * Math.cos(dir2 - difdir)) * (m2 - m) + 2 * m * v1 * Math.cos(dir1 - difdir))) / (m + m2)) * Math.sin(difdir) + (v2 * Math.sin(dir2 - difdir) * Math.sin(difdir + (Math.PI / 2)));
$\endgroup$
  • $\begingroup$ I personally wouldn't rely on a physics site written by someone who evidently neither knows vector mathematics nor the basic trigonometric identities. (Though at a glance it looks like a legit hideous formula.) It is hard to tell from your image exactly what it is that goes wrong, I gather that your animation would work a fair bit better when it is actually animated. $\endgroup$ – aaaaaaaaaaaa Nov 28 '13 at 20:03
  • $\begingroup$ Here is a writeup of of the vector maths equivalent to a similar piece of code: gamedev.stackexchange.com/questions/20516/… $\endgroup$ – aaaaaaaaaaaa Nov 28 '13 at 20:23
  • $\begingroup$ What integrator are you using right now for the simulation? I think this is where you are gaining energy, if you are using an explicit Euler integrator. Look into simplectic integration of 2nd order or above to get correct results. $\endgroup$ – ja72 Nov 28 '13 at 23:44
  • $\begingroup$ Thank you very much eBusiness, the code is much faster and better now. However, it's still losing energy somewhere... $\endgroup$ – Wuppy29 Nov 30 '13 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.