Collision Problem

Started by
0 comments, last by Gavin Williams 11 years, 4 months ago
Hello, i thinking about complete tilemap game, but i have one problem. Problem is with collision, i can't make normal collision for my game, here's the code:


for (int x = 0; x < tilemap.GetLength(1); x++)
{
for (int y = 0; y < tilemap.GetLength(0); y++)
{
if (tilemap[y, x] == -1) continue;
if (man.Position.Y > (y * TextureHeight) - TextureHeight &amp;&amp; man.Position.Y < y * TextureHeight)
tilesaty.Add(new Vector2(x * 40, y * 40));
}
}



foreach (Vector2 pos in tilesaty)
{
Rectangle rect = new Rectangle((int)pos.X, (int)pos.Y, TextureWidth, TextureHeight);
Rectangle rect2 = new Rectangle((int)man.Position.X, (int)man.Position.Y, (int)(man.mSpriteTexture.Width * man.Scale), (int)(man.mSpriteTexture.Height * man.Scale));
if (rect2.Right >= rect.Right &amp;&amp; rect2.Left < rect.Right)
man.Position.X -= 1;
}
Advertisement
Can I just make some comments ...

* It is conventional to put x before y, and generally have your variables in alphabetical order where possible and appropriate.
* Having literals such as 40 in your code can sometimes introduce problems because for instance, your TextureHeight might change and that Vector constructed will no longer be correct. You should use class variables, or local variables or consts. Or at least comment next to where you use those literals to make it clear for future reading what the numbers represent. I assume that 40 is your TextureHeight, if that's so, then you should just use TextureHeight var.

I find the way you have written this confusing. I had a few questions, but then decided that my questioning was confusing .. so if you just step through the code, where do the numbers look ok and where do they look wrong ?

This topic is closed to new replies.

Advertisement