Hello, I'm pretty new to programming, and I'm currently making a 2D game. The problem I'm having has to do with collision. I'm trying to make collision with boxes on screen, and it is working for the most part. However, when I move my player Left and Down Diagonally towards the right side of the box, I go through it. This also occurs when I move Left and Up Diagonally toward the bottom of the box. I've been trying to fix it for a while now, but I'm not having any luck. Here's the code:
if (dbSpriteExist(1) == 1 && dbLeftKey() == 1 && dbSpriteX(1) - dbSpriteWidth(1)/2 > dbSpriteX(101) + dbSpriteWidth(101)) // This is collision with the boundaries
{
bool move = true; //Collision withe the box is below
for (int x = 150 ; x <= 175 ; x++)
{
if (dbSpriteExist(x) == 1 && dbSpriteCollision(1,x) == 1)
{
if ((dbSpriteX(1) - (dbSpriteWidth(1)/2)) >= (dbSpriteX(x) + (dbSpriteWidth(x)/2)))
{
move = false;
}
}
}
if (move == true)
{
int x = dbSpriteAngle(1);
dbRotateSprite(1,270);
dbMoveSprite(1,speed);
dbRotateSprite(1,x);
}
}
if (dbSpriteExist(1) == 1 && dbUpKey() == 1 && dbSpriteY(1) - dbSpriteHeight(1)/2 > dbSpriteY(100) + dbSpriteHeight(100)) // This is collision with the boundaries
{
bool move = true; //Collision withe the box is below
for (int x = 150 ; x <= 175 ; x++)
{
if (dbSpriteExist(x) == 1 && dbSpriteCollision(1,x) == 1)
{
if ((dbSpriteY(1) - (dbSpriteHeight(1)/2)) >= (dbSpriteY(x) + (dbSpriteHeight(x)/2)))
{
move = false;
}
}
}
if (move == true)
{
int x = dbSpriteAngle(1);
dbRotateSprite(1,0);
dbMoveSprite(1,speed);
dbRotateSprite(1,x);
}
}
if (dbSpriteExist(1) == 1 && dbDownKey() == 1 && dbSpriteY(1) + dbSpriteHeight(1)/2 < dbSpriteY(103)) // This is collision with the boundaries
{
bool move = true; //Collision withe the box is below
for (int x = 150 ; x <= 175 ; x++)
{
if (dbSpriteExist(x) == 1 && dbSpriteCollision(1,x) == 1)
{
if ((dbSpriteY(1) + (dbSpriteHeight(1)/2)) <= (dbSpriteY(x) - (dbSpriteHeight(x)/2)))
{
move = false;
}
}
}
if (move == true)
{
int x = dbSpriteAngle(1);
dbRotateSprite(1,180);
dbMoveSprite(1,speed);
dbRotateSprite(1,x);
}
}
if (dbSpriteExist(1) == 1 && dbRightKey() == 1 && dbSpriteX(1) + dbSpriteWidth(1)/2 < dbSpriteX(102)) // This is collision with the boundaries
{
bool move = true; //Collision withe the box is below
for (int x = 150 ; x <= 175 ; x++)
{
if (dbSpriteExist(x) == 1 && dbSpriteCollision(1,x) == 1)
{
if ((dbSpriteX(1) + (dbSpriteWidth(1)/2)) <= (dbSpriteX(x) - (dbSpriteWidth(x)/2)))
{
move = false;
}
}
}
if (move == true)
{
int x = dbSpriteAngle(1);
dbRotateSprite(1,90);
dbMoveSprite(1,speed);
dbRotateSprite(1,x);
}
}