From 8d834431dfedbdb94d43f8a81fdf5f2d897f89a9 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Thu, 25 Nov 2021 20:11:58 -0800 Subject: feat(tprojvc): first commit --- .gitignore | 1 + Makefile | 4 ++- README.md | 2 ++ tprojvc.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tprojvc.c diff --git a/.gitignore b/.gitignore index 41db595..4de0843 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ a.out *.o expvc expexc +tprojvc diff --git a/Makefile b/Makefile index 99614c1..59d8476 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS += -I. CFLAGS += -std=c99 CFLAGS += -Wall LDLIBS += -lm -BIN = expvc expexc +BIN = expvc expexc tprojvc all: $(BIN) @@ -11,5 +11,7 @@ expvc: expvc.c expexc: expexc.c +tprojvc: tprojvc.c + clean: rm -rf $(BIN) diff --git a/README.md b/README.md index 496cb31..50d54ee 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ This repository contains codes for: * `expexc`: Explosion entity exposure ray casting simulator. +* `tprojvc`: Thrown projectile tracer. + ## Compile Just `make(1)`. How to build on Windows is left as an exercise to the reader. diff --git a/tprojvc.c b/tprojvc.c new file mode 100644 index 0000000..2fc3f53 --- /dev/null +++ b/tprojvc.c @@ -0,0 +1,99 @@ +/* + * Thrown projectile velocity calculator. + * + * @1.17.1 + * + */ +#include "common.h" + +int main(int argc, char **argv) +{ + if (argc <= 1) + { + goto usage; +usage: + fprintf(stderr, "Usage: %s -p 0,0,0 -v 0,0,0 -t 10 [-w] [-ng]\n" + "\n" + "Options:\n" + "\t-p vec3d\tInitial position [Mandatory]\n" + "\t-v vec3d\tInitial velocity [Mandatory]\n" + "\t-t int\tTicks to simulate [Mandatory]\n" + "\t-w\tIn water [Optional]\n" + "\t-ng\tNo gravity [Optional]\n" + "\t-h\tShow this help\n" + "\n" + "Vec3D Format: x,y,z. No spaces or brackets allowed.\n" + "Numbers represent post-tick values.\n", + argv[0] + ); + return 64; + } + struct vec3d v = { VEC3D, 0, 0, 0 }; + struct vec3d pos = { VEC3D, 0, 0, 0 }; + int ticks = 0; + int water = 0; + int no_gravity = 0; + + int r = 0; + for (int i = 1; i < argc; i ++) + { + const char *arg = argv[i]; + if (!strcmp("-h", arg)) { goto usage; } + else if (!strcmp("-w", arg)) { water = 1; continue; } + else if (!strcmp("-ng", arg)) { no_gravity = 1; continue; } + if (i == argc - 1) + { + fprintf(stderr, "%s requires an argument.\n", arg); + goto usage; + } + char *val = argv[++i]; + if (!strcmp("-p", arg)) { if ((r = val_parse(val, (struct val *)&pos))) return r; } + else if (!strcmp("-v", arg)) { if ((r = val_parse(val, (struct val *)&v))) return r; } + else if (!strcmp("-t", arg)) + { + sscanf(val, "%d", &ticks); + if (ticks <= 0) + { + fprintf(stderr, "-t requires a natural number.\n"); + goto usage; + } + } + else + { + fprintf(stderr, "Unknown argument: %s\n", arg); + goto usage; + } + } + + fprintf(stderr, "t,x,y,z,vx,vy,vz\n"); + /* Initial value. Print to stderr (classified as verbose msg) */ + fprintf(stderr, "%d,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f\n", + 0, + pos.x, + pos.y, + pos.z, + v.x, + v.y, + v.z); + const float deaccel = water ? 0.8f : 0.99f; + for (int i = 1; i <= ticks; i ++) + { + pos.x += v.x; + pos.y += v.y; + pos.z += v.z; + v.x *= deaccel; + v.y *= deaccel; + v.z *= deaccel; + v.y -= no_gravity ? 0.0f : 0.03f; + printf("%d,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f\n", + i, + pos.x, + pos.y, + pos.z, + v.x, + v.y, + v.z); + } + return 0; +} + -- cgit v1.2.3