So, my issue is how to properly do some Collision Detection on obstacles. Take a look on the picture below:

Basically, this is my player and I want him to collide with that kind of obstacles from every side. So there are 4 cases as shown on the picture.
For example, if we are on case 2, then this means that our player was falling from a higher position and we need to stop him on the top of that obstacle.
Below is my code's logic which is trying to detect case 1. In that case our player should stop moving and if he tried to jump on the obstacle but didnt jump high enough, he should fall back.
Heres a part of the code:
List<Obstacle> obstacles;
Player player;
Texture2D texture;
foreach (Obstacle ob in obstacles)
{
// We are either in case 1 or case 3
if ((position.Y + texture.Height / 2 > ob.Position.Y) && (position.Y + texture.Height / 2 < ob.Position.Y + ob.Texture.Height))
{
if((position.X + texture.Width > ob.Position.X) && (position.X < ob.Position.X))
{
// We detected case 1 and we could knowback the player or stop him from moving
}
}
}
- position is a Vector2 (X, Y) which holds the current position of our player or the obstacle.
- Texute.Height returns the Height of the obstacle.
So, my thought was that firstly I had to seperate the 4 cases into 2 cases. I groupped up case 1 with case 3 and case 2 with case 4.
So, applying the above logic for all the cases causes many bugs and basically it became quite complex.
I really doubt it that this is the best way to go, but I was not able of finding some good examples around in the web. So, my (finally) question to you guys is if there exists some better way to do such collision detection or I will have to keep struggling on the technique until everything works.
Later on I will have to add collision detection for enemies-obstacles, player-enemies and the above CD player-obstacles.
Some enlightenment would really help me move forward. Anything that you could add or notice or comment is welcome.
Thanks in advance for any kind of replies
This post has been edited by s0me0ne: 17 February 2013 - 06:31 AM

New Topic/Question
Reply



MultiQuote








|