| 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 |
|