optimize
This commit is contained in:
parent
df849d2fb0
commit
89c497da73
|
|
@ -27,19 +27,13 @@ int main(void)
|
|||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area");
|
||||
|
||||
// Box A: Moving box
|
||||
Rectangle boxA = { 10, GetScreenHeight()/2.0f - 50, 200, 100 };
|
||||
int boxASpeedX = 4;
|
||||
int r1 = 40;
|
||||
int c1x = 200;
|
||||
int c1y = 200;
|
||||
|
||||
// Box B: Mouse moved box
|
||||
Rectangle boxB = { GetScreenWidth()/2.0f - 30, GetScreenHeight()/2.0f - 30, 60, 60 };
|
||||
int r2 = 40;
|
||||
|
||||
Rectangle boxCollision = { 0 }; // Collision rectangle
|
||||
|
||||
int screenUpperLimit = 40; // Top menu limits
|
||||
|
||||
bool pause = false; // Movement pause
|
||||
bool collision = false; // Collision detection
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//----------------------------------------------------------
|
||||
|
|
@ -47,33 +41,11 @@ int main(void)
|
|||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//-----------------------------------------------------
|
||||
// Move box if not paused
|
||||
if (!pause) boxA.x += boxASpeedX;
|
||||
|
||||
// Bounce box on x screen limits
|
||||
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1;
|
||||
int c2x = GetMouseX();
|
||||
int c2y = GetMouseY();
|
||||
|
||||
// Update player-controlled-box (box02)
|
||||
boxB.x = GetMouseX() - boxB.width/2;
|
||||
boxB.y = GetMouseY() - boxB.height/2;
|
||||
|
||||
// Make sure Box B does not go out of move area limits
|
||||
if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width;
|
||||
else if (boxB.x <= 0) boxB.x = 0;
|
||||
|
||||
if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height;
|
||||
else if (boxB.y <= screenUpperLimit) boxB.y = (float)screenUpperLimit;
|
||||
|
||||
// Check boxes collision
|
||||
collision = CheckCollisionRecs(boxA, boxB);
|
||||
|
||||
// Get collision rectangle (only on collision)
|
||||
if (collision) boxCollision = GetCollisionRec(boxA, boxB);
|
||||
|
||||
// Pause Box A movement
|
||||
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
|
||||
//-----------------------------------------------------
|
||||
|
||||
// Draw
|
||||
|
|
@ -82,25 +54,13 @@ int main(void)
|
|||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK);
|
||||
Color col = BLUE;
|
||||
if (CheckCollisionCircles((Vector2) { c1x, c1y }, r1, (Vector2) { c2x, c2y }, r2)) col = YELLOW;
|
||||
else col = BLUE;
|
||||
DrawCircle(c1x, c1y, r1, RED);
|
||||
DrawCircle(c2x, c2y, r2, col);
|
||||
|
||||
DrawRectangleRec(boxA, GOLD);
|
||||
DrawRectangleRec(boxB, BLUE);
|
||||
|
||||
if (collision)
|
||||
{
|
||||
// Draw collision area
|
||||
DrawRectangleRec(boxCollision, LIME);
|
||||
|
||||
// Draw collision message
|
||||
DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK);
|
||||
|
||||
// Draw collision area
|
||||
DrawText(TextFormat("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK);
|
||||
}
|
||||
|
||||
// Draw help instructions
|
||||
DrawText("Press SPACE to PAUSE/RESUME", 20, screenHeight - 35, 20, LIGHTGRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
|
|
|
|||
|
|
@ -2233,9 +2233,10 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa
|
|||
float dx = center2.x - center1.x; // X distance between centers
|
||||
float dy = center2.y - center1.y; // Y distance between centers
|
||||
|
||||
float distance = sqrtf(dx*dx + dy*dy); // Distance between centers
|
||||
float distanceSquared = dx * dx + dy * dy; // Distance between centers squared
|
||||
float radiusSum = radius1 + radius2;
|
||||
|
||||
if (distance <= (radius1 + radius2)) collision = true;
|
||||
collision = (distanceSquared <= (radiusSum * radiusSum));
|
||||
|
||||
return collision;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user