Monday, 3 January 2011

Developer Journal 28

CHANGES
* Tuned forwards and backwards movement.
* Tuned left and right rotation.

NEXT ACTIONS
* Connect AI to linear and angular movement.
* Find a way to get cubes that have stopped moving for a while and gone into inactive mode to be able to become active again.

NOTES

My current goal is to be able to move a cube around comfortably.

The things that are available to me to adjust include linear and angular force, maximum linear and angular velocity, and friction.

It looks like you set friction when you create an object of class btRigidBodyConstructionInfo which you then use to create an object of class btRigidBody.

I'd also love to be able to adjust things in real time. It would be great if I could have for example, a text file which I can store and modify numbers and then get the simulation to reload them.

I'm also thinking of getting a more ergonomic keyboard, one which allows me to have my hands further apart and at a more comfortable angle.

I want the cube to have a higher max speed.

Changed max linear speed from 32 to 64.

Changed max linear speed from 64 to 128.

No big difference.

No wonder. I was changing the values in the wrong places.

Keeping max linear speed at 128.

I want the cube to take less time to start moving forwards.

Increasing linear force from 4 to 8.

I'm getting a craving for something cold and sweet.

Better.

Increasing linear force from 8 to 16.

Pretty good.

Increasing linear force from 16 to 32.

Increasing linear force from 32 to 64.

Increasing linear force from 64 to 128.

Nice.

Increasing linear force 128 to 256.

Increasing linear force 256 to 512.

Linear acceleration is nice but linear max speed is too high.

Decreasing linear max speed from 128 to 64.

Linear max speed 64 feels a little slow and 128 feels a little fast. Somewhere in there would be nice.

Trying max linear speed 96.

Max linear speed 96 feels a little fast.

Trying max linear speed 80.

Max linear speed 80 feels about right, a little on the fast side but I think that when I increase friction and tap the accelerator instead of holding the accelerator down, it will feel right.

btRigidBody inherits from btCollisionObject.

btCollisionObject has getFriction() and setFriction().

Just watched another Deus Ex 3 trailer. I'm so excited!

Cube friction is 0.5.

Ground friction is 0.5.

Increasing cube friction from 0.5 to 1.

Increasing cube friction from 1 to 2.

With cube friction 2, the cube tips over.

Should I increase the mass?

Should I decrease max linear speed?

Decreasing max speed to from 80 to 64.

Increasing mass from 1 to 2.

Increasing mass from 2 to 4.

Increasing mass from 4 to 8.

Increasing mass from 8 to 16.

With a mass of 16, my cube tips over straight away.

Decreasing cube friction from 2 to 1.

The cube stops tipping over.

Decreasing cube mass from 16 to 8.

The cube still slides too long before stopping.

Decreasing cube mass from 8 to 4.

I may have to apply a downwards force to cheat a little.

I may also have to apply a deceleration force.

If I'm not apply a force and if I'm moving, then apply a deceleration force.

No good.

I inverted the velocity, multiplied it by 10 and applied it as a force.

Nice.

Increasing max speed from 32 to 64.

Increasing linear force from 512 to 1024.

Decreasing mass from 4 to 1.

Nice.

Zippy acceleration and a nice deceleration.

Changing friction from 1 to the default of 0.5.

Still good.

Applying a downwards force is still probably a good idea.

The main lesson here was that adjusting friction was not the way to go, it just resulted in my cube flipping over. A better way was to apply a counter force to slow my cube down when I'm not accelerating it. Using the velocity, inverting it and scaling it provides a nice counter force. I don't really understand it but it works.

Would be fun to create a small racing game with this code, in a micro machines style.

Kind of fun, just playing with this. I haven't even got the angular force to work properly so no proper turning yet.

Strangely, due to small floating point numbers, the cube turns when I just move forwards and backwards. That's not a problem for now.

Hopefully, I can do similar things with the angular force.

Increasing angular force from 5 to 10.

Increasing angular force from 10 to 20.

I applied a counter angular force when I'm moving and not applying angular force.

Works well.

Increasing max angular speed from 2 to 4.

Increasing counter angular force from 10 to 20.

The cube still veers to the left or right when I'm moving forwards a little but it's good enough for now.

These values took a a fair few hours to figure out. Note that the mass is 1.

// apply angular force
    const btVector3 torque( 0.0f, 1.0f, 0.0f );
    const float angularForceMultiplier = 20.0f;
    const btVector3 finalTorque = torque
        * angularForceMultiplier
        * rotFrac;
    alpha->rigidBody->applyTorque( finalTorque );

    // limit angular velocity
    btVector3 angularVelocity = alpha->rigidBody->getAngularVelocity();
    const float angularSpeed = angularVelocity.length();
    const float maxAngularSpeed = 4.0f;
    if( angularSpeed > maxAngularSpeed )
    {
        angularVelocity *= maxAngularSpeed / angularSpeed;
        alpha->rigidBody->setAngularVelocity( angularVelocity );
    }

    // apply angular deceleration when moving and not accelerating 
    if( angularSpeed > 1.0f && rotFrac == 0.0f )
    {
        btVector3 inverted = angularVelocity * -1.0f * 20.0f;
        alpha->rigidBody->applyTorque( inverted );
    }

    // apply linear force
    const btVector3 linearForce( 1.0f, 0.0f, 0.0f );
    const btVector3 rotatedLinearForce = 
        alpha->rigidBody->getCenterOfMassTransform().getBasis() 
        * linearForce;
    const float linearForceMultiplier = 1024.0f;
    const btVector3 finalLinearForce = rotatedLinearForce 
        * linearForceMultiplier
        * moveFrac;
    alpha->rigidBody->applyCentralForce( finalLinearForce );

    // limit linear velocity
    btVector3 linearVelocity = alpha->rigidBody->getLinearVelocity();
    const float linearSpeed = linearVelocity.length();
    const float maxLinearSpeed = 64.0f;
    if( linearSpeed > maxLinearSpeed ) 
    {
        linearVelocity *= maxLinearSpeed / linearSpeed;
        alpha->rigidBody->setLinearVelocity( linearVelocity );
    }

    // apply linear deceleration when moving and not accelerating
    if( linearSpeed > 1.0f && moveFrac == 0.0f )
    {
        btVector3 inverted = linearVelocity * -1.0f * 10.0f;
        alpha->rigidBody->applyCentralForce( inverted );
    }

No comments:

Post a Comment