Added collission detection
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
+55
-4
@@ -792,20 +792,71 @@ void display()
|
||||
playerDeltaY = sin(playerAngle) * 5.0f;
|
||||
}
|
||||
|
||||
// We need to check if we're going to bump into a wall
|
||||
// and prevent that from happening.
|
||||
|
||||
int movementRestriction = 5;
|
||||
|
||||
int xOffset = 0;
|
||||
if (playerDeltaX < 0)
|
||||
{
|
||||
xOffset = -movementRestriction;
|
||||
}
|
||||
else
|
||||
{
|
||||
xOffset = movementRestriction;
|
||||
}
|
||||
|
||||
int yOffset = 0;
|
||||
if (playerDeltaY < 0)
|
||||
{
|
||||
yOffset = -movementRestriction;
|
||||
}
|
||||
else
|
||||
{
|
||||
yOffset = movementRestriction;
|
||||
}
|
||||
|
||||
// Convert pixel positions to map grid coordinates dynamically using mapSize
|
||||
int iPlayerX = (int)playerX / mapSize;
|
||||
int iPlayerY = (int)playerY / mapSize;
|
||||
|
||||
int iPlayerX_add_xOffset = (int)(playerX + xOffset) / mapSize;
|
||||
int iPlayerY_add_yOffset = (int)(playerY + yOffset) / mapSize;
|
||||
|
||||
int iPlayerX_sub_xOffset = (int)(playerX - xOffset) / mapSize;
|
||||
int iPlayerY_sub_yOffset = (int)(playerY - yOffset) / mapSize;
|
||||
|
||||
// ? Move forward
|
||||
if (Keys.w == 1)
|
||||
{
|
||||
// We change the X and Y values based on our delta, which is calculated
|
||||
// during our rotation.
|
||||
playerX += playerDeltaX * movementMultiplier;
|
||||
playerY += playerDeltaY * movementMultiplier;
|
||||
if (map[iPlayerY * mapX + iPlayerX_add_xOffset] == 0)
|
||||
{
|
||||
|
||||
playerX += playerDeltaX * movementMultiplier;
|
||||
}
|
||||
|
||||
if (map[iPlayerY_add_yOffset * mapX + iPlayerX] == 0)
|
||||
{
|
||||
|
||||
playerY += playerDeltaY * movementMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
// ? Move backwards
|
||||
if (Keys.s == 1)
|
||||
{
|
||||
playerX -= playerDeltaX * movementMultiplier;
|
||||
playerY -= playerDeltaY * movementMultiplier;
|
||||
if (map[iPlayerY * mapX + iPlayerX_sub_xOffset] == 0)
|
||||
{
|
||||
playerX -= playerDeltaX * movementMultiplier;
|
||||
}
|
||||
|
||||
if (map[iPlayerY_sub_yOffset * mapX + iPlayerX] == 0)
|
||||
{
|
||||
playerY -= playerDeltaY * movementMultiplier;
|
||||
}
|
||||
}
|
||||
glutPostRedisplay();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user