#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