Rotating A Wheeled Vehicle In Mid Air
by Quentin Headen · 02/20/2009 (3:15 pm) · 10 comments
First off I would like to think user Morrock for helping me in the irc chat with the math calculations.
Go to the rigid.h file in the game directory. At the very end of the Rigid class and add this line of code:
Now open up the rigid.cc file. At the very end of this file add:
Ok, we now have set up the functions for rotation, but now we have to allow that rotation to effect the vehicle.
Open up the vehicle.h file in the game/vehicles directory. Locate the "Vehicle" class and then locate the object "Rigid mRigid". That is the rigid body of the car itself. Replace the "Rigid mRigid" line of code with this:
That allows the rigid body to be exposed to other functions.
Now open up the wheeledvehicle.cc file in the same directory and add this code to the very bottom:
That is the console function that you use to spin the car.
Recompile the engine.
NOW IT IS TIME TO TEST THIS BABY!!!!!
Open up Torque and make the vehicle airborne somehow. You might want to delete the terrain or something so that the vehicle can't land. To test this code, the vehicle must be in the air.
Once that is done, open up the console and type:
NOTE!!!! You must replace the word %car with the name or ID of the vehicle you are rotating. AND you must replace rotx with the rotation force you want on the x axis, roty with the force on the y axis and rotz for the z axis!
Now hit enter. Your car should now rotate. If it doesn't rotate with a small value, try a higher one.
We had to use the MatrixMulVector so that the rotation axis will stay the same no matter where the car is facing.
I hope you liked this resource. Please leave a comment.
Thanks again to Morrock for the help with these code changes!
Go to the rigid.h file in the game directory. At the very end of the Rigid class and add this line of code:
void applyRotationImpulse(const Point3F &rotation);
Now open up the rigid.cc file. At the very end of this file add:
void Rigid::applyRotationImpulse(const Point3F &rotation)
{
angMomentum += rotation;
invWorldInertia.mulV(angMomentum, &angVelocity);
}Ok, we now have set up the functions for rotation, but now we have to allow that rotation to effect the vehicle.
Open up the vehicle.h file in the game/vehicles directory. Locate the "Vehicle" class and then locate the object "Rigid mRigid". That is the rigid body of the car itself. Replace the "Rigid mRigid" line of code with this:
public: Rigid mRigid; protected:
That allows the rigid body to be exposed to other functions.
Now open up the wheeledvehicle.cc file in the same directory and add this code to the very bottom:
ConsoleMethod(WheeledVehicle, applyRotationImpulse, void, 3, 3, "Spin the car. Usage: applyRotationImpulse(Vector3 rotation axis)")
{
argc;
Point3F force(0, 0, 0);
dSscanf(argv[2],"%g %g %g",&force.x,&force.y,&force.z);
object->mRigid.applyRotationImpulse(force);
}That is the console function that you use to spin the car.
Recompile the engine.
NOW IT IS TIME TO TEST THIS BABY!!!!!
Open up Torque and make the vehicle airborne somehow. You might want to delete the terrain or something so that the vehicle can't land. To test this code, the vehicle must be in the air.
Once that is done, open up the console and type:
%car.applyRotationImpulse(MatrixMulVector(%car.getTransform(), "rotx roty rotz"));
NOTE!!!! You must replace the word %car with the name or ID of the vehicle you are rotating. AND you must replace rotx with the rotation force you want on the x axis, roty with the force on the y axis and rotz for the z axis!
Now hit enter. Your car should now rotate. If it doesn't rotate with a small value, try a higher one.
We had to use the MatrixMulVector so that the rotation axis will stay the same no matter where the car is facing.
I hope you liked this resource. Please leave a comment.
Thanks again to Morrock for the help with these code changes!
About the author
Just your average programmer who tries to finish the projects he starts. :) I am currently focused on creating games with Torque engines. My website is http://phaseshiftsoftware.com
#2
02/21/2009 (1:07 am)
No prob. :)
#3
And you don't need to call MatrixMulVector in script, it will aut-align to the transform for you, that is only if you need to rotate along certain axis in the object's local-space and only like that (as this way it will always re-align the vector for you). I put //+ next to the lines I added.
02/21/2009 (1:34 pm)
Very nice resource.You could also also doConsoleMethod(WheeledVehicle, applyRotationImpulse, void, 3, 3, "Spin the car. Usage: applyRotationImpulse(Vector3 rotation axis)")
{
Point3F force(0, 0, 0);
dSscanf(argv[2],"%g %g %g",&force.x,&force.y,&force.z);
MatrixF transform = object->getTransform(); //+
transform.mulV(force); //+
object->mRigid.applyRotationImpulse(force);
}And you don't need to call MatrixMulVector in script, it will aut-align to the transform for you, that is only if you need to rotate along certain axis in the object's local-space and only like that (as this way it will always re-align the vector for you). I put //+ next to the lines I added.
#4
02/21/2009 (1:36 pm)
Thanks a lot for the extra changes Morrock. Save processing time and your fingers (from having to type so much into the console). :P
#5
02/22/2009 (8:39 am)
Very nice =)
#7
First make sure you recompiled the engine after you made the code changes.
Also make sure that your car is named DefaultCar. DefaultCar is the default datablock, but that might not be the name of the car. To check the name, look at it in the mission editor. The white text over it should tell the name.
BTW, it works perfectly in TGE 1.5.2. If you can't get Morrock's changes to work, just use the default code I posted. His should work though.
03/11/2009 (8:36 am)
@ChrisFirst make sure you recompiled the engine after you made the code changes.
Also make sure that your car is named DefaultCar. DefaultCar is the default datablock, but that might not be the name of the car. To check the name, look at it in the mission editor. The white text over it should tell the name.
BTW, it works perfectly in TGE 1.5.2. If you can't get Morrock's changes to work, just use the default code I posted. His should work though.
#8
Type:
And if you are using Morrock's mod, type this:
That should work. Let me know.
03/11/2009 (8:40 am)
I see your problem. You can't pass the transform as a function argument. You have to multiply the ammount of spin you want on the car by the transform. So instead of typing:DefaultCar.applyRotationImpulse(DefaultCar.getTransform(), "180 180 180");
Type:
DefaultCar.applyRotationImpulse(MatrixMulVector(DefaultCar.getTransform(), "180 180 180"));
And if you are using Morrock's mod, type this:
DefaultCar.applyRotationImpulse("180 180 180");That should work. Let me know.
#10
Can't thank you guys enough for this... my hoverboards and jetbikes are doing Ollies, flips, and 180s like crazy now - $HRED, DEWDZ!
02/08/2010 (5:21 am)
@Quentin y Morrock:Can't thank you guys enough for this... my hoverboards and jetbikes are doing Ollies, flips, and 180s like crazy now - $HRED, DEWDZ!

Torque 3D Owner Frogger