Sign in to follow this  
xrythx

[.net] problem with code for XNA Game

Recommended Posts

i was wondering... For my c# XNA game i have designed a tile map class that makes use of a tilesheet class full of source rects for a texture to divide them into 32 x 32 pixel tiles. The tilemap is where i run into the problem. inside the tile map there is a multidimensional integer array that keeps telling me its out of bounds. i stayed up untill 5am this morning trying to figure out what i was doing wrong. Here's the code. Im using 2 for loops to populate this multi-array.
public class TileMap
{
    public TileMapData[,,] tMapData;
    int width;
    int height;

    public TileMap(ref TileSheet TileSheet, int TilesWide, int TilesTall, int Layers)
    {
        tiles = TileSheet;
        width = TilesWide;
        height = TilesTall;
        iLayers = Layers;

        zoom = 0;
        tileData = new int[width+1, height+1];
        //tMapData = new TileMapData[width, height,iLayers];
            
        //TODO: Fix this stupid ass bug
        for (int i = 0; i <= height-1; i++)  //ROW Y Height
        {
            for (int j = 0; j <= width-1; j++) // COLUMN X Width
            {
                tileData[i, j] = 0;
            }
        }
    }
}


Say the map is 10 x 10. it will work, but if i do 10 x 11 it throws out of bounds. this is going to drive me insane. any help is appreciated. [Edited by - xrythx on February 1, 2008 1:38:52 AM]

Share this post


Link to post
Share on other sites

public class TileMap
{
public TileMapData[,,] tMapData;
int width;
int height;

public TileMap(ref TileSheet TileSheet, int TilesWide, int TilesTall, int Layers)
{
tiles = TileSheet;
width = TilesWide;
height = TilesTall;
iLayers = Layers;

zoom = 0;
tileData = new int[width+1, height+1];
//tMapData = new TileMapData[width, height,iLayers];

//TODO: Fix this stupid ass bug
for (int i = 0; i <= height-1; i++) //ROW Y Height
{
for (int j = 0; j <= width-1; j++) // COLUMN X Width
{
// NOTE: Fix is here...
tileData[j, i] = 0; // These were backwards
}
}
}
}

Share this post


Link to post
Share on other sites
bah, im dyslexic. i had a feeling i should have flipped the width and height for loop, thanks for clarifying this, you made my day ^__^. Also, in a 2d game it is not necessary to do world coord to screen is it? for example since all my tiles are 32 px i can divide the width of the screen (1024 in this case) by 32 to retrieve which tile he is standing on, correct? i made sure to set the origin in the SB.Draw to make sure this was all calculated by players feet and not 0,0 of sprite.

Share this post


Link to post
Share on other sites
I'm new to game design but I would do world coord. I did an assignment last semester and it seemed like a really good idea for when I moved the camera. I was realizing this on my own 2d game just a couple days ago, I had the camera follow my character and I realized that world coord just seemed like a much better method.

Share this post


Link to post
Share on other sites
my problem is that i just use the screen width and draw starting from px 0,0 in multiples of 32 (because of my tile size) towards the right of the screen and towards the bottom as well. also, im unsure how exactly i would implement a world coordinate system.

Share this post


Link to post
Share on other sites
hrm, so far the only way to keep everything moving consistantly when the TileMap is scrolled is to create a List<GameObject> (GameObject is the root class for any graphical related class) and move its x, or y the opposite ammount of the scrolling value for x or y. is there a better solution? i could create a class for the camera and have it hold the current screen position. but how would this build into my scrolling calculations? in my tilemap class theres a function for scrolling the map, perhaps if i change it to have a List<GameObject> passed to it and then foreach through them and adjust them to the inverse of what my tilemap is being scrolled?
**Edit**

let's see, is this any good? this is my scrollMap function. Location is a Vector 3 which is derived from gameobject. At the moment this is called when the character is scrolled which is done in the update function of Game class

public void ScrollMap(Directions Direction, List<GameObject> ObjsToScroll)
{
switch (Direction)
{
case Directions.North:
y++;
foreach( GameObject go in ObjsToScroll)
{
//go.Location.Y++;
go.Location += new Vector3(0f, 1, 0f);
}
break;
case Directions.East:
x--;
foreach (GameObject go in ObjsToScroll)
{
//go.Location.X--;
go.Location += new Vector3(-1f, 0f, 0f);
}
break;
case Directions.South:
y--;
foreach (GameObject go in ObjsToScroll)
{
//go.Location.Y--;
go.Location += new Vector3(0f, -1f, 0f);
}
break;
case Directions.West:
x++;
foreach (GameObject go in ObjsToScroll)
{
//go.Location.X++;
go.Location += new Vector3(1f, 0f, 0f);
}
break;
}




maybe i should just add a class called Viewport that defines a rectangle of my tile map to render?

[Edited by - xrythx on February 1, 2008 1:38:57 AM]

Share this post


Link to post
Share on other sites
You would create a viewing matrix. When I did it there was a lot of matrix work but it was so simple when it did work.

Basically, there are three coord, a local so that say a gun in a hand can be considered relative to the person, and other similiar things, then world, where you want it in the world (so this would be your normal quardinate system) and then you do a matrix to get it on screen. Basically when an object draws itself it draws itself on a normalized device I think it was called. I forget the exact order of operations but I can look it up. It's about 4 matrix multiplications. XNA probably has it all done internally for you though, I haven't done it with xna. look into spritebatch.begin. I think you set a viewing matrix there.

Share this post


Link to post
Share on other sites
thanks alot for your help. i think spritebatch handles it but im not sure if it handles all of it. perhaps the vector2D that i use in my cameratransform matrix is considered the world position;

public class Viewport
{
public Vector2 cameraWorldPosition;
public Matrix cameraTransform = Matrix.Identity;

public Rectangle BufferBox;

public Viewport(int X, int Y,int Width, int Height)
{
cameraWorldPosition = new Vector2(0, 0);
BufferBox = new Rectangle(64,64,Width-64,Height-64);

cameraTransform = Matrix.CreateTranslation(-cameraWorldPosition.X, -cameraWorldPosition.Y, 0);
}
}




Bufferbox will be a rect within the client area that controls scrolling, if that works. i think i can make it work.
is this efficient?

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