From 444c6642d67e63fc88e5fa1d7c06062d04106b44 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Mon, 23 Feb 2026 07:18:20 -0600 Subject: 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 --- src/1-1-walker.c | 2 +- src/1-2-vec-math.c | 2 +- src/1-3-rand-vec.c | 2 +- src/1-5-unit-vec.c | 3 +- src/1-6-mover.c | 2 +- src/2-1-gravity-wind.c | 2 +- src/2-2-mass-accel.c | 37 ++++++------ src/2-3-friction.c | 133 +++++++++++++++++++++++++++++++++++++++++ src/2-4-drag.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++ src/2-5-grav-attraction.c | 114 ++++++++++++++++++++++++++++++++++++ 10 files changed, 419 insertions(+), 24 deletions(-) create mode 100755 src/2-3-friction.c create mode 100755 src/2-4-drag.c create mode 100755 src/2-5-grav-attraction.c (limited to 'src') 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 #include -#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 #include -#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 #include -#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 #include #include #include #include -#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 #include -#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 #include -#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 #include -#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 -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 +#include + +#include +#include +#include + +#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 + +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; imass,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; iobj.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 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 +#include +#include + +#include +#include +#include + +#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 + +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; imass,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; iobj.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 + + +/* 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 +#include +#include + +#include +#include +#include + +#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 + +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; iobj.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; iobj.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 -- cgit v1.3