-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFood.pde
97 lines (82 loc) · 1.83 KB
/
Food.pde
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
class Food{
Body body;
Vec2 location;
Vec2 velocity;
Vec2 acceleration;
float lifespan;
float w,h;
float r;
Food(){
w = 7;
h = 19;
r = 7;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
location = bd.position.set(box2d.coordPixelsToWorld(mouseX,mouseY));
velocity = new Vec2(0,0);
acceleration = new Vec2(0,0.02);
lifespan = 255.0;
body = box2d.createBody(bd);
PolygonShape ps = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
ps.setAsBox(box2dW,box2dH);
CircleShape cs = new CircleShape();
cs.m_radius = box2d.scalarPixelsToWorld(r);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5;
body.createFixture(fd);
body.createFixture(cs,1.0);
body.setUserData(this);
//fd.setUserData("Food");
}
void run(){
update();
display();
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mulLocal(0);
lifespan -= 0.8;
if(lifespan <= 0){
box2d.destroyBody(body);
}
}
void display(){
Vec2 pos = box2d.getBodyPixelCoord(body);
float a = body.getAngle();
rectMode(CENTER);
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(175,lifespan);
stroke(0,lifespan);
rect(0,0,w,h);
ellipse(0,-h/2,r*2,r*2);
popMatrix();
}
void applyForce(Vec2 force){
Vec2 pos = body.getWorldCenter();
body.applyForce(force,pos);
}
boolean isExpired(){
if(lifespan <= 0.0){
return true;
}
else{
return false;
}
}
void killBody(){
lifespan = 0;
try{
box2d.world.destroyBody(body);
}
catch(AssertionError e){
}
}
}