Thanks for the answer.
I don't understand what you are saying just because I don't understand a lot Bullet. I have changed my code in order to approach... This is the result:
Code: Select all
///-----includes_start-----
#include "btBulletDynamicsCommon.h"
#include <stdio.h>
int main(int argc, char** argv)
{
///-----includes_end-----
int i;
///-----initialization_start-----
///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, -9.8, 0));
///-----initialization_end-----
//keep track of the shapes, we release memory at exit.
//make sure to re-use collision shapes among rigid bodies whenever possible!
btAlignedObjectArray<btCollisionShape*> collisionShapes;
///create a few basic rigid bodies
//Creating the ground the ground is a box of side 100x50 at position y = 0.
{
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(1000.), btScalar(1.), btScalar(500.)));
collisionShapes.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0, 0, 0));
btScalar mass(0.);
btVector3 localInertia(0, 0, 0);
//using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, groundShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//add the body to the dynamics world
dynamicsWorld->addRigidBody(body);
}
//creating a kinematic robot obj1
{
btCollisionShape* robot1Shape = new btSphereShape(btScalar(1.));
collisionShapes.push_back(robot1Shape);
btTransform robot1startTransform;
robot1startTransform.setIdentity();
// if mass != 0.f object is dynamic
btScalar robot1mass(0.f);
btVector3 robot1localInertia(0, 0, 0);
robot1Shape->calculateLocalInertia(robot1mass, robot1localInertia);
robot1startTransform.setOrigin(btVector3(50, 2, 0));
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState2 = new btDefaultMotionState(robot1startTransform);
btRigidBody::btRigidBodyConstructionInfo Robot1rbInfo(robot1mass, myMotionState2, robot1Shape, robot1localInertia);
btRigidBody* Robot1body = new btRigidBody(Robot1rbInfo);
Robot1body->setCollisionFlags(Robot1body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
Robot1body->setActivationState(DISABLE_DEACTIVATION);
dynamicsWorld->addRigidBody(Robot1body);
}
//creating a dynamic box obj2
{
btCollisionShape* boxShape = new btBoxShape(btVector3(btScalar(1.), btScalar(1.), btScalar(1.)));
collisionShapes.push_back(boxShape);
btTransform boxTransform;
boxTransform.setIdentity();
boxTransform.setOrigin(btVector3(20, 8, 0));
btScalar massBox(1.);
btVector3 BoxlocalInertia(0, 0, 0);
boxShape->calculateLocalInertia(massBox, BoxlocalInertia);
//using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(boxTransform);
btRigidBody::btRigidBodyConstructionInfo BoxrbInfo(massBox, myMotionState, boxShape, BoxlocalInertia);
btRigidBody* Boxbody = new btRigidBody(BoxrbInfo);
//add the body to the dynamics world
dynamicsWorld->addRigidBody(Boxbody);
}
/// Simulation
///-----stepsimulation_start-----
int numCollision;
btVector3 linealVel(0.5, 0, 0);
for (i = 0; i < 350; i++)
{
dynamicsWorld->stepSimulation(1.f / 60.f, 10);
numCollision = dynamicsWorld->getDispatcher()->getNumManifolds();
//print positions of all objects
for (int j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
{
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
btRigidBody* body = btRigidBody::upcast(obj);
btTransform trans;
if (j == 2) {
if (body && body->getMotionState())
{
body->getMotionState()->getWorldTransform(trans);
}
else
{
trans = obj->getWorldTransform();
}
}
if (j == 1) {
body->activate();
body->getMotionState()->getWorldTransform(trans);
body->setLinearVelocity(linealVel);
body->getMotionState()->setWorldTransform(trans);
}
if (i % 10 == 0)
{
printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
printf("\n numOfCollisions = %d", numCollision); printf("\n");
}
}
}
Then, I delete all the objects.
But the object1 is not moving now, while i expect to move each step simulation 0,5 in x.
What am i doing bad?
Thanks again.