averge joe

Hello, I have two models in my game but I'm currently only using one of them. I want to be able to add velocity in the direction that the model is pointing. I got help from the msdn info pages but they only help you with rotating the model on one axis, the y I believe now I have some code aded to make my model rotate on a different axis, X , to make the model(which i the ship from the spacewar stater kit)go into a dive and go down or pull up and go upwards. heres my code. use the arow keys to bank side to side and forward/back(rotation). use the w,a,s,d keys for movement backwards and forwards and turning left and right, you will get the picture and hopefully a bright individual can help.

#region Using Statements

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

#endregion

public class Game1 : Microsoft.Xna.Framework.Game

{

float adit;

float forward = 0.0f;

float rot = 0.0f;

GraphicsDeviceManager graphics;

GraphicsDeviceManager togglefullscreen;

ContentManager content;

KeyboardState oldState;

bool left = false;

bool right = false;

float axis = 80000f;

float exis = 80000f;

float spun = 0.0f;

float roty = 0.0f;

float camerarotation;

public Game1()

{

Window.AllowUserResizing = true;

graphics = new GraphicsDeviceManager(this);

content = new ContentManager(Services);

oldState = Keyboard.GetState();

}

protected override void Initialize()

{

modelPosition.X = 3000f;

modelPosition.Y = 0.0f;

modelPosition.Z = -5000f;

newmodelposition.X = -3000f;

newmodelposition.Y = 0.0f;

newmodelposition.Z = -5000f;

base.Initialize();

modelRotation = 0.5f;

}

//3d model to draw

Model myModel;

Model newmodel;

protected override void LoadGraphicsContent(bool loadAllContent)

{

if (loadAllContent)

{

newmodel = content.Load<Model>("Content\\Models\\p1_wedge");

myModel = content.Load<Model>("Content\\Models\\p2_saucer");

}

}

protected override void UnloadGraphicsContent(bool unloadAllContent)

{

if (unloadAllContent == true)

{

content.Unload();

}

}

Vector3 modelVelocity = Vector3.Zero;

Vector3 newmodelvelocity = Vector3.Zero;

protected override void Update(GameTime gameTime)

{

if (rotations.Z > 0.0f)

{

rotations.Z = rotations.Z - 0.005f;

}

if (rotations.Z < 0.0f)

{

rotations.Z = rotations.Z + 0.005f;

}

if (rotations.X < 0.0f)

{

rotations.X = rotations.X + 0.005f;

}

if (rotations.X > 0.0f)

{

rotations.X = rotations.X - 0.005f;

}

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

this.Exit();

base.Update(gameTime);

UpdateInput();

// introduce acceleration/velocity

modelPosition += modelVelocity;

newmodelposition += newmodelvelocity;

//bleed off velocity over time

modelVelocity *= 0.95f;

newmodelvelocity *= 0.95f;

}

float angle = 0.019f;

public void UpdateInput()

{

KeyboardState newState = Keyboard.GetState();

MouseState mosu = Mouse.GetState();

if (newState.IsKeyDown(Keys.End))

{

}

if (newState.IsKeyDown(Keys.Delete))

{

}

if (newState.IsKeyDown(Keys.Left))

{

rotations.Z = rotations.Z + 0.01f;

}

if (newState.IsKeyDown(Keys.Right))

{

rotations.Z = rotations.Z - 0.01f;

}

if (newState.IsKeyDown(Keys.D))

{

rotations.Y = rotations.Y - 0.05f;

}

if (newState.IsKeyDown(Keys.A))

{

rotations.Y = rotations.Y + 0.05f;

}

Vector3 modelVelocityAdd = Vector3.Zero;

modelVelocityAdd.X = -(float)Math.Sin(rotations.Y);

modelVelocityAdd.Z = -(float)Math.Cos(rotations.Y);

Vector3 newmodelvelocityadd = Vector3.Zero;

newmodelvelocityadd.X = -(float)Math.Sin(newmodelrotation);

newmodelvelocityadd.Z = -(float)Math.Cos(newmodelrotation);

if (newState.IsKeyDown(Keys.Up))

{

rotations.X = rotations.X + 0.02f;

}

if (newState.IsKeyDown(Keys.Down))

{

rotations.X = rotations.X - 0.02f;

}

if (newState.IsKeyDown(Keys.W))

{

}

if (newState.IsKeyDown(Keys.S))

{

}

oldState = newState;

if (newState.IsKeyDown(Keys.W))

{

modelVelocity += modelVelocityAdd;

modelVelocity += modelVelocityAdd;

modelVelocity += modelVelocityAdd;

modelVelocity += modelVelocityAdd;

}

if (newState.IsKeyDown(Keys.S))

{

modelVelocity -= modelVelocityAdd;

modelVelocity -= modelVelocityAdd;

modelVelocity -= modelVelocityAdd;

}

}

//Position of the model in world space, and rotation

Vector3 modelPosition = Vector3.Zero;

Vector3 newmodelposition = Vector3.Zero;

float modelRotation = 0.0f;

float newmodelrotation = 0.0f;

float modelrotationx = 0.6f;

Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 4000f);

Vector3 cameraangle;

//Position of the Camera in world space, for our view matrix

Vector3 cameraReference = new Vector3(0, 0, 10);

//Aspect ratio to use for the projection matrix

float aspectRatio = 640.0f / 480.0f;

Vector3 rotations;

protected override void Draw(GameTime gameTime)

{

//Copy any parent transforms

Matrix[] transforms = new Matrix[myModel.Bones.Count];

myModel.CopyAbsoluteBoneTransformsTo(transforms);

Matrix[] dude = new Matrix[newmodel.Bones.Count];

newmodel.CopyAbsoluteBoneTransformsTo(dude);

foreach (ModelMesh hesh in newmodel.Meshes)

//Draw the model, a model can have multiple meshes, so loop

foreach (ModelMesh mesh in myModel.Meshes)

{

//This is where the mesh orientation is set, as well as our camera and projection

foreach (BasicEffect effect in mesh.Effects)

foreach (BasicEffect effecd in hesh.Effects)

{

effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);

effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),

aspectRatio, 1.0f, exis);

effect.EnableDefaultLighting();

;

effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationZ(rotations.Z) * Matrix.CreateRotationY(rotations.Y) * Matrix.CreateRotationX(rotations.X) * Matrix.CreateTranslation(modelPosition);

effecd.EnableDefaultLighting();

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

effecd.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);

effecd.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),

aspectRatio, 1.0f, axis);

effecd.World = dude[hesh.ParentBone.Index] * Matrix.CreateRotationY(newmodelrotation)

* Matrix.CreateTranslation(newmodelposition);

}

//Draw the mesh, will use the effects set above.

mesh.Draw();

hesh.Draw();

}

}

}