pascalosti 100 Report post Posted October 19, 2006 I have a square, and a bunch of balls bouncing of them, sorta looks like a billiards table. Can someone post psuedo code, on how to rotate the balls, and when they hit the walls have them react accordingly(slowing the rotation, and change in bounce direction). or if you have a link you can recommend (this is 1dimension) looking down from the top) 0 Share this post Link to post Share on other sites
Verminox 166 Report post Posted October 20, 2006 Are you using 2D or 3D rendering? The best way to calculate collisions is with vectors. In general, consider you have a Velocity vector V and the ball is colliding with a plane surface (wall) having a unit normal Vector N.Resolve the velocity vector into two mutually perpendicular components, one along the line of collision (center of ball to point of contact) V_{n} and one tangential to the collision (along the wall) V_{t}.To resolve:V_{n} = (V.N) N // Scalar dot product * Unit vector along normalV_{t} = V - V_{n}Now, the problem becomes just one dimensional. The tangential velocity of the ball will remain unchanged but the collision (assuming elastic) will reverse the normal component of the velocity. So your new velocity will be:V_{new} = V_{t} - V_{n}This works for all dimensions. If you have two moving balls of equal mass colliding, simply exchange the normal components of both balls' velocities. For balls of different masses, see Conservation of Momentum in Elastic Collisions 0 Share this post Link to post Share on other sites
Bob Janova 769 Report post Posted October 20, 2006 Verminox, I think the OP is asking for advice on how to handle collisions with spin. Collisions involving spin are more complex because you have more unknowns (angular momentum, angular kinetic energy) and more constraints (conservation of angular momentum).I had a quick search but I didn't find anything obvious.Because altering the spin alters the energy balance, spin and velocity aren't independent. Here's what you do, from memory (but be warned, it's a while since I've done this stuff), for a general collision with spin between two mobile objects. This assumes that the problem is basically 2D, i.e. any spin on the balls is around the axis perpendicular to the plane of collision.Bring the system into the zero momentum frame, i.e. a constant velocity frame in which the total (linear) momentum of the system is zero. The initial momentum of the system is m_{1}u_{1}+m_{2}u_{2}, so the velocity of the ZMF is (m_{1}u_{1}+m_{2}u_{2})/(m_{1}+m_{2}).Subtract that velocity from the velocity of both colliding objects to move them into this frame (i.e. transformed velocity v_{n}=u_{n}-v_{ZMF}).If one object is a 'wall', v_{ZMF} is the velocity of the wall (often zero).It's probably helpful to make the velocities axis aligned, with the collision normal as an axis. Define 'axis vectors', normalised, n as the collision normal, p as a perpendicular that is within the collision plane and perpendicular to the spin axis (n×s). Thus v_{1x}=v_{1}.n and v_{1y}=v_{1}.p.Calculate the total energy in the system: energy per particle = ½mv²+½Iw². m and I are instrinsic properties of the particle, I being the moment of inertia about the axis of rotation. (For the purposes of the collision, a wall has no v or w and thus no energy, but infinite m and I, i.e. it doesn't alter its movement as a result of the collision.)Perform the spin-changing part of the rotation. This results in a force at the contact point of F=f(r_{1}w_{1}+r_{2}w_{2}), with f being a 'friction' coefficient (0: no change in spin and you can use simple elastic collision code; 1: complete swap of spin between the two balls), in a direction perpendicular to both the axis of rotation and the normal of collision (for spherical particles).Apply the torque on each ball (G=rF, dw_{n}=RF/I_{n}) and the force (dv_{n}=F/m_{n}). The force just affects the component of velocity perpendicular to the collision normal (v_{1y} in my notation above).Calculate the new total linear kinetic energy in the system (linear KE = total energy - (I_{1}w_{1}²+I_{2}w_{2}²)).We now have one unknown (the magnitude of the collision force along the collision normal; call this R) and one constraint (conservation of energy). Because the forces are perpendicular to each other, and we're in the ZMF, conservation of momentum is trivial (it's satisfied whatever we choose for the forces).The total KE is ½[m_{1}(v_{1x}²+v_{1y}²)+m_{2}(v_{2x}²+v_{2y}²)] (with the v values now being after the collision), and this needs to equal the value calculated two steps above.But by this point v_{ix} are known, and v_{iy} is just v_{iy}[before] ± R/m_{i} (plus for one ball, minus for the other, as the force is in opposite directions on each).Thus the equation is simplyE (constant) = ½[(big mungy constant not containing R^{*}) +(2v_{1y}[before]/m_{1})R+(1/m_{1}²)R² -(2v_{2y}[before]/m_{2})R+(1/m_{2}²)R²]... or [(1/m_{1}²)+(1/m_{2}²)]R² + [(v_{1y}[before]/m_{1})-(v_{2y}[before]/m_{2})]R + (½ big mungy constant - E) = 0Solve the quadratic to get the sane (positive I presume) value of R and thus calculate v_{iy}[after] for the two balls.'Uncomponentise' the velocities: v_{i}(after) = v_{ix}n + v_{iy}pConvert back to the original frame of motion:u_{i}(after)=v_{i}(after)+v_{ZMF}*: The big mungy constant is, if I haven't made a mistake,½[m_{1}(v_{1x}[after]²+v_{1y}[before]²)+m_{2}(v_{2x}²[after]+v_{2y}²[before])]... i.e. the linear KE of the system before applying the force along the normal.Please remember that I haven't tested this nor looked at stuff like this for a long time so it might not be entirely right! I'm pretty sure the basic idea is sound though.If you're bouncing off a wall, you can take any terms involving v_{2} and w_{2} away – the velocity of a wall is always zero in the ZMF, and we're assuming no spinning walls.Hope that helps![Edited by - Bob Janova on October 20, 2006 10:33:30 AM] 0 Share this post Link to post Share on other sites