SetLinearVelocity

killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

SetLinearVelocity

Post by killsto »

I've been trying to create a simple tutorial on how to use irrlicht and bullet. I've done ok after understanding the basics until I got to shooting. I then found Erwin's irrlicht demonstration which I used to figure out how to shoot my cubes.

Code: Select all

matrix4 mat;
mat.setRotationDegrees(startrot);
btVector3 forwardDir(mat[8],mat[9],mat[10]);
newobjectnode->getBody()->setLinearVelocity(forwardDir*50);
It works fine but I do not understand exactly what is going on. I'm setting a rotation matrix from the camera's rotation in degrees to quaternions, then I make a btVector3 from the rotations, then I set the velocity as an angle with 50 as the speed? If I multiply 50 by the angle, shouldn't it be changing the angle? And why are we using the matrix in the first place, couldn't we just convert degrees into quaternions?
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: SetLinearVelocity

Post by B_old »

Without having experience with Irrlicht, this is how I interpret the code-snippet:
forwardDir is the Vector that holds the direction in which the camera is looking. So this is also the direction in which the ball should fly. You multiply it by 50 to increase the speed.
The matrix is used in order to extract the direction vector of the camera.
killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Re: SetLinearVelocity

Post by killsto »

B_old wrote:Without having experience with Irrlicht, this is how I interpret the code-snippet:
forwardDir is the Vector that holds the direction in which the camera is looking. So this is also the direction in which the ball should fly. You multiply it by 50 to increase the speed.
The matrix is used in order to extract the direction vector of the camera.
I know what it does, but I don't understand how it does it. How does multiplying an angle by 50 get a new speed but the same direction? EX: 180 degrees * 2 = 360 degrees = new direction.

This is irrelevant, but not worthy starting a new thread: for the method "applyForce (const btVector3 &force, const btVector3 &rel_pos)", what is rel_pos? Is there a difference between it and the absolute position?
User avatar
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland

Re: SetLinearVelocity

Post by Dragonlord »

killsto wrote:

Code: Select all

mat.setRotationDegrees(startrot);
Sets the matrix to the rotation, in this case the camera rotation using Euler angles.

Code: Select all

btVector3 forwardDir(mat[8],mat[9],mat[10]);
Here we take the 3rd basis vector of the matrix. This basis points into the viewing direction of the camera. This is the case since each matrix is in fact a linear equation system composed of 3 linearly independent base vectors ( X-axis, Y-axis and Z-axis ) and the origin of the coordinate frame. This line is the same as transforming the vector (0,0,1) by the matrix mat.

Code: Select all

newobjectnode->getBody()->setLinearVelocity(forwardDir*50);
Now we have the direction the camera looks along. Since we got it from a rotation matrix this is a normalized vector ( hence the length of it equals to 1 unit ). So if we want a push in the direction of forwardDir with 50 unit strength we simply scale this unit vector by 50. The linear velocity is not a direction alone. The magnitude defines the actual velocity along the vector direction.

I hope this clears things up a bit. Simply keep in mind that the linear velocity can always be written as "directionUnitVector * velocityValue".
User avatar
Dragonlord
Posts: 198
Joined: Mon Sep 04, 2006 5:31 pm
Location: Switzerland

Re: SetLinearVelocity

Post by Dragonlord »

Concerning applyForce this applies a push to the object exactly at the center of mass point. Doing so the object receives a linear impulse altering the linear velocity but not inducing any torque. If you use rel_pos then you specify a push point other than the central mass point. Doing so does not only add a linear impulse but also an angular impulse inducing torque. So if you want to simply push your object into a direction without starting to rotate use applyForce without rel_pos. If you want the object to also start tumbling ( for example shot hits you in the shoulder ) then you need to use applyForce with rel_pos ( which is relative to the central mass point ).
killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Re: SetLinearVelocity

Post by killsto »

That makes sense. I understood what it was doing, I just didn't understand how the math worked. I guess I really don't need to know that right now.

Thanks.