aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-02-23 07:18:20 -0600
committericeyrazor <iceyrazor@mailfence.com>2026-02-23 07:18:20 -0600
commit444c6642d67e63fc88e5fa1d7c06062d04106b44 (patch)
tree25eaf36bed017c7e597fe85212486cdedd1c100c /src
parente06ab8f76be21b866207a708f1336a5c1a53f149 (diff)
2-3 - 2-5
- made obj array in init-multi a sub object in stuff - made previous src file conform with init-multi change - change project name
Diffstat (limited to 'src')
-rwxr-xr-xsrc/1-1-walker.c2
-rwxr-xr-xsrc/1-2-vec-math.c2
-rwxr-xr-xsrc/1-3-rand-vec.c2
-rwxr-xr-xsrc/1-5-unit-vec.c3
-rwxr-xr-xsrc/1-6-mover.c2
-rwxr-xr-xsrc/2-1-gravity-wind.c2
-rwxr-xr-xsrc/2-2-mass-accel.c37
-rwxr-xr-xsrc/2-3-friction.c133
-rwxr-xr-xsrc/2-4-drag.c146
-rwxr-xr-xsrc/2-5-grav-attraction.c114
10 files changed, 419 insertions, 24 deletions
diff --git a/src/1-1-walker.c b/src/1-1-walker.c
index fc67340..ba8f696 100755
--- a/src/1-1-walker.c
+++ b/src/1-1-walker.c
@@ -4,7 +4,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-walker"
typedef struct {
float x;
diff --git a/src/1-2-vec-math.c b/src/1-2-vec-math.c
index cf9a8cb..201bbc0 100755
--- a/src/1-2-vec-math.c
+++ b/src/1-2-vec-math.c
@@ -4,7 +4,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-vec math"
typedef struct {
Point pos;
diff --git a/src/1-3-rand-vec.c b/src/1-3-rand-vec.c
index 2880f6b..a5289d5 100755
--- a/src/1-3-rand-vec.c
+++ b/src/1-3-rand-vec.c
@@ -4,7 +4,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-rand vec"
typedef struct {
Point pos;
diff --git a/src/1-5-unit-vec.c b/src/1-5-unit-vec.c
index 84710eb..b888b13 100755
--- a/src/1-5-unit-vec.c
+++ b/src/1-5-unit-vec.c
@@ -1,11 +1,10 @@
-#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-unit vec"
typedef struct {
Point pos;
diff --git a/src/1-6-mover.c b/src/1-6-mover.c
index c7ff186..d4535d5 100755
--- a/src/1-6-mover.c
+++ b/src/1-6-mover.c
@@ -6,7 +6,7 @@
#include <SDL2/SDL_video.h>
#include <time.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-mover"
typedef struct {
Point pos;
diff --git a/src/2-1-gravity-wind.c b/src/2-1-gravity-wind.c
index 7a54b05..a5a2bb2 100755
--- a/src/2-1-gravity-wind.c
+++ b/src/2-1-gravity-wind.c
@@ -6,7 +6,7 @@
#include <SDL2/SDL_video.h>
#include <time.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-gravity wind"
typedef struct {
Point pos;
diff --git a/src/2-2-mass-accel.c b/src/2-2-mass-accel.c
index dc79d3e..48b1784 100755
--- a/src/2-2-mass-accel.c
+++ b/src/2-2-mass-accel.c
@@ -5,21 +5,24 @@
#include <SDL2/SDL_video.h>
#include <SDL2/SDL2_gfxPrimitives.h>
-#define PROJECT_NAME "vecotrs-main"
+#define PROJECT_NAME "vectors-mass accel"
+#define obj_n 2
-typedef struct OBJECT {
+typedef struct OBJECT_INER {
Point pos;
Point vel;
Point acc;
float r;
float mass;
-} OBJECT;
+} OBJECT_INER;
-#define obj_n 2
+typedef struct OBJECT {
+ OBJECT_INER *objs[obj_n];
+} OBJECT;
#include <init-multi.h>
-void constructor(int w, int h, OBJECT *obj, float x, float y, float mass){
+void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){
obj->pos.x = (float)w/2 + x;
obj->pos.y = (float)h/2 + y;
obj->vel = (Point){0,0};
@@ -28,7 +31,7 @@ void constructor(int w, int h, OBJECT *obj, float x, float y, float mass){
obj->r = sqrt(obj->mass) * 10;
}
-void init_stuffs(int w, int h, OBJECT *objs){
+void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){
constructor(w, h, &objs[0], 100, 100, 4);
constructor(w, h, &objs[1], -100, 100, 10);
}
@@ -38,13 +41,13 @@ Point vec_sub(Point A, Point B){
return A;
}
-void applyForce(OBJECT *obj, Point force){
+void applyForce(OBJECT_INER *obj, Point force){
//no need to make a copy. tis not a pointer
point_div(&force, &(Point){obj->mass,obj->mass});
point_add(&obj->acc, &force);
}
-void edges(STUFFS *stuff, OBJECT *obj) {
+void edges(STUFFS *stuff, OBJECT_INER *obj) {
if (obj->pos.y >= stuff->height - obj->r){
obj->pos.y = stuff->height - obj->r;
obj->vel.y = obj->vel.y * -1;
@@ -59,13 +62,13 @@ void edges(STUFFS *stuff, OBJECT *obj) {
}
}
-void update(STUFFS *stuff, OBJECT *obj){
+void update(STUFFS *stuff, OBJECT_INER *obj){
point_add(&obj->vel,&obj->acc);
point_add(&obj->pos, &obj->vel);
obj->acc = (Point){0,0};
}
-void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT *obj){
+void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT_INER *obj){
SDL_SetRenderDrawColor(renderer,150,50,255,10);
Sint16 x = (Sint16)obj->pos.x;
@@ -82,19 +85,19 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
Point gravity = {0, 0.2};
Point weight = {0};
- weight.x = gravity.x * stuff->obj[i]->mass;
- weight.y = gravity.y * stuff->obj[i]->mass;
+ weight.x = gravity.x * stuff->obj.objs[i]->mass;
+ weight.y = gravity.y * stuff->obj.objs[i]->mass;
if(stuff->mousedown == true){
Point wind = {0.1, 0};
- applyForce(stuff->obj[i], wind);
+ applyForce(stuff->obj.objs[i], wind);
}
- applyForce(stuff->obj[i], weight);
+ applyForce(stuff->obj.objs[i], weight);
- update(stuff,stuff->obj[i]);
- edges(stuff,stuff->obj[i]);
- show(renderer,stuff,stuff->obj[i]);
+ update(stuff,stuff->obj.objs[i]);
+ edges(stuff,stuff->obj.objs[i]);
+ show(renderer,stuff,stuff->obj.objs[i]);
}
SDL_RenderPresent(renderer);
diff --git a/src/2-3-friction.c b/src/2-3-friction.c
new file mode 100755
index 0000000..d78fc8e
--- /dev/null
+++ b/src/2-3-friction.c
@@ -0,0 +1,133 @@
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL2_gfxPrimitives.h>
+
+#define PROJECT_NAME "vectors-friction"
+#define obj_n 10
+
+typedef struct OBJECT_INER {
+ Point pos;
+ Point vel;
+ Point acc;
+ float r;
+ float mass;
+} OBJECT_INER;
+
+typedef struct OBJECT {
+ OBJECT_INER *objs[obj_n];
+} OBJECT;
+
+float mu=0.1;
+
+#include <init-multi.h>
+
+void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){
+ obj->pos.x = x;
+ obj->pos.y = y;
+ obj->vel = (Point){0,0};
+ obj->acc = (Point){0,0};
+ obj->mass = mass;
+ obj->r = sqrt(obj->mass) * 10;
+}
+
+void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){
+ for (int i=0; i<obj_n; i++){
+ constructor(w, h, &objs[i], RandomFloat(0, w), 200, RandomFloat(1, 8));
+ }
+}
+
+Point vec_sub(Point A, Point B){
+ point_sub(&A, &B);
+ return A;
+}
+
+void applyForce(OBJECT_INER *obj, Point force){
+ //no need to make a copy. tis not a pointer
+ point_div(&force, &(Point){obj->mass,obj->mass});
+ point_add(&obj->acc, &force);
+}
+
+void friction(STUFFS *stuff, OBJECT_INER *obj){
+ int diff = stuff->height - (obj->pos.y + obj->r);
+ if (diff < 1){
+ //will ignore mass
+ //point_mul(&obj->vel,&(Point){0.95,0.95});
+
+ // Direction of Friction
+ Point friction = { obj->vel.x, obj->vel.y, };
+ point_set_mag(&friction, 1.0);
+ point_mul(&friction,&(Point){-1,-1});
+
+ // Magnitude of Friction
+ float normal = obj->mass;
+ point_set_mag(&friction, mu * normal);
+
+ applyForce(obj, friction);
+ }
+}
+
+void edges(STUFFS *stuff, OBJECT_INER *obj) {
+ if (obj->pos.y >= stuff->height - obj->r){
+ obj->pos.y = stuff->height - obj->r;
+ obj->vel.y = obj->vel.y * -1;
+ }
+
+ if (obj->pos.x >= stuff->width - obj->r){
+ obj->pos.x = stuff->width - obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ } else if (obj->pos.x <= obj->r){
+ obj->pos.x = obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ }
+}
+
+void update(STUFFS *stuff, OBJECT_INER *obj){
+ point_add(&obj->vel,&obj->acc);
+ point_add(&obj->pos, &obj->vel);
+ obj->acc = (Point){0,0};
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT_INER *obj){
+ SDL_SetRenderDrawColor(renderer,150,50,255,10);
+
+ Sint16 x = (Sint16)obj->pos.x;
+ Sint16 y = (Sint16)obj->pos.y;
+
+ filledCircleRGBA(renderer, x, y, obj->r, 150, 50, 255, 255);
+}
+
+void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
+ SDL_SetRenderDrawColor(renderer,0,0,0,255);
+ SDL_RenderClear(renderer);
+
+ for (int i=0; i<obj_n; i++){
+ Point gravity = {0, 0.2};
+ Point weight = {0};
+
+ weight.x = gravity.x * stuff->obj.objs[i]->mass;
+ weight.y = gravity.y * stuff->obj.objs[i]->mass;
+ if(stuff->mousedown == true){
+ Point wind = {0.1, 0};
+ applyForce(stuff->obj.objs[i], wind);
+ }
+
+ applyForce(stuff->obj.objs[i], weight);
+
+ friction(stuff,stuff->obj.objs[i]);
+
+ update(stuff,stuff->obj.objs[i]);
+ edges(stuff,stuff->obj.objs[i]);
+ show(renderer,stuff,stuff->obj.objs[i]);
+ }
+
+ SDL_RenderPresent(renderer);
+}
+
+void mousePressed(STUFFS *stuff){
+
+}
+
+#include <init-multi.c>
diff --git a/src/2-4-drag.c b/src/2-4-drag.c
new file mode 100755
index 0000000..503af42
--- /dev/null
+++ b/src/2-4-drag.c
@@ -0,0 +1,146 @@
+#include <SDL2/SDL_render.h>
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL2_gfxPrimitives.h>
+
+#define PROJECT_NAME "vectors-drag"
+#define obj_n 10
+
+typedef struct OBJECT_INER {
+ Point pos;
+ Point vel;
+ Point acc;
+ float r;
+ float mass;
+} OBJECT_INER;
+
+typedef struct OBJECT {
+ OBJECT_INER *objs[obj_n];
+} OBJECT;
+
+float DragC = 0.2;
+
+#include <init-multi.h>
+
+void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){
+ obj->pos.x = x;
+ obj->pos.y = y;
+ obj->vel = (Point){0,0};
+ obj->acc = (Point){0,0};
+ obj->mass = mass;
+ obj->r = sqrt(obj->mass) * 10;
+}
+
+void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){
+ for (int i=0; i<obj_n; i++){
+ constructor(w, h, &objs[i], RandomFloat(0, w), 100, RandomFloat(1, 8));
+ }
+}
+
+Point vec_sub(Point A, Point B){
+ point_sub(&A, &B);
+ return A;
+}
+
+void applyForce(OBJECT_INER *obj, Point force){
+ point_div(&force, &(Point){obj->mass,obj->mass});
+ point_add(&obj->acc, &force);
+}
+
+void drag(STUFFS *stuff, OBJECT_INER *obj, float c){
+ // something something velocity is 0 objects goto corner idk why
+ if(magnitude(obj->vel.x, obj->vel.y) != 0){
+ Point drag = { obj->vel.x, obj->vel.y, };
+ point_set_mag(&drag, 1.0);
+ point_mul(&drag,&(Point){-1,-1});
+
+ float speed = magnitude(obj->vel.x, obj->vel.y);
+ point_set_mag(&drag, c * (speed * speed));
+
+ applyForce(obj, drag);
+ }
+}
+
+void edges(STUFFS *stuff, OBJECT_INER *obj) {
+ if (obj->pos.y >= stuff->height - obj->r){
+ obj->pos.y = stuff->height - obj->r;
+ obj->vel.y = obj->vel.y * -1;
+ }
+
+ if (obj->pos.x >= stuff->width - obj->r){
+ obj->pos.x = stuff->width - obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ } else if (obj->pos.x <= obj->r){
+ obj->pos.x = obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ }
+}
+
+void update(STUFFS *stuff, OBJECT_INER *obj){
+ point_add(&obj->vel,&obj->acc);
+ point_add(&obj->pos, &obj->vel);
+ obj->acc = (Point){0,0};
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT_INER *obj){
+ Sint16 x = (Sint16)obj->pos.x;
+ Sint16 y = (Sint16)obj->pos.y;
+
+ circleRGBA(renderer, x, y, obj->r, 190, 90, 255, 255);
+ filledCircleRGBA(renderer, x, y, obj->r, 150, 50, 255, 80);
+}
+
+void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
+ SDL_SetRenderDrawColor(renderer,0,0,0,255);
+ SDL_RenderClear(renderer);
+
+ //SDL_SetRenderDrawColor(renderer,0,0,0,255);
+ //SDL_RenderFillRect(renderer, &(SDL_Rect){0,0,stuff->width,stuff->height});
+
+ SDL_SetRenderDrawColor(renderer,255,255,255,90);
+ SDL_RenderFillRect(renderer, &(SDL_Rect){0,stuff->height/2,stuff->width,stuff->height});
+
+ for (int i=0; i<obj_n; i++){
+ Point gravity = {0, 0.2};
+ Point weight = {0};
+
+ weight.x = gravity.x * stuff->obj.objs[i]->mass;
+ weight.y = gravity.y * stuff->obj.objs[i]->mass;
+ if(stuff->mousedown == true){
+ Point wind = {0.1, 0};
+ applyForce(stuff->obj.objs[i], wind);
+ }
+
+ applyForce(stuff->obj.objs[i], weight);
+
+ if(stuff->obj.objs[i]->pos.y > (float)stuff->height/2){
+ drag(stuff,stuff->obj.objs[i],DragC);
+ }
+
+ update(stuff,stuff->obj.objs[i]);
+ edges(stuff,stuff->obj.objs[i]);
+ show(renderer,stuff,stuff->obj.objs[i]);
+ }
+
+ SDL_RenderPresent(renderer);
+}
+
+void mousePressed(STUFFS *stuff){
+
+}
+
+#include <init-multi.c>
+
+
+/* Exercises
+ *
+ * make a liquid class/object that
+ * describes a density
+ * coefficient
+ * and area of where that liquid or gas is
+ *
+ * Surface Area
+ */
diff --git a/src/2-5-grav-attraction.c b/src/2-5-grav-attraction.c
new file mode 100755
index 0000000..89ee28a
--- /dev/null
+++ b/src/2-5-grav-attraction.c
@@ -0,0 +1,114 @@
+#include <SDL2/SDL_render.h>
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL2_gfxPrimitives.h>
+
+#define PROJECT_NAME "vectors-gravity attraction"
+#define obj_n 10
+
+typedef struct ATTRACTOR {
+ Point pos;
+ float r;
+ float mass;
+} ATTRACTOR;
+
+typedef struct OBJECT_INER {
+ Point pos;
+ Point vel;
+ Point acc;
+ float r;
+ float mass;
+} OBJECT_INER;
+
+typedef struct OBJECT {
+ ATTRACTOR attractor;
+ OBJECT_INER *objs[obj_n];
+} OBJECT;
+
+#include <init-multi.h>
+
+void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){
+ obj->pos.x = x;
+ obj->pos.y = y;
+ obj->vel = (Point){RandomFloat(-1, 1),RandomFloat(-1, 1)};
+ point_mul(&obj->vel,&(Point){5,5});
+ obj->acc = (Point){0,0};
+ obj->mass = mass;
+ obj->r = sqrt(obj->mass) * 2;
+}
+
+void attractor_constructor(int w, int h, ATTRACTOR *attractor, float x, float y, float mass){
+ attractor->pos.x = x;
+ attractor->pos.y = y;
+ attractor->mass = mass;
+ attractor->r = sqrt(attractor->mass) * 2;
+}
+
+void init_stuffs(int w, int h, OBJECT_INER *objs,STUFFS *stuff){
+ for (int i=0; i<obj_n; i++){
+ constructor(w, h, &objs[i], RandomFloat(0, w), RandomFloat(0, h), RandomFloat(50, 150));
+ }
+ attractor_constructor(w,h,&stuff->obj.attractor,(float)w/2,(float)h/2,100);
+}
+
+void applyForce(OBJECT_INER *obj, Point force){
+ point_div(&force, &(Point){obj->mass,obj->mass});
+ point_add(&obj->acc, &force);
+}
+
+void update(STUFFS *stuff, OBJECT_INER *obj){
+ point_add(&obj->vel,&obj->acc);
+ point_add(&obj->pos, &obj->vel);
+ obj->acc = (Point){0,0};
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT_INER *obj){
+ Sint16 x = (Sint16)obj->pos.x;
+ Sint16 y = (Sint16)obj->pos.y;
+
+ circleRGBA(renderer, x, y, obj->r, 190, 90, 255, 255);
+ filledCircleRGBA(renderer, x, y, obj->r, 150, 50, 255, 80);
+}
+
+void attract(ATTRACTOR *attractor, OBJECT_INER *obj){
+ Point force = {attractor->pos.x - obj->pos.x, attractor->pos.y - obj->pos.y};
+ float dist = magnitude(force.x, force.y);
+ dist = dist * dist;
+ dist = constrain(dist,100,1000);
+
+ float G = 2;
+ float strength = G * (attractor->mass * obj->mass) / dist;
+
+ point_set_mag(&force, strength);
+ applyForce(obj,force);
+}
+
+void atr_show(SDL_Renderer* renderer,STUFFS *stuff,ATTRACTOR *attr){
+ SDL_RenderFillRect(renderer, &(SDL_Rect){attr->pos.x - attr->r, attr->pos.y - attr->r, attr->r*2, attr->r*2});
+}
+
+void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
+ SDL_SetRenderDrawColor(renderer,0,0,0,5);
+ //SDL_RenderClear(renderer);
+ SDL_RenderFillRect(renderer, &(SDL_Rect){0,0,stuff->width,stuff->height});
+
+ for (int i=0; i<obj_n; i++){
+ update(stuff,stuff->obj.objs[i]);
+ show(renderer,stuff,stuff->obj.objs[i]);
+
+ attract(&stuff->obj.attractor, stuff->obj.objs[i]);
+ }
+ SDL_SetRenderDrawColor(renderer, 100,20,20,180);
+ atr_show(renderer,stuff,&stuff->obj.attractor);
+
+ SDL_RenderPresent(renderer);
+}
+
+void mousePressed(STUFFS *stuff){
+
+}
+
+#include <init-multi.c>