Changeset e9347b4 in opengl-game
- Timestamp:
- Aug 3, 2018, 2:16:17 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 1f3d32b
- Parents:
- 612d1f6
- git-author:
- Dmitry Portnoy <dmp1488@…> (08/03/18 02:12:11)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (08/03/18 02:16:17)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r612d1f6 re9347b4 95 95 unsigned char* loadImage(string file_name, int* x, int* y); 96 96 97 void printVector(string label, vec3 v);98 void print4DVector(string label, vec4 v);97 void printVector(string label, vec3& v); 98 void print4DVector(string label, vec4& v); 99 99 100 100 void initObject(SceneObject& obj); … … 146 146 147 147 void translateLaser(SceneObject& laser, const vec3& translation, GLuint ubo); 148 void updateLaserTarget(SceneObject& laser, vector<SceneObject>& objects, GLuint points_vbo); 149 bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection); 148 150 149 151 void renderMainMenu(); … … 215 217 216 218 /* 217 * TODO: Asteroid movement currently depends on framerate, fix this in a generic/reusable way 219 * TODO: Asteroid and ship movement currently depend on framerate, fix this in a generic/reusable way 220 * Disabling vsync is a great way to test this 218 221 */ 219 222 … … 1005 1008 elapsed_seconds_spawn += elapsed_seconds; 1006 1009 if (elapsed_seconds_spawn > 0.5f) { 1007 spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp, 1010 //spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp, 1011 spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)), color_sp, 1008 1012 shaderBufferInfo, 1009 1013 points_vbo, … … 1042 1046 transformObject(objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1043 1047 1044 if (leftLaserIdx != -1 ) {1048 if (leftLaserIdx != -1 && !objects[leftLaserIdx].deleted) { 1045 1049 translateLaser(objects[leftLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo); 1046 1050 } 1047 if (rightLaserIdx != -1 ) {1051 if (rightLaserIdx != -1 && !objects[rightLaserIdx].deleted) { 1048 1052 translateLaser(objects[rightLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo); 1049 1053 } … … 1052 1056 transformObject(objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1053 1057 1054 if (leftLaserIdx != -1 ) {1058 if (leftLaserIdx != -1 && !objects[leftLaserIdx].deleted) { 1055 1059 translateLaser(objects[leftLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo); 1056 1060 } 1057 if (rightLaserIdx != -1 ) {1061 if (rightLaserIdx != -1 && !objects[rightLaserIdx].deleted) { 1058 1062 translateLaser(objects[rightLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo); 1059 1063 } … … 1109 1113 } 1110 1114 1111 // TODO: Probablu remove this since it was only added to test that object removal owrks 1112 if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) { 1113 removeObjectFromScene(objects[0], ubo); 1115 if (leftLaserIdx != -1 && !objects[leftLaserIdx].deleted) { 1116 updateLaserTarget(objects[leftLaserIdx], objects, points_vbo); 1117 } 1118 if (rightLaserIdx != -1 && !objects[rightLaserIdx].deleted) { 1119 updateLaserTarget(objects[rightLaserIdx], objects, points_vbo); 1114 1120 } 1115 1121 } … … 1400 1406 } 1401 1407 1402 void printVector(string label, vec3 v) {1408 void printVector(string label, vec3& v) { 1403 1409 cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl; 1404 1410 } 1405 1411 1406 void print4DVector(string label, vec4 v) {1412 void print4DVector(string label, vec4& v) { 1407 1413 cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl; 1408 1414 } … … 1535 1541 GLfloat radius_z = max_z - obj.bounding_center.z; 1536 1542 1537 // This actually underestimates the radius. Might need to be fixed at some point. 1543 // TODO: This actually underestimates the radius. Might need to be fixed at some point. 1544 // TODO: Does not take into account any scaling in the model matrix 1538 1545 obj.bounding_radius = radius_x; 1539 1546 if (obj.bounding_radius < radius_y) … … 1857 1864 1858 1865 void translateLaser(SceneObject& laser, const vec3& translation, GLuint ubo) { 1859 if (laser.deleted) return; 1860 1861 // TODO: A lot of the values I calculate here can be calculated once and saved when the laser is created, 1866 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 1862 1867 // and then re-used here 1863 1868 … … 1865 1870 1866 1871 vec3 start = vec3(laser.model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1867 vec3 end = vec3(laser.model_transform * vec4(0.0f, 0.0f, laser.points[ 2] + laser.points[20], 1.0f));1872 vec3 end = vec3(laser.model_transform * vec4(0.0f, 0.0f, laser.points[38], 1.0f)); 1868 1873 1869 1874 vec3 ray = end - start; … … 1887 1892 1888 1893 transformObject(laser, translate(mat4(1.0f), translation), ubo); 1894 } 1895 1896 void updateLaserTarget(SceneObject& laser, vector<SceneObject>& objects, GLuint points_vbo) { 1897 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 1898 // and then re-used here 1899 1900 vec3 start = vec3(laser.model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1901 vec3 end = vec3(laser.model_transform * vec4(0.0f, 0.0f, laser.points[2] + laser.points[20], 1.0f)); 1902 1903 vec3 intersection(0.0f), closestIntersection(0.0f); 1904 SceneObject* closestAsteroid = NULL; 1905 1906 for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) { 1907 if (it->type == TYPE_ASTEROID && !it->deleted && getLaserAndAsteroidIntersection(start, end, *it, intersection)) { 1908 // TODO: Implement a more generic algorithm for testing the closest object by getting the distance between the points 1909 if (closestAsteroid == NULL || intersection.z > closestIntersection.z) { 1910 // TODO: At this point, find the real intersection of the laser with one of the asteroid's sides 1911 closestAsteroid = &*it; 1912 closestIntersection = intersection; 1913 } 1914 } 1915 } 1916 1917 float width = laser.points[0] - laser.points[2]; 1918 1919 float length = 5.24f; 1920 if (closestAsteroid != NULL) { 1921 length = glm::length(closestIntersection - start); 1922 } 1923 1924 laser.points[20] = -length + width / 2; 1925 laser.points[23] = -length + width / 2; 1926 laser.points[29] = -length + width / 2; 1927 laser.points[38] = -length; 1928 laser.points[41] = -length; 1929 laser.points[44] = -length + width / 2; 1930 laser.points[47] = -length; 1931 laser.points[50] = -length + width / 2; 1932 laser.points[53] = -length + width / 2; 1933 1934 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 1935 glBufferSubData(GL_ARRAY_BUFFER, laser.vertex_vbo_offset * sizeof(GLfloat) * 3, laser.points.size() * sizeof(GLfloat), &laser.points[0]); 1936 } 1937 1938 bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection) { 1939 /* 1940 ### LINE EQUATIONS ### 1941 x = x1 + u * (x2 - x1) 1942 y = y1 + u * (y2 - y1) 1943 z = z1 + u * (z2 - z1) 1944 1945 ### SPHERE EQUATION ### 1946 (x - x3)^2 + (y - y3)^2 + (z - z3)^2 = r^2 1947 1948 ### QUADRATIC EQUATION TO SOLVE ### 1949 a*u^2 + b*u + c = 0 1950 WHERE THE CONSTANTS ARE 1951 a = (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2 1952 b = 2*( (x2 - x1)*(x1 - x3) + (y2 - y1)*(y1 - y3) + (z2 - z1)*(z1 - z3) ) 1953 c = x3^2 + y3^2 + z3^2 + x1^2 + y1^2 + z1^2 - 2(x3*x1 + y3*y1 + z3*z1) - r^2 1954 1955 u = (-b +- sqrt(b^2 - 4*a*c)) / 2a 1956 1957 If the value under the root is >= 0, we got an intersection 1958 If the value > 0, there are two solutions. Take the one closer to 0, since that's the 1959 one closer to the laser start point 1960 */ 1961 1962 vec3& center = asteroid.bounding_center; 1963 1964 float a = pow(end.x-start.x, 2) + pow(end.y-start.y, 2) + pow(end.z-start.z, 2); 1965 float b = 2*((start.x-end.x)*(start.x-center.x) + (end.y-start.y)*(start.y-center.y) + (end.z-start.z)*(start.z-center.z)); 1966 float c = pow(center.x, 2) + pow(center.y, 2) + pow(center.z, 2) + pow(start.x, 2) + pow(start.y, 2) + pow(start.z, 2) - 2*(center.x*start.x + center.y*start.y + center.z*start.z) - pow(asteroid.bounding_radius, 2); 1967 float discriminant = pow(b, 2) - 4*a*c; 1968 1969 if (discriminant >= 0.0f) { 1970 // In this case, the negative root will always give the point closer to the laser start point 1971 float u = (-b - sqrt(discriminant)) / (2 * a); 1972 1973 // Check that the intersection is within the line segment corresponding to the laser 1974 if (0.0f <= u && u <= 1.0f) { 1975 intersection = start + u * (end - start); 1976 return true; 1977 } 1978 } 1979 1980 return false; 1889 1981 } 1890 1982 … … 2137 2229 2138 2230 initObject(obj); 2231 // This accounts for the scaling in model_base. 2232 // Dividing by 8 instead of 10 since the bounding radius algorithm 2233 // under-calculates the true value. 2234 // TODO: Once the intersection check with the sides of the asteroid is done, 2235 // this can be removed. 2236 obj.bounding_radius /= 8.0f; 2237 2139 2238 addObjectToScene(obj, shaderBufferInfo, 2140 2239 points_vbo,
Note:
See TracChangeset
for help on using the changeset viewer.