Sign in to follow this  
Cornstalks

Jakobsen's Verlet Integrator for Physics... and Rotation?

Recommended Posts

Cornstalks    7030
I recently came accross Thomas Jakobsen's excellent paper "Advanced Character Physics" on using Verlet integration for physics. I want to create my own little 2D physics engine (just for kicks and giggles), and after reading Jakobsen's "Advanced Character Physics", I want to use his method of Verlet integration. However, I want to allow a point/joint to rotate (like a car's axle), so I'm going to have to use the methods presented in "Verlet Integration and Constraints in a Six Degree of Freedom Rigid Body Physics Simulation" by Rick Baltman and Ron Radeztsky Jr. (which can be found here). I have three questions about implementing such a system though: 1) Since I'm doing 2D (which means that my only axis of rotation is the z-axis, the one going in and out of the screen), how do I convert the quaternion to a 2D form such that my objects only rotate about the z-axis? 2) In Rick Baltman's paper he uses I, which he represents by a 3x1 matrix. How can I calculate I for my 2D objects? What is I? (He says it's a particle with a mass distribution, or inertia, I). 3) Again, in Rick Baltman's paper, he does I-1τ(t), where I is the I mentioned in question 2, and τ(t) is the torque at time t. After multiplying them though, he gets another vector, which he names ω. I thought multiplying vectors gave you a scalar (at least if you did the dot product)... What operation is he doing to get another vecter after multiplying I-1 and τ(t)? I think that's all the questions I have (at least at the moment). Thanks in advance!

Share this post


Link to post
Share on other sites
metiscus    166
A quaternion works like this:
You specify an axis of rotation and a rotation in radians around the axis.
If you only want to rotate around the z axis, then your axis of rotation should be <0.0, 0.0, 1.0>, and then your rotation. Mind you, you'll need to normalize the quaternion after you do that. A good quaternion library should have an option to take a vector and an angle.

I is an inertia tensor. Basically set it up like [ mass_x, mass_y, 0 ], this will probably give the desired result.

The cross product?

Share this post


Link to post
Share on other sites
juuso    289
Good news! If you're going 2D, almost all of your worries disappear. 2D physics are a lot easier than 3D.

Quote:
Original post by MikeTacular
1) Since I'm doing 2D (which means that my only axis of rotation is the z-axis, the one going in and out of the screen), how do I convert the quaternion to a 2D form such that my objects only rotate about the z-axis?

You don't need anything special for rotating joints. By just using Jakobsen's technique, the joints are flexible. If you have two sticks/springs attached to a single node, they can spin and wiggle without limitation. If you make a wheel out of springs and nodes and attach it to the stick, it will spin beautifully.

Quote:
2) In Rick Baltman's paper he uses I, which he represents by a 3x1 matrix. How can I calculate I for my 2D objects? What is I? (He says it's a particle with a mass distribution, or inertia, I).

I is called the moment of inertia. Basically, it tells you how "heavy" it is to spin an object. And don't worry, I is a scalar in 2D. Usually, you don't need to calculate I by hand. You can find a list of I for many objects at Wikipedia, for example.

Quote:
3) Again, in Rick Baltman's paper, he does I-1τ(t), where I is the I mentioned in question 2, and τ(t) is the torque at time t. After multiplying them though, he gets another vector, which he names ω. I thought multiplying vectors gave you a scalar (at least if you did the dot product)... What operation is he doing to get another vecter after multiplying I-1 and τ(t)?

He uses the cross product in that case.

Share this post


Link to post
Share on other sites
Cornstalks    7030
@metiscus: I've done a little work with quaternions before (I wanted to make a quaternion Julia set), though I haven't ever used quaternions for rotations. I'm using C# and XNA, and I'm not sure if it has quaternions built into it. I'll google some tutorials on quaternion rotations, but if you know of any good links I'd love to see them. When you say I is an inertia tensor and is set up like [mass_x, mass_y, 0], what exactly is mass_x, mass_y? I'm used to mass being a scalar without multiple components. And is there a way to calculate/approximate mass_x, mass_y for an irregular, convex, 2D polygon?

@juuso: Yes, that was what got me excited about Jakobsen's technique. Like you said, I could create a wheel by creating a center joint where the axle would be, and then extending two more joints out from the axle-center joint, and then just connect the axle-center joint to the car's "skeleton" to connect it to the car. I just thought it might make more sense to incorporate Rick Baltman's method because I could then represent a single, static body (for example, a rock not connected to anything) as one point rather than three points. It's not like this is a problem though, and I'd be happy to do it if it will be easier than Rick Baltman's method, but I have some questions about it.

