[SOLVED] bouncing circular objects: loss of velocity

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KaoS
Prole
Posts: 11
Joined: Sat Mar 02, 2013 2:06 am

[SOLVED] bouncing circular objects: loss of velocity

Post 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
Last edited by KaoS on Tue Jul 02, 2013 12:28 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: bouncing circular objects resulting in loss of velocity

Post 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.
KaoS
Prole
Posts: 11
Joined: Sat Mar 02, 2013 2:06 am

Re: bouncing circular objects resulting in loss of velocity

Post 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
KaoS
Prole
Posts: 11
Joined: Sat Mar 02, 2013 2:06 am

Re: bouncing circular objects resulting in loss of velocity

Post 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 :(
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: bouncing circular objects resulting in loss of velocity

Post 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.
KaoS
Prole
Posts: 11
Joined: Sat Mar 02, 2013 2:06 am

Re: bouncing circular objects resulting in loss of velocity

Post by KaoS »

I thank you for your amazing assistance kind sirs! Now I just need to stop the minuscule left pull on the collisions
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests