aboutsummaryrefslogtreecommitdiff
path: root/src/2-4-drag.c
blob: 1bcbe17f22f94d9670365ca040bfeeac0f9b7951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <SDL2/SDL_render.h>
#include <stdbool.h>
#include <vectorlib.c>

#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL2_gfxPrimitives.h>

#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 <init.h>

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; i<obj_n; i++){
        constructor(w, h, &objs[i], RandomFloat(0, w), 100, RandomFloat(1, 8));
    }
}

Point vec_sub(Point A, Point B){
    point_sub(&A, &B);
    return A;
}

void applyForce(OBJECT_INER *obj, Point force){
    point_div(&force, &(Point){obj->mass,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; i<obj_n; i++){
        Point gravity = {0, 0.2};
        Point weight = {0};

        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.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 <init.c>


/* Exercises
 *
 * make a liquid class/object that
 * describes a density
 * coefficient
 * and area of where that liquid or gas is
 *
 * Surface Area
 */