Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Collision Problem

Started by povilaslt2 Dec 15, 2012 at 7:13 PM 0 replies 850+ views
Original Post
povilaslt2
povilaslt2
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 && 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 && rect2.Left < rect.Right)
man.Position.X -= 1;
}
Gavin Williams
Gavin Williams
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 ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.