hi daniwebers. its been a while since iv visited. soz.
a friend of mine sugested i learn a bit of xna in c# so i could help him with a future project of his. im still a verry basic coder and dont have alot of programing knowledge but i try. this is all for fun, i dont code for a job.
so.
i'v been going through a couple of tutorials and i keep creating buggs i cant fix and its kind of stopping me going further through them. i must say many thanks to the authors of these two tutorials tho
http://tech.pro/tutorial/750/creating-a-textured-box-in-xna
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/Mouse_camera.php
iv had a few attempts at it and iv ended up with different bugs each time.
ill explain the bugs b4 i post the code .

my first attempt at putting a 3d box on screen was going good untill i tried to implement mouse looking then the texture dissappered and i cant figure out why pluss the code only seems to function smoothly on my system when built on another sys there is serious lag which was verry suprising as its spec is very nice. better than my sys.

me second attempt looked alot neater but as soon as i move the mouse or press a key the object dissaperes. im not sure if it is jumping out of the perspective view or its just not being rendered once i try to manipulate it

so if anyone reading this doesnt mind helping me, let me know :) im not sure if im sposed to post raw code in here or add a zip of my projects so ill leave that decision up to who ever replies

thankyou for taking the time to read this
much appreciated

Steve

Recommended Answers

All 6 Replies

Hiya, could you please post your Draw method, any classes that build up your vertex list and your main loop?

The slowdown sounds like it might be switching to software rendering mode for some reason, but that doesn't make sense really. Either that or he's using a different type of graphics card (between nvidia/amd) and what isn't a problem on yours, is on his. (Famously, ATi cards suffered a massive performance drop when you used a texture that wasn't a power of 2 in width and height which didn't occur on nVidia cards - this was back in 2004-ish)

As for the mouse camera, it could be that the scaling you use for your modelview matrix and world matrix might be different, causing the mathematics to be well off when it calculates the camera position.

Hi thanks for answering.

Thats pretty much all my code :)

I'll past some of it here:

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

    angle = 0.0f;
    timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;

    ProcessInput(timeDifference);

    Mouse.SetPosition(Window.ClientBounds.Center.X - Window.ClientBounds.Left, Window.ClientBounds.Center.Y - Window.ClientBounds.Top);//mouse to center

    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.RoyalBlue);


    cubeEffect.EnableDefaultLighting();
    //worldMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(angle)) * Matrix.CreateRotationX(MathHelper.ToRadians(angle));
    cubeEffect.World = worldMatrix;

    foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
    {
        pass.Apply();

        cubeEffect.Texture = cube.shapeTexture;

        cube.RenderShape(GraphicsDevice);

    }


    base.Draw(gameTime);
}

public void initializeWorld()
{
    cameraMatrix = Matrix.CreateLookAt(cameraPosition, new Vector3(0, 0, 0), new Vector3(0, 1, 0));
    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Window.ClientBounds.Width / Window.ClientBounds.Height, 1.0f, 50.0f);
    float tilt = MathHelper.ToRadians(22.5f);
    worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);

    cubeEffect = new BasicEffect(GraphicsDevice);//, null);
    cubeEffect.World = worldMatrix;
    cubeEffect.View = cameraMatrix;
    cubeEffect.Projection = projectionMatrix;
    cubeEffect.TextureEnabled = true;

}

private void UpdateViewMatrix()
{
    Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

    Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
    Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
    Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;

    Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
    Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);

    worldMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
    //viewMatrix
}


