Zadanie 2 improve code
This commit is contained in:
parent
8f61b79431
commit
8e12d9f86d
@ -25,11 +25,10 @@ const float initialAngle = 45.0f;
|
||||
const int windowWidth = 1200;
|
||||
const int windowHeight = 800;
|
||||
|
||||
float startTime;
|
||||
bool isLaunched = false;
|
||||
|
||||
const float delay = 0.0f; // 2-sekundowe opóźnienie
|
||||
|
||||
const int ballsAmount = 10;
|
||||
|
||||
struct Ball {
|
||||
float x, y, z;
|
||||
@ -42,27 +41,29 @@ float gravity = initialGravity;
|
||||
float airResistance = initialAirResistance;
|
||||
float speed = initialSpeed;
|
||||
float angle = initialAngle * M_PI / 180.0f;
|
||||
|
||||
float ballsAngle[ballsAmount] = {};
|
||||
|
||||
glm::vec3 cameraPos = initialCameraPos;
|
||||
glm::vec3 cameraTarget = initialCameraTarget;
|
||||
glm::vec3 cameraUp = initialCameraUp;
|
||||
|
||||
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 VAO, VBO;
|
||||
|
||||
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) {
|
||||
float localAngle = angle + static_cast<float>(rand() % 60); // <angle, angle + 60>
|
||||
float localSpeed = speed + static_cast<float>(rand() % 5);
|
||||
|
||||
ballsAngle[i] = localAngle;
|
||||
|
||||
balls[i] = {
|
||||
.x = 0.0f,
|
||||
.y = 2.0f * i, // Ustawienie kul na tej samej wysokości początkowej
|
||||
.y = 2.0f * i,
|
||||
.z = 0.0f,
|
||||
.vx = static_cast<float>(localSpeed * cos(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;
|
||||
}
|
||||
|
||||
@ -122,18 +122,23 @@ void imGuiRenderBallsControlsFrame(){
|
||||
void imGuiBallsThrowingStateControlsFrame(){
|
||||
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")) {
|
||||
initializeBalls();
|
||||
}
|
||||
|
||||
if (ImGui::Button("Launch Balls")) {
|
||||
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();
|
||||
@ -147,6 +152,7 @@ void renderImGui(){
|
||||
imGuiRenderBallsControlsFrame();
|
||||
imGuiRenderCameraControlsFrame();
|
||||
imGuiBallsThrowingStateControlsFrame();
|
||||
imGuiBallsMetricFrame();
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
@ -233,8 +239,8 @@ void display() {
|
||||
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);
|
||||
|
||||
// Renderowanie każdej z 10 kul
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// Renderowanie każdej z ballsAmount kul
|
||||
for (int i = 0; i < ballsAmount; ++i) {
|
||||
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));
|
||||
|
||||
@ -260,10 +266,9 @@ void display() {
|
||||
|
||||
|
||||
void update() {
|
||||
float currentTime = glfwGetTime();
|
||||
float dt = 0.01f;
|
||||
|
||||
if (isLaunched && (currentTime - startTime >= delay)) {
|
||||
if (isLaunched) {
|
||||
for (auto &ball : balls) {
|
||||
rungeKuttaStep(ball, dt);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user