Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:54:13+00:00 2026-06-14T21:54:13+00:00

I have created 3 classes: one for the game, one for the model and

  • 0

I have created 3 classes: one for the game, one for the model and one for the camera. I want my camera to follow my model as it moves, but my model is no longer appearing. Does anyone have any idea what I’m doing wrong in my camera and/or game class?


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 test1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;



    //Visual components
    Ship ship = new Ship();
    Cam cam1 = new Cam();        




    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }      

    protected override void LoadContent()
    {
        ship.Model = Content.Load<Model>("Models/p1_wedge");
        ship.Transforms = cam1.SetupEffectDefaults(ship.Model);            
    }

    protected override void Update(GameTime gameTime)
    {

        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
            Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        // Get some input.
        UpdateInput();

        // Add velocity to the current position.
        ship.Position += ship.Velocity;
        cam1.cameraPosition += cam1.cameraTarget;
        // Bleed off velocity over time.
        ship.Velocity *= 0.95f;
        cam1.cameraTarget *= 0.95f;

        base.Update(gameTime);
    }

    protected void UpdateInput()
    {
        // Get the game pad state.
        GamePadState currentState = GamePad.GetState(PlayerIndex.One);
        KeyboardState currentKeyState = Keyboard.GetState();

            ship.Update(currentState);
            cam1.Update(currentState);

            // In case you get lost, press A to warp back to the center.
            if (currentState.Buttons.A == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.Enter))
            {
                ship.Position = Vector3.Zero;
                ship.Velocity = Vector3.Zero;
                ship.Rotation = 0.0f;                  
            }            
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        Matrix shipTransformMatrix = ship.RotationMatrix
                * Matrix.CreateTranslation(ship.Position);
        DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
        base.Draw(gameTime);
    }


    public static void DrawModel(Model model, Matrix modelTransform,
Matrix[] absoluteBoneTransforms)
    {
        //Draw the model, a model can have multiple meshes, so loop
        foreach (ModelMesh mesh in model.Meshes)
        {
            //This is where the mesh orientation is set
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.World =
                    absoluteBoneTransforms[mesh.ParentBone.Index] *
                    modelTransform;
            }
            //Draw the mesh, will use the effects set above.
            mesh.Draw();
        }
    }
}
}

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace test1
{
class Ship
{
    public Model Model;
    public Matrix[] Transforms;

    //Position of the model in world space
    public Vector3 Position = Vector3.Zero;

    //Velocity of the model, applied each frame to the model's position
    public Vector3 Velocity = Vector3.Zero;
    private const float VelocityScale = 5.0f;

    public Matrix RotationMatrix =
 Matrix.CreateRotationX(MathHelper.PiOver2);

    private float rotation;

    public float Rotation
    {
        get { return rotation; }
        set
        {
            float newVal = value;
            while (newVal >= MathHelper.TwoPi)
            {
                newVal -= MathHelper.TwoPi;
            }
            while (newVal < 0)
            {
                newVal += MathHelper.TwoPi;
            }

            if (rotation != value)
            {
                rotation = value;
                RotationMatrix =
                    Matrix.CreateRotationX(MathHelper.PiOver2) *
                    Matrix.CreateRotationZ(rotation);
            }

        }
    }

    public void Update(GamePadState controllerState)
    {

        KeyboardState currentKeyState = Keyboard.GetState();
        if (currentKeyState.IsKeyDown(Keys.A))
            Rotation += 0.10f;
        else
        // Rotate the model using the left thumbstick, and scale it down.
        Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

        if (currentKeyState.IsKeyDown(Keys.D))
            Rotation -= 0.10f;

        if (currentKeyState.IsKeyDown(Keys.W))
            Velocity += RotationMatrix.Forward * VelocityScale;
        else
        // Finally, add this vector to our velocity.
        Velocity += RotationMatrix.Forward * VelocityScale *
         controllerState.Triggers.Right;
    }  

}
}

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace test1
{
class Cam : Microsoft.Xna.Framework.Game
{
    public Vector3 cameraPosition = new Vector3(0.0f, 2000.0f, 25000.0f);
    public Vector3 cameraTarget = Vector3.Zero;

   Matrix projectionMatrix;
    Matrix viewMatrix;

    protected override void Initialize(){

            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
       MathHelper.ToRadians(45.0f),
       GraphicsDevice.DisplayMode.AspectRatio,
       20000.0f, 30000.0f);

        viewMatrix = Matrix.CreateLookAt(cameraPosition,
            cameraTarget, Vector3.Up);

        base.Initialize();            
    }

    public Matrix[] SetupEffectDefaults(Model myModel)
    {
        Matrix[] absoluteTransforms = new Matrix[myModel.Bones.Count];
        myModel.CopyAbsoluteBoneTransformsTo(absoluteTransforms);

        foreach (ModelMesh mesh in myModel.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();
                effect.Projection = projectionMatrix;
                effect.View = viewMatrix;
            }
        }
        return absoluteTransforms;
    }

    private const float VelocityScale = 5.0f;

    public Matrix RotationMatrix =
 Matrix.CreateRotationX(MathHelper.PiOver2);

    private float rotation;

    public float Rotation
    {
        get { return rotation; }
        set
        {
            float newVal = value;
            while (newVal >= MathHelper.TwoPi)
            {
                newVal -= MathHelper.TwoPi;
            }
            while (newVal < 0)
            {
                newVal += MathHelper.TwoPi;
            }

            if (rotation != value)
            {
                rotation = value;
                RotationMatrix =
                    Matrix.CreateRotationX(MathHelper.PiOver2) *
                    Matrix.CreateRotationZ(rotation);
            }
        }
    }

    public void Update(GamePadState controllerState)
    {
        KeyboardState currentKeyState = Keyboard.GetState();
        if (currentKeyState.IsKeyDown(Keys.A))
            Rotation += 0.10f;
        else
            // Rotate the model using the left thumbstick, and scale it down.
            Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

        if (currentKeyState.IsKeyDown(Keys.D))
            Rotation -= 0.10f;

        if (currentKeyState.IsKeyDown(Keys.W))
            cameraTarget += RotationMatrix.Forward * VelocityScale;
        else
            // Finally, add this vector to our velocity.
            cameraTarget += RotationMatrix.Forward * VelocityScale *
             controllerState.Triggers.Right;
    }
}
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:54:15+00:00Added an answer on June 14, 2026 at 9:54 pm

    First, you should learn how to organize your code, you created classes for each object but you still do some of the processing in each object and some in game, this is very messy!

    The short answer to why your camera is not working, is simply because you are not using it!

    You should follow a proper tutorial on creating a camera in XNA, like this one, but from looking briefly at your code, adding something like cam1.SetupEffectDefaults(model); in your DrawModel might be sufficient to have your ViewMatrix and ProjectionMatrix applied. Calling it in the init phase won’t do much, as these matrices are updated during the game.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created some C++ classes to model a Solitaire game as a learning
I have created two viewController classes such that one is superclass of another.i have
I have created N classes that take from one to N ints into constructors
I have a button in one of my classes that i create it with
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created 2 classes as follows... public class A extends JFrame { public
I have created a jar file of few of my classes and preverified them
Say I have two classes created work and workItem. CWorker *work = new CWorker();
I have created a project that uses scala that has some abstract classes definitions
After I have created a serious bunch of classes (with initialize methods), I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.