gravity constant and gravitational acceleration
This commit is contained in:
44
physics.c
44
physics.c
@@ -4,13 +4,16 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define GRAVITY 0.01f
|
||||||
|
#define FRAMERATE 10
|
||||||
|
|
||||||
typedef struct Point{
|
typedef struct Point{
|
||||||
uint x;
|
double x;
|
||||||
uint y;
|
double y;
|
||||||
uint px;
|
double px;
|
||||||
uint py;
|
double py;
|
||||||
uint fx;
|
double fx;
|
||||||
uint fy;
|
double fy;
|
||||||
} Point;
|
} Point;
|
||||||
|
|
||||||
typedef struct Link {
|
typedef struct Link {
|
||||||
@@ -22,14 +25,14 @@ typedef struct Link {
|
|||||||
|
|
||||||
|
|
||||||
void draw_link(Canvas* c, Link* l){
|
void draw_link(Canvas* c, Link* l){
|
||||||
line(&c, l->a->x, l->a->y, l->b->x, l->b->y);
|
line(c, l->a->x, l->a->y, l->b->x, l->b->y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_point(Point* p){
|
void update_point(Point* p){
|
||||||
uint tempx = p->x;
|
uint tempx = p->x;
|
||||||
uint tempy = p->y;
|
uint tempy = p->y;
|
||||||
p->x += (p->x - p->px) + fx;
|
p->x += (p->x - p->px) + p->fx;
|
||||||
p->y += (p->y - p->py) + fy;
|
p->y += (p->y - p->py) + p->fy;
|
||||||
p->px = tempx;
|
p->px = tempx;
|
||||||
p->py = tempy;
|
p->py = tempy;
|
||||||
}
|
}
|
||||||
@@ -38,25 +41,30 @@ void update_point(Point* p){
|
|||||||
int main(){
|
int main(){
|
||||||
char *data = malloc(40*40);
|
char *data = malloc(40*40);
|
||||||
Canvas c = {40, 40, data};
|
Canvas c = {40, 40, data};
|
||||||
memset(c.data, '.', c.x * c.y);
|
|
||||||
|
|
||||||
|
Point a = { 10.0, 10.0,
|
||||||
|
10.0, 10.0,
|
||||||
|
0.0, GRAVITY/FRAMERATE};
|
||||||
|
|
||||||
|
Point b = { 20.0, 10.0,
|
||||||
|
20.0, 10.0,
|
||||||
|
0.0, GRAVITY/FRAMERATE};
|
||||||
|
|
||||||
Point a = {10, 10, 10, 10, 0, 5};
|
|
||||||
Point b = {20, 10, 10, 10, 0, 5};
|
|
||||||
Link l_ab = {&a, &b, 10};
|
Link l_ab = {&a, &b, 10};
|
||||||
|
|
||||||
//line(&c, 1, 1, 8, 8);
|
|
||||||
//line(&c, 8, 8, 1, 1);
|
|
||||||
//line(&c, 0, 5, 8, 8);
|
|
||||||
|
|
||||||
for(int i=0; i<100; i++){
|
while(1){
|
||||||
|
// clear canvas, calculate next frame and draw
|
||||||
|
memset(c.data, '.', c.x * c.y);
|
||||||
draw_link(&c, &l_ab);
|
draw_link(&c, &l_ab);
|
||||||
update_point(&a);
|
update_point(&a);
|
||||||
update_point(&b);
|
update_point(&b);
|
||||||
|
|
||||||
|
// then render and wait
|
||||||
|
render(&c);
|
||||||
|
usleep(1000000/FRAMERATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
render(&c);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user