private void ProcessInput(float amount)
{
    Vector3 moveVector = new Vector3(0, 0, 0);

    MouseState currentMouseState = Mouse.GetState();
    if (currentMouseState != _originalMouseState)
    {
        float xDifference = currentMouseState.X - _originalMouseState.X;
        float yDifference = currentMouseState.Y - _originalMouseState.Y;
        leftrightRot -= rotationSpeed * xDifference * amount;
        updownRot -= rotationSpeed * yDifference * amount;

        //AddToCameraPosition(moveVector * amount);

        //Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
        UpdateViewMatrix();
    }

    KeyboardState keyState = Keyboard.GetState();
    if (keyState != _oldState)// checking keyboard state against default state
    {
        if (keyState.IsKeyDown(Keys.W))                 // W forwards           
            moveVector += new Vector3(0, 0, -1);

        if (keyState.IsKeyDown(Keys.S))                 // S backwards                
            moveVector += new Vector3(0, 0, 1);

        if (keyState.IsKeyDown(Keys.D))                 // D strafe right
            moveVector += new Vector3(1, 0, 0);

        if (keyState.IsKeyDown(Keys.A))                 // A strafe left
            moveVector += new Vector3(-1, 0, 0);

        if (keyState.IsKeyDown(Keys.Q))                 // Q strafe up
            moveVector += new Vector3(0, 1, 0);

        if (keyState.IsKeyDown(Keys.Z))                 // Z strafe down
            moveVector += new Vector3(0, -1, 0);

        if (keyState.IsKeyDown(Keys.Enter))             //reset box
        {
            BasicShape cube = new BasicShape(new Vector3(2, 2, 2), new Vector3(0, 0, 0));//, 1);//(size,position,ID)
            cubes.Clear();
            cubes.Add(cube);
            boxPosY = 0;
            boxCount = 1;
        }

        if (keyState.IsKeyDown(Keys.Up))                // make bigger
            foreach (BasicShape cube in cubes)
                cube.shapeSize += new Vector3(+1, +1, +1);

        if (keyState.IsKeyDown(Keys.Down))              // make smaller
            foreach (BasicShape cube in cubes)
                if (cube.shapeSize != new Vector3(1, 1, 1))
                    cube.shapeSize += new Vector3(-1, -1, -1);

        if (keyState.IsKeyDown(Keys.Space))             //Change the boxes position 
        {
            foreach (BasicShape shift in cubes)
            {
                shift.shapePosition += new Vector3(+0, +0, +1);
            }
            boxPosY = 0;
        }

        if (keyState.IsKeyDown(Keys.Add))                //+ add boxes at the push of a button :)
        {
            BasicShape newCube = new BasicShape(new Vector3(2, 2, 2), new Vector3(0, boxPosY, 0));//, boxCount);//(size,position,ID)
            cubes.Add(newCube);
            boxPosY += 4;
            boxCount += 1;
        }

        if (keyState.IsKeyDown(Keys.Subtract))           // take boxes away   

            if (cubes.Count != 0)
            {
                cubes.RemoveAt(cubes.Count - 1);
                boxPosY -= 4;
                boxCount -= 1;
            }

        if (keyState.IsKeyDown(Keys.Left))               // rotate box
            angle += 5.0F;

        if (keyState.IsKeyDown(Keys.Right))
            angle -= 5.0F;


        AddToCameraPosition(moveVector * amount);

        //worldMatrix = Matrix.CreateRotationX(MathHelper.ToRadians(45.5f));
        //cubeEffect.World = worldMatrix;
    }

    //_oldState = Keyboard.GetState();
    //UpdateViewMatrix();
}
private void AddToCameraPosition(Vector3 vectorToAdd)
{
    Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
    Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
    cameraPosition += moveSpeed * rotatedVector;

    UpdateViewMatrix();
}

and the Cube

class BasicShape

{
    public Vector3 shapeSize;
    public Vector3 shapePosition;
    private VertexPositionNormalTexture[] shapeVertices;
    private int shapeTriangles;
    private VertexBuffer shapeBuffer;
    public Texture2D shapeTexture;



    public BasicShape(Vector3 size, Vector3 position)
    {
        shapeSize = size;
        shapePosition = position;
    }

    private void BuildShape()
    {
        shapeTriangles = 12;

        shapeVertices = new VertexPositionNormalTexture[36];

        Vector3 topLeftFront = shapePosition +
            new Vector3(-1.0f, 1.0f, -1.0f) * shapeSize;
        Vector3 bottomLeftFront = shapePosition +
            new Vector3(-1.0f, -1.0f, -1.0f) * shapeSize;
        Vector3 topRightFront = shapePosition +
            new Vector3(1.0f, 1.0f, -1.0f) * shapeSize;
        Vector3 bottomRightFront = shapePosition +
            new Vector3(1.0f, -1.0f, -1.0f) * shapeSize;
        Vector3 topLeftBack = shapePosition +
            new Vector3(-1.0f, 1.0f, 1.0f) * shapeSize;
        Vector3 topRightBack = shapePosition +
            new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
        Vector3 bottomLeftBack = shapePosition +
            new Vector3(-1.0f, -1.0f, 1.0f) * shapeSize;
        Vector3 bottomRightBack = shapePosition +
            new Vector3(1.0f, -1.0f, 1.0f) * shapeSize;

        Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
        Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f) * shapeSize;
        Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
        Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * shapeSize;
        Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * shapeSize;
        Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;

