gfx and 2-2 and multi objects

This commit is contained in:
2026-02-08 20:01:32 -06:00
parent ce57691f96
commit e06ab8f76b
12 changed files with 483 additions and 8 deletions

View File

@@ -1,4 +1,3 @@
#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
@@ -13,10 +12,6 @@ typedef struct {
#include <init.h>
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;

63
src/1-5-unit-vec.c Executable file
View File

@@ -0,0 +1,63 @@
#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#define PROJECT_NAME "vecotrs-main"
typedef struct {
Point pos;
} OBJECT;
#include <init.h>
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 <init.c>

80
src/1-6-mover.c Executable file
View File

@@ -0,0 +1,80 @@
#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <time.h>
#define PROJECT_NAME "vecotrs-main"
typedef struct {
Point pos;
Point vel;
Point acc;
} OBJECT;
#include <init.h>
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 <init.c>

103
src/2-1-gravity-wind.c Executable file
View File

@@ -0,0 +1,103 @@
#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <time.h>
#define PROJECT_NAME "vecotrs-main"
typedef struct {
Point pos;
Point vel;
Point acc;
float r;
} OBJECT;
#include <init.h>
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 <init.c>

107
src/2-2-mass-accel.c Executable file
View File

@@ -0,0 +1,107 @@
#include <stdbool.h>
#include <global_objects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#define PROJECT_NAME "vecotrs-main"
typedef struct OBJECT {
Point pos;
Point vel;
Point acc;
float r;
float mass;
} OBJECT;
#define obj_n 2
#include <init-multi.h>
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; i<obj_n; i++){
Point gravity = {0, 0.2};
Point weight = {0};
weight.x = gravity.x * stuff->obj[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 <init-multi.c>