akwk/zadanie-2/main_new.cpp
Władysław Kuczerenko e0a271fc8a Stable version
2024-06-06 23:28:15 +02:00

105 lines
2.8 KiB
C++

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <vector>
#include <iostream>
const int windowWidth = 800;
const int windowHeight = 600;
struct Ball {
float x, y, z;
float vx, vy, vz;
float r, g, b;
};
// Ustawienia rzutu ukośnego dla każdej kuli (kąt startowy, prędkość początkowa)
std::vector<Ball> balls;
void initializeBalls() {
for (int i = 0; i < 10; ++i) {
float angle = 15 + i * 6; // Kąty między 15 a 75 stopni
float speed = 10 + i; // Prędkości początkowe
Ball ball = {0, 0, 0, speed * cos(angle * M_PI / 180.0f), speed * sin(angle * M_PI / 180.0f), 0, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX};
balls.push_back(ball);
}
}
// Funkcja do integracji Rungego-Kutty
void rungeKuttaStep(Ball &ball, float dt) {
float k1vx = -0.1f * ball.vx;
float k1vy = -9.8f - 0.1f * ball.vy;
float k2vx = -0.1f * (ball.vx + 0.5f * dt * k1vx);
float k2vy = -9.8f - 0.1f * (ball.vy + 0.5f * dt * k1vy);
float k3vx = -0.1f * (ball.vx + 0.5f * dt * k2vx);
float k3vy = -9.8f - 0.1f * (ball.vy + 0.5f * dt * k2vy);
float k4vx = -0.1f * (ball.vx + dt * k3vx);
float k4vy = -9.8f - 0.1f * (ball.vy + dt * k3vy);
ball.vx += (dt / 6.0f) * (k1vx + 2.0f * k2vx + 2.0f * k3vx + k4vx);
ball.vy += (dt / 6.0f) * (k1vy + 2.0f * k2vy + 2.0f * k3vy + k4vy);
ball.x += ball.vx * dt;
ball.y += ball.vy * dt;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0);
for (const auto &ball : balls) {
glPushMatrix();
glTranslatef(ball.x, ball.y, ball.z);
glColor3f(ball.r, ball.g, ball.b);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
}
}
void update() {
float dt = 0.01f;
for (auto &ball : balls) {
rungeKuttaStep(ball, dt);
}
}
void setupOpenGL() {
glEnable(GL_DEPTH_TEST);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
}
int main() {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Animacja 10 kul - Rzut ukośny", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
initializeBalls();
setupOpenGL();
while (!glfwWindowShouldClose(window)) {
display();
update();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}