mirror of
				https://codeberg.org/kbruen/y3s2-gui-project.git
				synced 2025-11-04 17:06:38 +02:00 
			
		
		
		
	Refactored code to use Utils::toRad
This commit is contained in:
		
							parent
							
								
									50c9179575
								
							
						
					
					
						commit
						69bb8d27a5
					
				
					 3 changed files with 22 additions and 11 deletions
				
			
		
							
								
								
									
										21
									
								
								src/maze.cpp
									
										
									
									
									
								
							
							
						
						
									
										21
									
								
								src/maze.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -8,8 +8,6 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "utils.hpp"
 | 
					#include "utils.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
constexpr double PI = 3.14159265358979;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
MazeScreen::MazeScreen(const std::vector<std::vector<int>>& maze): 
 | 
					MazeScreen::MazeScreen(const std::vector<std::vector<int>>& maze): 
 | 
				
			||||||
	maze(maze), 
 | 
						maze(maze), 
 | 
				
			||||||
	angleX(0), 
 | 
						angleX(0), 
 | 
				
			||||||
| 
						 | 
					@ -27,9 +25,9 @@ void MazeScreen::display() {
 | 
				
			||||||
		posX, 
 | 
							posX, 
 | 
				
			||||||
		0, 
 | 
							0, 
 | 
				
			||||||
		posZ, 
 | 
							posZ, 
 | 
				
			||||||
		posX + cos((angleX * PI) / 180) * 3, 
 | 
							posX + cos(Utils::toRad(angleX)) * 3, 
 | 
				
			||||||
		0, 
 | 
							0, 
 | 
				
			||||||
		posZ - sin((angleX * PI) / 180) * 3, 
 | 
							posZ - sin(Utils::toRad(angleX)) * 3, 
 | 
				
			||||||
		0, 
 | 
							0, 
 | 
				
			||||||
		1, 
 | 
							1, 
 | 
				
			||||||
		0
 | 
							0
 | 
				
			||||||
| 
						 | 
					@ -47,18 +45,21 @@ void MazeScreen::display() {
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
			case 1:
 | 
								case 1:
 | 
				
			||||||
				glPushMatrix();
 | 
									glPushMatrix();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									glTranslatef(i, 0, j);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Scale green by X
 | 
									// Scale green by X
 | 
				
			||||||
				auto green = Utils::clamp<float>(Utils::nummap<float>(abs(i - posX), 0, 3, 0, 1), 0, 1);
 | 
									auto green = Utils::clamp<float>(Utils::nummap<float>(abs(i - posX), 0, 3, 0, 1), 0, 1);
 | 
				
			||||||
				// Scale blue by Z
 | 
									// Scale blue by Z
 | 
				
			||||||
				auto blue = Utils::clamp<float>(Utils::nummap<float>(abs(j - posZ), 0, 3, 0, 1), 0, 1);
 | 
									auto blue = Utils::clamp<float>(Utils::nummap<float>(abs(j - posZ), 0, 3, 0, 1), 0, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				glColor3f(1, green, blue);
 | 
									glColor3f(1, green, blue);
 | 
				
			||||||
				glTranslatef(i, 0, j);
 | 
									glutWireCube(1);
 | 
				
			||||||
				if (solid) {
 | 
									if (solid) {
 | 
				
			||||||
 | 
										glColor3f(1, green, blue);
 | 
				
			||||||
					glutSolidCube(1);
 | 
										glutSolidCube(1);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				else {
 | 
					
 | 
				
			||||||
					glutWireCube(1);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				glPopMatrix();
 | 
									glPopMatrix();
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					@ -114,8 +115,8 @@ void MazeScreen::idle() {
 | 
				
			||||||
	auto deltaTime = (newTime - gameTime) / 1000.0;
 | 
						auto deltaTime = (newTime - gameTime) / 1000.0;
 | 
				
			||||||
	gameTime = newTime;
 | 
						gameTime = newTime;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	posX += forceX * deltaTime * cos(angleX * PI / 180) + forceZ * deltaTime * sin(angleX * PI / 180);
 | 
						posX += forceX * deltaTime * cos(Utils::toRad(angleX)) + forceZ * deltaTime * sin(Utils::toRad(angleX));
 | 
				
			||||||
	posZ += forceZ * deltaTime * cos(angleX * PI / 180) - forceX * deltaTime * sin(angleX * PI / 180);
 | 
						posZ += forceZ * deltaTime * cos(Utils::toRad(angleX)) - forceX * deltaTime * sin(Utils::toRad(angleX));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glutPostRedisplay();
 | 
						glutPostRedisplay();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,4 +26,14 @@ namespace Utils {
 | 
				
			||||||
	constexpr T toRad(T deg) {
 | 
						constexpr T toRad(T deg) {
 | 
				
			||||||
		return deg * PI / 180;
 | 
							return deg * PI / 180;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						template <typename T>
 | 
				
			||||||
 | 
						constexpr T min(T a, T b) {
 | 
				
			||||||
 | 
							if (a < b) {
 | 
				
			||||||
 | 
								return a;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
 | 
								return b;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
Name:           y3s2-gui-project
 | 
					Name:           y3s2-gui-project
 | 
				
			||||||
Version:        1.0.1
 | 
					Version:        1.0.2
 | 
				
			||||||
Release:        1%{?dist}
 | 
					Release:        1%{?dist}
 | 
				
			||||||
Summary:        Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2
 | 
					Summary:        Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue