Created
December 20, 2024 09:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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