Compare commits
2 Commits
49fee6ded8
...
b01fa46b8a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b01fa46b8a | ||
|
|
4759a5419e |
227
main.cpp
227
main.cpp
@@ -181,129 +181,132 @@ static bool CheckAllFencesCollision(float roverXMin, float roverXMax, float rove
|
||||
}
|
||||
|
||||
static void UpdateRover(const std::vector<Plot>& 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<std::pair<float, float>> 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<std::pair<float, float>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1147,8 +1150,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);
|
||||
@@ -1165,8 +1168,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);
|
||||
@@ -1181,8 +1184,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);
|
||||
@@ -1197,8 +1200,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);
|
||||
@@ -1460,7 +1463,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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user