A simple gravity simulation inspired by "Spacewar!". I'm going to make this into a Spacewar! clone.

This commit is contained in:
Barra Ó Catháin 2023-03-15 23:27:36 +00:00
parent 6f966fec57
commit 6df411a1a2
1 changed files with 78 additions and 157 deletions

View File

@ -1,7 +1,8 @@
// SDL Experiment 04, Barra Ó Catháin. // SDL Experiment 05, Barra Ó Catháin.
// =================================== // ===================================
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h> #include <SDL2/SDL_timer.h>
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -43,28 +44,22 @@ void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32
} }
} }
// Get the largest radius for a circle that can fit in the width and height of a rectangle:
static inline int getRadius(int width, int height)
{
return (width/2 < height/2) ? width/2 : height/2;
}
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
SDL_Event event; SDL_Event event;
bool quit = false; bool quit = false;
int width = 0, height = 0; int width = 0, height = 0;
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
long positionX = 320, positionY = 320, velocityX = 0, velocityY = 0; double posX = 0, posY = 0;
bool accelLeft = false, accelRight = false, accelUp = false, accelDown = false; long positionX = 0, positionY = 0;
double velocityX = 20, velocityY = 0, gravityX = 0, gravityY = 0, gravityMagnitude = 0, gravityAcceleration = 0;
// Initialize the SDL library, video, sound, and input: // Initialize the SDL library, video, sound, and input:
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{ {
printf("SDL Initialization Error: %s\n", SDL_GetError()); printf("SDL Initialization Error: %s\n", SDL_GetError());
} }
// Create an SDL window and rendering context in that window: // Create an SDL window and rendering context in that window:
SDL_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, 0); SDL_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
@ -84,70 +79,6 @@ int main(int argc, char ** argv)
quit = true; quit = true;
break; break;
} }
// Begin accelerating in the direction that is pressed:
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
{
accelLeft = true;
break;
}
case SDLK_RIGHT:
{
accelRight = true;
break;
}
case SDLK_UP:
{
accelUp = true;
break;
}
case SDLK_DOWN:
{
accelDown = true;
break;
}
default:
{
break;
}
}
break;
}
// Stop accelerating in the direction that is released:
case SDL_KEYUP:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
{
accelLeft = false;
break;
}
case SDLK_RIGHT:
{
accelRight = false;
break;
}
case SDLK_UP:
{
accelUp = false;
break;
}
case SDLK_DOWN:
{
accelDown = false;
break;
}
default:
{
break;
}
}
break;
}
default: default:
{ {
break; break;
@ -155,106 +86,96 @@ int main(int argc, char ** argv)
} }
} }
// Accelerate: // Store the window's current width and height:
if(accelLeft)
{
velocityX -= 2;
}
if(accelRight)
{
velocityX += 2;
}
if(accelUp)
{
velocityY -= 2;
}
if(accelDown)
{
velocityY += 2;
}
// Limit velocity:
if(velocityX > 10)
{
velocityX = 10;
}
if(velocityX < -10)
{
velocityX = -10;
}
// Deccelerate:
if(velocityX != 0)
{
velocityX = (velocityX < 0) ? velocityX + 1 : velocityX - 1;
}
if(positionY < 640)
{
velocityY += 1;
}
if(positionY >= 640 && velocityY !=0 && !accelUp)
{
velocityY *= -1;
velocityY += 1;
}
// Move the position:
if(velocityX != 0)
{
positionX += velocityX;
if(positionX < 0)
{
positionX = 640;
}
if(positionX > 640)
{
positionX = 0;
}
}
if(velocityY != 0)
{
positionY += velocityY;
if(positionY < 0)
{
positionY = 0;
}
if(positionY > 640)
{
positionY = 640;
}
}
// Store the window's current width and height:
SDL_GetWindowSize(window, &width, &height); SDL_GetWindowSize(window, &width, &height);
// Calculate the gravity vector:
// Calculate the vector between the star and ship:
gravityX = (width/2 - posX);
gravityY = (height/2 - posY);
// Make it into a unit vector:
gravityMagnitude = sqrt(pow(gravityX, 2) + pow(gravityY, 2));
gravityX /= gravityMagnitude;
gravityY /= gravityMagnitude;
// Calculate the gravity between them and scale the vector:
if(gravityMagnitude > 15)
{
gravityAcceleration = 2 * (2500 / pow(gravityMagnitude, 2));
gravityX *= gravityAcceleration;
gravityY *= gravityAcceleration;
}
else
{
gravityAcceleration = 0.02 * (2500 / pow(gravityMagnitude, 2));
gravityX *= gravityAcceleration;
gravityY *= gravityAcceleration;
}
// Wrap the position if the ship goes off-screen:
if(posX > width + 15)
{
posX = 0;
velocityX *= 0.6;
}
if(posY > height + 15)
{
posY = 0;
velocityY *= 0.6;
}
if(posX < -15)
{
posX = width;
velocityX *= 0.6;
}
if(posY < -15)
{
posY = height;
velocityY *= 0.6;
}
// Calculate the new current velocity:
velocityX += gravityX;
velocityY += gravityY;
// Calculate the new position:
posX += velocityX;
posY += velocityY;
positionX = (long)posX;
positionY = (long)posY;
// Set the colour to black: // Set the colour to black:
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Clear the screen, filling it with black: // Clear the screen, filling it with black:
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// Set the colour to green:
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
// Draw a circle "ship" around the current position:
DrawCircle(renderer, positionX, positionY, 15);
// Set the colour to yellow: // Set the colour to yellow:
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
// Draw a circle around the position pointer:
DrawCircle(renderer, positionX, positionY, 15);
// Draw a circle in the center as the star:
DrawCircle(renderer, width/2, height/2, 30);
// Draw a line representing the velocity:
SDL_RenderDrawLine(renderer, positionX, positionY, (long)((posX + velocityX * 3)), (long)((posY + velocityY * 3)));
// Present the rendered graphics: // Present the rendered graphics:
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
// Delay enough so that we run at 144 frames: // Delay enough so that we run at 60 frames:
SDL_Delay(1000 / 60); SDL_Delay(1000 / 60);
} }
return 0; return 0;
} }
// =========================================================================================== // ===========================================================================================
// Local Variables: // Local Variables:
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-04.c -lm" // compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-05.c -lm"
// End: // End: