Norbo . first, I add the hull of the model to the space not the model it self !! and send the hull.world transform to offset the model when draw .
second , dose I have to update the transform matrix used to offset the model every time I draw the model with convex hull ?
Code: Select all
public ModelDrawer ModelDrawer;
//private ModelGraphics AM_GraphicsModelGraphics;
private List<ModelGraphics> models = new List<ModelGraphics>();
public Space space = new Space();
//Model CubeModel;
CharacterControllerInput cci;
GraphicsDevice graphicsDevice;
Game game;
// Load Models ,Charachter controller
public ModelManager(Game game , GraphicsDevice graphicdevice)
{
ModelDrawer = new BruteModelDrawer(game);
this.game = game;
this.graphicsDevice = graphicdevice;
}
public void LoadContent(ContentManager Content , Camera camera)
{
ConvexHull hull;
//CubeModel = Content.Load<Model>("Models/Level1/cube");
//-----------------------------
//--Level 1
//-----------------------------
Model Leve1 = Content.Load<Model>("Models/Level1/playground");
Vector3[] vertices;
int[] indices;
TriangleMesh.GetVerticesAndIndicesFromModel(Leve1, out vertices, out indices);
StaticMesh meshh = new StaticMesh(vertices, indices, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(0, -500, 0)));
// Add Hull
hull = new ConvexHull(vertices, 10);
space.Add(hull);
// Make it visible too.
// Physics engine require to not change the information about Model he store
// or if changed he will not reqoqnized of the model
//models.Add(new ModelGraphics(Leve1, hull.WorldTransform));
StaticModel m = new StaticModel(Content.Load<Model>("Models/Level1/playground"), hull.WorldTransform, game);
//-----------------------------
//--container
//-----------------------------
//Create a physical environment from a triangle mesh.
//First, collect the the mesh data from the model using a helper function.
//This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
//friction/bounciness data.
//The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
Vector3[] vertices2;
int[] indices2;
TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/container"), out vertices2, out indices2);
//Give the mesh information to a new StaticMesh.
//Give it a transformation which scoots it down below the kinematic box entity we created earlier.
var mesh3 = new StaticMesh(vertices2, indices2, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(-200, 0, 0)));
// Add hull
hull = new ConvexHull(vertices2, 10);
//Add it to the space!
space.Add(hull);
// Make it visible to
models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/container"), hull.WorldTransform));
//-----------------------------
//--chair
//-----------------------------
//Create a physical environment from a triangle mesh.
//First, collect the the mesh data from the model using a helper function.
//This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
//friction/bounciness data.
//The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
Vector3[] vertices3;
int[] indices3;
TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/chair"), out vertices3, out indices3);
//Give the mesh information to a new StaticMesh.
//Give it a transformation which scoots it down below the kinematic box entity we created earlier.
var mesh4 = new StaticMesh(vertices3, indices3, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(-200, 0, 0)));
// Add hull
hull = new ConvexHull(vertices3, 10);
//Add it to the space!
space.Add(hull);
// Make it visible to
models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/chair"), hull.WorldTransform));
//-----------------------------
//--chair2
//-----------------------------
//Create a physical environment from a triangle mesh.
//First, collect the the mesh data from the model using a helper function.
//This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
//friction/bounciness data.
//The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
Vector3[] vertices4;
int[] indices4;
TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/chair2"), out vertices4, out indices4);
//Give the mesh information to a new StaticMesh.
//Give it a transformation which scoots it down below the kinematic box entity we created earlier.
var mesh5 = new StaticMesh(vertices4, indices4, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(-200, 0, 0)));
// Add hull
hull = new ConvexHull(vertices4, 10);
//Add it to the space!
space.Add(hull);
// Make it visible to
models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/chair2"), hull.WorldTransform));
//-----------------------------
//--sphere
//-----------------------------
//Create a physical environment from a triangle mesh.
//First, collect the the mesh data from the model using a helper function.
//This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
//friction/bounciness data.
//The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
Vector3[] vertices5;
int[] indices5;
TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/sphere"), out vertices5, out indices5);
//Give the mesh information to a new StaticMesh.
//Give it a transformation which scoots it down below the kinematic box entity we created earlier.
var mesh6 = new StaticMesh(vertices5, indices5, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(-200, 0, 0)));
// Add hull
hull = new ConvexHull(vertices5, 10);
//Add it to the space!
space.Add(hull);
// Make it visible to
models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/sphere"), hull.WorldTransform));
//-----------------------------
//--stairs
//-----------------------------
//Create a physical environment from a triangle mesh.
//First, collect the the mesh data from the model using a helper function.
//This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
//friction/bounciness data.
//The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
Vector3[] vertices6;
int[] indices6;
TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/stairs"), out vertices6, out indices6);
//Give the mesh information to a new StaticMesh.
//Give it a transformation which scoots it down below the kinematic box entity we created earlier.
var mesh7 = new StaticMesh(vertices6, indices6, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(-200, 0, 0)));
// Add hull
hull = new ConvexHull(vertices6, 10);
//Add it to the space!
space.Add(hull);
// Make it visible to
models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/stairs"), hull.WorldTransform));
// Intialize the Chrachter Controller
cci = new CharacterControllerInput(space, camera);
cci.Activate();
cci.CharacterController.HorizontalMotionConstraint.Speed *= 10;
cci.CharacterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
cci.CharacterController.HorizontalMotionConstraint.MaximumForce = 2500;
cci.CharacterController.HorizontalMotionConstraint.MaximumAirForce = 2500;
cci.CharacterController.StepManager.MaximumStepHeight = 100;
// Add some entities to space
for (int i = 1; i < 10; i++) {
space.Add(new Box(new Vector3(0, (i * 10) + 40, 0), 5, 5, 0.5f, 1));
space.Add(new Sphere(new Vector3(2, (i * 10) + 100, 101), 1, 1));
}
foreach (Entity e in space.Entities){
ModelDrawer.Add(e);
}
space.ForceUpdater.Gravity = new Vector3(0, -40.81f, 0);
}
public void Update(GameTime gameTime, KeyboardState KeyboardInputt, KeyboardState PreviousKeyboardInputt)
{
cci.Update((float)gameTime.ElapsedGameTime.TotalSeconds, PreviousKeyboardInputt, KeyboardInputt);
ModelDrawer.Update();
space.Update();
}
public void Draw(Matrix View , Matrix projection) {
graphicsDevice.RasterizerState = RasterizerState.CullClockwise;
foreach(ModelGraphics modelll in this.models)
{
modelll.Draw(View, projection);
}
//AM_GraphicsModelGraphics.Draw(ModelDrawer, View, projection);
}