Impulse force w/ rigid body physics?
by Kyle Kronyak · in Torque Game Builder · 02/09/2006 (6:01 pm) · 4 replies
I'm sure I'm not the first to think of this. In fact I believe the TGE engine has a similar feature. What I'd like to do is apply a force to a part of an object. Clearly it works with collisions as seen in the physics demo. However does anyone know of a way of doing this through script without modifying the engine? I could probably simulate this in script by comparing forces and calculating the proper rotation (to set angular velocity and polar impulse) but it would be a huge dirty hack.
My idea to test such a use of rigid-body physics was to have a wide, flat ship with a thruster on either side. One thurster forces the left side up, one forces the right. Use both thrusters and the ship flies straight up. Alternating between the 2 could provide maneuvering.
Any thoughts?
My idea to test such a use of rigid-body physics was to have a wide, flat ship with a thruster on either side. One thurster forces the left side up, one forces the right. Use both thrusters and the ship flies straight up. Alternating between the 2 could provide maneuvering.
Any thoughts?
#2
02/09/2006 (6:18 pm)
I have a similar setup here. What I did was shoot the ship with a stream of small, invisible sceneobjects that can only collide with the ship. It creates a nice illusion of thrust.
#3
I have actually done the code for a vector-based torque force. so far it works great! You can make the object move perfectly straight by hitting it right in the middle. You can make it move and rotate by hitting it off to the side. You can also make it move perfectly straight by hitting 2 separate but equidistant and symmetrical points on the object.
For those who want to play around, here's the code I have so far:
Add this to t2dSceneObject.cc (at the bottom is fine):
Add this to t2dSceneObject.h (I put it directly under the "setImpulseForce" declaration):
Then rebuild the relevant parts of T2D and give it a shot!
invoke it by using a command like:
$player.setImpulseTorqueForce("0 10", "0 0");
this will make the object go straight down
$player.setImpulseTorqueForce("0 10", "3 0");
$player.setImpulseTorqueForce("0 -10", "-3 0");
this will make the object spin around!
I will post more code when I get more of it finished (it's getting late where I'm at but I'll probably crank like a madman on this tomorrow after work)
02/09/2006 (9:42 pm)
I may be impatient but I'm not lazy. I've figured out how to expose 2 very nice features to the console: directly applying a torque force (hitting a specific location on the object, causing both rotation and linear motion) as well as applying an angular force, causing the object to spin.I have actually done the code for a vector-based torque force. so far it works great! You can make the object move perfectly straight by hitting it right in the middle. You can make it move and rotate by hitting it off to the side. You can also make it move perfectly straight by hitting 2 separate but equidistant and symmetrical points on the object.
For those who want to play around, here's the code I have so far:
Add this to t2dSceneObject.cc (at the bottom is fine):
//-----------------------------------------------------------------------------
// Set Impulse Torque Force
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, setImpulseTorqueForce, void, 3, 5, "(forceX / forceY, forcePosX / forcePosY) - Apply a continuous force.")
{
// The force.
t2dVector force;
// The force position.
t2dVector forcePosition;
// Grab the element count.
U32 elementCount1 = t2dSceneObject::getStringElementCount(argv[2]);
U32 elementCount2 = t2dSceneObject::getStringElementCount(argv[3]);
U32 elementCount3 = t2dSceneObject::getStringElementCount(argv[4]);
// ("forceX forceY", [gravitic])
if ((elementCount1 == 2) && (argc < 5))
{
force = t2dSceneObject::getStringElementVector(argv[2]);
if ((elementCount2 == 2) && (argc < 5))
{
forcePosition = t2dSceneObject::getStringElementVector(argv[3]);
}
else if ((elementCount2 == 1) && (argc > 3))
{
forcePosition = t2dVector(dAtof(argv[3]), dAtof(argv[4]));
}
}
// (forceX, forceY, [gravitic])
else if ((elementCount1 == 1) && (argc > 3))
{
force = t2dVector(dAtof(argv[2]), dAtof(argv[3]));
if ((elementCount3 == 2) && (argc < 5))
{
forcePosition = t2dSceneObject::getStringElementVector(argv[4]);
}
else if ((elementCount3 == 1) && (argc > 3))
{
forcePosition = t2dVector(dAtof(argv[4]), dAtof(argv[5]));
}
}
// Invalid
else
{
Con::warnf("t2dSceneObject::setImpulseTorqueForce() - Invalid number of parameters!");
return;
}
// Set Constant Force.
object->setImpulseTorqueForce(force, forcePosition);
}
void t2dSceneObject::setImpulseTorqueForce( const t2dVector& force, const t2dVector& forcePosition )
{
// Get absolute location of force, relative to object
t2dVector objectPosition = getParentPhysics().getPosition() + forcePosition;
// Add Impulse Torque Force to Physics.
getParentPhysics().addGrossTorqueForce( force, objectPosition );
}Add this to t2dSceneObject.h (I put it directly under the "setImpulseForce" declaration):
void setImpulseTorqueForce( const t2dVector& force, const t2dVector& forcePosition );
Then rebuild the relevant parts of T2D and give it a shot!
invoke it by using a command like:
$player.setImpulseTorqueForce("0 10", "0 0");
this will make the object go straight down
$player.setImpulseTorqueForce("0 10", "3 0");
$player.setImpulseTorqueForce("0 -10", "-3 0");
this will make the object spin around!
I will post more code when I get more of it finished (it's getting late where I'm at but I'll probably crank like a madman on this tomorrow after work)
#4
www.garagegames.com/mg/forums/result.thread.php?qt=39869
Hope this helps!
02/09/2006 (10:50 pm)
I have posted my full code including polar torque, and angular force. Please see the link below:www.garagegames.com/mg/forums/result.thread.php?qt=39869
Hope this helps!
Torque Owner Kyle Kronyak