apply a force in BULLET

momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

apply a force in BULLET

Post by momoreda »

Hello!

I'm new to physics simualtion and I choose Bullet for learn.
I Want to know How to apply a force (N) to an objet in BULLET ?

Thanks
dreamtheater39
Posts: 13
Joined: Sat Nov 10, 2007 8:39 pm

Re: apply a force in BULLET

Post by dreamtheater39 »

momoreda wrote:Hello!

I'm new to physics simualtion and I choose Bullet for learn.
I Want to know How to apply a force (N) to an objet in BULLET ?

Thanks
Here is one of the many ways -

Code: Select all

pBody->activate(true);
pBody->applyCentralImpulse( btVector3( 0.f, 0.f, -force ) );
:)
San
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

thanks a lot for your answer.

But how to specify the coordinate of the point of application of force ?

Thanks
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: apply a force in BULLET

Post by Dirk Gregorius »

There is also another function that takes an application point. Why don't you look at btBody.h?
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

1.
I can't apply the force with :
pBody->activate(true);
pBody->applyCentralImpulse( btVector3( 0.f, 0.f, -force ) );
2.
i don't have any file with this name "btBody.h"
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: apply a force in BULLET

Post by bone »

btRigidBody.h, then
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

Hi !

i want to learn bullet so I took DemoRagdoll and I changed it. Now i want to apply a force to this box. i kow, i have to use :
pBody->activate(true);
pBody->applyCentralImpulse( btVector3( 0.f, 0.f, -force ) );
but I can not yet apply force to the box. So where i must put the two lines wich define the force.


thanks for your help

this is the code :

/*
Bullet Continuous Collision Detection and Physics Library
Ragdoll Demo
Copyright (c) 2007 Starbreeze Studios

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

Written by: Marten Svanfeldt
*/


#include "btBulletDynamicsCommon.h"
#include "GlutStuff.h"
#include "GL_ShapeDrawer.h"

#include "LinearMath/btIDebugDraw.h"

#include "GLDebugDrawer.h"
#include "RagdollDemo.h"


// Enrico: Shouldn't these three variables be real constants and not defines?

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif

#ifndef M_PI_4
#define M_PI_4 0.785398163397448309616
#endif

class RagDoll
{
public :
enum
{
BODYPART_PELVIS = 0,
BODYPART_COUNT
};

btDynamicsWorld* m_ownerWorld;
btCollisionShape* m_shapes[BODYPART_COUNT];

btRigidBody* localCreateRigidBody (btScalar mass, const btTransform& startTransform, btCollisionShape* shape)
{
bool isDynamic = (mass != 0.f);

btVector3 localInertia(0,0,0);
if (isDynamic)
shape->calculateLocalInertia(mass,localInertia);

btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody* body = new btRigidBody(mass,myMotionState,shape,localInertia);
m_ownerWorld->addRigidBody(body);

return body;
}

public:
btRigidBody* m_bodies[BODYPART_COUNT];
RagDoll (btDynamicsWorld* ownerWorld, const btVector3& positionOffset)
: m_ownerWorld (ownerWorld)
{
// Setup the geometry
m_shapes[BODYPART_PELVIS] = new btBoxShape(btVector3(btScalar(0.5), btScalar(0.5), btScalar(0.5)));
// Setup all the rigid bodies
btTransform offset; offset.setIdentity();
offset.setOrigin(positionOffset);

btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(btScalar(0.), btScalar(0.), btScalar(5.)));
m_bodies[BODYPART_PELVIS] = localCreateRigidBody(btScalar(1.), offset*transform, m_shapes[BODYPART_PELVIS]);

}

~RagDoll ()
{
int i;
// Remove all bodies and shapes
for ( i = 0; i < BODYPART_COUNT; ++i)
{
m_ownerWorld->removeRigidBody(m_bodies);

delete m_bodies->getMotionState();
delete m_bodies; m_bodies = 0;
delete m_shapes; m_shapes = 0;
}
}
};

void RagdollDemo::initPhysics()
{
// Setup the basic world

setCameraDistance(btScalar(10.));

btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

btPoint3 worldAabbMin(-10000,-10000,-10000);
btPoint3 worldAabbMax(10000,10000,10000);
btBroadphaseInterface* overlappingPairCache = new btAxisSweep3 (worldAabbMin, worldAabbMax);

btConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);


// Setup a big ground box
{
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(20.),btScalar(10.),btScalar(20.)));
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-10,0));
localCreateRigidBody(btScalar(0.),groundTransform,groundShape);
}

// Spawn one ragdoll
spawnRagdoll();

clientResetScene();
}
void RagdollDemo::spawnRagdoll(bool random)
{
RagDoll* ragDoll = new RagDoll (m_dynamicsWorld, btVector3 (0,1,0));

m_ragdolls.push_back(ragDoll);
}


void RagdollDemo::clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//simple dynamics world doesn't handle fixed-time-stepping
float ms = m_clock.getTimeMicroseconds();
m_clock.reset();
float minFPS = 1000000.f/60.f;
if (ms > minFPS)
ms = minFPS;

if (m_dynamicsWorld)
m_dynamicsWorld->stepSimulation(ms / 1000000.f);

renderme();

glFlush();

glutSwapBuffers();
}
void RagdollDemo::displayCallback()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (m_dynamicsWorld)
m_dynamicsWorld->updateAabbs();

renderme();

glFlush();
glutSwapBuffers();
}

void RagdollDemo::keyboardCallback(unsigned char key, int x, int y)
{
switch (key)
{
case 'e':
spawnRagdoll(true);
break;
default:
DemoApplication::keyboardCallback(key, x, y);
}


}
dreamtheater39
Posts: 13
Joined: Sat Nov 10, 2007 8:39 pm

Re: apply a force in BULLET

Post by dreamtheater39 »

m_ragdolls[iRagdollIndex].m_bodies[bodyIndex].applyforces.....

come on dude, look harder, it cant get simpler than bullet!
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

thanks for your answer. but in which party of code ?
dreamtheater39
Posts: 13
Joined: Sat Nov 10, 2007 8:39 pm

Re: apply a force in BULLET

Post by dreamtheater39 »

Are you familiar with C++ coding at all?

If you want to add an impulse when you press a key, add it under your keyboard callback.
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

Not at all I am a mechanical engineer, but i try
momoreda
Posts: 25
Joined: Mon Oct 08, 2007 2:09 pm
Location: France

Re: apply a force in BULLET

Post by momoreda »

i added this line in the file DemoApplication.cpp :
body->applyCentralForce((btVector3(btScalar(100.),btScalar(0.),btScalar(0.))));

like this :
#ifdef SHOW_NUM_DEEP_PENETRATIONS
gNumDeepPenetrationChecks = 0;
gNumGjkChecks = 0;
#endif //SHOW_NUM_DEEP_PENETRATIONS

int numObjects = 0;
if (m_dynamicsWorld)
{
m_dynamicsWorld->stepSimulation(1.f/60.f,0);
numObjects = m_dynamicsWorld->getNumCollisionObjects();
}

for (int i=0;i<numObjects;i++)
{
btCollisionObject* colObj = m_dynamicsWorld->getCollisionObjectArray();
btRigidBody* body = btRigidBody::upcast(colObj);
if (body)
{
if (body->getMotionState())
{
btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
myMotionState->m_graphicsWorldTrans = myMotionState->m_startWorldTrans;
colObj->setWorldTransform( myMotionState->m_graphicsWorldTrans );
colObj->setInterpolationWorldTransform( myMotionState->m_startWorldTrans );
colObj->activate();
}
//removed cached contact points
m_dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(colObj->getBroadphaseHandle(),getDynamicsWorld()->getDispatcher());

btRigidBody* body = btRigidBody::upcast(colObj);
if (body && !body->isStaticObject())
{
btRigidBody::upcast(colObj)->setLinearVelocity(btVector3(0,0,0));
btRigidBody::upcast(colObj)->setAngularVelocity(btVector3(0,0,0));
body->applyCentralForce((btVector3(btScalar(100.),btScalar(0.),btScalar(0.)))); }
}
}


In this case, the force applied to all bodies. While I want the force applied to a single body. How can I do to be able to access one of the bodies, then apply force?

Thanks for your help