First, let's say I have a rectangle and three joints; one joint is in the rectangle's center, and the other two joints extend out of the center joint parallel to each other, though on opposite sides. If the rectangle is 10 meters long, and I apply a force 4 meters from its center (I'll let you pick what side you are applying to force to), what force would I apply to the three joints? Obviously the rectangle should start rotating. Would I need to calculate the torque and apply that to each joint, and in addition to the torque, apply the linear force to the joints too?

And second, would the two joints extending from the center need to be any specific distance from the center? Could I just put the joints 1 meter away from the center, and have that distance my constant standard? Like if I had another object, an irregular (though not crazy) rock with maximum diameter 3 meters, could I just put the two joints 1 meter away from the center too?

Okay, back to my response to your post. I've dealt with moment of inertia stuff in AP Physics last year, though it was always a scalar when we used it. Judging by Rick Baltman's description of I, it looks more like he was talking about a moment of inertia tensor (like what metiscus said). That would make sense too because when he does I-1τ(t) he could just do matrix multiplication if I was a 3x3 matrix and still get a vector (though maybe this would be the same as the cross product like you two mentioned? I don't know, I'm too lazy at the moment to write it out and see if they would be the same).

Well, okay, I think I'm almost fully convinced to drop Rick Baltman's method and just use Jakobsen's technique, but my two questions above in this post are the only things holding me back from getting to work.

Share this post


Link to post
Share on other sites
metiscus    166
Okay Mike, I think this will make some things a bit more apparent for you. Imagine a barbell with significant weight on it. The barbell is laying down the x axis centered around the origin. So []----0----[]. Rotation around the x axis (rolling) is going to be easier than rotation around the y axis which entails moving the entire mass with the weights farther from the axis of rotation. The inertia tensor I, represents this fact by making the Ix number smaller than the Iy number. Of course, since you are in 2d, you don't have a z axis and rotation around it is fruitless.

Share this post


Link to post
Share on other sites
Cornstalks    7030
Quote:
Original post by metiscus
Okay Mike, I think this will make some things a bit more apparent for you. Imagine a barbell with significant weight on it. The barbell is laying down the x axis centered around the origin. So []----0----[]. Rotation around the x axis (rolling) is going to be easier than rotation around the y axis which entails moving the entire mass with the weights farther from the axis of rotation. The inertia tensor I, represents this fact by making the Ix number smaller than the Iy number. Of course, since you are in 2d, you don't have a z axis and rotation around it is fruitless.

Thanks! That's making a whole lot more sense now.

Share this post


Link to post
Share on other sites
Cornstalks    7030
Quote:
Original post by metiscus
Can you post pseudocode for how you took the derivative of the quaternion from Baltman's paper?


I haven't implemented it yet, though I will someday soon (I just started college and it's eating my life away). In the bottom of Baltman's paper in Appendix A, he writes down multiple useful quaternion operations. At the top of page 23 he shows how he takes the derivative of the quaternion q, and at the bottom of page 22 he shows how he multiplies two quaternions (I just found this awhile ago, but it answers my original question number 3). Those two formulas should be enough for you to be able to properly take the derivative of the quaternion. However, if you still need me to post some pseudocode or anything just let me know and I'd be happy to.

Share this post


Link to post
Share on other sites
oliii    2196
The inertia tensor matrix in 3D can be reduced to just one scalar in 2D. The calculations are much simpler, as the orientation components can also be reduced to one scalar (instead of a matrix).

I have some sample code for a rigid body demo here.

Share this post


Link to post
Share on other sites
Cornstalks    7030
Quote:
Original post by oliii
The inertia tensor matrix in 3D can be reduced to just one scalar in 2D. The calculations are much simpler, as the orientation components can also be reduced to one scalar (instead of a matrix).

I have some sample code for a rigid body demo here.


Oh ok, sweet. Thanks for the apps/code oliii! I'll go through them later today after my classes. Just ran the .exe's and they seem to cover just what I need, so I'm excited to dig around the source code. I'll let you know how it goes. Thanks!

Share this post


Link to post
Share on other sites
Cornstalks    7030
Thanks for the code oliii, I just spent the past little bit going through it and it covers everything I need! (And thanks for putting in comments and writing clearly). I've got a question though. If my game is going to have multiple 2D ragdolls as well as singular/jointless objects (like the ones in your example 7), and possibly some 2D cars, do you think it would be a good idea to use Baltman's method? Or should I just attach a couple of extra joints onto objects that should spin as if they were on an axle? The reason I ask is because I'm not sure if Baltman's method is the best for rigid body joints, like knee joints and what not.

Share this post


Link to post
Share on other sites
oliii    2196
Well, that's where I'm kind of stuck atm. Rigid constraints and solving them. Even in 2D, I'm not really sure what to do to get a stable physics simulation, from what I have (simple free rigid bodies colliding, with friction -> complex articulated bodies).

I knew of that document ages ago, but I've never really considered implementing it. Maybe now's the time to give it a go.

Iterative constraint solvers are much easier to implement, but I'm not sure if they will yield a good result. I would give the method a go, because the alternative seems much more difficult. But I cant say how well they will perform.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this