]> git.xolatile.top Git - soikk-bouncy-ball.git/commitdiff
Initial commit
authorsoikk <76824648+Soikk@users.noreply.github.com>
Thu, 4 Nov 2021 20:17:54 +0000 (21:17 +0100)
committersoikk <76824648+Soikk@users.noreply.github.com>
Thu, 4 Nov 2021 20:17:54 +0000 (21:17 +0100)
README.md [new file with mode: 0644]
a.out [new file with mode: 0755]
ball.c [new file with mode: 0644]
ball.c~ [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..266098e
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+Inspired by Tsoding's ball example in his linear algebra repo
diff --git a/a.out b/a.out
new file mode 100755 (executable)
index 0000000..db7a9c6
Binary files /dev/null and b/a.out differ
diff --git a/ball.c b/ball.c
new file mode 100644 (file)
index 0000000..569f975
--- /dev/null
+++ b/ball.c
@@ -0,0 +1,149 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <unistd.h>
+
+#define WIDTH 96
+#define HEIGHT 48
+#define FPS 30
+#define GRAVITY 2.0f
+#define DECELERATION 0.65f
+
+
+typedef enum PIXEL{
+                  MPTY,
+                  FULL
+} PIXEL;
+
+static PIXEL display[WIDTH*HEIGHT];
+static char pixels[4] = " -^C";
+
+
+
+typedef struct v2{
+  float x;
+  float y;
+} v2;
+
+typedef struct ball{
+  v2 center;
+  float radius;
+} ball;
+
+
+void clear(){
+  #ifdef WIN32
+  system("cls");
+  #else
+  system("clear");
+  #endif
+}
+
+void move(int x, int y){
+  printf("\033[%d;%dH", y, x);
+}
+
+void hide(int o){
+  if(o)
+    printf("\e[?25h");
+  else
+    printf("\e[?251");
+}
+
+void back(){
+  move(0, 0);
+}
+
+void clearDisplay(){
+  for(int y = 0; y < HEIGHT; ++y){
+    for(int x = 0; x < WIDTH; ++x){
+      display[x + y*WIDTH] = MPTY;
+    }
+  }
+}
+
+v2 initV2(float x, float y){
+  v2 newV2;
+  newV2.x = x;
+  newV2.y = y;
+  return newV2;
+}
+
+ball initBall(float x, float y, float radius){
+  ball newBall;
+  newBall.center = initV2(x, y);
+  newBall.radius = radius;
+  return newBall;
+}
+
+void drawBall(ball b){
+  clearDisplay();
+  v2 min = initV2(floor(b.center.x-b.radius), floor(b.center.y-b.radius));
+  v2 max = initV2(ceil(b.center.x+b.radius), ceil(b.center.y+b.radius));
+  for(int y = min.y; y < max.y; ++y){
+    for(int x = min.x; x < max.x; ++x){
+      float px = b.center.x - x - 0.5f, py = b.center.y - y - 0.5f;
+      if((px*px + py*py) <= b.radius*b.radius){
+       if(x >= 0 && x <= WIDTH && y >= 0 && y <= HEIGHT){
+         display[x + y*WIDTH] = FULL;
+       };
+      }
+    }
+  }
+}
+
+void drawDisplay(){
+  for(int y = 0; y < HEIGHT/2; ++y){
+    for(int x = 0; x < WIDTH; ++x){
+      PIXEL t = display[x + (2*y+0)*WIDTH];
+      PIXEL b = display[x + (2*y+1)*WIDTH];
+      putchar(pixels[b + t*2]);
+    }
+    putchar('\n');
+  }
+}
+
+int main(){
+  clear();
+  clearDisplay();
+  back();
+  hide(0);
+  ball b = initBall(10, 10, 8);
+  v2 vel = initV2(0, 0.1);
+  
+  while(vel.y != 0){
+    
+    int v = 1;
+    if(b.center.y+b.radius-HEIGHT == 0)
+      v = 0;
+    
+    vel.y += (GRAVITY/FPS)*v;
+    b.center.x += vel.x;
+    b.center.y += vel.y;
+    if(v == 0){
+      printf("CERO: %f\n", vel.y);
+    }
+    printf("v: %f\n", vel.y);
+    getchar();
+    if(b.center.y+b.radius > HEIGHT){
+      b.center.y = HEIGHT - b.radius;
+      //b.center.y -= floor(b.radius*(vel.y*(GRAVITY/FPS)));
+      float a = vel.y;
+      vel.y *= -DECELERATION;
+      printf("\n%f *= -%f = %f", a, DECELERATION, vel.y);
+      getchar();
+    }
+
+    
+    clear();
+    back();
+    drawBall(b);
+    drawDisplay();
+
+   
+    usleep(1000*1000/FPS);
+  }
+  
+  return 0;
+}
diff --git a/ball.c~ b/ball.c~
new file mode 100644 (file)
index 0000000..bc026ff
--- /dev/null
+++ b/ball.c~
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+void back(){
+  printf("\x1b[H")
+}
+
+int main(){
+
+  
+}