• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
FantasyVII

[Solved] Bad scrolling / inaccuracy building.

3 posts in this topic

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
0

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.
0

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.
1

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 :)
0

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  
Followers 0