[XNA] Simple Problem, i cant seem to get my head around

Started by
8 comments, last by dannielum 13 years, 6 months ago
Hi there, hopefully a fairly simple problem here


Center is the Center of the texture with the position added#region Camera ControlCameraPosition.X = (TestPlayer.Center.X - (WindowRez.X / 2));CameraPosition.Y = (TestPlayer.Center.Y - (WindowRez.Y / 2));#endregionCameraPosition.X = MathHelper.Clamp(CameraPosition.X, 0, currentGameMap.PixelWidth - WindowRez.X);CameraPosition.Y = MathHelper.Clamp(CameraPosition.Y, 0, currentGameMap.PixelHeight - WindowRez.Y);

- PixelWidth/Height = Height/width of the Map in Pixels

In my head, this code should centre the image of my Sprite in the centre of the screen.

I quite simply move the camera half the dimensions of the screen away from the centre of the Sprite.

This seems to work, until I move the sprite, and then it doesn't stay centred.
Advertisement
I think you can just set:
CameraPosition.X = TestPlayer.Center.X;
CameraPosition.Y = TestPlayer.Center.Y;

Make sure you update the player position correctly though.
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
Thanks for the reply dannielum, Unfortuantly it doesnt quite work. My sprite can still go zooming off the screen before the camera clamping has taken effect, which is what im trying to avoid.

I am updating the player Position every call to Draw, i just do:

Position += Velocity;


I just realize one thing... Is the CameraPosition not the center of the camera position? Is that why you subtracting (WindowRez.X / 2)?

Actually there are two ways you can do.
1. you move the player on the map, then move the camera position to follow the character.
2. don't move the player, just move the map as the player moves
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
No ther CameraPosition is a Vector2. Representing the top left hand corner of the camera.

Quote:
1. you move the player on the map, then move the camera position to follow the character.


This is what I was trying xD

I was hoping, if I set the Camera position to the Centre(which is the centre of the Sprite Texture with the position added) I would always have the sprite in the top right corner of the screen. (however this Isn't working :S) Then I simply subtract 1/2 of the screen to make the sprite always in the centre.
Actually I think the second method is easier and will have less calculation to do and suppose to be faster.

But anyway, I think you should first make sure if the player sprite is moving correctly on the map first. Try zooming out the map, and remove the camera first. See if the player movement is correct when you move it around with the keyboard. If this works well, then you can work on the camera. The camera is just a rectangle that store the position of the scene you want to display at each frame.
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor

This is how i draw that Map
for (int y = 0; y < MapHeight; y++){for (int x = 0; x < MapWidth; x++){Rectangle DrawRect = new Rectangle();DrawRect.X = (x * TileWidth) -(int)camera.X;DrawRect.Y = (y * TileHeight) -(int)camera.Y;DrawRect.Width = TileWidth;DrawRect.Height = TileHeight;Batch.Draw(d_textureDict[Index],          DrawRect,          Color.White);


So, I am essentially moving the map by subtracting the CameraPosition. It works properly if I use the Keyboard to move the map left right up and down etc.

This is all of my Input Method
float acceleration = 0.1f;            #region MOVEMENT                        if(Input.Keyboard.IsKeyDown(Keys.Up, false))                TestPlayer.ChangeSpeed(acceleration);            else if(Input.Keyboard.IsKeyDown(Keys.Down, false))                TestPlayer.ChangeSpeed(-acceleration);            TestPlayer.MoveToMouse(Input.Mouse.Position);// See Below            #endregion                        #region Camera Control            CameraPosition.X = TestPlayer.Center.X;            CameraPosition.Y = TestPlayer.Center.Y;            #endregion            CameraPosition.X = MathHelper.Clamp(CameraPosition.X, 0, currentGameMap.PixelWidth - WindowRez.X);            CameraPosition.Y = MathHelper.Clamp(CameraPosition.Y, 0, currentGameMap.PixelHeight - WindowRez.Y);


public void MoveToMouse(Point MousePosition)        {            /*Todo:             * 1. Find out how much we need to rotate             * 2. Find out what the Vector is and Normalise it             */            //1.            Vector2 StartDir = new Vector2(0, -1);            float rot = (float)Math.Atan2(MousePosition.Y - Center.Y,                Center.X - MousePosition.X);            rot = -(float)(rot + MathHelper.ToRadians(90.0f));            PlayerStatistics.Rotation = rot;            //2.            Matrix rotMat = Matrix.CreateRotationZ(rot);            Vector2 MoveDir = Vector2.Transform(StartDir, rotMat);            MoveDirection = MoveDir;        }

In this function I set the Movement direction. Then to get the velocity i multiply the speed by the Movement Direction
How come you have a "camera" at the top and "CameraPosition" at the bottom? What is the difference between camera.X and CameraPosition.X?

It is hard for me to visualizing it in my head. Maybe you can show a screenshot of the rendering problem?
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
I slept on the problem last night, and it occured to me, there is acctually nothing wrong with it, just the concept. I move the map and it clamps as it should, but my sprite goes off the edge cus thats where im telling it to draw, it will clamp to the map but somtimes the edge is over the screen width.

Gunna have a rethink and try ur 2nd suggested Method ;)
Good for you. Sometimes it is better to take a rest when you cannot solve a problem and come back after taking a break. Good luck with your project!
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor

This topic is closed to new replies.

Advertisement