## Combining then splitting angular and linear velocity

Member
Posts: 320
Joined: 2003.06
Post: #1
Hi

I am trying to give accurate 2D collision response to a box hitting a line mesh. I think I have most of it right, the offsetting of positions is correct, it is the velocities I'm having troubles with. Linear velocity by itself is fine but angular velocity is causing me a bit of grief.

My problem appears to be in the conversion from the separate linear and angular components of the corner of a box to a single linear velocity for the corner, and then back again to separate components after the response velocity vector is calculated. If anyone can see that I'm doing it all wrong, or can see some slight errors please let me know. This must be a fairly common problem?

This is the part of the code that does the velocity stuff:

(I also later add each corner's rotation velocity together to get a combined rotation velocity for the whole quad - could be dodgy too)

Code:
```vec2 combinedVelocity = vec2(boxVelocity[VX] - pointOffsetFromCenterOfMass[VY] * boxRotationalVelocity,                              boxVelocity[VY] + pointOffsetFromCenterOfMass[VX] * boxRotationalVelocity);                            vec2 reflectVector = reflect(combinedVelocity, surfaceNormal); vec2 bounceComponent = reflectVector * surfaceNormal; vec2 frictionComponent = reflectVector - bounceComponent; vec2 newVelocity = bounceComponent * bounce + frictionComponent * (1.0f - friction); float linearVelocityFraction = fabsf(newVelocityNormal.dotProduct(newPositionNormalFromCenterOfMass)); velocityOffsets[i] = newVelocity * linearVelocityFraction - boxVelocity; vec2 linearVelocityDifference = (newVelocity - velocityOffsets[i]); vec2 offset = ((quad[i] + newPositionOnLineOffset) - centerOfMass); if((linearVelocityDifference[VX] * offset[VY] - linearVelocityDifference[VY] * offset[VX]) >= 0) {     rotationalVelocityOffsets[i] = -atanf(linearVelocityDifference.length() / offset.length()) - boxRotationalVelocity; } else {     rotationalVelocityOffsets[i] = atanf(linearVelocityDifference.length() / offset.length()) - boxRotationalVelocity; }```

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
First of all, are you using impulses? I don't really recognize any of your equations. Seeing atan() in there has me a bit worried that you're trying something hacky.

To answer your original question: The velocity at any given point on a rigid body is the rotational velocity multiplied by the perpendicular vector to the offset added to the translational velocity.

v_point = v + w*offset_perp

You might find this a good resource: Box2D
I'm really digging his time-stepping algorithm. It has problems compiling under GCC due to some issue with the STL though. OSC might still have the quick fix that would allow you to compile it for evaluation purposes though.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
You should probably have a read of this http://www.d6.com/users/checker/pdfs/gdmphys3.pdf
The physics formulas get pretty complicated by it does kinda make sense.. good luck anyway.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
unknown Wrote:You should probably have a read of this http://www.d6.com/users/checker/pdfs/gdmphys3.pdf
The physics formulas get pretty complicated by it does kinda make sense.. good luck anyway.

Ah, I had forgotten about Chris Hecker. That's good stuff too.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Member
Posts: 320
Joined: 2003.06
Post: #5
Thanks for your replies, the links look very useful. I'll let you know how I get on when I sit down and give it another crack this weekend.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com