-
Senior Member
Sticky Balls
I've got the following code, which should work out 2 new vectors after the collision of 2 circles
ball1x = ball1._x;
ball1y = ball1._y;
ball2x = ball2._x;
ball2y = ball2._y;
distx = ball1x-ball2x;
disty = ball1y-ball2y;
hy = Math.sqrt((distx*distx)+(disty*disty));
if (hy<radius*2+4) {
normalX = distx/hy;
normalY = disty/hy;
Vector1 = ball1.xmove*normalX+ball1.ymove*normalY;
Vector2 = ball2.xmove*normalX+ball2.ymove*normalY;
Vector3 = Vector1-Vector2;
dv0 = -0.9*Vector3;
dv1 = 0.9*Vector3;
ball1.xmove += normalX*dv0;
ball1.ymove += normalY*dv0;
ball2.xmove += normalX*dv1;
ball2.ymove += normalY*dv1;
}
but for some reason, the above code works sometimes and the 2 circles bounce away more or less corectly, but other times, the faster moving circle will become "stuck" with the other circle, move around the edge of the circle and then move away from it
are the new vectors not being correctly calculated with the above code, and is that creating this problem?
thanks for any help
phil.
-
After you check to see if the distance between two balls is less than twice their radii, try repositioning both balls so that they are EXACTLY that distance apart, then apply the change in direction and speed. Its just that some direction and velocity changes won't move the balls APART, so the next time you check they are still "touching"
Hope it works
http://194.131.128.207/flash/circlebounce/Movie1.swf
swills
-
that's nice code. simpler than i have been using for the same purpose. to elaborate on the sticky problem, upon collision, the balls are overlapping. you then adjust their velocities so they will bounce away from each other, subtracting some speed (the .9). this in itself may not be enough to separate them though. so the next frame, they move away from each other, but are still touching. you'll go through the function again, which will wind up moving them back towards each other, and they'll get stuck in that loop.
as mentioned above, one solution is to move them so they are exactly touching each other's edges. i've seen other methods, but this is the best looking, even if not fully accurate mathematically.
-
actually, the more i look at it, i think it is too simple. when you subtract vector2 from vector1, and then use the result as the speed of both balls after collision, it results in both balls having the same speed after collision, just in opposite directions. say vector1 was 10, and vector2 was 0. vector3 is then 10 and dv0 is -10, dv1 is 10. not right. now i see that my more complex method is still necessary. too bad.
-
Senior Member
I changed the code, and took out a lot of lines and now it works fine. I know its not accurate, but for what I wanted it does the job. thanks for your help.
ball1x = ball1._x;
ball1y = ball1._y;
ball2x = ball2._x;
ball2y = ball2._y;
distx = ball1x-ball2x;
disty = ball1y-ball2y;
hy = Math.sqrt((distx*distx)+(disty*disty));
if (hy<radius*2) {
normalX = distx/hy;
normalY = disty/hy;
dv1 = -8;
ball1.xmove = normalX;
ball1.ymove = normalY;
ball2.xmove = normalX*dv1;
ball2.ymove = normalY*dv1;
-
Senior Member
be nice though, if someone could post the link/code for some ball collision code, that solves the problems discussed here
-
Senior Member
using that code though, what would be a simple way to move ball1, outide of the area of ball 2?, just reset to the old position?
-
Hi, bit-101
I was surprised at that codes too. Too simple to believe. (Shame on me ! I used globalToLocal function to work-around the math.);
I tested it by drawing several triangles. I can not find any fault. I kind of agree with that first codes.
----------------
Hi Boombanguk,
In my experience, I have got sticky ball in two occasions.
1. Friction is incorporated in the bounce codes. After bouncing, both balls get their speed decreased and unable to go untouched.
2. The _x rounding side effect. Make a mc on the stage. mc._x-=1.03; The result is that _x decreased by 1.05; Next, we test mc._x+=1.03; The result is _x increased by 1.00; (Why not 1.03, weird ? That will make balls unable to go untouched.
I wrote an article in my web site discussing this:http://ericlin2.tripod.com/excursion/excursiont.html
I will use x instead of _x, y instead of _y to do calculation and check collision. I am not sure it will 100% avoid the sticky ball but it will improve much.
-
just gave that first code yet another look. seems to work out, oddly enough. i gotta work it over a bit to be sure. seems like it's omitting several factors...
-
ok, finally worked it out. that first code is awesome. after about 3 pages of sketches, i realized what it is doing, and it is quite sweet. i will be using that. far simpler than what i have used.
as far as moving them to touch each other, what i have done is figure out the mid point between the two objects as they collide. then, move each one back along the line of collision exactly one radius from that mid point.
midpointX=(ball1._x+ball2._x)/2;
midpointY=(ball1._y+ball2._y)/2;
your normalx and normaly are cos and sin of the angle of collision. so,
ball1._x=midpointX + normalX*radius;
ball1._y=midpointY + normalY*radius;
ball2._x=midpointX - normalX*radius;
ball2._y=midpointY - normalY*radius;
i may have the +'s and -'s backwards. i always get confused.
this will put each ball one radius away frome this midpoint, so they will be exactly touching. again, not 100% accurate, but good enough.
thanks for the code in the first post. it is perfect. just add the above to it and it should be fine.
-
Mental Deficit
k im lost in this code.
I made 2 ball mcs.
called them ball1 and ball2
and i pasted this code in it
code:
ball1x = ball1._x;
ball1y = ball1._y;
ball2x = ball2._x;
ball2y = ball2._y;
distx = ball1x-ball2x;
disty = ball1y-ball2y;
hy = Math.sqrt((distx*distx)+(disty*disty));
if (hy<radius*2+4) {
normalX = distx/hy;
normalY = disty/hy;
Vector1 = ball1.xmove*normalX+ball1.ymove*normalY;
Vector2 = ball2.xmove*normalX+ball2.ymove*normalY;
Vector3 = Vector1-Vector2;
dv0 = -0.9*Vector3;
dv1 = 0.9*Vector3;
ball1.xmove += normalX*dv0;
ball1.ymove += normalY*dv0;
ball2.xmove += normalX*dv1;
ball2.ymove += normalY*dv1;
midpointX=(ball1._x+ball2._x)/2;
midpointY=(ball1._y+ball2._y)/2;
ball1._x=midpointX + normalX*radius;
ball1._y=midpointY + normalY*radius;
ball2._x=midpointX - normalX*radius;
ball2._y=midpointY - normalY*radius;
}
I can only postulate the probability of performing at a paramount level of perfection praised by the pulchritudinous paragon whose only practice is to preserve such a paradigm.
-
15 days passed and I try to figure out the mechanism of the "sticky" things today. When will sticky balls happens ? Why the sticky balls always "rololing" ? Now I got it. When we detect overlapping, there are two conditions. One is in the safe collision region, the other is the sticky collision region. I will explain later after I figure out the way to "demonstrate".
Anyway, for the codes post by Boombanguk, to avoid sticky balls just add a line:
ball1x = ball1._x;
ball1y = ball1._y;
ball2x = ball2._x;
ball2y = ball2._y;
distx = ball1x-ball2x;
disty = ball1y-ball2y;
hy = Math.sqrt((distx*distx)+(disty*disty));
if (hy<radius*2+4) {
normalX = distx/hy;
normalY = disty/hy;
Vector1 = ball1.xmove*normalX+ball1.ymove*normalY;
Vector2 = ball2.xmove*normalX+ball2.ymove*normalY;
Vector3 = Vector1-Vector2;
if(Vector3>=0)return;
dv0 = -0.9*Vector3;
dv1 = 0.9*Vector3;
ball1.xmove += normalX*dv0;
ball1.ymove += normalY*dv0;
ball2.xmove += normalX*dv1;
ball2.ymove += normalY*dv1;
}
-
Untitled-1.fla
Another solution would be to always check if a circle will collide with another circle if it's moved along a speed vector. If it does collide, then the largest speed vector is calculated that doesn't result in a collision (or rather a scalar between 0 and 1 to multiply the speed vector with... can be done with some linear algebra). Then the circle is moved to the new position using the speed vector multiplied with the scalar. Last, but not least, a new direction of the speed vector is calculated for the two circles.
That way you not only avoid sticky balls, but you will never move a ball/circle to a position where it overlaps another circle. Worked great when I tried it.
-
strille, mind sharing that fla? ive been trying to fix this exact problem
-
Senior Member
hmm...
i got those two balls collide... but if their speed is too high they just go through each others....
swills movie (balls inside circle) is what I'm looking for.... how He do that?
I'm doing old Amiga-game Projectyle in my school project, and I'm quite good in maths, but I don't get how to work with colliding balls
-
-
Senior Member
I'm trying to use these formulas what have been used in this thread, but some how my players move too fast so these formulas are too inaccurate for my game...
I've been studied physics, but maybe not too hard I guess. Some how I should try to use different formulas...
I know It has something to do with masses, velocity, direction and collision angle, but I can't figure out the formula..
here's my game
Projectyle remake
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|