Changeset 612d1f6 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jul 31, 2018, 3:05:35 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
e9347b4
Parents:
fabed35
Message:

As a laser moves, update its rotation so it always faces the camera.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rfabed35 r612d1f6  
    145145void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
    146146
     147void translateLaser(SceneObject& laser, const vec3& translation, GLuint ubo);
     148
    147149void renderMainMenu();
    148150void renderMainMenuGui();
     
    213215
    214216/*
    215 * TODO: Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so
    216217* TODO: Asteroid movement currently depends on framerate, fix this in a generic/reusable way
    217218*/
     
    10411042            transformObject(objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
    10421043
    1043             // TODO: Update the rotation of the lasers as they moves so they point towards the camera
    10441044            if (leftLaserIdx != -1) {
    1045                transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
     1045               translateLaser(objects[leftLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo);
    10461046            }
    10471047            if (rightLaserIdx != -1) {
    1048                transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
     1048               translateLaser(objects[rightLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo);
    10491049            }
    10501050         }
     
    10521052            transformObject(objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
    10531053
    1054             // TODO: Update the rotation of the lasers as they moves so they point towards the camera
    10551054            if (leftLaserIdx != -1) {
    1056                transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
     1055               translateLaser(objects[leftLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo);
    10571056            }
    10581057            if (rightLaserIdx != -1) {
    1059                transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
     1058               translateLaser(objects[rightLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo);
    10601059            }
    10611060         }
     
    16301629   float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam));
    16311630
    1632    obj.model_base = mat4(1.0f);
    1633    obj.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)) * obj.model_base;
    1634    obj.model_base = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj.model_base;
    1635    obj.model_base = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_base;
    1636    obj.model_base = translate(mat4(1.0f), start) * obj.model_base;
     1631   obj.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));
    16371632
    16381633   initObject(obj);
     1634
     1635   obj.model_transform = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj.model_transform;
     1636   obj.model_transform = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_transform;
     1637   obj.model_transform = translate(mat4(1.0f), start) * obj.model_transform;
    16391638
    16401639   return obj;
     
    18571856}
    18581857
     1858void 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,
     1862   // and then re-used here
     1863
     1864   mat4 new_model_transform = translate(mat4(1.0f), translation) * laser.model_transform;
     1865
     1866   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));
     1868
     1869   vec3 ray = end - start;
     1870   float length = glm::length(ray);
     1871
     1872   float xAxisRotation = asin(ray.y / length);
     1873   float yAxisRotation = atan2(-ray.x, -ray.z);
     1874
     1875   vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) *
     1876      rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) *
     1877      vec4(0.0f, 1.0f, 0.0f, 1.0f));
     1878
     1879   // To project point P onto line AB:
     1880   // projection = A + dot(AP,AB) / dot(AB,AB) * AB
     1881   vec3 projOnLaser = start + glm::dot(cam_pos - start, ray) / (length*length) * ray;
     1882   vec3 laserToCam = cam_pos - projOnLaser;
     1883
     1884   float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam));
     1885
     1886   laser.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));
     1887
     1888   transformObject(laser, translate(mat4(1.0f), translation), ubo);
     1889}
     1890
    18591891void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    18601892                  GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
Note: See TracChangeset for help on using the changeset viewer.