-
Notifications
You must be signed in to change notification settings - Fork 1
/
coins.c
86 lines (67 loc) · 2.23 KB
/
coins.c
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
#include <stdlib.h>
#include <ncurses.h>
#include <caca.h>
#include <chipmunk/chipmunk.h>
int main(int argc, char **argv)
{
WINDOW *w;
cpVect gravity = cpv(0, -100);
cpSpace *space = cpSpaceNew();
int ch;
initscr();
start_color();
cbreak();
cpSpaceSetGravity(space, gravity);
cpShape *ground = cpSegmentShapeNew(cpSpaceGetStaticBody(space),
cpv(-20, 5),
cpv(20, -5),
0);
cpShapeSetFriction(ground, 1);
cpSpaceAddShape(space, ground);
cpFloat radius = 5;
cpFloat mass = 1;
// The moment of inertia is like mass for rotation
// Use the cpMomentFor*() functions to help you approximate it.
cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);
// The cpSpaceAdd*() functions return the thing that you are adding.
// It's convenient to create and add an object in one line.
cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
cpBodySetPos(ballBody, cpv(0, 15));
cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
cpShapeSetFriction(ballShape, 0.7);
// Now that it's all set up, we simulate all the objects in the space by
// stepping forward through time in small increments called steps.
// It is *highly* recommended to use a fixed size time step.
cpFloat timeStep = 1.0/60.0;
keypad(stdscr, TRUE);
noecho();
init_pair(1, COLOR_CYAN, COLOR_RED);
attron(COLOR_PAIR(1));
printw("Hello");
cpVect pos = cpBodyGetPos(ballBody);
w = newwin(5, 5, 20 - pos.y, pos.x);
//box(w, 0, 0);
waddstr(w, "O");
refresh();
wrefresh(w);
while ((ch = getch()) != KEY_F(1)) {
cpVect pos = cpBodyGetPos(ballBody);
cpVect vel = cpBodyGetVel(ballBody);
werase(w);
wrefresh(w);
refresh();
mvwin(w, 20 - pos.y, pos.x);
waddstr(w, "O");
wrefresh(w);
refresh();
cpSpaceStep(space, timeStep);
}
delwin(w);
endwin();
// Clean up our objects and exit!
cpShapeFree(ballShape);
cpBodyFree(ballBody);
cpShapeFree(ground);
cpSpaceFree(space);
return 0;
}