Checking the surrounding area

Last post 05-09-2008, 7:29 PM by JohnWestMinor. 4 replies.
Sort Posts: Previous Next
  •  05-08-2008, 5:44 PM

    Checking the surrounding area

    Hey

    I am currently in the progress of making a 2D grid based game. What I am attempting to do, is to place a character within a certain grid and move towards the grid cell that is the near object, e.g. a tree.

    I can make it so it does work, but it would require too many lines of code (well I think so) as my grid is 40 by 30 cells. The image below shows my current thinking:

    What I would really like is a small (in terms of lines) algorithm that is capable of checking every cell within the 40 by 30 grid, if needed. I could just check each cell in sequence, but I would like to expand from the origin (by that I mean the characters position).

    Any help is greatly appreciated, thank you.

  •  05-08-2008, 11:51 PM

    Re: Checking the surrounding area

    I'm not sure if you're looking for something more than this, but what I'd planned on doing is like so...

    int SpacialCubeSize = 10;

    Objects[] Space = new Objects[x, y, z]

    Objects Player_Object;

    int offset;

    Space[(int)(Player_Object.Player_Pos.x / SpacialCubeSize)+offset, (int)(Player_Object.Player_Pos.y / SpacialCubeSize)+offset, (int)(Player_Object.Player_Pos.z / SpacialCubSize)+offset] = Player_Object;

    For getting the surrounding areas, you could probably do some loops to change the offset to place your object in the surrounding cubes.

    Anyhow, I'm not sure if this would exactly work the way I've written it out (it's just pseudo code too, and won't actually compile) and I'm horrible at explaining things - but anyway, I thought I'd take the time to write this. Hopefully it might help some. : /
     
     
  •  05-09-2008, 2:43 AM

    Re: Checking the surrounding area

    If you're talking about placing a character and a tree on a 40x30 grid, and having the player move to the tree, then you'll likely want Dijkstra's or an A* algorithm, neither of which is very small.

    Unless you cheat by placing them in the same 2 spots everytime, which means you can pre-define the path.

    If you're not talking about path-finding then forget everything I just said. :)


    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  •  05-09-2008, 4:24 AM

    Re: Checking the surrounding area

    Is the problem this?

    1) Find the tree that is closest to the character

    2) Move to that tree

    If trees are rare, and cells are plentiful, then it's probably easier to keep a separate list of each tree, together with which cell it is in. Then run through all the trees, calculating the distance between the player and the trees, and remembering which tree is closest. At the end, you know which tree you want to move towards, which should give you the direction to move.

    You do not need Dijkstra or A*, because the cost of examining a square is very small, and you don't know which square is actually the goal -- A* works great when you know the goal, but your problem is finding the goal.

    If you can't keep a list of trees, then examine each of the grid cells, and if the cell has a tree, calculate distance from cell to player, and if closer than the previously closest cell, then remember that cell. When done, you will be remembering the closest tree cell, which is the cell you want to walk towards. Examining 1200 cells (40x30) really doesn't take any noticeable time on a modern CPU; at least as long as you only do it once in a while.

     


    --
    Jon Watte, Direct3D MVP
    kW X-port 3ds Max .X exporter
    14 days after getting my RROD box back, it's going back for service again. Grr.
  •  05-09-2008, 7:29 PM

    Re: Checking the surrounding area

    If you want to check in a radius around your character, you can do something like this:

    The radius will be separated into quarters in the code. Each quarter will be checked with its own script.

    I'll show you how the code will work for the lower lefthand quarter, and you can then use the same technique on the others.

    //This first for loop moves the checking to the left after the second for loop is finished.
    for(i=0,i>-radius && (i+Xposition)>0;i--)
    {
        //And this moves the checking downwards. For each placement left, it cuts one block off of its search to form the circle.
        for(ii=0,ii<(radius+i) && (ii+Yposition)<ymax,ii++)
        {
           //This here checks the land with relevance to the checking object.
            if(land[Xposition+i,Yposition+ii]==Treesignature)
            {
                Stuff to store the found tree's coordinants for later use.
            }
        }
    }
View as RSS news feed in XML
©2007 Microsoft Corporation. All rights reserved. Privacy Statement Terms of Use Code of Conduct Feedback