Hey, i've been looking at the tutorial provided by microsoft on how to render a 3D model of a spaceship. I have completed it and added a reversing option with left trigger but now i want to make the camera follow the spaceship and to be able to also rotate the camera using the right thumbstick, does anyone know how to do this i will post my code below so you know what i have, any help would be nice.
Thanks
#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
namespace My3DGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Model myModel;
//Position of the model in world space, and rotation
Vector3 modelposition = Vector3.Zero;
float modelrotationX = 0.0f;
//Position of the Camera in world space, for our view matrix
Vector3 camposition = new Vector3(0.0f, 20f, 5000f);
//Aspect ratio to use for the projection matrix
float aspectRatio = 640.0f / 480.0f;
//Velocity of the model, applied each frame to the model's position
Vector3 modelvelocity = Vector3.Zero;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
myModel = content.Load<Model>("Content\\Models\\p1_wedge");
}
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//add velocity to current position
modelposition += modelvelocity;
//bleed off velocity over time
modelvelocity *= 0.95f;
//Get some input
UpdateInput();
base.Update(gameTime);
}
protected void UpdateInput()
{
//get the gamepad state
GamePadState current_state = GamePad.GetState(PlayerIndex.One);
if (current_state.IsConnected)
{
//rotate the model using the left thumbstick, scale it down
modelrotationX -= current_state.ThumbSticks.Left.X * 0.10f;
//create some velocity if the right trigger is down
Vector3 modelvelocity_add = Vector3.Zero;
Vector3 modelvelocity_minus = Vector3.Zero;
//find out what direction we should be thrusting, using rotation
modelvelocity_add.X = -(float)Math.Sin(modelrotationX);
modelvelocity_add.Z = -(float)Math.Cos(modelrotationX);
modelvelocity_minus.X = -(float)Math.Sin(modelrotationX);
modelvelocity_minus.Z = -(float)Math.Cos(modelrotationX);
//now scale our direction by how hard the trigger is down
modelvelocity_add *= current_state.Triggers.Right;
modelvelocity_minus *= current_state.Triggers.Left;
//finally, add this vector to our velocity.
modelvelocity += modelvelocity_add;
modelvelocity -= modelvelocity_minus;
//GamePad.SetVibration(PlayerIndex.One, current_state.Triggers.Right, current_state.Triggers.Right);
//in case you get lost, press A to warp back to the center
if (current_state.Buttons.A == ButtonState.Pressed)
{
modelposition = Vector3.Zero;
modelvelocity = Vector3.Zero;
modelrotationX = 0.0f;
}
}
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
//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)
{
effect.EnableDefaultLighting();
effect.World = Matrix.CreateRotationY(modelrotationX) * mesh.ParentBone.Transform * Matrix.CreateTranslation(modelposition);
effect.View = Matrix.CreateLookAt(camposition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), aspectRatio, 10.0f, 10000.0f);
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}
base.Draw(gameTime);
}
}
}

Help With Model of SpaceShip
MarcoViY
jasonhc
EDIT: i cant seem to add a value to the model position it says cannot add int to float
PsycoDaD
The Tutorials are located at:
XNA Game Studio Express
--Getting Started with XNA Game Studio Express
----Going Beyond: XNA Game Studio Express in 3D
------Tutorial 1 - 3
spree
Donal McWeeney
Thanks for all the help
Frank VDL
I got this to work like so:
// Update cameraCameraTarget = ship.Position;
CameraPosition = CameraTarget + new Vector3((float)Math.Sin(ship.Rotation) * 7000, 2000, (float)Math.Cos(ship.Rotation) * 7000);
The position is relative to the target by the ship's rotation. This is stolen from the 3D tutorial in help. I just reversed the vector it uses to move the ship model forward.
bofey
In order for the camera to follow the spaceship you simply update the camera target and camera position each frame. The camera target will be the position of the space ship and the camera position some distance behind the model.
Note: It won't look like the space ship is moving anymore if you don't have something else drawn in the background as your camera is now moving with the model.