How to add collision to an asset

From TrainzOnline
(Difference between revisions)
Jump to: navigation, search
(Making the Collision mesh)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
== How to add Collision to an Asset ==
+
== Introduction ==
  
 
Since T:ANE, assets have the ability to have collision meshes for PhysX simulation. By default, Trainz will create an rectangular outline of the mesh using the highest point of the mesh for the top of the collision mesh. As a result, smoke will not react properly.  
 
Since T:ANE, assets have the ability to have collision meshes for PhysX simulation. By default, Trainz will create an rectangular outline of the mesh using the highest point of the mesh for the top of the collision mesh. As a result, smoke will not react properly.  
Line 33: Line 33:
 
When the collision mesh is complete, export it like any other mesh for Trainz. While no specific name is required for the mesh, a good name for it could be mesh_collision.trainzmesh (or im).
 
When the collision mesh is complete, export it like any other mesh for Trainz. While no specific name is required for the mesh, a good name for it could be mesh_collision.trainzmesh (or im).
  
'''TODO'''
+
Here is the mesh table of my asset before I add anything related to the collision. Note here there are several different meshes.
  
== Sample Files ==
 
  
Included in this zip are the .blend and FBX files for the base Thomas body and the collision mesh. Textures are not included but are not necessary for this example.
+
  mesh-table
 +
  {
 +
  default
 +
  {
 +
    auto-create                        1
 +
    mesh                                "mesh\main\tomythomas.lm"
 +
    anim                                "mesh\main\tomythomas_lod0_scene.kin"
 +
    animation-loop-speed                1
 +
  }
 +
  modelface
 +
  {
 +
    mesh                                "mesh\face\modelface.lm"
 +
    auto-create                        0
 +
    att-parent                          "default"
 +
    att                                "a.face"
 +
    anim                                "mesh\face\modelface_lod0_scene.kin"
 +
    animation-loop-speed                0
 +
  }
 +
  cgiface
 +
  {
 +
    mesh                                "mesh\face\cgiface.lm"
 +
    auto-create                        0
 +
    att-parent                          "default"
 +
    att                                "a.face"
 +
    anim                                "mesh\face\cgiface_lod0_scene.kin"
 +
    animation-loop-speed                0
 +
  }
 +
  wheels
 +
  {
 +
    anim                                "mesh\bogey\wheels_lod0_scene.kin"
 +
    animation-loop-speed                0.75
 +
    mesh                                "mesh\bogey\tomythomas.lm"
 +
    auto-create                        1
 +
  }
 +
  }
 +
 
 +
 
 +
I want to specify a collision mesh for "default", so I'm going to add the tag:
 +
 
 +
  collision-mesh                      "collision"
 +
 
 +
Note here the "collision" in quotes is the name of the collision mesh in the mesh-table. It can be called anything you want. Next thing I want to do is add the collision mesh into the mesh-table. I will create a new mesh-table entry with something like this:
 +
 
 +
  collision
 +
  {
 +
    mesh                                "mesh\main\tomythomas_collision.trainzmesh"
 +
  }
 +
 
 +
Now the most important part. We need to specify to Trainz what kind of collision generation for it to create with our mesh. Since we want it to use every triangle in our collision mesh as the collision data, we need to add the following tag:
 +
 
 +
 
 +
  collision-data-generation-mode      "separate-shape-triangle-mesh"
 +
 
 +
 
 +
Our collision mesh entry will now look like this:
 +
 
 +
 
 +
  collision
 +
  {
 +
    mesh                                "mesh\main\tomythomas_collision.trainzmesh"
 +
    collision-data-generation-mode      "separate-shape-triangle-mesh"
 +
  }
 +
 
 +
The last thing to do is disable collision generation for every other mesh, since we don't want it to generate for the other parts of Thomas. We can do this by adding the following tag:
 +
 
 +
  collision-data-generation-mode      "disabled"
 +
 
 +
Here is what the completed mesh table will look like:
 +
 
 +
 
 +
  mesh-table
 +
  {
 +
  default
 +
  {
 +
    auto-create                        1
 +
    mesh                                "mesh\main\tomythomas.lm"
 +
    anim                                "mesh\main\tomythomas_lod0_scene.kin"
 +
    animation-loop-speed                1
 +
    collision-data-generation-mode      "disabled"
 +
    collision-mesh                      "collision"
 +
  }
 +
  collision
 +
  {
 +
    mesh                                "mesh\main\tomythomas_collision.trainzmesh"
 +
    collision-data-generation-mode      "separate-shape-triangle-mesh"
 +
  }
 +
  modelface
 +
  {
 +
    mesh                                "mesh\face\modelface.lm"
 +
    auto-create                        0
 +
    att-parent                          "default"
 +
    att                                "a.face"
 +
    anim                                "mesh\face\modelface_lod0_scene.kin"
 +
    animation-loop-speed                0
 +
    collision-data-generation-mode      "disabled"
 +
  }
 +
  cgiface
 +
  {
 +
    mesh                                "mesh\face\cgiface.lm"
 +
    auto-create                        0
 +
    att-parent                          "default"
 +
    att                                "a.face"
 +
    anim                                "mesh\face\cgiface_lod0_scene.kin"
 +
    animation-loop-speed                0
 +
    collision-data-generation-mode      "disabled"
 +
  }
 +
  wheels
 +
  {
 +
    anim                                "mesh\bogey\wheels_lod0_scene.kin"
 +
    animation-loop-speed                0.75
 +
    mesh                                "mesh\bogey\tomythomas.lm"
 +
    auto-create                        1
 +
    collision-data-generation-mode      "disabled"
 +
  }
 +
  }
 +
 
 +
