Bullet + ogre mouse moving object

Runemaster
Posts: 4
Joined: Wed Apr 13, 2011 11:37 am

Bullet + ogre mouse moving object

Post by Runemaster »

Hi everyone! In my project I'm using Ogre + bullet and is now realizing the manipulation of the object with the mouse. My problem is as follows:
Quest 2.jpg
Code for moving object:

Code: Select all

void TestComponet::move(const Ogre::Vector2& currentMousePosition, const Ogre::Vector2& oldMousePosition)
{
    Ogre::Ray ray = camera_->getCameraToViewportRay(currentMousePosition.x, currentMousePosition.y);
    Ogre::Vector3 objectPosition = cvt(collisionObject_->getWorldTransform().getOrigin()); // cvt function convert bullet to ogre and ogre to bullet (vectors, quaternions)
    Ogre::Real a = (objectPosition - ray.getOrigin()).length();
    Ogre::Real b = (currentMousePosition - oldMousePosition).length();
    Ogre::Real distance = sqrt(a*a + b*b);
    Ogre::Vector3 newPosition = ray.getPoint(distance);

    btTransform tr = collisionObject_->getWorldTransform();
    tr.setOrigin(cvt(newPosition));
    collisionObject_->setWorldTransform(tr);
    entityNode_->setPosition(newPosition);
}
Code for raycast object this:

Code: Select all

void RenderWidget::_mouseRaycast(Ogre::Real x, Ogre::Real y)
{
    Ogre::Ray     ray  = camera_->getCameraToViewportRay(x, y);
    Ogre::Vector3 dest = ray.getPoint(FAR_CLIP_DISTANCE);

    btVector3 rayFrom, rayTo;

    rayFrom            = cvt(ray.getOrigin());
    rayTo              = cvt(dest);

    btCollisionWorld::ClosestRayResultCallback rayResultCallBack(rayFrom, rayTo);
    collisionWorld_->rayTest(rayFrom, rayTo, rayResultCallBack);

    if(rayResultCallBack.hasHit())
    {
        for(register int i = 0; i < SCROLLAREA_CONTENT_COUNT; i++)
        {
            if(components_[i]->isThisObject(rayResultCallBack.m_collisionObject))
            {
                if(activeComponent_ != components_[i])
                {
                    prevActiveComponent_     = activeComponent_;
                    activeComponent_         = components_[i];
                    activeComponent_->setPickPosition(rayResultCallBack.m_hitPointWorld);
                    isActiveComponentChange_ = true;
                }
                else { isActiveComponentChange_ = false; activeComponent_->setPickPosition(rayResultCallBack.m_hitPointWorld); }

                return;
            }
        }
    }
    else
    {
        prevActiveComponent_     = activeComponent_;
        activeComponent_         = false;
        isActiveComponentChange_ = true;
    }
}
P.S. Thank you
You do not have the required permissions to view the files attached to this post.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Bullet + ogre mouse moving object

Post by dphil »

This part of your code:

Code: Select all

btTransform tr = collisionObject_->getWorldTransform();
tr.setOrigin(cvt(newPosition));
collisionObject_->setWorldTransform(tr);
entityNode_->setPosition(newPosition);
will always just move the centre (origin) of your object to the new mouse location, disregarding exactly where on the object you originally clicked. Instead, you'd want to do something like:

Code: Select all

Ogre::Vector3 objectoffset = ...; // vector from the clicked point on your object (in world space) to the centre of the object (its original world origin) projected on the viewing plane so we ignore depth and just get the lateral offset
btTransform tr = collisionObject_->getWorldTransform();
tr.setOrigin(cvt(newPosition + objectOffset)); // add offset here
collisionObject_->setWorldTransform(tr);
entityNode_->setPosition(newPosition);
I'm not sure the best way you want to get the objectOffset vector, but anyway the point is just to make an extra translation to account for the offset of the click from the object centre.
Runemaster
Posts: 4
Joined: Wed Apr 13, 2011 11:37 am

Re: Bullet + ogre mouse moving object

Post by Runemaster »

Code: Select all

Ogre::Vector3 offset = cvt(getPickPosition()) - objectPosition;
When I look forward bias, the object is moving at "twitch", that I am doing wrong?
Thank you.