Translating and rotating objects by mouse

Post Reply
spirtaat
Posts: 10
Joined: Wed Nov 17, 2010 11:35 am

Translating and rotating objects by mouse

Post by spirtaat »

Hi all,
I'm trying to simulate some behavior of table football/footzy/bar football/foosball/table soccer//Futbolìn/baby-foot/fußball/Kicker/Calcio balilla (tha name varies, see this video to understand the play which i refer to: http://www.youtube.com/watch?v=3fHL8wYb ... DD&index=6). More in detail, I would like to simulate the translation and rotations of its rods. I'm trying to develope this using OSG and Bullet Physics. I loaded a model from sketchup and I'm currently able to pick the rigid body of each rod (setted as kinematic, with mass 0).
Reading the following post - http://bulletphysics.org/Bullet/phpBB3/ ... f=9&t=4517 - I've seen that to apply a translation or a rotation, i need to:
1) remove the rod rigidbody from bulletWorld;
2) get the current rod position and orientation (from the rod's motionstate)
3) set new matrix applying new rotation and position to the rod's motionstate);
4) re-add the rod rigidbody to the bulletWorld

Do I understand right?
And, supposing that these operations really rotate/translate the rod, the second issue is:
does the ball (dynamic object) collide with players and be kicked from these while applying the rotation/translation?

If I understand wrong, could anyone help me? I've already spent a lot of time to develope the scene and correct the original model of the table... I'm quite desperate with physics simulation.
Cheers,

Axel
ghertum
Posts: 3
Joined: Wed Jan 26, 2011 11:11 pm

Re: Translating and rotating objects by mouse

Post by ghertum »

I think you only have to remove/re-add the btRigidBody whenever you change something critical, like turning a static body into a kinematic one. So, providing all of your rods are kinematic at all times (which shouldn't be much of a problem, considering there are rather few of them), you should be fine with doing just number 2 and 3 from your list.

Your dynamic objects will react to the kinematic objects. Just make sure to keep the forces down, as pushing something with a kinematic object is a bit like hitting it with a train. Massive force.
spirtaat
Posts: 10
Joined: Wed Nov 17, 2010 11:35 am

Re: Translating and rotating objects by mouse

Post by spirtaat »

Hi ghertum,
thanks for your reply. I succesfully implemented the translation operation on rod. I have some problem on rotations: I perform them by scrolling the mouse on the rod. If I scroll up then it rotates by +angle, if I scroll down it rotates by -angle (both on rod axis). The issue is this: initially i can only scroll up and not scroll down (and this is wrong), i don't know why. If i scrooll up more times, then i can scroll down up to the original orientation. However, scrolling up i can make more than 360 degrees, but not scrolling down... Why this behavior?

Below i post the code for translating the objects on Y axis (maybe can be helpful for someone) and then i post also the code rotating an object on its Y axis (with the issue described above).
Could you help me?

Code: Select all

    float dx, dy, distance;
    if (_firstMouseEvent.get() == NULL || _lastMouseEvent.get() == NULL) {
        dx = dy = distance = 0;
    } else {
        dx = _firstMouseEvent->getX() - _lastMouseEvent->getX();
        dy = _firstMouseEvent->getY() - _lastMouseEvent->getY();
        distance = sqrt(dx * dx + dy * dy);
    }

    osgbBullet::MotionState* motion = _pickedRod->getRigidRodMotionState();
    btTransform btt;

    btt.setIdentity();
    motion->getWorldTransform(btt);
    btQuaternion rotation = btt.getRotation();
    btVector3 movement(0.,distance, 0.);
    if(_direction.x() > 0) {
        if(dx >= 0)
            btt.setOrigin(btt.getOrigin() - movement);
        else
            btt.setOrigin(btt.getOrigin() + movement);
    } else if(_direction.x() < 0) {
        if(dx >= 0)
            btt.setOrigin(btt.getOrigin() + movement);
        else
            btt.setOrigin(btt.getOrigin() - movement);
    } else {
        if(dy > 0)
            btt.setOrigin(btt.getOrigin() + btVector3(0, dy*dy*_direction.y(), 0));
        else
            btt.setOrigin(btt.getOrigin() - btVector3(0, dy*dy*_direction.y(), 0));
    }

    btt.setRotation(rotation);
    motion->setWorldTransform(btt);
Rotation:

Code: Select all

    osgbBullet::MotionState* motion = _pickedRod->getRigidRodMotionState();
    btTransform btt;
    motion->getWorldTransform(btt);

    btQuaternion rotation = btt.getRotation();
    btVector3 offset = btt.getOrigin();
    rotation.setRotation( rotation.getAxis(), rotation.getAngle() + rotationAngle );
    btt.setOrigin(btVector3(0., 0., 0.));
    btt.setRotation(rotation);
    btt.setOrigin(offset);

    motion->setWorldTransform(btt);
Unfortunatelly, I'm in trouble, I thought this was easier since I've already done the visually rotation on OSG and it works.
spirtaat
Posts: 10
Joined: Wed Nov 17, 2010 11:35 am

Re: Translating and rotating objects by mouse

Post by spirtaat »

Pardon,
I used the osgbBulletDebugDrawer to visually check if the above code really workds and the answer seems to be negative: translation and rotation move/rotate only the visible rod, but not its related btRigidBody...
Desperation is back...
spirtaat
Posts: 10
Joined: Wed Nov 17, 2010 11:35 am

Re: Translating and rotating objects by mouse

Post by spirtaat »

Maybe I obtained the correct moving result... now rods translate and rotation both visually and physically (even if the rotation by scroll down is performed with the same problem above described).
To do that, I setted both btRigidBody and motionState the setWorldTransform...
Furthermore, if the ball is standing in front of a player and I rotate the rod to kick it, the ball does not move... :-( This was precisely the fear I had before implementing the movements of the auction as an kinematic object...
Post Reply