-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (123 loc) · 4.73 KB
/
index.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
// Implementation of makeBody() //
(function(window, _) {
window.TheGame = window.TheGame || {
numz: {
getDistance(pointA, pointB) {
const
distanceX = pointB.x - pointA.x,
distanceY = pointB.y - pointA.y,
distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
return distance;
},
getAngleDegrees(pointA, pointB) {
const
distanceX = pointB.x - pointA.x,
distanceY = pointB.y - pointA.y,
radians = Math.atan2(distanceY, distanceX),
degrees = radians * 180 / Math.PI;
return degrees;
},
degreesToRadians(degrees) {
return degrees * Math.PI / 180;
},
radiansToDegrees(radians) {
return radians * 180 / Math.PI;
},
},
phyz: {
/**
* Returns an Object with basic properties utilized in a
* 2D physics system. On top of simple physical properties,
* the body has template methods handleCollision() and update().
*
* @param {String} type: A String, should be unique to your
* system, representing the type of body.
* @param {Object} options.
* @param {Number} options.velocityX: The body's velocity on the x axis.
* @param {Number} options.velocityY: The body's velocity on the y axis.
* @param {Number} options.rotationalVelocity: The body's rotational velocity.
* @param {Number} options.integrity: The body's integrity. 0 means the
* body is no longer intact and should explode or break apart, 1 means
* the body is fully intact.
* @param {Number} options.density: The density of the body, can be
* used when calculating the force of impact of a collision, which can
* then be distributed to affect the kinetic energy of the colliding bodies.
* @param {Number} options.volatility: The body's volatility, how unstable or
* explosive it may be. Can be used as a multiplyer when calculating the
* force of impact of a collision.
* @return {Object} The body.
*/
makeBody: function(type, {
velocityX = 0,
velocityY = 0,
rotationalVelocity = 0,
integrity = 1,
density = 1,
volatility = 0
} = {}) {
if (type === undefined) throw new Error('You must provide a valid String for the type parameter!');
return {
type: type,
velocityX: velocityX,
velocityY: velocityY,
rotationalVelocity: rotationalVelocity,
integrity: integrity,
density: density,
volatility: volatility,
/**
* @param {Number} A number representing the force of the impact.
* @param {Object} The other body involved in the collision.
*/
handleCollision(impact, body) {
// template method //
},
update(event) {
// template method //
}
};
},
/**
* Updates the diagonal velocity properties of a body,
* taking into account the body's current velocity
* and applying any forces acting against the body
* as acceleration on both the x and y axis.
*
* NOTE: This method DOES NOT update the position of
* the body, it only updates its velocity.
*
* @param {Object} body: The body must be an Object
* with velocityX, velocityY and rotation properties.
* @param {Number} forceOnX: The force acting against
* the body on the x axis.
* @param {Number} forceOnY: The force acting against
* the body on the y axis.
*/
updateVelocity(body, forceOnX, forceOnY) {
const
angle = body.rotation * Math.PI / 180,
accelerationOnX = Math.cos(angle) * forceOnX,
accelerationOnY = Math.sin(angle) * forceOnY;
body.velocityX += accelerationOnX;
body.velocityY += accelerationOnY;
},
/**
* Updates the x and y properties of a body based on its
* velocityX and velocityY, and, updates the rotation of
* a body based on its rotationalVelocity.
*
* @param {Object} body: The body must be an Object
* with x, y, rotation, velocityX, velocityY, and
* rotationalVelocity properties.
*/
updatePosition(body) {
body.x += body.velocityX;
body.y += body.velocityY;
body.rotation += body.rotationalVelocity;
},
/**
* Can be overridden in the concrete body to provide a custom update()
* method.
*/
},
};
}(window, window._));