== Video Demonstration ==

Latest revision as of 13:07, 17 September 2019

Contents

[edit] Introduction

Since T:ANE, assets have the ability to have collision meshes for PhysX simulation. By default, Trainz will create an rectangular outline of the mesh using the highest point of the mesh for the top of the collision mesh. As a result, smoke will not react properly.

DefaultTrainzCollision.png ThomasCollision.png

To rectify this, collision must be disabled completely in the Trainz Settings, or the mesh must have it disabled manually. In this tutorial, we will be adding a collision asset to a KIND_MOCrossing asset, but it can be applied to any asset type in the game. This tutorial will work for FBX file format or legacy IM files.

At the moment, self-collision (smoke generated by a locomotive hitting locomotive collision volumes) is not supported.

[edit] Making the Collision mesh

A collision mesh should be a very simple representation of your asset, with all of the important areas you want to have collision water tight (no gaps in the mesh). This asset should be no more than a couple hundred triangles, with a M.notex material used to prevent extra draw calls.

In this example, I am using a Thomas the tank tunnel. The pink tracks are examples where trains run through the "tunnels" inside Thomas. We will want to make a mesh that contains cut outs for the trains to go through. The holes in this mesh will represent the surface area the particles can bounce off of. Note: The holes in the collision mesh will not prevent a locomotive from entering if it is completely solid. Only particles will react to the collision mesh.

ThomasTrackExample.png

Here is the main body separated from the rest of the asset. The holes represents where we want our smoke particles to be able to hit, but nothing else.

Body.png

Our collision mesh does not include the smoke box area since this part is animated. Notice the faces on the "tunnel" part are facing inward, since we want the smoke particles to hit the top of the "tunnel". Every part of the mesh you'd like to have a particle hit, make sure it is solid on your collision mesh.

CollsionOnly.png NormalDirection.png

In this final picture we can see what our collision mesh looks like overlaid on top of the asset.

CollisionWithBody.png

[edit] Setup In-game

When the collision mesh is complete, export it like any other mesh for Trainz. While no specific name is required for the mesh, a good name for it could be mesh_collision.trainzmesh (or im).

Here is the mesh table of my asset before I add anything related to the collision. Note here there are several different meshes.


 mesh-table
 {
 default
 {
   auto-create                         1
   mesh                                "mesh\main\tomythomas.lm"
   anim                                "mesh\main\tomythomas_lod0_scene.kin"
   animation-loop-speed                1
 }
 modelface
 {
   mesh                                "mesh\face\modelface.lm"
   auto-create                         0
   att-parent                          "default"
   att                                 "a.face"
   anim                                "mesh\face\modelface_lod0_scene.kin"
   animation-loop-speed                0
 }
 cgiface
 {
   mesh                                "mesh\face\cgiface.lm"
   auto-create                         0
   att-parent                          "default"
   att                                 "a.face"
   anim                                "mesh\face\cgiface_lod0_scene.kin"
   animation-loop-speed                0
 }
 wheels
 {
   anim                                "mesh\bogey\wheels_lod0_scene.kin"
   animation-loop-speed                0.75
   mesh                                "mesh\bogey\tomythomas.lm"
   auto-create                         1
 }
 }


I want to specify a collision mesh for "default", so I'm going to add the tag:

 collision-mesh                      "collision"

Note here the "collision" in quotes is the name of the collision mesh in the mesh-table. It can be called anything you want. Next thing I want to do is add the collision mesh into the mesh-table. I will create a new mesh-table entry with something like this:

 collision
 {
   mesh                                "mesh\main\tomythomas_collision.trainzmesh"
 }

Now the most important part. We need to specify to Trainz what kind of collision generation for it to create with our mesh. Since we want it to use every triangle in our collision mesh as the collision data, we need to add the following tag:


 collision-data-generation-mode      "separate-shape-triangle-mesh"


Our collision mesh entry will now look like this:


 collision
 {
   mesh                                "mesh\main\tomythomas_collision.trainzmesh"
   collision-data-generation-mode      "separate-shape-triangle-mesh"
 }

The last thing to do is disable collision generation for every other mesh, since we don't want it to generate for the other parts of Thomas. We can do this by adding the following tag:

 collision-data-generation-mode      "disabled"

Here is what the completed mesh table will look like:


 mesh-table
 {
 default
 {
   auto-create                         1
   mesh                                "mesh\main\tomythomas.lm"
   anim                                "mesh\main\tomythomas_lod0_scene.kin"
   animation-loop-speed                1
   collision-data-generation-mode      "disabled"
   collision-mesh                      "collision"
 }
 collision
 {
   mesh                                "mesh\main\tomythomas_collision.trainzmesh"
   collision-data-generation-mode      "separate-shape-triangle-mesh"
 }
 modelface
 {
   mesh                                "mesh\face\modelface.lm"
   auto-create                         0
   att-parent                          "default"
   att                                 "a.face"
   anim                                "mesh\face\modelface_lod0_scene.kin"
   animation-loop-speed                0
   collision-data-generation-mode      "disabled"
 }
 cgiface
 {
   mesh                                "mesh\face\cgiface.lm"
   auto-create                         0
   att-parent                          "default"
   att                                 "a.face"
   anim                                "mesh\face\cgiface_lod0_scene.kin"
   animation-loop-speed                0
   collision-data-generation-mode      "disabled"
 }
 wheels
 {
   anim                                "mesh\bogey\wheels_lod0_scene.kin"
   animation-loop-speed                0.75
   mesh                                "mesh\bogey\tomythomas.lm"
   auto-create                         1
   collision-data-generation-mode      "disabled"
 }
 }

[edit] Video Demonstration

Personal tools