Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So, I have three classes . Game1,GameModel and GameCameraFPS

Here are the codes:

public class Game1 : Microsoft.Xna.Framework.Game
{
    public static GameWindow window;
    GraphicsDeviceManager graphics;
    SpriteBatch spritebatch;
    public static GameModel myModel;
    Copy1 mymodel1;
    //GameCameraFixed myCamera;
    public static GameCameraFPS mycamera;
    Vector3 campos;
    //GameCameraFPS2 camera;
    public static Texture2D btnNormal, btnOver, cursor, fridge, chicken, milk, pizza, fridge2;
    MouseState mouseState;
    int mousex;
    int mousey;
    SpriteFont myfont;
    GameCameraFPS cameraPosition;




    //GameSkybox myskybox;


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




    }

    /// <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);

        mycamera = new GameCameraFPS(GraphicsDevice, campos, 45.0f, 30.0f);
        myModel = new GameModel(Content);
        myModel.Load("haws5");
        mymodel1 = new Copy1(Content);
        mymodel1.Load("Samsung Smart 3D HDTV");
        btnNormal = Content.Load<Texture2D>("btnnormal");
        btnOver = Content.Load<Texture2D>("btnover");
        cursor = Content.Load<Texture2D>("cursor");
        //myfont = Content.Load<SpriteFont>("Arial");
        fridge = Content.Load<Texture2D>("Fridge");
        chicken = Content.Load<Texture2D>("Chicken");
        milk = Content.Load<Texture2D>("milk");
        fridge2 = Content.Load<Texture2D>("Fridge2");
        pizza = Content.Load<Texture2D>("pizza");




        //myCamera = new GameCameraFixed(GraphicsDevice, new Vector3(0, 0, 300), 45.0f);

        //myskybox=new GameSkybox(Content,mycamera,"skybox/skybox");


    }


    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // 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)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        mouseState = Mouse.GetState();

        mousex = mouseState.X;
        mousey = mouseState.Y;
        //Game1.window.Title = mymodel1.Position.ToString();
        mycamera.Update(gameTime);
        //myCamera.Update();
        //mycamera.cameraposition += new Vector3(0, 0, -1);
        base.Update(gameTime);
    }

    /// <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);
        //myModel.Render(mycamera.View, mycamera.Projection);



        /*
        //Game1.window.Title = GameCameraFPS.cameraposition.ToString();
        //spritebatch.Draw(fridge, new Vector2(200.0f, 200.0f), Color.White);

        spritebatch.Begin();

        //if (cameraposition )// && (cameraPosition.Y > 0 && cameraPosition.Y < 1000000) && (cameraPosition.Z > 0 && cameraPosition.Z < 1000000))
        //if ((cameraPosition.Z - mymodel1.Position.Z > -3000 && cameraPosition.Z - mymodel1.Position.Z < 3000))// && (cameraPosition.Y > 0 && cameraPosition.Y < 1000000) && (cameraPosition.Z > 0 && cameraPosition.Z < 1000000))
         {
            mymodel1.Render(mycamera.View, mycamera.Projection);
            spritebatch.Draw(fridge2, new Vector2(200.0f, 200.0f), Color.White);
            spritebatch.Draw(pizza, new Vector2(200f, 100.0f), Color.White);
            spritebatch.Draw(milk, new Vector2(271.0f, 100.0f), Color.White);
            spritebatch.Draw(chicken, new Vector2(340.0f, 100.0f), Color.White);

            spritebatch.Draw(cursor, new Vector2(mousex, mousey), Color.White);



            //myskybox.Draw(spriteBatch, mycamera.View,mycamera.Projection);




            //draw the c
        }


        spritebatch.End();
         */
        base.Draw(gameTime);

    }
}

}

        //spritebatch.End();



public class GameCameraFPS 
{

    public static GameCameraFPS mycamera;
    public static GameModel mymodel;


    // View
    public  Vector3 cameraposition { get; set; }
    Vector3 cameraTarget;
    Vector3 cameraUpVector;
    Vector3 cameraForward;

    // Projection

    float aspectRatio;
    float nearPlane;
    float farPlane;
    float fieldOfView;

    // Metrices

    private Matrix view;
    private Matrix projection;

    // Public ReadOnly View

    public Matrix View
    {

        get { return view; }

    }

    // Public ReadOnly Projection
    public Matrix Projection
    {

        get { return projection; }

    }


    // Mouse

    float cameraYaw; // rotation about the Y avis
    float cameraPitch; // rotation about X axis
    MouseState originalmouseState;

    // Framework at class level

    private GraphicsDevice device;

    // Public Property
    public float camSpeed { get; set; }

