Browse Source

deltatime

Iver 2 weeks ago
parent
commit
3109e74cfe
3 changed files with 28 additions and 9 deletions
  1. 2 1
      changelog.txt
  2. 22 4
      src/launch program/main.c
  3. 4 4
      src/main/main.c

+ 2 - 1
changelog.txt

@@ -2,4 +2,5 @@
 -basic nessecary functions
 -headers
 -debug renderer
--basic 2d euler physics (velocity angular velocity)
+-basic 2d euler physics (velocity angular velocity)
+-deltatime

+ 22 - 4
src/launch program/main.c

@@ -1,4 +1,5 @@
 #include "../headers/fizzix.h"
+#include <time.h>
 
 int main(){
     FZ_init();
@@ -26,14 +27,31 @@ int main(){
 
     scene->shape_count = 2;
 
-    shape_1->velocity = (FZ_vector_2){0.01, .02};
+    shape_1->velocity = (FZ_vector_2){10, 20};
     
-    shape_1->angular_veclocity = 0.001;
-    shape_2->angular_veclocity = 0.002;
+    shape_1->angular_veclocity = 1;
+    shape_2->angular_veclocity = 2;
+
+    long int start, end;
+    double fps = 0;
+
+    float total_fps = 0;
+
+    double deltatime = 0;
+    double delta_min = 0.0001;
+    double delta_max = 100000;
 
     while (context->is_running){
-        FZ_tick(scene);
+        start = clock();
+
+        FZ_tick(scene, deltatime);
         FZ_render_debug(scene);
+
+        end = clock();
+
+        deltatime = fmin(fmax((double)(end - start) / (double)(CLOCKS_PER_SEC), delta_min), delta_max);
+    
+        printf("%d fps      \r", (int)(1.0 / deltatime));
     }
 
     return 0;

+ 4 - 4
src/main/main.c

@@ -40,15 +40,15 @@ FZ_scene* FZ_new_scene(){
     return new_scene;
 }
 
-int FZ_tick(FZ_scene* scene){
+int FZ_tick(FZ_scene* scene, double deltatime){
     for (int i = 0; i < scene->shape_count; i++){
         FZ_shape* shape = scene->shapes[i];
 
         if (!(shape->flags & FZ_SHAPE_IS_STATIC)){
-            shape->velocity = v2_ew_add(shape->velocity, scene->gravity);
+            shape->velocity = v2_ew_add(shape->velocity, v2_times(scene->gravity, deltatime));
 
-            shape->position = v2_ew_add(shape->position, shape->velocity);
-            shape->angle += shape->angular_veclocity;
+            shape->position = v2_ew_add(shape->position, v2_times(shape->velocity, deltatime));
+            shape->angle += shape->angular_veclocity * deltatime;
         }
 
         for (int j = 0; j < shape->point_count; j++){