Mine Model is breaking when i am moving it why is it so ... please help
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Avtars
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Matrix world=Matrix.Identity;
Model sphere;
Model ground;
SpriteFont sf;
String str = "";
Matrix []bones;
Matrix []original;
int index = 0;
Song song;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
sphere = this.Content.Load<Model>("tank");
sf = this.Content.Load<SpriteFont>("SpriteFont1");
bones = new Matrix[sphere.Bones.Count];
original = new Matrix[sphere.Bones.Count];
sphere.CopyAbsoluteBoneTransformsTo(original);
song=this.Content.Load<Song>("videoplayback");
ground = this.Content.Load<Model>("Ground");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
MediaPlayer.Stop();
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Right)) {
sphere.CopyAbsoluteBoneTransformsTo(bones);
index = sphere.Bones["l_back_wheel_geo"].Index;
mediaplayer();
sphere.Bones[index].Transform = Matrix.CreateRotationX(0.1f) * sphere.Bones[index].Transform;
index = sphere.Bones["r_back_wheel_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationX(0.1f) * sphere.Bones[index].Transform;
index = sphere.Bones["l_front_wheel_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationX(0.1f) * sphere.Bones[index].Transform;
index = sphere.Bones["r_front_wheel_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationX(0.1f) * sphere.Bones[index].Transform;
index = sphere.Bones["l_front_wheel_geo"].Index;
str = "pressed Backward";
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
sphere.CopyAbsoluteBoneTransformsTo(bones);
index = sphere.Bones["l_back_wheel_geo"].Index;
sphere.Bones[index].Transform =Matrix.Identity* Matrix.CreateRotationX(-0.1f) * sphere.Bones[index].Transform*Matrix.CreateTranslation(0,0,1);
index = sphere.Bones["r_back_wheel_geo"].Index;
sphere.Bones[index].Transform =Matrix.Identity* Matrix.CreateRotationX(-0.1f) * sphere.Bones[index].Transform*Matrix.CreateTranslation(0,0,1);
index = sphere.Bones["l_front_wheel_geo"].Index;
sphere.Bones[index].Transform = Matrix.Identity*Matrix.CreateRotationX(-0.1f) * sphere.Bones[index].Transform*Matrix.CreateTranslation(0,0,1);
index = sphere.Bones["r_front_wheel_geo"].Index;
sphere.Bones[index].Transform =Matrix.Identity* Matrix.CreateRotationX(-0.1f) * sphere.Bones[index].Transform;
index = sphere.Bones["l_front_wheel_geo"].Index;
world =world* Matrix.CreateTranslation(0, 0, 1);
index = sphere.Bones["l_steer_geo"].Index;
sphere.Bones[index].Transform = sphere.Bones[index].Transform*Matrix.CreateTranslation(0,0,1);
index = sphere.Bones["turret_geo"].Index;
sphere.Bones[index].Transform =sphere.Bones[index].Transform*Matrix.CreateTranslation(0,0,1);
str = "pressed Forward";
mediaplayer();
}
else if (Keyboard.GetState().IsKeyDown(Keys.Q)) {
world = world* Matrix.CreateRotationX(0.1f);
str = "Rotation ON Positive X Axis";
}
else if (Keyboard.GetState().IsKeyDown(Keys.W)) {
world = world* Matrix.CreateRotationX(-0.1f);
str = "Rotation on Negative X Axis";
}else if(Keyboard.GetState().IsKeyDown(Keys.Enter)){
world = Matrix.Identity;
mediaplayer();
}
else if(Keyboard.GetState().IsKeyDown(Keys.Up)){
world = world* Matrix.CreateRotationZ(0.1f);
str = "Rotating on Y Axis";
}
else if (Keyboard.GetState().IsKeyDown(Keys.Down)) {
world = world * Matrix.CreateRotationZ(-0.1f);
str = "Rotating on Negative Yaxis";
}
else if (Keyboard.GetState().IsKeyDown(Keys.E)) {
index = sphere.Bones["l_steer_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(0.1f) * sphere.Bones[index].Transform;
str = "Steering Right";
}
else if (Keyboard.GetState().IsKeyDown(Keys.R))
{
index = sphere.Bones["l_steer_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(-0.1f) * sphere.Bones[index].Transform;
str = "Steering Left";
}
else if (Keyboard.GetState().IsKeyDown(Keys.Y))
{
index = sphere.Bones["turret_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(0.1f) * sphere.Bones[index].Transform;
str = "Turrent Right";
}
else if (Keyboard.GetState().IsKeyDown(Keys.T))
{
index = sphere.Bones["turret_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(-0.1f) * sphere.Bones[index].Transform;
str = "Turrent Left";
}
else if (Keyboard.GetState().IsKeyDown(Keys.I) ){
index = sphere.Bones["canon_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(0.1f) * sphere.Bones[index].Transform;
str = "Cannon Left Pressed";
}
else if (Keyboard.GetState().IsKeyDown(Keys.O))
{
index = sphere.Bones["canon_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(-0.1f) * sphere.Bones[index].Transform;
str = "Cannon Right Pressed";
}
else if (Keyboard.GetState().IsKeyDown(Keys.P))
{
index = sphere.Bones["hatch_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(0.1f) * sphere.Bones[index].Transform;
str = "Cannon Left Pressed";
}
else if (Keyboard.GetState().IsKeyDown(Keys.Insert))
{
index = sphere.Bones["hatch_geo"].Index;
sphere.Bones[index].Transform = Matrix.CreateRotationY(-0.1f) * sphere.Bones[index].Transform;
str = "Cannon Right Pressed";
}
else
{
if (MediaPlayer.State == MediaState.Playing) {
MediaPlayer.Pause();
}
str = "tank game";
}
// TODO: Add your update logic here
base.Update(gameTime);
}
private void mediaplayer()
{
if (MediaPlayer.State == MediaState.Stopped)
{
MediaPlayer.Play(song);
MediaPlayer.IsRepeating = true;
}
else if (MediaPlayer.State == MediaState.Paused)
{
MediaPlayer.Stop();
MediaPlayer.Resume();
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix mi = Matrix.CreateLookAt(new Vector3(1000, 500, 0),
new Vector3(0, 150, 0),
Vector3.Up);
// TODO: Add your initialization code here
List<ModelBone> si = sphere.Bones.ToList();
spriteBatch.Begin();
spriteBatch.DrawString(sf,str,new Vector2(0,0),Color.CornflowerBlue);
Matrix identity = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.DisplayMode.AspectRatio, 20.0f, 1000.0f);
sphere.CopyBoneTransformsTo(bones);
this.Window.Title = str;
sphere.Root.Transform=world;
foreach (ModelMesh mesh in sphere.Meshes) {
foreach (Effect es in mesh.Effects) {
BasicEffect bs = es as BasicEffect;
bs.EnableDefaultLighting();
bs.World = world;
}
}
ground.Draw(world, mi, identity);
sphere.Draw(world, mi, identity);
spriteBatch.End();
}
}
}