rotating ball

Started by
1 comment, last by Bob Janova 17 years, 6 months ago
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)
Advertisement
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) Vn and one tangential to the collision (along the wall) Vt.

To resolve:
Vn = (V.N) N // Scalar dot product * Unit vector along normal
Vt = V - Vn

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:

Vnew = Vt - Vn


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

--------------------------------------Amaze your friends! Astound your family! Kennify your text!
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 m1u1+m2u2, so the velocity of the ZMF is (m1u1+m2u2)/(m1+m2).
    Subtract that velocity from the velocity of both colliding objects to move them into this frame (i.e. transformed velocity vn=un-vZMF).
    If one object is a 'wall', vZMF 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 v1x=v1.n and v1y=v1.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(r1w1+r2w2), 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, dwn=RF/In) and the force (dvn=F/mn). The force just affects the component of velocity perpendicular to the collision normal (v1y in my notation above).
  • Calculate the new total linear kinetic energy in the system (linear KE = total energy - (I1w1²+I2w2²)).
  • 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 ½[m1(v1x²+v1y²)+m2(v2x²+v2y²)] (with the v values now being after the collision), and this needs to equal the value calculated two steps above.
    But by this point vix are known, and viy is just viy[before] ± R/mi (plus for one ball, minus for the other, as the force is in opposite directions on each).
    Thus the equation is simply
    E (constant) = ½[(big mungy constant not containing R*)
    +(2v1y[before]/m1)R+(1/m1²)R²
    -(2v2y[before]/m2)R+(1/m2²)R²
    ]
    ... or [(1/m1²)+(1/m2²)]R² + [(v1y[before]/m1)-(v2y[before]/m2)]R + (½ big mungy constant - E) = 0
    Solve the quadratic to get the sane (positive I presume) value of R and thus calculate viy[after] for the two balls.
  • 'Uncomponentise' the velocities: vi(after) = vixn + viyp
  • Convert back to the original frame of motion:
    ui(after)=vi(after)+vZMF


*: The big mungy constant is, if I haven't made a mistake,
½[m1(v1x[after]²+v1y[before]²)+m2(v2x²[after]+v2y²[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 v2 and w2 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]

This topic is closed to new replies.

Advertisement