Applying force in object's local space, rotation

Kukanani
Posts: 25
Joined: Sat Feb 21, 2009 5:08 am

Applying force in object's local space, rotation

Post by Kukanani »

Hi all, I'm a n00b here (although not with C++).

I have a simple question. How can I apply force on a body relative to the direction the body's pointing? I tried doing this with rotate(), like so:

Code: Select all

        btVector3 force = btVector3(moveX, moveY, moveZ);
        force = force.rotate(btVector3(0,1,0), body->getOrientation().getY());
        body->applyForce(force, btVector3(0,0,0));
But my object moves at all kinds of odd angles. What am I doing wrong here?

thanks,
Kukanani
Last edited by Kukanani on Wed Mar 04, 2009 2:07 pm, edited 1 time in total.
Kukanani
Posts: 25
Joined: Sat Feb 21, 2009 5:08 am

Re: Applying force in direction that body faces - n00b question

Post by Kukanani »

I have partially fixed the problem with this code:

Code: Select all

        btVector3 force = btVector3(mDirection.x, mDirection.y, mDirection.z);
        force = force.rotate(btVector3(0,1,0), -body->getOrientation().getAngle());

        std::cout << force.getX() << ", " << force.getY() << ", " << force.getZ() << ".\n";

        body->applyForce(force, btVector3(0, 50, 0));
        body->setAngularVelocity(btVector3(0, mRotation.y, 0));
But, above angles of about 120 degrees and -50 degrees, my motion is perpendicular to how it should be. I tried using the modulus (%) operator with pi, but I can't use it on a btScalar.

Kukanani
Xuhai.Tang
Posts: 16
Joined: Wed Feb 23, 2011 4:54 pm

Re: Applying force in direction that body faces - n00b quest

Post by Xuhai.Tang »

Kukanani wrote:I have partially fixed the problem with this code:

Code: Select all

        btVector3 force = btVector3(mDirection.x, mDirection.y, mDirection.z);
        force = force.rotate(btVector3(0,1,0), -body->getOrientation().getAngle());

        std::cout << force.getX() << ", " << force.getY() << ", " << force.getZ() << ".\n";

        body->applyForce(force, btVector3(0, 50, 0));
        body->setAngularVelocity(btVector3(0, mRotation.y, 0));
But, above angles of about 120 degrees and -50 degrees, my motion is perpendicular to how it should be. I tried using the modulus (%) operator with pi, but I can't use it on a btScalar.

Kukanani

body->applyForce(force, btVector3(0,0,0));

why are the position of force always (0,0,0)?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Applying force in object's local space, rotation

Post by Flix »

Code: Select all

body->getWorldTransform()->getBasis()->getColumn(2);
gives the local Z direction of the body in world coordinates.
P.S. In some cases it can be useful to retrieve the body world transform from the body motion state.