-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.js
98 lines (91 loc) · 2.72 KB
/
ball.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
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A circular particle
// Constructor
class Ball {
constructor(x, y) {
this.r = 25;
this.col = color(0);
this.ableToJump = true;
// Define a body
let bd = new box2d.b2BodyDef();
bd.type = box2d.b2BodyType.b2_dynamicBody;
bd.position = scaleToWorld(x, y);
// Define a fixture
let fd = new box2d.b2FixtureDef();
// Fixture holds shape
fd.shape = new box2d.b2CircleShape();
fd.shape.m_radius = scaleToWorld(this.r);
// Some physics
fd.density = 1.0;
fd.friction = 0.1;
fd.restitution = 0.3;
// Create the body
this.body = world.CreateBody(bd);
// Attach the fixture
this.body.CreateFixture(fd);
// Some additional stuff
this.body.SetAngularVelocity(random(-5, 5));
this.body.SetUserData(this);
}/**
* this action i fired by the collision detection
*/
change() {
this.ableToJump = true;
}
setColor(col){
this.col = col;
}
motion(x) {
// x = scaleToWorld(x);
let pos = this.body.GetPosition();
let y = this.body.GetLinearVelocity().y;
this.body.SetLinearVelocity(new box2d.b2Vec2(x, y));
}
jump() {
if (this.ableToJump) {
let x = this.body.GetLinearVelocity().x;
this.body.SetLinearVelocity(new box2d.b2Vec2(x, -50));
this.ableToJump = false;
}
}
edge() {
let pos = this.position();
let w = scaleToWorld(width);
let h = scaleToWorld(height)
let x = scaleToWorld(pos.x);
let y = scaleToWorld(pos.y);
if (pos.x < 0) this.body.SetPositionXY(w, y);
if (pos.x > width) this.body.SetPositionXY(0, y);
if (pos.y < 0) this.body.SetPositionXY(x, h);
if (pos.y > height) {
this.body.SetPositionXY(x, 0);
// when the ball falls off the screen the game swears!
swearText();
}
}
position() {
return scaleToPixels(this.body.GetPosition());
}
// Drawing the box
display() {
// Get the body's position
// let pos = scaleToPixels(this.body.GetPosition());
// Get its angle of rotation
let a = this.body.GetAngleRadians();
// Draw it!
rectMode(CENTER);
push();
translate(this.position().x, this.position().y);
rotate(a);
fill(this.col);
noStroke();
ellipse(0, 0, this.r * 2, this.r * 2);
// Let's add a line so we can see the rotation
stroke(200);
strokeWeight(2);
line(0, 0, this.r, 0);
pop();
}
}