Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Solved] Bad scrolling / inaccuracy building.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 FantasyVII   Members   -  Reputation: 284

Like
0Likes
Like

Posted 23 September 2012 - 05:51 AM

Hello everyone

I'm working on a tower defense game. I got everything setup and I can build tower no problems. What I do is check where is the mouse position and then divide the mouse position by 32 pixels (becuase its the Tile or Tower size) and convert the floating point to integer . and then I multiply by 32 pixels.

so lets say mouse position is X200 Y150. so 200/32 = 6.25 convert that to int removing the decimal so its 6 * 32 = 192 pixels on X axis. same thing for Y. I do this to make sure that I build towers every 32 pixel.

Now it works fine when I don't scroll the game. However when I start scrolling and try to build out of screen the position becomes inaccurate. I just want to know why does it become inaccuracy.

I think the way I'm doing the scrolling is wrong and that is why I'm getting wrong position.

Here is a video that explains my problem more.
http://www.youtube.com/watch?v=D0tBQthEG4o

from 0:00 till 0:30 it will show that when I build the tower its accurate. It builds where I tell it to. However from 0:33 till 0:54 it show how it becomes inaccurate when I scroll. Then from 0:55 to 1:06 its accurate again and from 1:07 till the end of the video it show how it becomes inaccurate again.

Sorry for my bad english.

Here is my code.

class Tower
	{
		Texture2D Texture;
		Vector2 Position;
		SpriteFont Text;
		bool Clicked = false;
		public void LoadContent(ContentManager Content)
		{
			Texture = Content.Load<Texture2D>("Tiles/Towers/Tower1");
			Text = Content.Load<SpriteFont>("Fonts/Font");
		}

		/// <Add tower to the mouse position summary>
		/// In this update method when the mouse is clicked, We will calculate the mouse position in the game world and then set it to the tower.
		/// We divide by 32 and then multiple by 32 so we can move the tower every 32px instead of every one pixel
		/// </Add tower to the mouse position summary>
		public void Update()
		{
			//ClickOnTower();
			if (MouseCursor.LastMouseState.LeftButton == ButtonState.Released && MouseCursor.CurrentMouseState.LeftButton == ButtonState.Pressed)
			{
				Position.X = (int)(MouseCursor.Position.X -ScreenScrolling.ScreenOffset.X) / 32;
				Position.Y = (int)(MouseCursor.Position.Y -ScreenScrolling.ScreenOffset.Y) / 32;
				Position.X = Position.X * 32;
				Position.Y = Position.Y * 32;
			}
		}
		public void Draw(SpriteBatch spriteBatch)
		{
			spriteBatch.Draw(Texture, Position + ScreenScrolling.ScreenOffset, Color.White);
			//spriteBatch.DrawString(Text, " " + MouseCursor.Position, new Vector2(100, 150), Color.Black);
		}




class TowerManager
	{
		List<Tower> Towers = new List<Tower>();
		void AddTower()
		{
		}
		public void Update(ContentManager Content)
		{
			if (MouseCursor.LastMouseState.LeftButton == ButtonState.Released && MouseCursor.CurrentMouseState.LeftButton == ButtonState.Pressed)
			{
				Towers.Add(new Tower());
				for (int i = 0; i < Towers.Count; i++)
					Towers[i].LoadContent(Content);
			  
				for (int i = Towers.Count - 1; i < Towers.Count; i++)
					Towers[i].Update();
			}
		}
		public void Draw(SpriteBatch spriteBatch)
		{
			for (int i = 0; i < Towers.Count; i++)
				Towers[i].Draw(spriteBatch);
		}
	}

class ScreenScrolling
	{
		Texture2D Texture;
		public static Vector2 ScreenOffset;
		Rectangle right, left, up, down;
		public ScreenScrolling(GraphicsDeviceManager graphics)
		{
			right = new Rectangle(0, 0, 25, graphics.PreferredBackBufferHeight); //Right red line
			left = new Rectangle(graphics.PreferredBackBufferWidth - 25, 0, 25, graphics.PreferredBackBufferHeight); // Left Red Line
			up = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, 25); //Up red Line
			down = new Rectangle(0, graphics.PreferredBackBufferHeight - 25, graphics.PreferredBackBufferWidth, 25); // Down Red Line
		}
		public void LoadContent(ContentManager Content)
		{
			Texture = Content.Load<Texture2D>("Side");
		}
		public void Update()
		{
			if (MouseCursor.MouseCursoreRec.Intersects(right))
			{
				ScreenOffset.X += 5f;
			}
			if (MouseCursor.MouseCursoreRec.Intersects(left))
			{
				ScreenOffset.X -= 5f;
			}
			if (MouseCursor.MouseCursoreRec.Intersects(up))
			{
				ScreenOffset.Y += 5f;
			}
			if (MouseCursor.MouseCursoreRec.Intersects(down))
			{
				ScreenOffset.Y -= 5f;
			}
		}
		public void Draw(SpriteBatch spriteBatch)
		{
			spriteBatch.Draw(Texture, right, Color.White);
			spriteBatch.Draw(Texture, left, Color.White);
			spriteBatch.Draw(Texture, up, Color.White);
			spriteBatch.Draw(Texture, down, Color.White);
		}
	}

Edited by FantasyVII, 23 September 2012 - 10:54 AM.


Sponsor:

#2 FantasyVII   Members   -  Reputation: 284

Like
0Likes
Like

Posted 23 September 2012 - 10:53 AM

ok new update. if I try to scroll up and build top where Y is negative ( for example - 200) the mouse becomes inaccurate. also if I try to scroll left and build there where X is also negative (for example X -100) the mouse becomes inaccurate.

So i can build from (0, 0) till (+10000000, +1000000000) and the mouse will be accurate.

so problem solved I guess. I'll just start my maps from (0,0) X, Y.

#3 Postie   Members   -  Reputation: 744

Like
1Likes
Like

Posted 23 September 2012 - 07:42 PM

Your problem is likely when you convert your mouse position to your integer multiples of 32.

For values greater than 0, doing (int)value/32 will work ok. But for values less than 0 it will be a problem. For example, if value is -16, the division will give you -0.5, which is rounded down to 0. So all values between -31 and +31 will map to 0. What you really need is to use the floor() function instead of casting it as an int, this will cause -0.5 to be rounded to -1, which is correct.
Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler

#4 FantasyVII   Members   -  Reputation: 284

Like
0Likes
Like

Posted 23 September 2012 - 11:37 PM

Your problem is likely when you convert your mouse position to your integer multiples of 32.

For values greater than 0, doing (int)value/32 will work ok. But for values less than 0 it will be a problem. For example, if value is -16, the division will give you -0.5, which is rounded down to 0. So all values between -31 and +31 will map to 0. What you really need is to use the floor() function instead of casting it as an int, this will cause -0.5 to be rounded to -1, which is correct.


thank you. i'll try that :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS