Kolizja podczas obrotu ktora dziala (kod wyglada jak kupa i nie wiem co robi ale dziala)
This commit is contained in:
225
main.cpp
225
main.cpp
@@ -181,129 +181,132 @@ static bool CheckAllFencesCollision(float roverXMin, float roverXMax, float rove
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void UpdateRover(const std::vector<Plot>& fences) {
|
static void UpdateRover(const std::vector<Plot>& fences) {
|
||||||
// Przyspieszanie w przód
|
// Przyspieszanie w przód
|
||||||
if (keyWPressed) {
|
if (keyWPressed) {
|
||||||
velocity += acceleration;
|
velocity += acceleration;
|
||||||
if (velocity > maxSpeed) velocity = maxSpeed;
|
if (velocity > maxSpeed) velocity = maxSpeed;
|
||||||
}
|
}
|
||||||
// Przyspieszanie w tył
|
// Przyspieszanie w tył
|
||||||
else if (keySPressed) {
|
else if (keySPressed) {
|
||||||
velocity -= acceleration;
|
velocity -= acceleration;
|
||||||
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
||||||
}
|
}
|
||||||
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
||||||
else {
|
else {
|
||||||
if (velocity > 0) {
|
if (velocity > 0) {
|
||||||
velocity -= friction;
|
velocity -= friction;
|
||||||
if (velocity < 0) velocity = 0;
|
if (velocity < 0) velocity = 0;
|
||||||
}
|
}
|
||||||
else if (velocity < 0) {
|
else if (velocity < 0) {
|
||||||
velocity += friction;
|
velocity += friction;
|
||||||
if (velocity > 0) velocity = 0;
|
if (velocity > 0) velocity = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obracanie (rotacja z driftowaniem)
|
// Obracanie (rotacja z driftowaniem)
|
||||||
if (keyAPressed) {
|
if (keyAPressed) {
|
||||||
rotationVelocity += rotationAcceleration;
|
rotationVelocity += rotationAcceleration;
|
||||||
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
|
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
|
||||||
}
|
}
|
||||||
else if (keyDPressed) {
|
else if (keyDPressed) {
|
||||||
rotationVelocity -= rotationAcceleration;
|
rotationVelocity -= rotationAcceleration;
|
||||||
if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed;
|
if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Jeśli żadna z klawiszy A/D nie jest wciśnięta, to stopniowo spowalniamy rotację (drift)
|
// 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
|
float driftFactor = 0.1f; // Mniejsza wartość = dłuższy drift
|
||||||
if (rotationVelocity > 0) {
|
if (rotationVelocity > 0) {
|
||||||
rotationVelocity -= rotationFriction * driftFactor;
|
rotationVelocity -= rotationFriction * driftFactor;
|
||||||
if (rotationVelocity < 0) rotationVelocity = 0;
|
if (rotationVelocity < 0) rotationVelocity = 0;
|
||||||
}
|
}
|
||||||
else if (rotationVelocity < 0) {
|
else if (rotationVelocity < 0) {
|
||||||
rotationVelocity += rotationFriction * driftFactor;
|
rotationVelocity += rotationFriction * driftFactor;
|
||||||
if (rotationVelocity > 0) rotationVelocity = 0;
|
if (rotationVelocity > 0) rotationVelocity = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wyliczenie nowej pozycji na podstawie prędkości
|
// Wyliczenie nowej pozycji na podstawie prędkości
|
||||||
float radRotation = Rotation * GL_PI / 180.0f; // Przeliczamy rotację na radiany
|
float radRotation = Rotation * GL_PI / 180.0f; // Przeliczamy rotację na radiany
|
||||||
float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X
|
float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X
|
||||||
float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z
|
float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z
|
||||||
|
|
||||||
// Wymiary łazika (połówki w osi X i Z)
|
// Wymiary łazika (połówki w osi X i Z)
|
||||||
const float roverHalfWidthX = 19.0f; // 38/2
|
const float roverHalfWidthX = 19.0f; // 38/2
|
||||||
const float roverHalfLengthZ = 12.0f; // 24/2
|
const float roverHalfLengthZ = 12.0f; // 24/2
|
||||||
|
|
||||||
// Wyliczenie obszaru zajmowanego przez łazik
|
// Wyliczenie obszaru zajmowanego przez łazik
|
||||||
float roverXMin = newSides - roverHalfWidthX;
|
float roverXMin = newSides - roverHalfWidthX;
|
||||||
float roverXMax = newSides + roverHalfWidthX;
|
float roverXMax = newSides + roverHalfWidthX;
|
||||||
float roverZMin = newFoward - roverHalfLengthZ;
|
float roverZMin = newFoward - roverHalfLengthZ;
|
||||||
float roverZMax = newFoward + roverHalfLengthZ;
|
float roverZMax = newFoward + roverHalfLengthZ;
|
||||||
|
|
||||||
// Sprawdzanie kolizji przed aktualizacją pozycji
|
// Sprawdzanie kolizji przed aktualizacją pozycji
|
||||||
if (Kolizja == true) {
|
if (!Kolizja == true) {
|
||||||
if (CheckAllFencesCollision(roverZMin, roverZMax, roverXMin, roverXMax, fences)) {
|
if (CheckAllFencesCollision(roverZMin, roverZMax, roverXMin, roverXMax, fences)) {
|
||||||
// Jeśli jest kolizja, zatrzymujemy łazik
|
// Jeśli jest kolizja, zatrzymujemy łazik
|
||||||
velocity = 0.0f;
|
velocity = 0.0f;
|
||||||
cout << "Kolizja podczas ruchu\n";
|
//cout << "Kolizja podczas ruchu\n";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Jeśli brak kolizji, aktualizujemy pozycję
|
// Jeśli brak kolizji, aktualizujemy pozycję
|
||||||
Sides = newSides;
|
Sides = newSides;
|
||||||
Foward = newFoward;
|
Foward = newFoward;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sprawdzanie kolizji podczas obrotu
|
// Sprawdzanie kolizji podczas obrotu
|
||||||
if (rotationVelocity != 0.0f) {
|
if (rotationVelocity != 0.0f) {
|
||||||
// Wyliczamy nową rotację
|
// Wyliczamy nową rotację
|
||||||
float newRotation = Rotation + rotationVelocity;
|
float newRotation = Rotation + rotationVelocity;
|
||||||
float radNewRotation = newRotation * GL_PI / 180.0f;
|
float radNewRotation = newRotation * GL_PI / 180.0f;
|
||||||
|
|
||||||
// Obracamy narożniki łazika
|
// Obracamy narożniki łazika
|
||||||
std::vector<std::pair<float, float>> corners(4);
|
std::vector<std::pair<float, float>> corners(4);
|
||||||
corners[0] = { Sides - roverHalfWidthX, Foward - roverHalfLengthZ }; // Lewy dolny
|
corners[0] = {Sides - roverHalfWidthX, Foward - roverHalfLengthZ}; // Lewy dolny
|
||||||
corners[1] = { Sides + roverHalfWidthX, Foward - roverHalfLengthZ }; // Prawy dolny
|
corners[1] = {Sides + roverHalfWidthX, Foward - roverHalfLengthZ}; // Prawy dolny
|
||||||
corners[2] = { Sides - roverHalfWidthX, Foward + roverHalfLengthZ }; // Lewy górny
|
corners[2] = {Sides - roverHalfWidthX, Foward + roverHalfLengthZ}; // Lewy górny
|
||||||
corners[3] = { Sides + roverHalfWidthX, Foward + roverHalfLengthZ }; // Prawy górny
|
corners[3] = {Sides + roverHalfWidthX, Foward + roverHalfLengthZ}; // Prawy górny
|
||||||
|
bool collisionDetected = false;
|
||||||
// Obracamy wszystkie narożniki
|
// Obracamy wszystkie narożniki
|
||||||
for (auto& corner : corners) {
|
for (auto& corner : corners) {
|
||||||
float x = corner.first;
|
float x = corner.first;
|
||||||
float z = corner.second;
|
float z = corner.second;
|
||||||
|
|
||||||
corner.first = Sides + (x - Sides) * cos(radNewRotation) - (z - Foward) * sin(radNewRotation);
|
corner.first = Sides + (x - Sides) * cos(radNewRotation) - (z - Foward) * sin(radNewRotation);
|
||||||
corner.second = Foward + (x - Sides) * sin(radNewRotation) + (z - Foward) * cos(radNewRotation);
|
corner.second = Foward + (x - Sides) * sin(radNewRotation) + (z - Foward) * cos(radNewRotation);
|
||||||
}
|
|
||||||
|
|
||||||
// Sprawdzamy kolizję na podstawie obróconych narożników
|
|
||||||
bool collisionDetected = false;
|
// Sprawdzamy kolizję na podstawie obróconych narożników
|
||||||
for (const auto& corner : corners) {
|
|
||||||
|
|
||||||
if (CheckAllFencesCollision(corner.first, corner.first, corner.second, corner.second, fences)) {
|
if (CheckAllFencesCollision(corner.first, corner.first, corner.second, corner.second, fences)) {
|
||||||
collisionDetected = true;
|
collisionDetected = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (CheckAllFencesCollision( corner.second, corner.second, corner.first, corner.first, fences)) {
|
||||||
|
collisionDetected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collisionDetected) {
|
if (collisionDetected) {
|
||||||
rotationVelocity = 0.0f; // Zatrzymujemy obrót
|
rotationVelocity = 0.0f; // Zatrzymujemy obrót
|
||||||
cout << "Kolizja podczas obrotu\n";
|
//cout << "Kolizja podczas obrotu\n";
|
||||||
}
|
} else {
|
||||||
else {
|
// Aktualizujemy rotację, jeśli nie ma kolizji
|
||||||
// Aktualizujemy rotację, jeśli nie ma kolizji
|
Rotation = newRotation;
|
||||||
Rotation = newRotation;
|
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
||||||
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
if (Rotation < 0.0f) Rotation += 360.0f;
|
||||||
if (Rotation < 0.0f) Rotation += 360.0f;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
else {
|
// Jeśli kolizje są wyłączone, aktualizujemy wszystko bez sprawdzania
|
||||||
// Jeśli kolizje są wyłączone, aktualizujemy wszystko bez sprawdzania
|
Sides = newSides;
|
||||||
Sides = newSides;
|
Foward = newFoward;
|
||||||
Foward = newFoward;
|
Rotation += rotationVelocity;
|
||||||
Rotation += rotationVelocity;
|
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
||||||
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
if (Rotation < 0.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
|
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_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
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_S, GL_CLAMP);
|
||||||
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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);
|
bitmapData = LoadBitmapFile((char*)"res/img/grass02.bmp", &bitmapInfoHeader);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury
|
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_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
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_S, GL_MIRRORED_REPEAT);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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);
|
bitmapData = LoadBitmapFile((char*)"res/img/barnroof.bmp", &bitmapInfoHeader);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[2]); // aktywuje obiekt tekstury
|
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_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
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_S, GL_CLAMP);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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);
|
bitmapData = LoadBitmapFile((char*)"res/img/brickwall.bmp", &bitmapInfoHeader);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[3]); // aktywuje obiekt tekstury
|
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_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
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_S, GL_CLAMP);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
||||||
|
|||||||
Reference in New Issue
Block a user