Get Forward, Right, Up Vector From Transformation Matrix

Post Reply
User avatar
KKlouzal
Posts: 44
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Get Forward, Right, Up Vector From Transformation Matrix

Post by KKlouzal » Sun Dec 01, 2019 10:54 pm

How can I get a Forward, Right, and Up Vector from a Transformation Matrix?

I want to use these vectors as a basis to apply force to rigid bodies in different directions.

Code: Select all

btTransform Trans = _RigidBody->getWorldTransform();

btVector3 Forward;	// ???
btVector3 Right;	// ???
btVector3 Up;		// ???

_RigidBody->activate(true);
//
//	Move forward/backward
_RigidBody->applyCentralForce(Forward * 5);
_RigidBody->applyCentralForce(-Forward * 5);
//
//	Jump
_RigidBody->applyCentralForce(Up * 5);
//
//	Move left/right
_RigidBody->applyCentralForce(Right * 5);
_RigidBody->applyCentralForce(-Right * 5);

User avatar
KKlouzal
Posts: 44
Joined: Thu Mar 06, 2014 5:56 am
Location: USA - Arizona

Re: Get Forward, Right, Up Vector From Transformation Matrix

Post by KKlouzal » Mon Dec 02, 2019 8:58 am

It was actually rather simple:

Code: Select all

	void moveForward(const btScalar& Speed) {
		btTransform Trans = _RigidBody->getWorldTransform();
		const btVector3 Forward = Trans(btVector3(1 * Speed, 0, 0));
		Trans.setOrigin(Forward);
		_RigidBody->activate(true);
		_RigidBody->setWorldTransform(Trans);
	}

	void moveBackward(const btScalar& Speed) {
		btTransform Trans = _RigidBody->getWorldTransform();
		const btVector3 Backward = Trans(btVector3(-1 * Speed, 0, 0));
		Trans.setOrigin(Backward);
		_RigidBody->activate(true);
		_RigidBody->setWorldTransform(Trans);
	}

	void moveLeft(const btScalar& Speed) {
		btTransform Trans = _RigidBody->getWorldTransform();
		const btVector3 Left = Trans(btVector3(0, 0, -1 * Speed));
		Trans.setOrigin(Left);
		_RigidBody->activate(true);
		_RigidBody->setWorldTransform(Trans);
	}

	void moveRight(const btScalar& Speed) {
		btTransform Trans = _RigidBody->getWorldTransform();
		const btVector3 Right = Trans(btVector3(0, 0, 1 * Speed));
		Trans.setOrigin(Right);
		_RigidBody->activate(true);
		_RigidBody->setWorldTransform(Trans);
	}

	void doJump(const btScalar& Speed) {
		btTransform Trans = _RigidBody->getWorldTransform();
		const btVector3 Up = Trans(btVector3(0, 1 * Speed, 0));
		Trans.setOrigin(Up);
		_RigidBody->activate(true);
		_RigidBody->setWorldTransform(Trans);
	}

Post Reply