• Create Account

## Collision Problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1povilaslt2  Members

489
Like
0Likes
Like

Posted 15 December 2012 - 01:13 PM

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;
}


Edited by povilaslt2, 15 December 2012 - 01:14 PM.

### #2Gavin Williams  Members

963
Like
0Likes
Like

Posted 15 December 2012 - 11:25 PM

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 ?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.