Jump to content
  • Advertisement
Sign in to follow this  
norper

update() question

This topic is 2495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know I am doing something wrong but I don't know what yet.

how many if() and for() can i put inside my update() before I hit problems??

at the moment i am in the beginning of coding a tile/cell based game. and each time i run through my update i am checking every single tile if it has collided with each character/mouse and if its clicked and if another million things.

for instance



for (int x = 0; x < mapinfo.width; x++)
{
for (int y1 = 0; y1 < mapinfo.height; y1++)
{
Vector2(mapinfo.Rows[x].Columns[y1].Position.X, mapinfo.Rows[x].Columns[y1].Position.Y), null, Color.White, mapinfo.Rotation, new Vector2(0, 0), mapinfo.scale, SpriteEffects.None, 0.9f);

if (miner1.minerRec.Intersects(mapinfo.Rows[x].Columns[y1].Cellrec) && mapinfo.Rows[x].Columns[y1].passable == false)
{
miner1.standing = true;

}



if (lClicked == true && mouse.clickRectangle.Intersects(mapinfo.Rows[x].Columns[y1].Cellrec))
{
mapinfo.Rows[x].Columns[y1].highLighted = true;
}
if (lHeld == true && mouse.clickRectangle.Intersects(mapinfo.Rows[x].Columns[y1].Cellrec))
{
mapinfo.Rows[x].Columns[y1].highLighted = true;
}

these if's and for's are only the beginning and i can see it becoming a problem later on, is that the case?

im sure im going about it the wrong way but i dont know better yet?

any advice is welcome

Share this post


Link to post
Share on other sites
Advertisement
You can put quite many ifs inside the update, its not a big deal.

Just remember that any code inside a loop runs multiple times so you want to place as much of the code as possible outside the loop, (IF you have nested loops you want to move code as far out as you can) and you want to keep the number of iterations of the loop low.

When it comes to your example there is one thing you can do to greatly cut down on the number of tests:
If all your tiles are of the same size then a tiles X position should be its index multiplied by its width and its Y position should be its index multiplied by its height.

Thus if you for example have a click rectangle x1,y1,x2,y2 you can get the 2 corner tiles that the click rectangle intersects easily.
given that we know the corner tiles that the clickrectangle intersects we can get all tiles inside the click rectangle aswell

if ((lClicked==true || lHeld==true) {
/*x1,y1,x2,y2 are the corners of the clickRectangle ,
ti1X etc are the indices (the numbers you put in mapInfo.Rows
  • etc) for the tiles under the clickRects corners
    If the tiles don't cover the full screen or if you are scrolling you want to ensure that the mouse is in the tile area first and translate its position to match the tiles)
    */
    int ti1X = x1/tileWidth;
    int ti1Y = y1/tileHeight;
    int ti2X = x2/tileWidth;
    int ti2Y = y2/tileHeight;
    for (int i=ti1X;i<ti2X;i++) {
    for (int j=ti1Y;i<ti2Y;j++) {
    mapInfo.Rows.Columns[j].highLighted=true;
    }
    }
    }

    This greatly cuts down on the number of times the loop runs and also minimizes the code inside the loop to a simple assignment.
    For collission you can do the same, test each character against nearby tiles (which you get as i showed above) rather than each tile against each character.

    The hard part is to optimize collissions between moving objects as you can't easily get a list of objects that are near for example the player,, one "easy" way to solve that in your case is to give your tiles a list of enemies and have enemies attach and detatch themselves to/from nearby tiles(By passing a pointer or reference to themselves to the tile(s) for example) (This lets you quite easily take any position and get a list of nearby mobile entities (you can use the Distinct() method on an array to filter out duplicates if you have one enemy attach to multiple tiles (due to being large or between tiles)).

    There is a cleaner way to do this by storing all mobile entities in a space partitioning tree or graph but thats probably overkill right now.

    Share this post


    Link to post
    Share on other sites
    http://blogs.msdn.co...g-gametime.aspx

    The above blog talks about XNA Gametime.

    All of the games updating is done via update in one form or another.

    In fixed step this is called 60 times per second.

    For a method not to be called so often you would have to write an access condition.

    At the moment my beginners game gets choppy because I have 3 objects each, calling a method which checks a number of things.

    So in the end I have to rewrite the checking condition.

    It's probably better to check based on the characters position whether a collision is possible.

    Obviously I dont know your game but perhaps just look at it on the screen and try and determine the conditions under which a method should be accessed.

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

    Sign me up!