Need help fully integrating bullet into my space sim

User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Need help fully integrating bullet into my space sim

Post by chaosavy »

In case you dear reader are interested here's how I implemented Bullet into my project

http://www.ogre3d.org/forums/viewtopic.php?f=1&t=60403

Basically instead of letting Bullet set positions and orientations for my Ogre scene nodes, I do the opposite (the scene nodes set positions for Bullet, I move the scene nodes via ->translate(position to traslate) and rotate via ->rotate(angle of rotation, axis of rotation), then when a collision happens, I let Bullet take over and the scene node gets its position and rotation from Bullet

I'm on the path to going legit and letting Bullet have all the control but I'm running into problems.

I got my engines (thrust forward, aft, side etc) working (yay!).

Now I'm working on rotations:

If I do this:

rigidBody->applyTorque(btVector3(0,10*mTimeSinceLastFrame,0));

Then the ship turns (yay!) however, the ship picks up so much turning speed that it is difficult to maneuver (would have to apply an opposite force, to stop it, then some more to turn the other way)

I know this is how physics works (or I should say: I guess this is how physics work)

But what I'm trying to achieve is this:

player commands ship to turn (eg moves mouse, moves joystick) ship turns (according to its max degre per second turning ability), player stops moving mouse/joystick, ship turns a litle bit still, but then stops turning

does it have something to do with damping? I have no idea what this does.

is this not applyTorque? I tried applyTorqueImpulse and the effect was multiplied.

thank you in advance
m-ryan
Posts: 11
Joined: Sat Mar 19, 2011 11:47 pm

Re: Need help fully integrating bullet into my space sim

Post by m-ryan »

Make sure you're not applying values in radians when it should be degrees, and vice-versa. That always causes issues. :wink:
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

Right now I threw in a value to test things (10)


rigidBody->applyTorque(btVector3(0,-10*mTimeSinceLastFrame,0));

I figure once I get this working I'll put in custom values for different ship types like I had before when Ogre was doing the rotations

eg: 45 degrees for a small fighter
10 degrees for a large destroyer
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

ok I think I got it (thought about your reply some more)


I'll need to convert my turning units from degrees to radians, then compensate for the mass of the object and use that as the value for applyTorque()


then while turning I'll need to get the angular velocity of the object, and make sure that it isn't exceeding the max permitted turning speed that I specify, that will give me the control

thanks!
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

argh!


I found this code

Code: Select all

btVector3 relativeForce = btVector3(0,10,0);
btMatrix3x3& boxRot = boxRigidBody->getWorldTransform().getBasis();
btVector3 correctedForce = boxRot * relativeForce;
boxRigidBody->applyCentralForce(correctedForce);
from this post:

http://bulletphysics.org/Bullet/phpBB3/ ... ion#p17919

and I thought I got my answer, but it is erratic.

Is there someone who has accomplished this? Mind sharing the code?

to recap: I'm trying to turn a rigid body reletive to its orientation using torque, it works but not reletive to the object

would appreciate any help/hints/etc

thanks
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Need help fully integrating bullet into my space sim

Post by Flix »

chaosavy wrote:to recap: I'm trying to turn a rigid body reletive to its orientation using torque, it works but not reletive to the object
(No time to read the whole series of posts, so thanks for the recap!).
If you want to add a torque impulse of 10 around the "global" vertical axis, you should probably do something like:

Code: Select all

body->addTorqueImpulse(btVector3(0,10,0));
If you want to do the same around the body local Y axis, you may try something like:

Code: Select all

body->addTorqueImpulse(body->getWorldTransform()->getBasis()->getColumn(1)*10);
If that does not work, try to retrieve the world transform from the body motion state ( and just in case make sure that if a COM (center of mass) offset transform is present, its orientation is set to identity ).
Hope it works...

[edit] addTorqueImpulse should probably be: applyTorqueImpulse, or something else...
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

well we are getting somewhere... thank you

I can maneuver reletive to the ship's orientation, and I can extract the momentum vector reletive to my orientation


a lot of my problems were caused by me trying to reduce the turning force back to 0 per axis, and this problem still remains


lets say I turn at 1 units of radians on the y axis reletive to my orientation - aka I turn/yaw left

when I let go of the joystick or mouse or whatever, I want the ship to stop turning, so I'm thinking I need to add the opposite force to do so

however I can't seem to extract that force

I would think that this similar to the code posted above would do it, except this time getting at the angular force:

Code: Select all

	btMatrix3x3& boxRot = rigidBody->getWorldTransform().getBasis();
	btVector3 correctedForce = boxRot * rigidBboody->getAngularVelocity() ;
It seems to work, but eventually becomes erratic
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

hmm it seems to work via this change:

btMatrix3x3& boxRot = rigidBody->getWorldTransform().getBasis();
btVector3 correctedForce = boxRot.inverse() * rigidBboody->getAngularVelocity() ; //Taking inverse of rotation matrix
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Need help fully integrating bullet into my space sim

Post by chaosavy »

It works!

http://www.youtube.com/watch?v=-xk-Yu8QS88
Made a youtube vid of the before/after, the change may appear slight, but I think its awesome. Also the maneuvering of the ship feels much more like a ship in space (before it could stop rotation on a dime, now there's is a bit of a deceleration going on when turning then stopping the turn).

In the end I stopped using torque and impulse and am using setLinearForce and setAngularForce like so:

Code: Select all

	
btMatrix3x3& boxRot = rigidBody->getWorldTransform().getBasis();
btVector3 correctedForce = boxRot * (maneuveringVector * mTimeSinceLastFrame);

rigidBody->setAngularVelocity(rigidBody->getAngularVelocity() + correctedForce);

maneuveringVector.setZero();
maneuveringVector gets added the radians for the move based on joystick/mouse input. I correct the force via multiplying it by the rotation matrix, then I add this to the force I'm working with. Movement works the same way. I find that using the set force works much better as I don't have to worry about the mass of hte object, so I set the rotation per the max rotate per second attribute that my mover class has.

Now I have to get the AI to use Bullet fully as well.