rotation help

markbaker
Posts: 7
Joined: Mon Dec 01, 2008 3:37 am

rotation help

Post by markbaker »

I'm trying to create a rolling ball. I can't figure out why my sphere bounces along the plane fine, but doesn't roll/rotate at all. I tried applying torque, torque impulse, etc. but no luck. Can anyone spot anything missing in my code?

Code: Select all

#include <iostream>

#include <btBulletDynamicsCommon.h>

#include "btBulletTest.h"

int main (void)
{

        btVector3 worldAabbMin(-10000,-10000,-10000);
        btVector3 worldAabbMax(10000,10000,10000);
        int maxProxies = 1024;
        btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);

        btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
        btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

        btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

        btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);

        dynamicsWorld->setGravity(btVector3(0,0,-10));

        btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,0,1),1);

        btCollisionShape* fallShape = new btSphereShape(5);

        btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,0,1)));
        btRigidBody::btRigidBodyConstructionInfo
                groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
        btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
        dynamicsWorld->addRigidBody(groundRigidBody);

        btDefaultMotionState* fallMotionState =
                new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,0,50)));
        btScalar mass = 1;
        btVector3 fallInertia(1,1,1);
        fallShape->calculateLocalInertia(mass,fallInertia);
        btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
        btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
        dynamicsWorld->addRigidBody(fallRigidBody);


        for (int i=0 ; i<10000 ; i++) {

	        fallRigidBody->applyCentralImpulse(btVector3(1, 0, 0));
				fallRigidBody->applyTorqueImpulse(btVector3(1, 0, 0));
		
                dynamicsWorld->stepSimulation(1/60.f,10);

                btTransform trans;
                fallRigidBody->getMotionState()->getWorldTransform(trans);

				float x[16];

				trans.getOpenGLMatrix(x);

				printf("x[0]:%d x[1]:%d x[2]:%d x[3]:%d \n", int(x[0]), int(x[1]), int(x[2]), int(x[3]));
				printf("x[4]:%d x[5]:%d x[6]:%d x[7]:%d \n", int(x[4]), int(x[5]), int(x[6]), int(x[7]));
				printf("x[8]:%d x[9]:%d x[10]:%d x[11]:%d \n", int(x[8]), int(x[9]), int(x[10]), int(x[11]));
				printf("x[12]:%d x[13]:%d x[14]:%d x[15]:%d \n\n", int(x[12]), int(x[13]), int(x[14]), int(x[15]));
        }

        dynamicsWorld->removeRigidBody(fallRigidBody);
        delete fallRigidBody->getMotionState();
        delete fallRigidBody;

        dynamicsWorld->removeRigidBody(groundRigidBody);
        delete groundRigidBody->getMotionState();
        delete groundRigidBody;


        delete fallShape;

        delete groundShape;


        delete dynamicsWorld;
        delete solver;
        delete collisionConfiguration;
        delete dispatcher;
        delete broadphase;

        return 0;
}
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: rotation help

Post by mickey »

Perhaps the ball might be just falling vertically?

It helps to have a graphical visual of the simulation too.
markbaker
Posts: 7
Joined: Mon Dec 01, 2008 3:37 am

Re: rotation help

Post by markbaker »

the loop prints out the values of the transformation matrix.

you can see the translation matrix value changes (which is correct) but the rotation matrix is all 0
ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Re: rotation help

Post by ola »

If I'm not mistaken I can't see you setting the friction anywhere. If the friction is zero (which is default, I believe), then your sphere won't start to roll. Try to set the friction to 1.0 or something (on both sphere and ground, if either has zero friction you won't have any at all).

Best regards,
Ola
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: rotation help

Post by mreuvers »

default friction is 0.5, not 0. And if I'm not mistaken a higher friction setting will result in a higher friction, not the other way around. I.e., a friction value of 0.0 will make objects look like water ;-)