/
world.cpp
74 lines (53 loc) · 1.32 KB
/
world.cpp
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
#include "world.h"
#include <stdio.h>
float human_size=0.5;
World::World(cpVect size) {
space=cpSpaceNew();
static_body=cpBodyNewStatic();
printf("new space\n"); fflush(stdout);
cpVect s2=cpvmult(size,0.5);
cpVect c1=s2;
cpVect c2=cpv(s2.x,-s2.y);
cpVect c3=cpv(-s2.x,-s2.y);
cpVect c4=cpv(-s2.x,s2.y);
printf("foo space\n"); fflush(stdout);
cpBodyInitStatic(space->staticBody);
addStaticLine(c1,c2);
addStaticLine(c2,c3);
addStaticLine(c3,c4);
addStaticLine(c4,c1);
printf("space done\n"); fflush(stdout);
}
World::~World() {
cpSpaceDestroy(space);
}
void World::update(long dt) {
cpSpaceStep(space,dt);
}
Human* World::addHuman(float size) {
Human* h=new Human();
cpShape* shape=cpCircleShapeNew(h->body,size,cpvzero);
shape->e=0.5;
shape->u=0.2;
h->shape=shape;
humans.push_back(h);
return h;
}
void World::removeHuman(Human* h) {
cpSpaceRemoveShape(space,h->shape);
cpSpaceRemoveBody(space,h->body);
for(int i=0;i<humans.size();i++) {
if(h==humans[i]) {
humans.erase(humans.begin()+i);
break;
}
}
}
void World::addStaticLine(cpVect start,cpVect end) {
cpShape* s=cpSegmentShapeNew(static_body,start,end,0.1);
cpSpaceAddStaticShape(space,s);
}
void World::addStaticCircle(cpVect center,float r) {
cpShape* s=cpCircleShapeNew(space->staticBody,r,center);
cpSpaceAddShape(space,s);
}