-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerson.js
153 lines (115 loc) · 3.78 KB
/
Person.js
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
147
148
149
150
151
152
153
class Person {
constructor(x, y, personWidth, personHeight, world) {
this.world = world;
this.x = x;
this.y = y;
this.height = personHeight;
this.width = personWidth;
this.torso = new Torso(x, y - (personHeight / 2), personHeight, personWidth, this.world);
this.head = new Head(x, y - (personHeight + personWidth * 1), personWidth * 1, this.world);
var revJointDef = new b2RevoluteJointDef();
var jointPos = new Vec2(x / SCALE, (y - personHeight) / SCALE);
revJointDef.Initialize(this.head.body, this.torso.body, jointPos);
this.headJoint = this.world.CreateJoint(revJointDef);
var distJointDef = new b2DistanceJointDef();
var anchorTorso = new Vec2(x / SCALE, (y) / SCALE);
var anchorHead = new Vec2(x / SCALE, this.head.startingPosition.y / SCALE);
distJointDef.Initialize(this.head.body, this.torso.body, anchorHead, anchorTorso);
this.distJoint = this.world.CreateJoint(distJointDef);
}
show() {
this.head.show();
this.torso.show();
}
}
class Head {
constructor(x, y, r, world) {
this.world = world;
this.startingPosition = createVector(x, y);
this.radius = r;
this.body;
this.id = "head";
this.makeBody();
this.isCB = false;
}
makeBody() {
let bodyDef = new b2BodyDef();
bodyDef.type = b2DynamicBody;
bodyDef.position.x = this.startingPosition.x / SCALE;
bodyDef.position.y = this.startingPosition.y / SCALE;
bodyDef.angle = 0;
let fixDef = new b2FixtureDef();
fixDef.density = 0.001;
fixDef.friction = 0.01;
fixDef.restitution = 0.01;
fixDef.shape = new b2CircleShape(this.radius / SCALE);
this.body = this.world.CreateBody(bodyDef);
this.body.SetUserData(this);
var filtData = new b2FilterData();
filtData.categoryBits = PERSON_CATEGORY;
filtData.maskBits = PERSON_MASK;
this.body.CreateFixture(fixDef).SetFilterData(filtData);
}
show() {
let x = this.body.GetPosition().x * SCALE;
let y = this.body.GetPosition().y * SCALE;
let angle = this.body.GetAngle();
push();
translate(x - panX, y - panY);
rotate(angle);
if (cbHead) {
image(CBHeadSprite, -this.radius - 7, -this.radius - 15, this.radius * 3, this.radius * 3);
} else {
image(headSprite, -this.radius - 8, -this.radius - 15, this.radius * 3, this.radius * 3);
}
pop();
}
}
class Torso {
constructor(centerX, centerY, height, width, world) {
this.id = "torso";
this.world = world;
this.width = width;
this.height = height;
this.startingPosition = createVector(centerX, centerY);
this.body;
this.colour = color(0, 0, 0);
this.makeBody();
}
makeBody() {
let bodyDef = new b2BodyDef();
bodyDef.type = b2DynamicBody;
bodyDef.position.x = this.startingPosition.x / SCALE;
bodyDef.position.y = this.startingPosition.y / SCALE;
bodyDef.angle = 0;
let fixDef = new b2FixtureDef();
fixDef.density = 0.002;
fixDef.friction = 0.01;
fixDef.restitution = 0.01;
fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(this.width / 2 / SCALE, this.height / 2 / SCALE);
this.body = this.world.CreateBody(bodyDef);
this.body.SetUserData(this);
var filtData = new b2FilterData();
// filtData.groupIndex = -1;
filtData.categoryBits = PERSON_CATEGORY;
filtData.maskBits = PERSON_MASK;
this.body.CreateFixture(fixDef).SetFilterData(filtData);
}
show() {
let x = this.body.GetPosition().x * SCALE;
let y = this.body.GetPosition().y * SCALE;
let angle = this.body.GetAngle();
push();
translate(x - panX, y - panY);
rotate(angle);
// fill(60, 80, 10);
fill(this.colour);
// noStroke();
stroke(0);
strokeWeight(1);
rectMode(CENTER);
rect(0, 0, this.width, this.height);
pop();
}
}