Sign in to follow this  
FantasyVII

[Solved] Bad scrolling / inaccuracy building.

Recommended Posts

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.
[media]http://www.youtube.com/watch?v=D0tBQthEG4o[/media]

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.

[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);
}
[/CODE]




[CODE]
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);
}
}
[/CODE]

[CODE]
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);
}
}
[/CODE] Edited by FantasyVII

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Postie' timestamp='1348450933' post='4983073']
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.
[/quote]

thank you. i'll try that :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this