Simply moving my Rigid Body

Excizetd
Posts: 5
Joined: Sat Jun 12, 2010 9:01 pm

Simply moving my Rigid Body

Post by Excizetd »

Hello people,

I am trying to use Bullet.
I have made my own Motion State to get data from Bullet.

It updates me just fine with transformation.
But I have no clue how to force it into a new position, as getWorldTransform does not get called - unless it is a kinematic body, but I only want to force it into a new position once.

So whats the magic function to do it?
In both Havok and Newton I just call to set position, but since all setting position does, is calling to my own MotionState::setWorldTransform, the Bullet world will never notice.

Thank you and sorry for the simple question. Believe me, I have read through the manual. Just tell me if I will find the answer by reading through some of the pages again.
Cobra
Posts: 6
Joined: Wed Apr 29, 2009 11:45 pm

Re: Simply moving my Rigid Body

Post by Cobra »

Hi. This should help you in positioning your rigid body:

Using an OpenGL matrix (will set both location, rotation):

Code: Select all

btTransform transform;
transform.setFromOpenGLMatrix(someOpenGLMatrix);

myRigidBody->setWorldTransform(transform);
Using the other method:

Code: Select all

btTransform transform;
transform.setOrigin(desiredPositionVector);
transform.setRotation(someRotationQuaternion);
// transform.setBasis(someMatrix) // Optionally rotate by a btMatrix3x3

myRigidBody->setWorldTransform(transform);
I prefer the OpenGL matrix method. This is how I do it.

I hope this helps!

- Josiah
Excizetd
Posts: 5
Joined: Sat Jun 12, 2010 9:01 pm

Re: Simply moving my Rigid Body

Post by Excizetd »

I thought I tried this, and it only called the MotionState setWorldTransform?
Guess not then, will try it. :) thank you
Cobra
Posts: 6
Joined: Wed Apr 29, 2009 11:45 pm

Re: Simply moving my Rigid Body

Post by Cobra »

What you need to do is, in your motion state, you need to keep a btTransform as your world transform, and then on getWorldTransform() you need to return that transform, and on setWorldTransform() you need to set the internal btTransform to the transform that's in the parameters.

There's no reason the above shouldn't work, given that the motion state is properly implemented. :)


As an example, here is the motion state I wrote for my Bullet wrapper for the Irrlicht rendering engine (I suggest that you look over the setWorldTransform() function closely):

Code: Select all

// Copyright (C) 2009-2010 Josiah Hartzell (Skyreign Software)
// This file is part of the "irrBullet" Bullet physics extension library and wrapper.
// For conditions of distribution and use, see copyright notice in irrbullet.h

#include "Bullet/btBulletDynamicsCommon.h"
#include "Bullet/btBulletCollisionCommon.h"
#include "motionstate.h"
#include "bulletworld.h"

using namespace irr;
using namespace core;
using namespace scene;

IMotionState::IMotionState(const btTransform &initialPos)
{
    worldTransform = initialPos;
    ManualRotation = false;
    VelocityAsRotation = false;
}

void IMotionState::getWorldTransform(btTransform &worldTrans) const
{
    worldTrans = worldTransform;
}

void IMotionState::setWorldTransform(const btTransform &worldTrans)
{
    if(object)
    {
        ISceneNode *node = object->getCollisionShape()->getSceneNode();

        irr::core::matrix4 matr;
        btTransformToIrrlichtMatrix(worldTrans, matr);

        node->setPosition(matr.getTranslation());
        if(!ManualRotation)
        {
                node->setRotation(matr.getRotationDegrees());
        }

        worldTransform = worldTrans;
    }

    else if(failed == false)
            printf("irrBullet: [ERR] Object (%i) could not be updated\n", object->getUniqueID());
}


IMotionState::~IMotionState()
{
}

I hope this helps.

- Josiah
Excizetd
Posts: 5
Joined: Sat Jun 12, 2010 9:01 pm

Re: Simply moving my Rigid Body

Post by Excizetd »

It worked just fine in the start.
Now I seam to have another issue..

Say I call RigidBody->setWorldTransformation, with the position of 0, 50, 0 every step.
- It will keep my graphical object at 0, 50, 0 just fine.

Then when I don't set transformation every step, the object falls down (very fast for an object with 0.2 mass and default gravity in my opinion).

But now I have implemented a debug drawer. I can see the wireframe sloowly falling down, leaving the graphical box it should wire around.

First of all I don't understand why it is moving.
Secondly I don't understand why it is not moving at the same speed as it would, when not forcing it to be at 0, 50, 0.
Thirdly, why is it not calling back to the motion state, moving my graphical box as it otherwise would?


A side issue I am having, is the Bullet world seam to be scaled 1 unit = two metres.
My 1x1x1 graphical box, needs a physics box of 0.5, 0.5, 0.5 to have the wireframe fit nicely.

Please advice! :)
Thank you..