From e06ab8f76be21b866207a708f1336a5c1a53f149 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Sun, 8 Feb 2026 20:01:32 -0600 Subject: gfx and 2-2 and multi objects --- src/1-3-rand-vec.c | 5 --- src/1-5-unit-vec.c | 63 +++++++++++++++++++++++++++++ src/1-6-mover.c | 80 ++++++++++++++++++++++++++++++++++++ src/2-1-gravity-wind.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++ src/2-2-mass-accel.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+), 5 deletions(-) create mode 100755 src/1-5-unit-vec.c create mode 100755 src/1-6-mover.c create mode 100755 src/2-1-gravity-wind.c create mode 100755 src/2-2-mass-accel.c (limited to 'src') diff --git a/src/1-3-rand-vec.c b/src/1-3-rand-vec.c index f592fdc..2880f6b 100755 --- a/src/1-3-rand-vec.c +++ b/src/1-3-rand-vec.c @@ -1,4 +1,3 @@ -#include #include #include @@ -13,10 +12,6 @@ typedef struct { #include -float RandomFloat(float min, float max){ - return ((max - min) * ((float)rand() / RAND_MAX)) + min; -} - void constructor(int w, int h, OBJECT *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; diff --git a/src/1-5-unit-vec.c b/src/1-5-unit-vec.c new file mode 100755 index 0000000..84710eb --- /dev/null +++ b/src/1-5-unit-vec.c @@ -0,0 +1,63 @@ +#include +#include +#include + +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + Point pos; +} OBJECT; + +#include + +void constructor(int w, int h, OBJECT *obj){ + obj->pos.x = (float)w/2; + obj->pos.y = (float)h/2; +} + +OBJECT init_stuffs(int w, int h){ + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +void normalize(Point *p){ + point_set_mag(p, 1); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,0,0,0,255); + SDL_RenderClear(renderer); + + Point pos = { 200,200 }; + Point mouse = {stuff->mx, stuff->my}; + + Point v = { + mouse.x - pos.x, + mouse.y - pos.y, + }; + + //float mag = magnitude(v.x, v.y); + //point_div(&v,&(Point){mag,mag}); + + //normalize(&v); + + //point_mul(&v,&(Point){50,50}); + + point_set_mag(&v, 50); + + SDL_SetRenderDrawColor(renderer,150,50,255,255); + SDL_RenderDrawLine(renderer,stuff->width/2,stuff->height/2,v.x + (float)stuff->width/2,v.y + (float)stuff->height/2); + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include diff --git a/src/1-6-mover.c b/src/1-6-mover.c new file mode 100755 index 0000000..c7ff186 --- /dev/null +++ b/src/1-6-mover.c @@ -0,0 +1,80 @@ +#include +#include +#include + +#include +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + Point pos; + Point vel; + Point acc; +} OBJECT; + +#include + +void constructor(int w, int h, OBJECT *obj){ + obj->pos.x = (float)w/2; + obj->pos.y = (float)h/2; + obj->vel.x = RandomFloat(-1, 1); + obj->vel.y = RandomFloat(-1, 1); + point_mul(&obj->vel, &(Point){3,3}); + obj->acc.x = RandomFloat(-1, 1); + obj->acc.y = RandomFloat(-1, 1); + point_set_mag(&obj->acc, 0.01); +} + +OBJECT init_stuffs(int w, int h){ + srand(time(NULL)); + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +Point vec_sub(Point A, Point B){ + point_sub(&A, &B); + return A; +} + +void update(STUFFS *stuff){ + Point mouse = { + stuff->mx, + stuff->my, + }; + + stuff->obj->acc = vec_sub(mouse, stuff->obj->pos); + point_set_mag(&stuff->obj->acc, 1); + + //stuff->obj->acc.x = RandomFloat(-1, 1); + //stuff->obj->acc.y = RandomFloat(-1, 1); + + point_add(&stuff->obj->vel,&stuff->obj->acc); + point_limit(&stuff->obj->vel, 5); + + point_add(&stuff->obj->pos, &stuff->obj->vel); +} + +void show(SDL_Renderer* renderer,STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,150,50,255,255); + SDL_RenderFillRect(renderer,&(SDL_Rect){.x = stuff->obj->pos.x - 5, .y=stuff->obj->pos.y - 5, .w = 10, .h = 10}); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,0,0,0,255); + SDL_RenderClear(renderer); + + update(stuff); + show(renderer,stuff); + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include diff --git a/src/2-1-gravity-wind.c b/src/2-1-gravity-wind.c new file mode 100755 index 0000000..7a54b05 --- /dev/null +++ b/src/2-1-gravity-wind.c @@ -0,0 +1,103 @@ +#include +#include +#include + +#include +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + Point pos; + Point vel; + Point acc; + float r; +} OBJECT; + +#include + +void constructor(int w, int h, OBJECT *obj){ + obj->pos.x = (float)w/2; + obj->pos.y = (float)h/2; + obj->vel = (Point){0,0}; + obj->acc = (Point){0,0}; + obj->r = 40; +} + +OBJECT init_stuffs(int w, int h){ + srand(time(NULL)); + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +Point vec_sub(Point A, Point B){ + point_sub(&A, &B); + return A; +} + +void applyForce(OBJECT *obj, Point force){ + point_add(&obj->acc, &force); +} + +void edges(STUFFS *stuff) { + if (stuff->obj->pos.y >= stuff->height - stuff->obj->r / 2){ + stuff->obj->pos.y = stuff->height - stuff->obj->r / 2; + stuff->obj->vel.y = stuff->obj->vel.y * -1; + } + + if (stuff->obj->pos.x >= stuff->width - stuff->obj->r / 2){ + stuff->obj->pos.x = stuff->width - stuff->obj->r / 2; + stuff->obj->vel.x = stuff->obj->vel.x * -1; + } else if (stuff->obj->pos.x <= stuff->obj->r / 2){ + stuff->obj->pos.x = stuff->obj->r / 2; + stuff->obj->vel.x = stuff->obj->vel.x * -1; + } +} + +void update(STUFFS *stuff){ + //Point mouse = { + // stuff->mx, + // stuff->my, + //}; + + //stuff->obj->acc = vec_sub(mouse, stuff->obj->pos); + //point_set_mag(&stuff->obj->acc, 0.1); + + point_add(&stuff->obj->vel,&stuff->obj->acc); + point_add(&stuff->obj->pos, &stuff->obj->vel); + stuff->obj->acc = (Point){0,0}; +} + +void show(SDL_Renderer* renderer,STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,150,50,255,255); + SDL_RenderFillRect(renderer,&(SDL_Rect){.x = stuff->obj->pos.x - (stuff->obj->r / 2), .y=stuff->obj->pos.y - (stuff->obj->r / 2), .w = stuff->obj->r, .h = stuff->obj->r}); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,0,0,0,255); + SDL_RenderClear(renderer); + + if(stuff->mousedown == true){ + Point wind = {0.1, 0}; + applyForce(stuff->obj, wind); + } + + Point gravity = {0, 0.2}; + applyForce(stuff->obj, gravity); + + + update(stuff); + edges(stuff); + show(renderer,stuff); + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include diff --git a/src/2-2-mass-accel.c b/src/2-2-mass-accel.c new file mode 100755 index 0000000..dc79d3e --- /dev/null +++ b/src/2-2-mass-accel.c @@ -0,0 +1,107 @@ +#include +#include + +#include +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct OBJECT { + Point pos; + Point vel; + Point acc; + float r; + float mass; +} OBJECT; + +#define obj_n 2 + +#include + +void constructor(int w, int h, OBJECT *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}; + obj->acc = (Point){0,0}; + obj->mass = mass; + obj->r = sqrt(obj->mass) * 10; +} + +void init_stuffs(int w, int h, OBJECT *objs){ + constructor(w, h, &objs[0], 100, 100, 4); + constructor(w, h, &objs[1], -100, 100, 10); +} + +Point vec_sub(Point A, Point B){ + point_sub(&A, &B); + return A; +} + +void applyForce(OBJECT *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) { + 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 *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){ + 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[i]->mass; + weight.y = gravity.y * stuff->obj[i]->mass; + if(stuff->mousedown == true){ + Point wind = {0.1, 0}; + applyForce(stuff->obj[i], wind); + } + + applyForce(stuff->obj[i], weight); + + + update(stuff,stuff->obj[i]); + edges(stuff,stuff->obj[i]); + show(renderer,stuff,stuff->obj[i]); + } + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include -- cgit v1.3