Zadanie 2 improve code

This commit is contained in:
Władysław Kuczerenko 2024-06-10 11:34:14 +02:00
parent 8f61b79431
commit 8e12d9f86d

View File

@ -25,11 +25,10 @@ const float initialAngle = 45.0f;
const int windowWidth = 1200; const int windowWidth = 1200;
const int windowHeight = 800; const int windowHeight = 800;
float startTime;
bool isLaunched = false; bool isLaunched = false;
const float delay = 0.0f; // 2-sekundowe opóźnienie
const int ballsAmount = 10;
struct Ball { struct Ball {
float x, y, z; float x, y, z;
@ -42,27 +41,29 @@ float gravity = initialGravity;
float airResistance = initialAirResistance; float airResistance = initialAirResistance;
float speed = initialSpeed; float speed = initialSpeed;
float angle = initialAngle * M_PI / 180.0f; float angle = initialAngle * M_PI / 180.0f;
float ballsAngle[ballsAmount] = {};
glm::vec3 cameraPos = initialCameraPos; glm::vec3 cameraPos = initialCameraPos;
glm::vec3 cameraTarget = initialCameraTarget; glm::vec3 cameraTarget = initialCameraTarget;
glm::vec3 cameraUp = initialCameraUp; glm::vec3 cameraUp = initialCameraUp;
GLFWwindow* window; GLFWwindow* window;
std::vector<Ball> balls(10); // Wektor przechowujący 10 kul std::vector<Ball> balls(ballsAmount); // Wektor przechowujący ballsAmount kul
GLuint shaderProgram; GLuint shaderProgram;
GLuint VAO, VBO; GLuint VAO, VBO;
void initializeBalls() { void initializeBalls() {
srand(static_cast<unsigned>(time(0))); // Inicjalizujemy generator liczb losowych srand(static_cast<unsigned>(time(0)));
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
float localAngle = angle + static_cast<float>(rand() % 60); // <angle, angle + 60> float localAngle = angle + static_cast<float>(rand() % 60); // <angle, angle + 60>
float localSpeed = speed + static_cast<float>(rand() % 5); float localSpeed = speed + static_cast<float>(rand() % 5);
ballsAngle[i] = localAngle;
balls[i] = { balls[i] = {
.x = 0.0f, .x = 0.0f,
.y = 2.0f * i, // Ustawienie kul na tej samej wysokości początkowej .y = 2.0f * i,
.z = 0.0f, .z = 0.0f,
.vx = static_cast<float>(localSpeed * cos(localAngle * M_PI / 180.0f)), .vx = static_cast<float>(localSpeed * cos(localAngle * M_PI / 180.0f)),
.vy = static_cast<float>(localSpeed * sin(localAngle * M_PI / 180.0f)), .vy = static_cast<float>(localSpeed * sin(localAngle * M_PI / 180.0f)),
@ -73,7 +74,6 @@ void initializeBalls() {
}; };
} }
startTime = glfwGetTime(); // Ustawienie początkowego czasu symulacji
isLaunched = false; isLaunched = false;
} }
@ -122,18 +122,23 @@ void imGuiRenderBallsControlsFrame(){
void imGuiBallsThrowingStateControlsFrame(){ void imGuiBallsThrowingStateControlsFrame(){
ImGui::Begin("Balls Throwing State"); ImGui::Begin("Balls Throwing State");
for(int i = 0; i < 10; i++){
ImGui::Text("Ball %d Position: (%.2f, %.2f, %.2f)", i + 1, balls[i].x, balls[i].y, balls[i].z);
}
if (ImGui::Button("Reset Balls")) { if (ImGui::Button("Reset Balls")) {
initializeBalls(); initializeBalls();
} }
if (ImGui::Button("Launch Balls")) { if (ImGui::Button("Launch Balls")) {
isLaunched = true; isLaunched = true;
startTime = glfwGetTime(); // Resetowanie czasu startu }
ImGui::End();
}
void imGuiBallsMetricFrame(){
ImGui::Begin("Balls Metric");
for(int i = 0; i < ballsAmount; i++){
ImGui::Text("Ball %d Position: (%.2f, %.2f, %.2f)", i + 1, balls[i].x, balls[i].y, balls[i].z);
ImGui::Text("Ball %d Angle: %.2f", i + 1, ballsAngle[i]);
} }
ImGui::End(); ImGui::End();
@ -147,6 +152,7 @@ void renderImGui(){
imGuiRenderBallsControlsFrame(); imGuiRenderBallsControlsFrame();
imGuiRenderCameraControlsFrame(); imGuiRenderCameraControlsFrame();
imGuiBallsThrowingStateControlsFrame(); imGuiBallsThrowingStateControlsFrame();
imGuiBallsMetricFrame();
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@ -233,8 +239,8 @@ void display() {
glm::mat4 view = glm::lookAt(cameraPos, cameraTarget, cameraUp); glm::mat4 view = glm::lookAt(cameraPos, cameraTarget, cameraUp);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)windowWidth / (float)windowHeight, 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)windowWidth / (float)windowHeight, 0.1f, 100.0f);
// Renderowanie każdej z 10 kul // Renderowanie każdej z ballsAmount kul
for (int i = 0; i < 10; ++i) { for (int i = 0; i < ballsAmount; ++i) {
Ball &ball = balls[i]; // Uzyskanie dostępu do danej kuli Ball &ball = balls[i]; // Uzyskanie dostępu do danej kuli
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(ball.x, ball.y, ball.z)); glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(ball.x, ball.y, ball.z));
@ -260,10 +266,9 @@ void display() {
void update() { void update() {
float currentTime = glfwGetTime();
float dt = 0.01f; float dt = 0.01f;
if (isLaunched && (currentTime - startTime >= delay)) { if (isLaunched) {
for (auto &ball : balls) { for (auto &ball : balls) {
rungeKuttaStep(ball, dt); rungeKuttaStep(ball, dt);
} }