    public GameCameraFPS(GraphicsDevice device, Vector3 cameraPosition, float
        fieldOfview, float camSpeed)
    {

        // Initialize
        this.device = device;
        this.camSpeed = camSpeed;

        // Projection
        this.nearPlane = 1.0f;
        this.farPlane = 5000000000.0f;
        this.fieldOfView = fieldOfview;
        this.aspectRatio = this.device.Viewport.AspectRatio;

        // View
        this.cameraposition = cameraPosition;
        this.cameraUpVector = Vector3.Up;
        this.cameraForward = Vector3.Forward;
        this.cameraTarget = this.cameraForward + this.cameraposition;

        // Mouse

        this.cameraYaw = 0.0f;
        this.cameraPitch = 0.0f;

        Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
        originalmouseState = Mouse.GetState();

        this.projection = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(this.fieldOfView), this.aspectRatio, this.nearPlane, this.farPlane);


    }




    public void Update(GameTime gametime)
    {
        //spritebatch = new SpriteBatch(GraphicsDevice);


        // Set Keyboard and Mouse State
        KeyboardState keyboardstate = Keyboard.GetState();
        MouseState currentmousestate = Mouse.GetState();

        float timedelta = (float)gametime.ElapsedGameTime.TotalSeconds;

        Vector3 movevector = Vector3.Zero;
        float movefactor = camSpeed * 500 * timedelta;


        // Process Keyboard Input
        if (keyboardstate.IsKeyDown(Keys.W))
        {
            movevector.Z -= movefactor;
        }
        if (keyboardstate.IsKeyDown(Keys.S))
        {
            movevector.Z += movefactor;
        }
        if (keyboardstate.IsKeyDown(Keys.A))
        {
            movevector.X -= movefactor;
        }
        if (keyboardstate.IsKeyDown(Keys.D))
        {
            movevector.X += movefactor;
        }

        if (keyboardstate.IsKeyDown(Keys.Q))
        {
            movevector.Y -= movefactor;
        }
        if (keyboardstate.IsKeyDown(Keys.E))
        {
            movevector.Y += movefactor;
        }



        //Game1.window.Title = cameraposition.ToString();


            // Get the mouse position
            float mouseX = currentmousestate.X - originalmouseState.X;
            float mouseY = currentmousestate.Y - originalmouseState.Y;

            // Calculate Yaw and Pitch positions
            cameraYaw -= (mouseX * 0.05f) * timedelta;
            cameraPitch -= (mouseY * 0.05f) * timedelta;

            // Clamp the pitch to a range of -90 to 90 degrees
            cameraPitch = MathHelper.Clamp(cameraPitch, MathHelper.ToRadians(-90),
            MathHelper.ToRadians(90));

            Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);

            Matrix camerarotationmatrix = Matrix.CreateRotationX(cameraPitch)
                * Matrix.CreateRotationY(cameraYaw);

            Vector3 transformedCameraReference = Vector3.Transform(cameraForward, camerarotationmatrix);

            cameraposition += Vector3.Transform(movevector, camerarotationmatrix);

            cameraTarget = transformedCameraReference + cameraposition;

            Vector3 camerarotateupvector = Vector3.Transform(cameraUpVector, camerarotationmatrix);

            this.view = Matrix.CreateLookAt(this.cameraposition, cameraTarget, camerarotateupvector);

            if (cameraposition.Z > 3000)
            {
                Game1.window.Title = cameraposition.ToString();
                //Game1.myModel.Render(mycamera.view, mycamera.projection);

            }

            // && (cameraPosition.Y > 0 && cameraPosition.Y < 1000000) && (cameraPosition.Z > 0 && cameraPosition.Z < 1000000))
        //if ((cameraPosition.Z - mymodel1.Position.Z > -3000 && cameraPosition.Z - mymodel1.Position.Z < 3000))// && (cameraPosition.Y > 0 && cameraPosition.Y < 1000000) && (cameraPosition.Z > 0 && cameraPosition.Z < 1000000))
         {
             //.Render(mycamera.view, mycamera.Projection);

            //spritebatch.Draw(fridge2, new Vector2(200.0f, 200.0f), Color.White);
            //spritebatch.Draw(pizza, new Vector2(200f, 100.0f), Color.White);
            //spritebatch.Draw(milk, new Vector2(271.0f, 100.0f), Color.White);
            //spritebatch.Draw(chicken, new Vector2(340.0f, 100.0f), Color.White);

            //spritebatch.End();



            //myskybox.Draw(spriteBatch, mycamera.View,mycamera.Projection);




            //draw the c
        }





        }
    }




public class GameModel
{
    public Vector3 Position { get; set; }
    public float RotationX { get; set; }
    public float RotationY { get; set; }
    public float Scale { get; set; }

    public Model mymodel;
    private ContentManager Content;
    private Matrix[] transforms;

    public GameModel(ContentManager Content)
    {
        //Constructor
        this.Content = Content;
        this.Position = new Vector3(0, 0, 0);
        this.RotationX = 0;
        this.RotationY = 0;
        this.Scale = 500;

    }

    public void Load(string modelAsset)
    {
        mymodel = Content.Load<Model>(modelAsset);
        transforms = new Matrix[mymodel.Bones.Count];
        mymodel.CopyAbsoluteBoneTransformsTo(transforms);
    }

    public void Render(Matrix view, Matrix projection)
    {
        foreach (ModelMesh mesh in mymodel.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                //effect.EnableDefaultLighting();
                effect.World = transforms[mesh.ParentBone.Index]
                    * Matrix.CreateScale(Scale)
                    * Matrix.CreateRotationY(MathHelper.ToRadians(RotationY))
                    * Matrix.CreateRotationX(MathHelper.ToRadians(RotationX))
                    * Matrix.CreateTranslation(Position);

                effect.View = view;
                effect.Projection = projection;




            }

            mesh.Draw();
        }

    }
}

}

        }

I want to make an if. So when the camera is in one specified position to rend a model in a specified place. I tried rendering it from GameCameraFPS class and there is an error. I tried from the Game1 but the cameraposition is null. What can I do?

share|improve this question

closed as too localized by Byte56, bummzack, Josh Petrie, Trevor Powell, Anko Apr 7 '13 at 16:30

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Neaten up your code because people (including myself) are too lazy to read through all of that... Condense it down to the problem area :) –  Savlon Mar 30 '13 at 15:11
    
far plane at 5 billion? No. –  Dialock Mar 30 '13 at 16:34

Browse other questions tagged or ask your own question.