Accessing simple physics features
by Nmuta Jones · in Torque Game Engine · 06/16/2008 (5:23 am) · 16 replies
After reading this post
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5495
I can surmise that
1. Basic physics functions are available in Torque (maybe originally as part of vehicle classes?) and that with some basic mods to engine and scripts you can access them,
2. The most recent version of the engine MAY already have these functions even more accessible than they were in 2004 when this post was created.
So, my question is: how do I access physics functions in Torque now? I am using 1.5.2
BTW , my game is NOT going to use Network features.
Specifically, I want to have a few rolling objects, like a rolling boulder as in the video, but also objects that the player can push.
Both of these are present in the video in the post above, but that was a much earlier version of the engine. So I wanted to check with people and see if there is anything new I should now about in this respect.
thanks in advance.
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5495
I can surmise that
1. Basic physics functions are available in Torque (maybe originally as part of vehicle classes?) and that with some basic mods to engine and scripts you can access them,
2. The most recent version of the engine MAY already have these functions even more accessible than they were in 2004 when this post was created.
So, my question is: how do I access physics functions in Torque now? I am using 1.5.2
BTW , my game is NOT going to use Network features.
Specifically, I want to have a few rolling objects, like a rolling boulder as in the video, but also objects that the player can push.
Both of these are present in the video in the post above, but that was a much earlier version of the engine. So I wanted to check with people and see if there is anything new I should now about in this respect.
thanks in advance.
About the author
Lead Developer for MediaBreeze Multimedia
#2
06/16/2008 (5:46 am)
That's already built into Torque 1.5.2, you just have to use it. I might be wrong about this, but I think one of the starter games comes with a pre-made RigidShape (a boulder) that you could try toying around with.
#3
you're right. It's built into 1.5.2 and there is a boulder that you can play with in the shapes, and yes, it is all accessible thru script.
For the benefit of others reading this post, I am posting a link to the documentation:
http://tdn.garagegames.com/wiki/TorqueGameEngine/RigidShape
06/16/2008 (6:01 am)
@ Nathan: you're right. It's built into 1.5.2 and there is a boulder that you can play with in the shapes, and yes, it is all accessible thru script.
For the benefit of others reading this post, I am posting a link to the documentation:
http://tdn.garagegames.com/wiki/TorqueGameEngine/RigidShape
#4
06/16/2008 (9:27 am)
What game is it in? Is it one of the starters, or the demo? I've forgotten, and I'm to lazy to look right now. =P
#5
you still have to write a small script to set up the boulder as a rigid body, of course, and all that is in the docs:
http://tdn.garagegames.com/wiki/TorqueGameEngine/RigidShape
06/16/2008 (9:51 am)
Its in starter.fps I believe, yes.you still have to write a small script to set up the boulder as a rigid body, of course, and all that is in the docs:
http://tdn.garagegames.com/wiki/TorqueGameEngine/RigidShape
#6
06/16/2008 (10:54 am)
I'm talking about the script. I'm fairly certain that one of the games comes with a prebuilt RigidBody script.
#7
06/16/2008 (11:03 am)
Oh, sorry. Probably. I will look as well.
#8
datablock RigidShapeData( BouncingBoulder )
{
category = "RigidShape";
shapeFile = "~/data/shapes/rocks/boulder.dts";
emap = true;
// Rigid Body
mass = 200;
massCenter = "0 0 0"; // Center of mass for rigid body
massBox = "0 0 0"; // Size of box used for moment of inertia,
// if zero it defaults to object bounding box
drag = 0.2; // Drag coefficient
bodyFriction = 0.2;
bodyRestitution = 0.1;
minImpactSpeed = 5; // Impacts over this invoke the script callback
softImpactSpeed = 5; // Play SoftImpact Sound
hardImpactSpeed = 15; // Play HardImpact Sound
integration = 4; // Physics integration: TickSec/Rate
collisionTol = 0.1; // Collision distance tolerance
contactTol = 0.1; // Contact velocity tolerance
minRollSpeed = 10;
maxDrag = 0.5;
minDrag = 0.01;
triggerDustHeight = 1;
dustHeight = 10;
dragForce = 0.05;
vertFactor = 0.05;
normalForce = 0.05;
restorativeForce = 0.05;
rollForce = 0.05;
pitchForce = 0.05;
};
// Hook into the mission editor.
function RigidShapeData::create(%data)
{
// The mission editor invokes this method when it wants to create
// an object of the given datablock type.
%obj = new RigidShape() {
dataBlock = %data;
};
return %obj;
}
06/16/2008 (11:06 am)
You're right, its in the demo. in server/scripts there is a rigidShapes.cs file included :datablock RigidShapeData( BouncingBoulder )
{
category = "RigidShape";
shapeFile = "~/data/shapes/rocks/boulder.dts";
emap = true;
// Rigid Body
mass = 200;
massCenter = "0 0 0"; // Center of mass for rigid body
massBox = "0 0 0"; // Size of box used for moment of inertia,
// if zero it defaults to object bounding box
drag = 0.2; // Drag coefficient
bodyFriction = 0.2;
bodyRestitution = 0.1;
minImpactSpeed = 5; // Impacts over this invoke the script callback
softImpactSpeed = 5; // Play SoftImpact Sound
hardImpactSpeed = 15; // Play HardImpact Sound
integration = 4; // Physics integration: TickSec/Rate
collisionTol = 0.1; // Collision distance tolerance
contactTol = 0.1; // Contact velocity tolerance
minRollSpeed = 10;
maxDrag = 0.5;
minDrag = 0.01;
triggerDustHeight = 1;
dustHeight = 10;
dragForce = 0.05;
vertFactor = 0.05;
normalForce = 0.05;
restorativeForce = 0.05;
rollForce = 0.05;
pitchForce = 0.05;
};
// Hook into the mission editor.
function RigidShapeData::create(%data)
{
// The mission editor invokes this method when it wants to create
// an object of the given datablock type.
%obj = new RigidShape() {
dataBlock = %data;
};
return %obj;
}
#9
06/16/2008 (11:40 am)
Ha! Told you so! ;-)
#10
06/16/2008 (11:46 am)
True, true.
#11
I am having problems, though, getting it to respond to being pushed by the player. To my player.cs I added:
but the boulder does not budge even one centimeter when pushed. I tried giving it less mass, but still no dice. Any ideas how to get the "pushing" to work?
Thanks.
06/16/2008 (1:46 pm)
I put it in the starter.fps and I'm playing with it now. I like how it hooks right into the Mission Editor. ....so you can create a boulder rigid body right there while in game. I am having problems, though, getting it to respond to being pushed by the player. To my player.cs I added:
function Armor::onCollision(%this,%obj,%col)
{
...
if (%col.getDataBlock().getName() $= "BouncingBoulder") {
// Apply an impulse to the object we collided with
%eye = %obj.getEyeVector();
%vec = vectorScale(%eye, 10);
// Add a vertical component to give the item a better arc
%dot = vectorDot("0 0 1",%eye);
if (%dot < 0)
%dot = -%dot;
%vec = vectorAdd(%vec,vectorScale("0 0 2",1 - %dot));
// Set the object's position and initial velocity
%trans = %col.getTransform();
// Heres the position and rotation.
%pos = getWords(%trans, 0, 2);
%col.applyImpulse(%pos,%vec);
}
...
}but the boulder does not budge even one centimeter when pushed. I tried giving it less mass, but still no dice. Any ideas how to get the "pushing" to work?
Thanks.
#12
That is wayyyy to heavy to push around. Try making it 40 or 50. You'll have to play with the mass and friction to get the rock to roll the way you want it too.
Edit:
Ok, I looked at my script. Here is is in it's entirety:
06/16/2008 (2:46 pm)
Mass = 200;That is wayyyy to heavy to push around. Try making it 40 or 50. You'll have to play with the mass and friction to get the rock to roll the way you want it too.
Edit:
Ok, I looked at my script. Here is is in it's entirety:
datablock RigidShapeData( BouncingBoulder )
{
category = "RigidShape";
shapeFile = "~/data/shapes/boulder/boulder.dts";
emap = true;
// Rigid Body
mass = 100;
massCenter = "0 0 0"; // Center of mass for rigid body
massBox = "0 0 0"; // Size of box used for moment of inertia,
// if zero it defaults to object bounding box
drag = 0.2; // Drag coefficient
bodyFriction = 0.2;
bodyRestitution = 0.1;
minImpactSpeed = 5; // Impacts over this invoke the script callback
softImpactSpeed = 5; // Play SoftImpact Sound
hardImpactSpeed = 15; // Play HardImpact Sound
integration = 4; // Physics integration: TickSec/Rate
collisionTol = 0.1; // Collision distance tolerance
contactTol = 0.1; // Contact velocity tolerance
minRollSpeed = 10;
maxDrag = 0.5;
minDrag = 0.01;
triggerDustHeight = 1;
dustHeight = 10;
dragForce = 0.05;
vertFactor = 0.05;
normalForce = 0.05;
restorativeForce = 0.05;
rollForce = 0.05;
pitchForce = 0.05;
};
function RigidShapeData::create(%data)
{
// The mission editor invokes this method when it wants to create
// an object of the given datablock type.
%obj = new RigidShape() {
dataBlock = %data;
};
return %obj;
}
function RigidShapeData::onCollision(%data, %obj, %col, %vec, %speed)
{
//if it is colliding with it's self or the terrain, ignore it
if(%obj == %col || %col.getType() & $terrainObjectType)
return;
error("RigidShapeData::onCollision %col:" SPC %col.getDataBlock().getName());
error("RigidShapeData::onCollision %obj:" SPC %obj.getDataBlock().getName());
%normal = vectorDot(%speed, vectorNormalize(%speed));
if(%normal > 58)
%scale = %normal / 2;
%objMass = %obj.getDataBlock().mass;
%colVel = vectorLen(%col.getVelocity());
//add some random boost to the Z
%objVec = getWord(%vec, 0) SPC getWord(%vec, 1) SPC mFloor(getRandom(2, 5));
%objImpulse = %objMass * ((%speed / 100) + (%colVel / 15));
//the case is, we don't want to apply an impulse greater then X times the objects mass, so
//clamp it down, otherwise it might visit the moon
%objImpulse = %objImpulse / 8 > %objMass ? %objMass * 8 : %objImpulse;
error("objImpulse:" SPC %objImpulse);
%obj.applyImpulse(%obj.getWorldBoxCenter(), VectorScale(%objVec, %objImpulse));
}You can download the game from here if you want to check it out. I used the resource you linked too in tge 1.4 and made this little game for my grandson. It works.
#13
at the end of the function just to make sure that the collision callback was happening and returning values , and it is. How come when I shoot the ball, it responds, but it simply refuses to accept any impulses from the player??? I'm sure it's a simple problem. I've been playing around with the datablock variables for the boulder but nothing seems to make ANY difference as it relates to the player being able to push the ball when it's not already rolling.
06/16/2008 (2:55 pm)
P.S. I know that the function is being called because I putecho ("you hit the ball. Pos=" @ %pos @ " Vec= "@ %vec) ;at the end of the function just to make sure that the collision callback was happening and returning values , and it is. How come when I shoot the ball, it responds, but it simply refuses to accept any impulses from the player??? I'm sure it's a simple problem. I've been playing around with the datablock variables for the boulder but nothing seems to make ANY difference as it relates to the player being able to push the ball when it's not already rolling.
#14
I am getting some pushing now. THANKS! I just need to tweak the physics parameters.
06/16/2008 (3:00 pm)
Ok, I see, I did not have the function RigidShapeData::onCollision functinon in my code.I am getting some pushing now. THANKS! I just need to tweak the physics parameters.
#15
06/16/2008 (3:02 pm)
That's usually what it is. Glad I could help. :)
#16
08/13/2008 (9:26 am)
Even if I reduce the mass to 40 or 15 - I have to take a big run up and maybe (or mostly not) move the box I have added. It's like the box weighs heaps - sometimes...
Associate Andy Hawkins
DrewFX