So I am having issues with my platformer that I am developing. I can detect collisions fine when there are a line of parralel blocks or even single blocks. The issue I have is when blocks form an L shape.
[] for example.
[]
When my character runs into the wall, he sort of just squeaks through and gets zoomed out. I've tried using an else statement that just sends him back a couple pixels back, but it doesn't work because I don't think it accounts for one of the two blocks. Each block is it's own object.
Any help?
This collision code is in my Player class. This is the only detection I have so far in the game. I attempted to use and adjust code from a breakernoid tutorial I found somehwere, and I realized that the code doesn't totally work for what I am doing.
public void checkYCollisions(block[] platforms)
{
if (position.Y >= 700)
grounded = true;
else
grounded = false;
float Xradius = Width / 2;
float Yradius = Height / 2;
block collidedPlatform = null;
foreach (block p in platforms)
{
if ((position.X > (p.position.X - p.Width / 2 - Xradius )) &&
(position.X < (p.position.X + p.Width / 2 + Xradius )) &&
(position.Y > (p.position.Y - p.Height / 2 - Yradius )) &&
(position.Y < (p.position.Y + p.Height / 2 + Yradius)))
{
collidedPlatform = p;
break;
}
}
//collisions work for all side of blocks.
if (collidedPlatform != null)
{
if ((position.Y <
(collidedPlatform.position.Y - collidedPlatform.Height / 1.5 /*+ radius*/)))
{
grounded = true;
}
else if ((position.Y >
(collidedPlatform.position.Y + collidedPlatform.Height / 2 )))
{
if(y_vel < 0)
y_vel *= -1;
//player1.direction.Y = -1.0f * player1.direction.Y;
}
else
{
x_vel *= -2;
}
}
I am guessing the issue is because I only have one collided block selected at any time. So I don't really know how to fix it that I can have two blocks selected. Note, once again, this code works for all blocks that are by themselves, or just a horizontal(same x position) line of blocks. It's just that L situation where I run into a corner.