XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

JigLibX model collision skin

Last post 9/17/2008 1:49 AM by Aaron Dale. 5 replies.
  • 9/16/2008 1:43 AM

    JigLibX model collision skin

    Hey guys,

     I've recently started using JigLibX to handle the physics part of a game a couple friends and I are developing as a school project.

    I'm looking to make a collision-skin layer to my model to use for the collision instead of the actual detail mesh that's going to be rendered. The concept of making a simpler mesh to calculate the collisions with makes great sense to me, except I just can't seem to figure out how to actually use that mesh as the body's collision skin with JigLibX. I've heard a couple things tossed around like using a custom model importer to get the vertices, use triangle mesh for the collision skin, but I'm not quite sure where to start.

    I'm sure many of you know theres not much documentation on this yet. Everything is going good so far, but this is my one snag right now.

    Thanks!
    Aaron

  • 9/16/2008 5:38 AM In reply to

    Re: JigLibX model collision skin

    This really isnt the place to ask this question, I would ask it at their forums at jiglibx.wikidot.com.

    However, I will answer your question as best I can. What you would want to do is load the simpler mesh in using their "VertexProcessor". Then you would create a TriangleMesh using this model. (The TriangleMeshObject in the source code shows how to do this). From there, you would create a body, and add that TriangleMesh to the body's CollisionSkin. You would also load your higher detail mesh using the model processor xna provides, and when you draw it you would use the body's Position and Orientation.

    You should note that the TriangleMesh can't move. Other objects can collide with it, but it won't move around. This makes it useful for things like level geometry, but not much else.

  • 9/16/2008 6:07 PM In reply to

    Re: JigLibX model collision skin

    I figured this wasn't the place for the question, but since this forum gets more traffic than the JigLibX forums, I figured it couldn't hurt.

     I wasn't aware that the triangle mesh had to remain stationary. At least that will provide a solution with which to build buildings and what not for my environment. There must be a way though to make a collision skin out of a model that will move with the body, if not, I suppose I'm stuck with making a collision skin out of primitives the way they do it with two cubes for the car in the demo game.

    I just figured there had to be a better method

    Thanks
  • 9/16/2008 10:22 PM In reply to

    Re: JigLibX model collision skin

    Their VertexProcessor is not needed; you can get the same data from the Model at runtime, keeping the built XNB file smaller.

    The easiest way to use a different mesh for collision than for rendering is to have two "Mesh" properties on your game entity. Have a "RenderMesh" property and a "CollisionMesh" property, and load the two models, and use the collision mesh when creating the Mesh primitive to add to the skin for the entity.


    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/17/2008 12:19 AM In reply to

    Re: JigLibX model collision skin

    I'm not sure I know what you mean exactly, well I do, I'm just not exactly sure how to execute this.

    I'm doing some experimentation right now with the JigLibX triangle mesh and using their "ExtractData()" method in the TriangleMeshObject class, and I'm hoping I will be successful. Right now it looks like it's colliding with the other objects and moving with the model. I'm just not sure if the collisions are accurrate. I'll post again with my findings :D


  • 9/17/2008 1:49 AM In reply to

    Re: JigLibX model collision skin

    Ok So thus far I have my "wedge" shape colliding somewhat with the world. I have some cubes that I've verified are working properly, and I'm trying to get this wedge to work with the cubes and act like a wedge should (flat on bottom and sides, with a sloped surface on the front, cubes should fall onto it and slide down the slope). Right now if a cube falls on the wedge it doesn't seem to interact in the slightest with the sloped surface. The boxes/cubes also seem to be selective on when and where they want to collide with the sides of my wedge. Sometimes they will be affected by it, other times they will just go through it.

    I know it's wishful thinking, hoping for an answer to a JigLibX question here, but I might as well try. Here's my code for my wedge object:

    I also used to have the physics creation code in the constructor. I moved it to the load content method since thats where the model comes in, it's temporary until I get this sorted out.

    1 using System; 
    2 using System.Collections.Generic; 
    3 using Microsoft.Xna.Framework; 
    4 using Microsoft.Xna.Framework.Graphics; 
    5 using JigLibX.Math; 
    6 using JigLibX.Physics; 
    7 using JigLibX.Geometry; 
    8 using JigLibX.Collision; 
    9  
    10 namespace Tanks.Game.Tanks 
    11
    12     class Tank : DrawableGameComponent 
    13     { 
    14         protected Vector3 position; 
    15         protected Vector3 scale; 
    16  
    17         protected Texture2D collisionTexture; 
    18  
    19         protected Model model; 
    20  
    21         protected Body body; 
    22         public Body Body 
    23         { 
    24             get { return body; } 
    25

     

            } 
    26  
    27         protected CollisionSkin skin; 
    28         public CollisionSkin Skin 
    29         { 
    30             get { return skin; } 
    31         } 
    32  
    33         protected TriangleMesh triangleMesh; 
    34  
    35         public Tank(Microsoft.Xna.Framework.Game game, Vector3 position, Vector3 scale) 
    36             : base(game) 
    37         { 
    38             this.position = position; 
    39             this.scale = scale; 
    40         } 
    41  
    42         protected override void LoadContent() 
    43         { 
    44             model = Game.Content.Load<Model>("Models/wedge"); 
    45  
    46             collisionTexture = Game.Content.Load<Texture2D>("Textures/Models/collision"); 
    47  
    48             body = new Body(); 
    49             skin = new CollisionSkin(body); 
    50  
    51             body.CollisionSkin = skin; 
    52  
    53             triangleMesh = new TriangleMesh(); 
    54  
    55             List<Vector3> vertexList = new List<Vector3>(); 
    56             List<TriangleVertexIndices> indexList = new List<TriangleVertexIndices>(); 
    57  
    58             ExtractModelData(vertexList, indexList, model); 
    59  
    60             triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); 
    61  
    62             skin.AddPrimitive(triangleMesh, 1, new MaterialProperties(0.8f, 0.7f, 0.6f)); 
    63  
    64             Vector3 com = setMass(10.0f); 
    65  
    66             body.MoveTo(position, Matrix.Identity); 
    67  
    68             skin.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); 
    69  
    70             body.EnableBody(); 
    71         } 
    72  
    73         private Matrix getWorldMatrix() 
    74         { 
    75             return Matrix.CreateScale(scale) * skin.GetPrimitiveLocal(0).Transform.Orientation * body.Orientation * Matrix.CreateTranslation(body.Position); 
    76         } 
    77  
    78         public override void Draw(GameTime gameTime) 
    79         { 
    80             Game1 game = (Game1)Game; 
    81  
    82             Matrix[ transforms = new Matrix[model.Bones.Count]; 
    83             model.CopyAbsoluteBoneTransformsTo(transforms); 
    84  
    85             Matrix worldMatrix = getWorldMatrix(); 
    86  
    87             foreach (ModelMesh mesh in model.Meshes) 
    88             { 
    89                 foreach (BasicEffect effect in mesh.Effects) 
    90                 { 
    91                     effect.Texture = collisionTexture;      // Assign the texture 
    92                     effect.TextureEnabled = true;           // Enable drawing the texture 
    93                     effect.EnableDefaultLighting(); 
    94                     effect.PreferPerPixelLighting = true
    95                     effect.World = transforms[mesh.ParentBone.Index] 
    96                                  * worldMatrix; 
    97                     effect.View = game.View; 
    98                     effect.Projection = game.Projection; 
    99                 } 
    100                 mesh.Draw(); 
    101             } 
    102         } 
    103  
    104         public override void Update(GameTime gameTime) 
    105         { 
    106             base.Update(gameTime); 
    107         } 
    108  
    109         protected Vector3 setMass(float mass) 
    110         { 
    111             PrimitiveProperties primitiveProperties = new PrimitiveProperties(PrimitiveProperties.MassDistributionEnum.Solid,  
    112                                                                               PrimitiveProperties.MassTypeEnum.Mass,  
    113                                                                               mass); 
    114  
    115             float junk; 
    116             Vector3 com; 
    117             Matrix it; 
    118             Matrix itCoM; 
    119  
    120             Skin.GetMassProperties(primitiveProperties, out junk, out com, out it, out itCoM); 
    121  
    122             Body.BodyInertia = itCoM; 
    123             Body.Mass = junk; 
    124  
    125             return com; 
    126         } 
    127  
    128         protected void ExtractModelData(List<Vector3> vertices, List<TriangleVertexIndices> indices, Model model) 
    129         { 
    130             Matrix[ bones_ = new Matrix[model.Bones.Count]; 
    131             model.CopyAbsoluteBoneTransformsTo(bones_); 
    132             foreach (ModelMesh mm in model.Meshes) 
    133             { 
    134                 Matrix xform = bones_[mm.ParentBone.Index]; 
    135                 foreach (ModelMeshPart mmp in mm.MeshParts) 
    136                 { 
    137                     int offset = vertices.Count; 
    138                     Vector3[ a = new Vector3[mmp.NumVertices]; 
    139                     mm.VertexBuffer.GetData<Vector3>(mmp.StreamOffset + mmp.BaseVertex * mmp.VertexStride, 
    140                         a, 0, mmp.NumVertices, mmp.VertexStride); 
    141                     for (int i = 0; i != a.Length; ++i) 
    142                         Vector3.Transform(ref a[i], ref xform, out a[i]); 
    143                     vertices.AddRange(a); 
    144  
    145                     if (mm.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) 
    146                         throw new Exception( 
    147                             String.Format("Model uses 32-bit indices, which are not supported.")); 
    148                     short[ s = new short[mmp.PrimitiveCount * 3]; 
    149                     mm.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); 
    150                     JigLibX.Geometry.TriangleVertexIndices[ tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; 
    151                     for (int i = 0; i != tvi.Length; ++i) 
    152                     { 
    153                         tvi[i].I0 = s[i * 3 + 2] + offset; 
    154                         tvi[i].I1 = s[i * 3 + 1] + offset; 
    155                         tvi[i].I2 = s[i * 3 + 0] + offset; 
    156                     } 
    157                     indices.AddRange(tvi); 
    158                 } 
    159             } 
    160         } 
    161     } 
    162
    163  

Page 1 of 1 (6 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG