How do I apply angular velocity vector after a collision? - Game Development Stack Exchange most recent 30 from gamedev.stackexchange.com 2019-12-16T11:23:35Z https://gamedev.stackexchange.com/feeds/question/173305 https://creativecommons.org/licenses/by-sa/4.0/rdf https://gamedev.stackexchange.com/q/173305 3 How do I apply angular velocity vector after a collision? Abdulrahman https://gamedev.stackexchange.com/users/126741 2019-06-29T12:12:52Z 2019-07-02T01:30:35Z <p>I'm writing a small 3D physics engine, and I am trying to get the angular velocity vector of two shapes, specifically two cubes.</p> <p>For deeper understanding I have divide the motion to linear and angular motion.</p> <p>For the angular motion I have done a few steps.</p> <p>I calculated the inertia tensor matrix which is 3x3 so I could get the torque as follows:</p> <p><span class="math-container">$$\tau=I\alpha \\ \begin{bmatrix} \tau_x \\ \tau_y \\ \tau_z \end{bmatrix} = \begin{bmatrix} I_{xx} &amp; I_{xy} &amp; I_{xz} \\ I_{xy} &amp; I_{yy} &amp; I_{yz} \\ I_{xz} &amp; I_{yz} &amp; I_{zz} \end{bmatrix} \begin{bmatrix} \alpha_x \\ \alpha_y \\ \alpha_z \end{bmatrix} \\ \tau_x = I_{xx}\alpha_x+I_{xy}\alpha_y+I_{xz}\alpha_z \\ \tau_y = I_{xy}\alpha_x+I_{yy}\alpha_y+I_{yz}\alpha_z \\ \tau_z = I_{xz}\alpha_x+I_{yz}\alpha_y+I_{zz}\alpha_z$$</span></p> <p>And for calculating the matrix I have used: <span class="math-container">$$I= \begin{bmatrix} m(y^2+z^2) &amp; -mxy &amp; -mxz \\ -mxy &amp; m(x^2+z^2) &amp; -myz \\ -mxz &amp; -myz &amp; m(x^2+y^2) \end{bmatrix}$$</span></p> <p>Using the formulas <a href="https://www.euclideanspace.com/physics/dynamics/collision/index.htm" rel="nofollow noreferrer">described here</a>, I could get the impulse and the final angular velocities for all shapes as follows:</p> <p>Impulse: <span class="math-container">\$j= -(1+e) \frac{(v_a-v_b)•n + (r_a×n)•\omega a - (r_b×n)•\omega_b}{ 1/m_a+1/m_b+(r_a×n)•([I_a]^{-1}(r_a×n))+(r_b×n)•([I_b]^{-1}(r_b×n))}\$</span><br> Final velocity of object a:<span class="math-container">\$\vec V_{af} =Vai-J/Ma\$</span><br> Final velocity of object b:<span class="math-container">\$\vec V_{bf} =Vbi-J/Mb\$</span><br> Final angular velocity of object a:<span class="math-container">\$w_{af}=wai-[Ia]^{-1}(J x ra) \$</span><br> Final angular velocity of object b:<span class="math-container">\$w_{bf}=wbi-[Ib]^{-1}(J x rb) \$</span><br></p> <p>So my question is: how could I calculate the rotational angular which each object should rotate and for how long it should last?</p> https://gamedev.stackexchange.com/questions/173305/-/173351#173351 5 Answer by DMGregory for How do I apply angular velocity vector after a collision? DMGregory https://gamedev.stackexchange.com/users/39518 2019-07-01T15:44:56Z 2019-07-01T15:44:56Z <p>The angular velocity vector you've computed using the method you've shown gives you two things:</p> <ul> <li>The direction the vector points is the axis of rotation</li> <li>The magnitude of the vector is the speed of rotation (typically in radians per second)</li> </ul> <p>So, to rotate the object, you can take:</p> <pre><code>// Separate axis and speed into separate variables. float angularSpeed = angularVelocity.Length; Vector3 axis = angularVelocity / angularSpeed; // Compute how far the object rotates in the time // duration of the current simulation step. float angularIncrement = angularSpeed * deltaTime; </code></pre> <p>Then <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle" rel="noreferrer">construct a rotation matrix from the axis and angle</a>, or a quaternion like so:</p> <pre><code>Quaternion rotationIncrement; rotationIncrement.xyz = Sin(angularIncrement/2f) * axis; rotationIncrement.w = Cos(angularIncrement/2f); </code></pre> <p>This lets you rotate the objects frame-by-frame according to their angular velocities.</p> <p>As for "how long" to rotate them, in Newtonian physics, "an object in motion tends to stay in motion," so they'll keep spinning this way like a gyroscope or an asteroid tumbling in space until another collision or force acts on them to cancel the spin.</p> <p>In games, we'll often simulate friction by applying a damping to the angular velocity, multiplying it by a constant less than one each frame so that the rotation will eventually slow to [low enough to round down to] nothing.</p>