Sign in to follow this  
Crazyfool

Tiled Game Problems

Recommended Posts

Hi, in my game I have added layers to my 2D tiled engine that reads a map file for the appropriate texture. Everything seems to be working right but when I walk in a direction, sometimes (often) it will cause some problems while rendering. Here is a picture of what is happening: The left portion (i.e. the horizontal line/tearing) came from walking vertically. The right (vertical tearing) portion came from walking horizontally. I am sitting because in my game it stops my animation and so I could more easily identify good examples. The two different sides come from two separate images that I combined. I tested the method that obtains the rectangle associated with the tile in the tilesheet. This seems to be working perfectly. I think this has to do with movement and the game rendering tiles at slightly different intervals. If you move from x = 5.0 to x = 6.0, your position will be varying values between those two. I think that's the problem. I am unsure why that is causing the issue, because if 1 tile is drawn slightly more to the left (or right, up or down) then shouldn't every other one do that as well, thus making it slightly off by 1ish pixel for a split second? Please help, this is annoying me beyond compare! Thanks ahead of time!

Share this post


Link to post
Share on other sites
You might want to mention little details like what library/API you're using to do your rendering. Maybe what operating system or programming language too. It might not even kill you to show some code for how you're doing your rendering.

Share this post


Link to post
Share on other sites
Sure thing. The code is C++, using DirectX sprite interface to render, and working on Windows XP 32 bit.

Edit: the code may be ugly, and advice is always accepted.

Some source for rendering the tiles:


Map* map = world->getMap(x, y);
if(map != NULL)
{
Tile* tile = world->getTile(x, y);
if(tile != NULL)
{
int tsNumber = tile->textureIndex[0];
ArtFile* tsArt = artManager.getTileSheet(map->tileSheet[tsNumber]);
if(tsArt != NULL)
{
RECT rect;

if(!tsArt->loaded)
tsArt->load(d3ddev);


findRect(&rect, tile->layer[0], 0, tsArt->width, tsArt->height, 64, 64);
d3dspt->Draw(tsArt->texture, &rect, NULL, position, fullColor);

/* other code removed, this drew layers but i commented it out for testing*/
}
}
}




fullColor is just a color thats used if a player is dead /stealthed /etc.
position is a pointer to a d3dxvector3 object.
findRect() has been tested a decent amount, but I will post the code just to be extra sure:


//rect is incoming rect
//index is the specific section you want of a given image
//texturePos is used for characters (splitting the image in two)
//width/height of image, for tilesheets, its 512 by 1024
//rectWidth/rectHeight of desired rect, for tiles, its 64x64
void SpriteRenderer::findRect( RECT* rect , int index , int texturePosition , int width , int height , int rectWidth , int rectHeight )
{
if(rect == NULL)
return; //bad input

int numOfX = width / rectWidth;
int numOfY = height / rectHeight;

int posX = index % numOfX;
int posY = index / numOfX;

if(posY >= numOfY)
return; //error

//960
rect->top = posY * rectHeight;
rect->bottom = rect->top + rectHeight - 1;
rect->left = posX * rectWidth;
rect->right = rect->left + rectWidth - 1;

rect->top += (height / 2) * texturePosition;
rect->bottom += (height / 2) * texturePosition;
}




Also keep in mind, it works flawlessly (at least IMO) when not moving. This leads me to believe that findRect() is fine.

Share this post


Link to post
Share on other sites
Crazyfool,

I use OpenGL and not DirectX, but I have had a very similar problem in the past. Hopefully it can translate into a DirectX solution.

When loading textures, they need to be configured correctly so that the texture edges CLAMP and not REPEAT.

For whatever reason, the API sometimes displays a misaligned texture. If the texture is set to REPEAT, it will wrap the texture around your quad, often resulting in these bands, whether they are black lines or just lines from another part of your texture.

If CLAMPing is enabled, it fixes the edges so that it will not wrap like this, and hence gets rid of your lines.

I hope for your sake there is a similar DirectX solution.

Good luck,

Share this post


Link to post
Share on other sites
Awesome, thanks for the help!

Also of note, in case anyone else wants to chime in (or if I cannot figure out about CLAMPing): I tried making it so that the player's position are never more than 3 decimal places, so the player may be 100.123 but it wont be 100.1234. What this would do, I have no idea, I thought there might have been problems with rounding errors and things getting off very slightly. But it still shows up when I do the following:


player->x *= 100;
player->x = (int)player->x;
player->x /= 100.0f;

player->y *= 100.0f;
player->y = (int)player->y;
player->y /= 100.0f;





Edit: Also, this looks very promising. I seemed to have found the directx equivalent and added this code to my init() method. However, nothing seems to be changed, but I think I didn't use it properly. Going to keep reading, thanks again for the tip!

d3ddev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
d3ddev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);





Does clamping for the entire texture or for when you make draw calls? I draw portions of a texture, so I am not sure if CLAMP is having any effect at all.

[Edited by - Crazyfool on January 18, 2009 7:52:41 PM]

Share this post


Link to post
Share on other sites



Click for larger view

This is a picture taken to compare two tiles. The left tile is drawn while the player is moving, and the right tile is drawn while the player is standing.

This brings me a problem that might be causing this: With a sprite, shouldnt it be rendering it the same? Something is causing it to get distorted and that could be whats causing the extra row of pixels.

Thanks again.

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