Browse Source

cleanup, squad fix for same quaternions, editor mode

Kajetan Johannes Hammerle 3 years ago
parent
commit
3f67bfe7ad
3 changed files with 39 additions and 24 deletions
  1. 30 20
      client/Game.cpp
  2. 3 2
      client/Game.h
  3. 6 2
      client/math/Quaternion.cpp

+ 30 - 20
client/Game.cpp

@@ -30,9 +30,7 @@ pointIndex(0), moveSpeed(0.125f), movedLength(0.0f), mode(Mode::AUTO) {
         q.mul(Quaternion(Vector(r.nextFloat() * 360.0f, r.nextFloat() * -90.0f), -10.0f));
         cameraPoints.add( {v, q, 0.0f});
     }
-    for(uint i = 0; i < cameraPoints.getLength(); i++) {
-        cameraPoints[i].distance = distance(i, 10000);
-    }
+    updateDistances();
 }
 
 void Game::tick() {
@@ -81,11 +79,18 @@ void Game::tick() {
         if(control.keys.camDown.isDown()) {
             rotation.mul(Quaternion(right, -rotationSpeed));
         }
+
+        if(control.keys.test3.getDownTime() == 1) {
+            cameraPoints.add( {pos, rotation, 0.0f});
+        }
     } else if(mode == Mode::AUTO) {
         movedLength += moveSpeed;
 
         if(control.keys.camUp.isDown()) {
             moveSpeed += 0.0125f;
+            if(moveSpeed > 1.0f) {
+                moveSpeed = 1.0f;
+            }
         }
         if(control.keys.camDown.isDown()) {
             moveSpeed -= 0.0125f;
@@ -93,15 +98,21 @@ void Game::tick() {
                 moveSpeed = 0.0f;
             }
         }
+
+        if(control.keys.test3.isDown()) {
+            mode = Mode::PLAYER;
+            cameraPoints.clear();
+        }
     }
 
     if(control.keys.test.isDown()) {
         mode = Mode::PLAYER;
     }
-    if(control.keys.test2.isDown()) {
+    if(control.keys.test2.isDown() && cameraPoints.getLength() >= 3) {
         mode = Mode::AUTO;
+        movedLength = 0.0f;
+        updateDistances();
     }
-
     if(control.keys.test4.getDownTime() == 1) {
         renderSettings.shadows = !renderSettings.shadows;
     }
@@ -120,18 +131,13 @@ void Game::renderWorld(float lag, Renderer& renderer) const {
         }
         float t = leftLength / cameraPoints[index].distance;
         Vector interpolatedPos = pointUntilDistance(leftLength, index, 4000);
-        
-        
+
         uint a = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
-        uint b = (a + 1) %  cameraPoints.getLength();
-        uint c = (a + 2) %  cameraPoints.getLength();
-        uint d = (a + 3) %  cameraPoints.getLength();
-        
+        uint b = (a + 1) % cameraPoints.getLength();
+        uint c = (a + 2) % cameraPoints.getLength();
+        uint d = (a + 3) % cameraPoints.getLength();
+
         renderer.update(interpolatedPos, cameraPoints[b].q.squad(t, cameraPoints[a].q, cameraPoints[c].q, cameraPoints[d].q));
-        
-        //renderer.update(interpolatedPos, cameraPoints[index].q.slerp(t, cameraPoints[(index + 1) % cameraPoints.getLength()].q));
-        
-        
         pos = interpolatedPos;
     } else if(mode == Mode::PLAYER) {
         Vector v(lastPos);
@@ -146,11 +152,9 @@ void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) co
     renderer.scale(2.0f).update();
 
     String s;
-    fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond())
-            .append(" ").append("%0.8f", renderSettings.testBias).append(" ").append("%0.8f", renderSettings.testRadius));
-    fr.drawString(10, 19, s.clear().append("TPS: ").append(tps.getUpdatesPerSecond()));
-    s.clear();
-    pos.toString(s);
+    fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond()).append(" TPS: ").append(tps.getUpdatesPerSecond()));
+    fr.drawString(10, 19, s.clear().append("Speed: ").append(moveSpeed));
+    pos.toString(s.clear());
     fr.drawString(10, 28, s);
     for(uint i = 0; i < cameraPoints.getLength(); i++) {
         s.clear().append(i + 1).append(": ");
@@ -233,4 +237,10 @@ void Game::getPointsAndTangents(uint index, Vector& a, Vector& b, Vector& tanA,
 
     tanA = splineTangent(cameraPoints[prev].pos, a, b);
     tanB = splineTangent(a, b, cameraPoints[next].pos);
+}
+
+void Game::updateDistances() {
+    for(uint i = 0; i < cameraPoints.getLength(); i++) {
+        cameraPoints[i].distance = distance(i, 10000);
+    }
 }

+ 3 - 2
client/Game.h

@@ -22,13 +22,14 @@ public:
 
     bool isRunning() const;
 
+private:
     Vector splineTangent(const Vector& prev, const Vector& current, const Vector& next) const;
     Vector interpolate(const Vector& a, const Vector& b, const Vector& tanA, const Vector& tanB, float t) const;
     float distance(uint index, uint splits) const;
     Vector pointUntilDistance(float leftDistance, uint index, uint splits) const;
     void getPointsAndTangents(uint index, Vector& a, Vector& b, Vector& tanA, Vector& tanB) const;
-
-private:
+    void updateDistances();
+    
     const Control& control;
     const Clock& fps;
     const Clock& tps;

+ 6 - 2
client/math/Quaternion.cpp

@@ -40,6 +40,10 @@ Quaternion Quaternion::interpolate(float a, float b, const Quaternion& other) co
 }
 
 Quaternion Quaternion::squad(float f, const Quaternion& prev, const Quaternion& next, const Quaternion& nextNext) const {
+    float diff = Vector(xyz).sub(next.xyz).squareLength() + (w - next.w) * (w - next.w);
+    if(diff < 0.0001f) {
+        return *this;
+    }
     Quaternion s1 = squadControl(prev, next);
     Quaternion s2 = next.squadControl(*this, nextNext);
     return slerp(f, next).slerp(2.0f * f * (1.0f - f), s1.slerp(f, s2));
@@ -114,7 +118,7 @@ Quaternion& Quaternion::log() {
     //float arccos = acos(w / length);
     //w = logf(length);
     //xyz.mul(arccos / xyz.length());
-    
+
     // optimized use case log
     xyz.normalize().mul(acos(w));
     w = 0.0f;
@@ -127,7 +131,7 @@ Quaternion& Quaternion::exp() {
     //float length = xyz.length();
     //w = expw * cosf(length);
     //xyz.mul((expw * sin(length)) / length);
-    
+
     // optimized use case exp
     float length = xyz.length();
     w = cosf(length);