Box2D Forums

It is currently Sat May 25, 2013 7:41 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Tue Aug 05, 2008 7:01 am 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
Hi all,
in my plateformer game, i want to limit the rotation of my character controller for more realisme, is there any proposition to do that?


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 9:40 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1518
Location: Tokyo
If you just want your character never to rotate at all, you can just set fixedRotation to be true in the body def:
Code:
b2BodyDef bodyDef;
bodyDef.fixedRotation = true;

If you want to allow your character a certain amount of rotation so that he is not totally rigid, you could apply a torque to correct the rotation as necessary. This is a little more involved. I am using this code:
Code:
   float desiredAngle = 0;
   float angleNow = body->GetAngle();
   float changeExpected = body->GetAngularVelocity() * timeStepLengthInSeconds; //expected angle change in next timestep
   float angleNextStep = angleNow + changeExpected;
   float changeRequiredInNextStep = desiredAngle - angleNextStep;
   float rotationalAcceleration = timeStepsPerSecond * changeRequiredInNextStep;
   float torque = rotationalAcceleration * torqueAdjustment;
   if ( torque > maximumPossibleTorque )
      torque = maximumPossibleTorque;
   body->ApplyTorque(torque);

It looks quite long-winded but I left it like that so I could figure out what I was doing.
torqueAdjustment is a value which you will need to play with, it alters how quickly the rotation will be corrected. I've found that a value of 3 for this makes my 75kg character look rigid almost all the time, except when he slams into something while running fast. Don't set this value too high because the rotation in the next time step might end up way too far in the opposite direction, causing the next correction to be even larger, etc.
Although this works fine I've always wondered if there was another way.... ?


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 10:03 am 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
ah ok thanks for your help ;)


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 12:28 pm 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
what value should i give to timeStepLengthInSeconds, timeStepsPerSecond, maximumPossibleTorque for a player with 86 as weight, 1/60hz, and 10 iterations ?


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 7:40 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1518
Location: Tokyo
timeStepLengthInSeconds = 1 / 60
timeStepsPerSecond = 60

maximumPossibleTorque is up to you. I am using 20000 for my character with mass of 75 and this allows him to correct his rotation so quickly you can hardly tell, except when he slams into a wall or something. http://www.youtube.com/watch?v=diA8TFrs_r4
My characters center of mass is offset though, so he requires more torque to stay up straight. If your character has his center of mass in the center of his shape as normal, you probably won't need so much torque to rotate it.


Top
 Profile  
 
PostPosted: Wed Aug 06, 2008 5:57 am 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
Your method fodn't work :(
here one demo for windows who shows the problem: http://www.assembla.com/spaces/white_night/documents/dRPWcQy7yr3Apwab7jnrAJ/download/demo.rar
it contains two executable, the first with fied rotation, and the second, with your method


Top
 Profile  
 
PostPosted: Wed Aug 06, 2008 6:46 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1518
Location: Tokyo
Those exe's seem to be invalid, I can't run them.

Unless you really need your character to have some flexibility (most games dont), then I would use the fixed rotation method.


Top
 Profile  
 
PostPosted: Wed Aug 06, 2008 7:12 am 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
invalid? there si no error message or something like that?


Top
 Profile  
 
PostPosted: Wed Aug 06, 2008 4:08 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1518
Location: Tokyo
Yes, the message says the executable is invalid :?


Top
 Profile  
 
PostPosted: Thu Aug 07, 2008 6:47 am 
Offline

Joined: Thu Jul 17, 2008 11:24 am
Posts: 54
that's beacause i had compile it with VC++ 2008 ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group