Page 1 of 1
[SOLVED] bouncing circular objects: loss of velocity
Posted: Sat Jun 29, 2013 7:46 am
by KaoS
Hi pros,
I am still a beginner to this whole system and have been using it primarily to make small cinematic type deals with objects attracting eachother etc. I have succeeded in bouncing objects off one another now but for some reason they sometimes just stick to the surface of the object they should be bouncing off. It must be a problem in my maths somewhere but I can't seem to find it
the .love file can be downloaded
here and the code can be viewed
here
I understand that reading through maths isn't the most entertaining thing in the world but I would greatly appreciate it if you would go through the trouble
Re: bouncing circular objects resulting in loss of velocity
Posted: Sat Jun 29, 2013 10:37 am
by micha
Hi,
i have looked into your code and there is one thing I don't understand (which does not mean that this is the part that causes the error): What exactly is the quantity "vAng" returned by obj:compare? Maybe, if I understand that, I can help you.
Besides that, my personal taste is to avoid using angles if possible and to express all vectors in cartesian coordinates. My approach to circle-circle collision would be this:
If two circles collide, then calculate the vector from one midpoint to the other. Decompose the circles velocities into components parallel to this vector and perpendicular to this vector. Swap the velocities parallel part of the two circles (assuming the same mass for both). All can be done without calculating angles.
Re: bouncing circular objects resulting in loss of velocity
Posted: Sun Jun 30, 2013 9:50 am
by KaoS
vAng is the angle the the current body is travelling at relative to the body it is bouncing off. If vAng is within 90 degrees of ang then it should bounce. I then reflect vAng's angle around that of ang and rotate it by 180 to get the new angle of velocity. I must admit your method is nice and simple. It may require a little more computation but it is easier to use
Re: bouncing circular objects resulting in loss of velocity
Posted: Sun Jun 30, 2013 11:00 am
by KaoS
I followed your advice and used the system you suggested to no avail.
Here is your version but it has the same issue
EDIT: just to be clear though I didn't swop their velocities. I calculated the total velocity directly towards eachother, inverted and averaged it out for now to see what would happen. Either way you always get velocity but sometimes it just vanishes
![Sad :(](./images/smilies/ms-sad.png)
Re: bouncing circular objects resulting in loss of velocity
Posted: Sun Jun 30, 2013 12:39 pm
by micha
I found the problem.
Replace this line:
Code: Select all
elseif math.abs(vAng-ang)<=math.pi/2 then
by this line
Code: Select all
elseif math.abs((vAng-ang+math.pi)%(2*math.pi)-math.pi)<=math.pi/2 then
Explanation: When you compare two angles and one of them is slighly smaller than 2*pi and the other one is slightly bigger than 0, then your comparison will not recognize that the difference between the two angles is actually very small. To correct this, you need to wrap the difference around.
This solution works on both of your implementations.
Re: bouncing circular objects resulting in loss of velocity
Posted: Tue Jul 02, 2013 9:40 am
by KaoS
I thank you for your amazing assistance kind sirs! Now I just need to stop the minuscule left pull on the collisions