        Vector2 textureTopLeft = new Vector2(0.5f * shapeSize.X, 0.0f * shapeSize.Y);
        Vector2 textureTopRight = new Vector2(0.0f * shapeSize.X, 0.0f * shapeSize.Y);
        Vector2 textureBottomLeft = new Vector2(0.5f * shapeSize.X, 0.5f * shapeSize.Y);
        Vector2 textureBottomRight = new Vector2(0.0f * shapeSize.X, 0.5f * shapeSize.Y);

        // Front face.
        shapeVertices[0] = new VertexPositionNormalTexture(topLeftFront, frontNormal, textureTopLeft);
        shapeVertices[1] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
        shapeVertices[2] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);
        shapeVertices[3] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
        shapeVertices[4] = new VertexPositionNormalTexture(bottomRightFront, frontNormal, textureBottomRight);
        shapeVertices[5] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);

        // Back face.
        shapeVertices[6] = new VertexPositionNormalTexture(topLeftBack, backNormal, textureTopRight);
        shapeVertices[7] = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
        shapeVertices[8] = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
        shapeVertices[9] = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
        shapeVertices[10] = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
        shapeVertices[11] = new VertexPositionNormalTexture(bottomRightBack, backNormal, textureBottomLeft);

        // Top face.
        shapeVertices[12] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
        shapeVertices[13] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);
        shapeVertices[14] = new VertexPositionNormalTexture(topLeftBack, topNormal, textureTopLeft);
        shapeVertices[15] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
        shapeVertices[16] = new VertexPositionNormalTexture(topRightFront, topNormal, textureBottomRight);
        shapeVertices[17] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);

        // Bottom face. 
        shapeVertices[18] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
        shapeVertices[19] = new VertexPositionNormalTexture(bottomLeftBack, bottomNormal, textureBottomLeft);
        shapeVertices[20] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
        shapeVertices[21] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
        shapeVertices[22] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
        shapeVertices[23] = new VertexPositionNormalTexture(bottomRightFront, bottomNormal, textureTopRight);

        // Left face.
        shapeVertices[24] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);
        shapeVertices[25] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
        shapeVertices[26] = new VertexPositionNormalTexture(bottomLeftFront, leftNormal, textureBottomRight);
        shapeVertices[27] = new VertexPositionNormalTexture(topLeftBack, leftNormal, textureTopLeft);
        shapeVertices[28] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
        shapeVertices[29] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);

        // Right face. 
        shapeVertices[30] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
        shapeVertices[31] = new VertexPositionNormalTexture(bottomRightFront, rightNormal, textureBottomLeft);
        shapeVertices[32] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);
        shapeVertices[33] = new VertexPositionNormalTexture(topRightBack, rightNormal, textureTopRight);
        shapeVertices[34] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
        shapeVertices[35] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);
    }


    public void RenderShape(GraphicsDevice device)
    {
        BuildShape();
        shapeBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, shapeVertices.Length, BufferUsage.WriteOnly);
        shapeBuffer.SetData(shapeVertices);

        device.SetVertexBuffer(shapeBuffer);    //.Vertices[0].SetSource(shapeBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
                                                //device.VertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
        device.DrawPrimitives(PrimitiveType.TriangleList, 0, shapeTriangles);
    }
}

Does it work when you hit Enter to reset? It could just be that you're moving the camera way to fast (imagine you could be moving 1000 units by the time you take your finger off the key)

You could try lock-step movement which means you can't move another unit until you release the key. That way you can find out quickly if it's your movement code or just going too fast.

in the update method im setting a game time und using that to slow down the updating i think. is that not a similar thing as this lock stepping you mention. pressing return doesn't restore the box to its original position.any button i press or mouse movement just makes it dissapere:(

No, the reason I suggested lock step is so that you know you're only moving by one set of your units at a time. Even if you slow down the animation you can't be sure how many units you've just asked it to move :)

In your reset code; reset your camera back to the default position as well as the box graphics. This will at least give you an easy way to test things like different directions.

Secondly, abandon mouse movement until you can get keyboard movement working. May as well work on one bit first and get that working. Usually, solving the simpler problem can fix the more complicated looking ones.

If your keyboard movement is working, it may be worth looking at the values you're sending through in mouse movement. Even if you slow your animation down using dt, if you've told it to rotate 120 units when your scale is in the region of 10 units, then you're just going to spin around madly.

You can do this either with debug points (admittedly difficult when talking about mouse movement) or print them to file. Would be worth tracking when movement starts and stops, then print the difference and some of the values sent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.