Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created December 20, 2024 09:15
Show Gist options
  • Save ProfAndreaPollini/2f95da8ab155936f28a5cf15fcfbf6c9 to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/2f95da8ab155936f28a5cf15fcfbf6c9 to your computer and use it in GitHub Desktop.
Moto di una pallina con c++ e raylib : cinematica e rimbalzi a schermo
#include <array>
#include <vector>
#include "raylib.h"
#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 800
using pallina_info = std::array<float,4>;
int main() {
std::vector<pallina_info> palline;
std::array<float,4> obj; // x,y, vx, vy
obj[0]=50;
obj[1] = 60;
obj[2] = GetRandomValue(50,250);
obj[3] = 100;
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Falling Sand Simulation");
SetTargetFPS(30); // Set our game to run at 60 frames-per-second
float dt = 0;
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
dt = GetFrameTime();
obj[1] = obj[1] + obj[3]*dt; // y = y + vy*dt
obj[0] = obj[0] + obj[2]*dt;
if(obj[1]+ 30 > SCREEN_HEIGHT or obj[1]-30 <0) {
obj[3] *= -1;
}
if(obj[0]+ 30 > SCREEN_WIDTH or obj[0]-30 <0) {
obj[2] *= -1;
}
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText(TextFormat("dt = %.4f",dt),100,30,32,BLACK);
DrawCircle(obj[0],obj[1],30,RED);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment