#include #include #include #include #include #include #define PROJECT_NAME "vectors-gravity wind" #define obj_n 1 typedef struct { Point pos; Point vel; Point acc; float r; } OBJECT_INER; typedef struct OBJECT { OBJECT_INER *objs[obj_n]; } OBJECT; #include void constructor(int w, int h, OBJECT_INER *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; } void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ srand(time(NULL)); constructor(w,h,&objs[0]); } Point vec_sub(Point A, Point B){ point_sub(&A, &B); return A; } void applyForce(OBJECT_INER *obj, Point force){ point_add(&obj->acc, &force); } void edges(STUFFS *stuff, OBJECT_INER *obj) { if (obj->pos.y >= stuff->height - obj->r / 2){ obj->pos.y = stuff->height - obj->r / 2; obj->vel.y = obj->vel.y * -1; } if (obj->pos.x >= stuff->width - obj->r / 2){ obj->pos.x = stuff->width - obj->r / 2; obj->vel.x = obj->vel.x * -1; } else if (obj->pos.x <= obj->r / 2){ obj->pos.x = obj->r / 2; obj->vel.x = obj->vel.x * -1; } } void update(STUFFS *stuff, OBJECT_INER *obj){ //Point mouse = { // stuff->mx, // stuff->my, //}; //obj->acc = vec_sub(mouse, obj->pos); //point_set_mag(&obj->acc, 0.1); point_add(&obj->vel,&obj->acc); point_add(&obj->pos, &obj->vel); obj->acc = (Point){0,0}; } void show(SDL_Renderer* renderer,OBJECT_INER *obj){ SDL_SetRenderDrawColor(renderer,150,50,255,255); SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - (obj->r / 2), .y=obj->pos.y - (obj->r / 2), .w = obj->r, .h = 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.objs[0], wind); } Point gravity = {0, 0.2}; applyForce(stuff->obj.objs[0], gravity); update(stuff,stuff->obj.objs[0]); edges(stuff,stuff->obj.objs[0]); show(renderer,stuff->obj.objs[0]); SDL_RenderPresent(renderer); } #include