Pinball multiball collision
Hi, I''m making a Pinball game and I can''t figure out how to calculate this:
Say 2 balls (pinballs that is...) collides and "bounce" from each other, how do I calculate which angle and what speed they have after the collision.
Really need help with this one...
I used this same code for another post, but I never got a post and the topic died after that, so here it is again. I did something just like that, so i am using my same code. Try this:
Hope that helps. Let me know if it does. Code comments were from something else (I forget what), but they seemed applicable, so I added them in. I modeled the code after another code like it, but a wee bit more optimized. There ''ya go!
=======
~Khaos~
=======
for (int ball_a = 0; ball_a < NUM_BALLS; ball_a++){for (int ball_b = ball_a+1; ball_b < NUM_BALLS; ball_b++){if (ball_a == ball_b) continue;// compute the normal vector from a->bfloat nabx = (balls[ball_b].varsF[INDEX_X] - balls[ball_a].varsF[INDEX_X] );float naby = (balls[ball_b].varsF[INDEX_Y] - balls[ball_a].varsF[INDEX_Y] );float length = sqrt(nabx*nabx + naby*naby);// is there a collision?if (length <= 2.0*(BALL_RADIUS*.75)){// the balls have made contact, compute response// compute the response coordinate system axes// normalize normal vectornabx/=length;naby/=length;// compute the tangential vector perpendicular to normal, simply rotate vector 90float tabx = -naby;float taby = nabx;// draw collisionDDraw_Lock_Primary_Surface();// blue is normalDraw_Clip_Line(balls[ball_a].varsF[INDEX_X]+0.5, balls[ball_a].varsF[INDEX_Y]+0.5,balls[ball_a].varsF[INDEX_X]+20*nabx+0.5,balls[ball_a].varsF[INDEX_Y]+20*naby+0.5,252, primary_buffer, primary_lpitch); // yellow is tangentialDraw_Clip_Line(balls[ball_a].varsF[INDEX_X]+0.5, balls[ball_a].varsF[INDEX_Y]+0.5,balls[ball_a].varsF[INDEX_X]+20*tabx+0.5,balls[ball_a].varsF[INDEX_Y]+20*taby+0.5,251, primary_buffer, primary_lpitch); DDraw_Unlock_Primary_Surface();// tangential is also normalized since it''s just a rotated normal vector// step 2: compute all the initial velocities// notation ball: (a,b) initial: i, final: f, n: normal direction, t: tangential directionfloat vait = DOT_PRODUCT(balls[ball_a].varsF[INDEX_XV], balls[ball_a].varsF[INDEX_YV], tabx, taby);float vain = DOT_PRODUCT(balls[ball_a].varsF[INDEX_XV], balls[ball_a].varsF[INDEX_YV], nabx, naby);float vbit = DOT_PRODUCT(balls[ball_b].varsF[INDEX_XV], balls[ball_b].varsF[INDEX_YV], tabx, taby);float vbin = DOT_PRODUCT(balls[ball_b].varsF[INDEX_XV], balls[ball_b].varsF[INDEX_YV], nabx, naby);// now we have all the initial velocities in terms of the n and t axes// step 3: compute final velocities after collision, from book we have// note: all this code can be optimized, but I want you to see what''s happening float ma = balls[ball_a].varsF[INDEX_MASS];float mb = balls[ball_b].varsF[INDEX_MASS];float vafn = (mb*vbin*(cof_E+1) + vain*(ma - cof_E*mb)) / (ma + mb);float vbfn = (ma*vain*(cof_E+1) - vbin*(ma - cof_E*mb)) / (ma + mb);// now luckily the tangential components are the same before and after, sofloat vaft = vait;float vbft = vbit;// and that''s that baby!// the velocity vectors are:// object a (vafn, vaft)// object b (vbfn, vbft) // the only problem is that we are in the wrong coordinate system! we need to // translate back to the original x,y coordinate system, basically we need to // compute the sum of the x components relative to the n,t axes and the sum of// the y components relative to the n,t axis, since n,t may both have x,y// components in the original x,y coordinate systemfloat xfa = vafn*nabx + vaft*tabx;float yfa = vafn*naby + vaft*taby;float xfb = vbfn*nabx + vbft*tabx;float yfb = vbfn*naby + vbft*taby;// store resultsballs[ball_a].varsF[INDEX_XV] = xfa;balls[ball_a].varsF[INDEX_YV] = yfa;balls[ball_b].varsF[INDEX_XV] = xfb;balls[ball_b].varsF[INDEX_YV] = yfb;// update positionballs[ball_a].varsF[INDEX_X]+=balls[ball_a].varsF[INDEX_XV];balls[ball_a].varsF[INDEX_Y]+=balls[ball_a].varsF[INDEX_YV];balls[ball_b].varsF[INDEX_X]+=balls[ball_b].varsF[INDEX_XV];balls[ball_b].varsF[INDEX_Y]+=balls[ball_b].varsF[INDEX_YV];} // end if} // end for ball2} // end for ball1} // end Collision_Response
Hope that helps. Let me know if it does. Code comments were from something else (I forget what), but they seemed applicable, so I added them in. I modeled the code after another code like it, but a wee bit more optimized. There ''ya go!
=======
~Khaos~
=======
If you''d like some math info on how it''s done.
Check out this cool link.. Took me 10 minutes to do my own snooker balls =)
-Hans [home page] [e-mail]
Check out this cool link.. Took me 10 minutes to do my own snooker balls =)
-Hans [home page] [e-mail]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement