Keep your player on the ground
by Henry Shilling · in Torque X 3D · 04/27/2010 (10:15 pm) · 5 replies
OK I have not seen this posted, however I have seen where some of us have struggled with this. How to keep our objects from sinking slowly into the terrain. The examples are missing a pretty important part, a RigidMaterial in the T3DRigidComponent attached to our guy:
As I put in the comments I am not exactly certain what effect the frictions have, however if you turn them down, your guy will slide down hills. I would imagine if you turn them down and turn them down on the terrain you would not be able to climb steep hills, of course I could be wrong.
As a simple test create a new project simple 3D. You can make one with a gui but you are on your own! Add this code to your Game.cs file. Add the CreatePlayer() method to your BeginRun() method. Everything else here is documented in John's book. If you need more just post.
public void CreatePlayer()
{
//find the physics resolver
RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");
TorqueObject objPlayer = new TorqueObject();
objPlayer.Name = "PlayerObject";
T3DSceneComponent componentScene = new T3DSceneComponent();
componentScene.Position = new Vector3(1220, 990, 218);
componentScene.SceneGroup = "PlayerObject";
objPlayer.Components.AddComponent(componentScene);
// put your own guy in the path
T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
componentRender.ShapeName = @"datashapestorqueorc.dts";
componentRender.SceneGroupName = "PlayerObject";
objPlayer.Components.AddComponent(componentRender);
//give the player some physics attributes, like gravity and collision
T3DRigidComponent componentPhysics = new T3DRigidComponent();
componentPhysics.SceneGroupName = "PlayerObject";
componentPhysics.RenderCollisionBounds = false;
componentPhysics.ResolveCollisions = true;
// this seems to be the one part people miss, need a collision shape
// I use two spheres, like the FPS demo
CollisionSphereShape css1 = new CollisionSphereShape();
css1.Radius = 0.65f;
css1.Center = new Vector3(0f, -0.2f, 0.65f);
componentPhysics.CollisionBody.AddCollisionShape(css1);
CollisionSphereShape css2 = new CollisionSphereShape();
css2.Radius = 0.65f;
css2.Center = new Vector3(0f, 0f, 1.85f);
componentPhysics.CollisionBody.AddCollisionShape(css2);
componentPhysics.RotationScale = 0;
componentPhysics.GravityScale = 25f;
componentPhysics.Mass = 300;
// really important need to attach to the rigid manager
componentPhysics.RigidManager = rigidManager;
// this keeps us from sinkin into the terrain
// the object needs a rigid material. This is what apparently
// gives it the attributes that keep it from sinking.
RigidMaterial rm = new RigidMaterial();
// OK someone who knows more can explain what Restitution etc are.
// I guess Restitution is like softness or bounciness.
// Kinetic friction is friction between moving surfaces, what it means to
// this I do not know. Mess with it. If you change the KineticFriction
// on this and the terrain then maybe you can get hills too steep to climb.
// StaticFriction is similar, however between non moving bodies. One again
// your mileage may vary. If you look at the terrain ti also has a rigid material
// between this and the terrain you can get some physics effects. If you turn down
// the friction settings your dude will slide down hills.
rm.Restitution = 0.5f;
rm.KineticFriction = 0.5f;
rm.StaticFriction = 0.5f;
componentPhysics.RigidMaterial = rm;
objPlayer.Components.AddComponent(componentPhysics);
//add the animation component
T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
componentAnimation.SceneGroupName = "PlayerObject";
objPlayer.Components.AddComponent(componentAnimation);
//add the player movement conmponent
MovementComponent3D componentMovement = new MovementComponent3D();
componentMovement.SceneGroupName = "PlayerObject";
objPlayer.Components.AddComponent(componentMovement);
//add a simple shadow component
T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
componentShadow.SceneGroupName = "PlayerObject";
objPlayer.Components.AddComponent(componentShadow);
TorqueObjectDatabase.Instance.Register(objPlayer);
}As I put in the comments I am not exactly certain what effect the frictions have, however if you turn them down, your guy will slide down hills. I would imagine if you turn them down and turn them down on the terrain you would not be able to climb steep hills, of course I could be wrong.
As a simple test create a new project simple 3D. You can make one with a gui but you are on your own! Add this code to your Game.cs file. Add the CreatePlayer() method to your BeginRun() method. Everything else here is documented in John's book. If you need more just post.
About the author
http://twitter.com/theBigDaddio
#2
04/27/2010 (11:04 pm)
This has been driving all of us mad for a while, this and the terrain thing.
#3
04/28/2010 (1:04 am)
Thanks for this great contribution Henry ;)
#4
I have two questions for you, though:
1. If you are adding your player via the XML Schema (levelData.txscene), How would you go about adding the rigid material you describe into the XML schema for your player's rigid component?
This is what I have so far, and I suspect my player is sinking in (I'm using the RTS Camera Component John describes how to make here
2. Would you recommend adding things via the XML Schema files at all? If not, what do you find is a good workflow/code organization for coding all this stuff up? (i.e. where would you add all of your CreateObject methods, etc?
Any advice is appreciated, thanks for being awesome!
edit: After further investigation, it seems that the line
06/28/2010 (7:58 am)
First of all, thank you for this kind of example Henry! You certainly seem like an invaluable resource to this community.I have two questions for you, though:
1. If you are adding your player via the XML Schema (levelData.txscene), How would you go about adding the rigid material you describe into the XML schema for your player's rigid component?
This is what I have so far, and I suspect my player is sinking in (I'm using the RTS Camera Component John describes how to make here
<TorqueObject type="GarageGames.Torque.Core.TorqueObject" name="PrinterPlayer">
<Components>
<T3DSceneComponent type="GarageGames.Torque.T3D.T3DSceneComponent" name="">
<SceneGroup>PrinterPlayer</SceneGroup>
<Position>
<X>100</X>
<Y>0</Y>
<Z>30</Z>
</Position>
<Scale>
<X>80</X>
<Y>80</Y>
<Z>80</Z>
</Scale>
</T3DSceneComponent>
<T3DTSRenderComponent type="GarageGames.Torque.T3D.T3DTSRenderComponent" name="">
<ShapeName>dataModelsprinterfax.dts</ShapeName>
<SceneGroupName>PrinterPlayer</SceneGroupName>
</T3DTSRenderComponent>
<T3DRigidComponent type="GarageGames.Torque.T3D.T3DRigidComponent">
<SceneGroupName>PrinterPlayer</SceneGroupName>
<RenderCollisionBounds>false</RenderCollisionBounds>
<ResolveCollisions>true</ResolveCollisions>
<CollisionShapes>
<CollisionShape>
<Shape type="GarageGames.Torque.T3D.RigidCollision.CollisionSphereShape">
<Radius>20.0</Radius>
<Center>
<X>0.0</X>
<Y>0.0</Y>
<Z>0.0</Z>
</Center>
</Shape>
</CollisionShape>
</CollisionShapes>
<RotationScale>0.0</RotationScale>
<GravityScale>0.0</GravityScale>
<RigidManager nameRef="RigidManager" />
<RigidMaterial type="GarageGames.Torque.T3D.RigidCollision.RigidMaterial">
<Restitution>0.0</Restitution>
</RigidMaterial>
</T3DRigidComponent>
</Components>2. Would you recommend adding things via the XML Schema files at all? If not, what do you find is a good workflow/code organization for coding all this stuff up? (i.e. where would you add all of your CreateObject methods, etc?
Any advice is appreciated, thanks for being awesome!
edit: After further investigation, it seems that the line
<RigidManager nameRef="RigidManager" />is Causing the problem. If I remove that line, I can get the object to render in the scene again, but I don't know if I can make it move along the ground at all then.
#5
I will take a look later today
06/28/2010 (11:39 am)
You need the rigid manager to keep from falling through the ground. why is your scale so huge? Is your model tiny? One of the secrets to modeling for TX is 1 unit = 1 meter. So a dude is around 2 Meters tall. OK yea that's one tall dude.I will take a look later today
Torque Owner Sharpie