From 4759a5419ed833e528cd94e3f9d41a6ac28d3c1c Mon Sep 17 00:00:00 2001 From: Pc Date: Sun, 19 Jan 2025 18:06:35 +0100 Subject: [PATCH] Kolizja podczas obrotu ktora dziala (kod wyglada jak kupa i nie wiem co robi ale dziala) --- main.cpp | 227 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 115 insertions(+), 112 deletions(-) diff --git a/main.cpp b/main.cpp index c2abc90..6e118e7 100644 --- a/main.cpp +++ b/main.cpp @@ -181,129 +181,132 @@ static bool CheckAllFencesCollision(float roverXMin, float roverXMax, float rove } static void UpdateRover(const std::vector& fences) { - // Przyspieszanie w przód - if (keyWPressed) { - velocity += acceleration; - if (velocity > maxSpeed) velocity = maxSpeed; - } - // Przyspieszanie w tył - else if (keySPressed) { - velocity -= acceleration; - if (velocity < -maxSpeed) velocity = -maxSpeed; - } - // Hamowanie (wytracanie prędkości z powodu tarcia) - else { - if (velocity > 0) { - velocity -= friction; - if (velocity < 0) velocity = 0; - } - else if (velocity < 0) { - velocity += friction; - if (velocity > 0) velocity = 0; - } - } + // Przyspieszanie w przód + if (keyWPressed) { + velocity += acceleration; + if (velocity > maxSpeed) velocity = maxSpeed; + } + // Przyspieszanie w tył + else if (keySPressed) { + velocity -= acceleration; + if (velocity < -maxSpeed) velocity = -maxSpeed; + } + // Hamowanie (wytracanie prędkości z powodu tarcia) + else { + if (velocity > 0) { + velocity -= friction; + if (velocity < 0) velocity = 0; + } + else if (velocity < 0) { + velocity += friction; + if (velocity > 0) velocity = 0; + } + } - // Obracanie (rotacja z driftowaniem) - if (keyAPressed) { - rotationVelocity += rotationAcceleration; - if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed; - } - else if (keyDPressed) { - rotationVelocity -= rotationAcceleration; - if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed; - } - else { - // Jeśli żadna z klawiszy A/D nie jest wciśnięta, to stopniowo spowalniamy rotację (drift) - float driftFactor = 0.1f; // Mniejsza wartość = dłuższy drift - if (rotationVelocity > 0) { - rotationVelocity -= rotationFriction * driftFactor; - if (rotationVelocity < 0) rotationVelocity = 0; - } - else if (rotationVelocity < 0) { - rotationVelocity += rotationFriction * driftFactor; - if (rotationVelocity > 0) rotationVelocity = 0; - } - } + // Obracanie (rotacja z driftowaniem) + if (keyAPressed) { + rotationVelocity += rotationAcceleration; + if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed; + } + else if (keyDPressed) { + rotationVelocity -= rotationAcceleration; + if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed; + } + else { + // Jeśli żadna z klawiszy A/D nie jest wciśnięta, to stopniowo spowalniamy rotację (drift) + float driftFactor = 0.1f; // Mniejsza wartość = dłuższy drift + if (rotationVelocity > 0) { + rotationVelocity -= rotationFriction * driftFactor; + if (rotationVelocity < 0) rotationVelocity = 0; + } + else if (rotationVelocity < 0) { + rotationVelocity += rotationFriction * driftFactor; + if (rotationVelocity > 0) rotationVelocity = 0; + } + } - // Wyliczenie nowej pozycji na podstawie prędkości - float radRotation = Rotation * GL_PI / 180.0f; // Przeliczamy rotację na radiany - float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X - float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z + // Wyliczenie nowej pozycji na podstawie prędkości + float radRotation = Rotation * GL_PI / 180.0f; // Przeliczamy rotację na radiany + float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X + float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z - // Wymiary łazika (połówki w osi X i Z) - const float roverHalfWidthX = 19.0f; // 38/2 - const float roverHalfLengthZ = 12.0f; // 24/2 + // Wymiary łazika (połówki w osi X i Z) + const float roverHalfWidthX = 19.0f; // 38/2 + const float roverHalfLengthZ = 12.0f; // 24/2 - // Wyliczenie obszaru zajmowanego przez łazik - float roverXMin = newSides - roverHalfWidthX; - float roverXMax = newSides + roverHalfWidthX; - float roverZMin = newFoward - roverHalfLengthZ; - float roverZMax = newFoward + roverHalfLengthZ; + // Wyliczenie obszaru zajmowanego przez łazik + float roverXMin = newSides - roverHalfWidthX; + float roverXMax = newSides + roverHalfWidthX; + float roverZMin = newFoward - roverHalfLengthZ; + float roverZMax = newFoward + roverHalfLengthZ; - // Sprawdzanie kolizji przed aktualizacją pozycji - if (Kolizja == true) { - if (CheckAllFencesCollision(roverZMin, roverZMax, roverXMin, roverXMax, fences)) { - // Jeśli jest kolizja, zatrzymujemy łazik - velocity = 0.0f; - cout << "Kolizja podczas ruchu\n"; - } - else { - // Jeśli brak kolizji, aktualizujemy pozycję - Sides = newSides; - Foward = newFoward; - } + // Sprawdzanie kolizji przed aktualizacją pozycji + if (!Kolizja == true) { + if (CheckAllFencesCollision(roverZMin, roverZMax, roverXMin, roverXMax, fences)) { + // Jeśli jest kolizja, zatrzymujemy łazik + velocity = 0.0f; + //cout << "Kolizja podczas ruchu\n"; + } + else { + // Jeśli brak kolizji, aktualizujemy pozycję + Sides = newSides; + Foward = newFoward; + } - // Sprawdzanie kolizji podczas obrotu - if (rotationVelocity != 0.0f) { - // Wyliczamy nową rotację - float newRotation = Rotation + rotationVelocity; - float radNewRotation = newRotation * GL_PI / 180.0f; + // Sprawdzanie kolizji podczas obrotu + if (rotationVelocity != 0.0f) { + // Wyliczamy nową rotację + float newRotation = Rotation + rotationVelocity; + float radNewRotation = newRotation * GL_PI / 180.0f; - // Obracamy narożniki łazika - std::vector> corners(4); - corners[0] = { Sides - roverHalfWidthX, Foward - roverHalfLengthZ }; // Lewy dolny - corners[1] = { Sides + roverHalfWidthX, Foward - roverHalfLengthZ }; // Prawy dolny - corners[2] = { Sides - roverHalfWidthX, Foward + roverHalfLengthZ }; // Lewy górny - corners[3] = { Sides + roverHalfWidthX, Foward + roverHalfLengthZ }; // Prawy górny - - // Obracamy wszystkie narożniki + // Obracamy narożniki łazika + std::vector> corners(4); + corners[0] = {Sides - roverHalfWidthX, Foward - roverHalfLengthZ}; // Lewy dolny + corners[1] = {Sides + roverHalfWidthX, Foward - roverHalfLengthZ}; // Prawy dolny + corners[2] = {Sides - roverHalfWidthX, Foward + roverHalfLengthZ}; // Lewy górny + corners[3] = {Sides + roverHalfWidthX, Foward + roverHalfLengthZ}; // Prawy górny + bool collisionDetected = false; + // Obracamy wszystkie narożniki for (auto& corner : corners) { float x = corner.first; float z = corner.second; corner.first = Sides + (x - Sides) * cos(radNewRotation) - (z - Foward) * sin(radNewRotation); corner.second = Foward + (x - Sides) * sin(radNewRotation) + (z - Foward) * cos(radNewRotation); - } - // Sprawdzamy kolizję na podstawie obróconych narożników - bool collisionDetected = false; - for (const auto& corner : corners) { + + // Sprawdzamy kolizję na podstawie obróconych narożników + + if (CheckAllFencesCollision(corner.first, corner.first, corner.second, corner.second, fences)) { collisionDetected = true; break; } + if (CheckAllFencesCollision( corner.second, corner.second, corner.first, corner.first, fences)) { + collisionDetected = true; + break; + } } - if (collisionDetected) { - rotationVelocity = 0.0f; // Zatrzymujemy obrót - cout << "Kolizja podczas obrotu\n"; - } - else { - // Aktualizujemy rotację, jeśli nie ma kolizji - Rotation = newRotation; - if (Rotation >= 360.0f) Rotation -= 360.0f; - if (Rotation < 0.0f) Rotation += 360.0f; - } - } - } - else { - // Jeśli kolizje są wyłączone, aktualizujemy wszystko bez sprawdzania - Sides = newSides; - Foward = newFoward; - Rotation += rotationVelocity; - if (Rotation >= 360.0f) Rotation -= 360.0f; - if (Rotation < 0.0f) Rotation += 360.0f; - } + if (collisionDetected) { + rotationVelocity = 0.0f; // Zatrzymujemy obrót + //cout << "Kolizja podczas obrotu\n"; + } else { + // Aktualizujemy rotację, jeśli nie ma kolizji + Rotation = newRotation; + if (Rotation >= 360.0f) Rotation -= 360.0f; + if (Rotation < 0.0f) Rotation += 360.0f; + } + } + } + else { + // Jeśli kolizje są wyłączone, aktualizujemy wszystko bez sprawdzania + Sides = newSides; + Foward = newFoward; + Rotation += rotationVelocity; + if (Rotation >= 360.0f) Rotation -= 360.0f; + if (Rotation < 0.0f) Rotation += 360.0f; + } } @@ -1145,8 +1148,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); @@ -1163,8 +1166,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) bitmapData = LoadBitmapFile((char*)"res/img/grass02.bmp", &bitmapInfoHeader); glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); @@ -1179,8 +1182,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) bitmapData = LoadBitmapFile((char*)"res/img/barnroof.bmp", &bitmapInfoHeader); glBindTexture(GL_TEXTURE_2D, texture[2]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); @@ -1195,8 +1198,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) bitmapData = LoadBitmapFile((char*)"res/img/brickwall.bmp", &bitmapInfoHeader); glBindTexture(GL_TEXTURE_2D, texture[3]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); @@ -1458,7 +1461,7 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { SetDlgItemText(hDlg, IDC_ERROR1 + i, (LPCSTR)gluErrorString(glError)); i++; } while (i < 6 && glError != GL_NO_ERROR); - + return (TRUE);