Plank that is self-righting when tossed up

Official forum for the Chipmunk2D Physics Library.
Post Reply
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Plank that is self-righting when tossed up

Post by cpk »

Hello, I have an application where a plank (2d) is thrown into the air, and it should always to come to rest with one of the smallest sides down (board sticks up vertically on ground created with cpBodyNew(INFINITY, INFINITY)). I put together a sample of such a thing using cocos2d iphone and watched the v, w, and t parameters as I tossed it. I tried to make changes without success. Is it the case that 'v' is the over all velocity of the body, and 'w' is a measure of the spin or torque applied to the body? My assumption is that while it is "in the air" that these values need to be reversed and then moved to zero. Am I close? Any hints as to how to go about this would be appreciated! Thanks in advance!!!
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

Would a book like "Physics for Game Developers" by David Bourg allow me to better understand how the Chipmunk code works so that I can answer this kind of question myself? Any other "self help" suggestions? Thanks in advance!
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

It looks like I may need to do something like this...
http://www.slembcke.net/forums/viewtopic.php?f=1&t=518
where the user is putting a limit to the velocity of a body.

Would something similar done with 'cpBodyPositionFunc position_func' and cpBodyUpdatePosition be appropriate?
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

The following seems to work rather well... Any other suggestions??? Thanks!

Code: Select all

cpFloat v_limit = 40.0f;
cpFloat w_limit = 0.05f;

// (cpBody *)body->velocity_func = customBodyUpdateVelocity;
// 	cpFloat v_limit = 200.0f; // limit the velocity to this...
// cpFloat w_limit = 0.1f; // limit the angular velocity to this...
void customBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) {
	// Update the velocity the normal way...
	cpBodyUpdateVelocity(body, gravity, damping, dt);  
	
	cpFloat v_mag = cpvlength(body->v);
	if(v_mag > v_limit) {
		float v_scale = v_limit / v_mag;
		body->v = cpvmult(body->v, v_scale < 0.99 ? 0.99 : v_scale);
	}
	if (abs(body->w) > w_limit) {
		float w_scale = w_limit / body->w;
		body->w *= w_scale < 0.4 ? 0.4 : w_scale;
	}
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by slembcke »

Using the trunk version, you could do it with constraints. If your way seems to work then go with it though.

Though a couple things did stick out to me:

Code: Select all

   if(v_mag > v_limit) {
      float v_scale = v_limit / v_mag; // will always be greater than 1.0
      body->v = cpvmult(body->v, v_scale < 0.99 ? 0.99 : v_scale); // so why compare to 0.99, and why 0.99?
   }
   if (abs(body->w) > w_limit) {
      float w_scale = w_limit / body->w;
      body->w *= w_scale < 0.4 ? 0.4 : w_scale; // What is the significance of 0.4?
   }
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

This is what you get when you copy someone elses code without thinking about it. Yes, v_scale is always < 1.0, and so you will always be multiplying the velocity by 0.99. Amazing how well things seam to work when you want them too...

Code: Select all

   if(v_mag > v_limit) {
      float v_scale = v_limit / v_mag; // will always be greater than 1.0
      body->v = cpvmult(body->v, v_scale < 0.99 ? 0.99 : v_scale); // so why compare to 0.99, and why 0.99?
   }
This seems to have the effect of scaling back the angular velocity when it approaches the limit. The problem is that it is not selective to the beam being vertical. I think that you could safely describe this code as having a strong placebo effect.

Code: Select all

   if (abs(body->w) > w_limit) {
      float w_scale = w_limit / body->w;
      body->w *= w_scale < 0.4 ? 0.4 : w_scale; // What is the significance of 0.4?
   }
So we move on... I am using the trunk version as of last week. By constraints are you thinking that the beam is attached to some unseen mass who's position is always vertical and so you use something like a pivot and a gear joint with a small maximum force to keep the beam (seen mass) always moving to the upright position as constrained by the unseen mass? If so can you force a mass's orientation to always be vertical or fixed? Thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by slembcke »

Sounds like you are thinking along the right track.

You would just need to attach a gear joint with a ratio of 1:1 from your plank's body to whatever static body your world is using. Using the phase parameter of the joint, you can control the angle it goes towards and then use the max force, max bias, and bias coefficient to control of fast and strongly the plank rights itself.

You don't need or want a pivot joint as that would fix its rotation around a particular point.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

Worked like a charm! Thank you so much! Would a book like "Physics for Game Developers" by David Bourg allow me to better understand how the Chipmunk code works? For example, you mentioned the biasCoef, and maxBias. The code doesn't talk about units, usual values, or in what manner a variable contributes to the whole. I just put a printf in the code and watched them. It seems that the bias rarely goes over 10.0f. The biasCoef is 0.1f (as I recall). Again thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by slembcke »

Is that the O'Rielly book? I'm mixed on whether or not that book is any good. I got a copy like 7 years ago now hoping to learn how to make a physics engine, but it definitely doesn't cover anything in enough depth. It might be a good enough start if you just need to get familiar with some of the equations though.

As for some sensible values:
maxForce: In the case of rotary joints it's actually the maximum torque. This really depends on the mass of the objects and more importantly their size. I almost always just guess by starting with 10 then increasing by 10x until it's about right and then adjust until it looks right.
biasCoef: This is the fraction of the joint error (how far the joint is separated) that should be resolved in each step. The default is 0.1. Larger values make the joints seem more solid, but can make the simulation less stable. Valid values are 0 to 1 not including 0.
maxBias: This is actually referring to the max speed the joint is corrected at. In the case of rotary joints, the units are radians per second. The default is infinity, so the rate of correction applied will increases as the amount of joint error does.

The bias stuff might sound a bit abstract, but try playing with the values. You'll probably not need to adjust the biasCoef, but it's helpful to know what it does.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
cpk
Posts: 24
Joined: Wed Sep 02, 2009 9:16 pm
Contact:

Re: Plank that is self-righting when tossed up

Post by cpk »

I think that you mention the book as a reference somewhere on your site.

Yes, thanks much... It would be wonderful if you could stuff the comments that you made above into the code, at least I havn't run across this mentioned anywhere, and it is quite useful.

Again, thank you!
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests