-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWheel.js
183 lines (131 loc) · 4.58 KB
/
Wheel.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
////////////////////////////////////////////////////////////////////////////////
// Wheel class
//
////////////////////////////////////////////////////////////////////////////////
function Wheel( _image, _x, _y, _radius, _factor )
{
this.image = new Image();
this.image.src = _image;
this.x = _x;
this.y = _y;
this.radius = _radius;
this.factor = _factor || 1;
this.platformDistance = 0.9;
this.platformWidth = 30 * this.factor;
this.platformHeight = 4;
this.rotation = 0;
this.speed = 0.00;
this.resistance = 0.98;
this.weightDistribution = 0;
// TODO: move to player
this.playerWeight = 0.001;
}
Wheel.prototype.tick = function()
{
this.weightDistribution = 0;
// Do the player frame and get their position
for ( var idx = 0; idx < players.length; idx++)
{
players[idx].tick();
var playerRotation = idx / players.length * 2 * Math.PI + Math.PI / 4;
this.weightDistribution -= (players[idx].jumping?1:0) * Math.cos( this.rotation + playerRotation );
/*
if ( players[idx].jumping )
console.log( Math.cos( this.rotation + playerRotation ) )
*/
}
this.weightDistribution *= this.playerWeight;
//console.log( this.weightDistribution );
//console.log( _event );
//this.weightDistribution = 0;
/*
Cylinder_MassaVerdeling =
Speler_Massa * (
Speler1_x * cos(Cylinder_Rotatie + ? * PI) +
Speler2_x * cos(Cylinder_Rotatie + ? * PI)
);
Cylinder_Snelheid = Cylinder_Snelheid * Cylinder_Weerstand + Cylinder_MassaVerdeling;
Cylinder_Rotatie = Cylinder_Rotatie + Cylinder_Snelheid;
*/
//this.speed *= this.resistance + this.weightDistribution;
this.speed = this.speed * this.resistance + this.weightDistribution;
// Wheel rotation cheat
if ( this.cheat )
this.speed += this.cheat;
this.rotation += this.speed;
};
Wheel.prototype.draw = function( _context )
{
// Save original context
_context.save();
var cw = document.getElementById('canvas').width;
var ch = document.getElementById('canvas').height;
var x = this.x;
var y = this.y;
if ( x < 1 )
x *= cw;
if ( y < 1 )
y *= ch;
_context.translate( x, y - ( this.radius / 2 ) );
// Save axis for drawing wheel and arms
_context.save();
_context.rotate( this.rotation );
_context.drawImage( this.image, -this.radius/2, -this.radius/2, this.radius, this.radius);
_context.strokeStyle = "#000";
_context.lineWidth = this.platformHeight;
for ( var idx = 0; idx < players.length; idx++)
{
var playerRotation = idx / players.length * 2 * Math.PI + Math.PI / 4;
//playerRotation = 0;
// Save arm
_context.save();
_context.rotate( playerRotation );
// 2x width
//_context.translate( this.radius*this.platformDistance/2, this.radius*this.platformDistance/2 );
_context.translate( this.radius*this.platformDistance/2, 0 );
_context.rotate( - playerRotation - this.rotation);
// Player tick and draw on each frame
players[idx].tick( );
players[idx].draw( _context );
_context.beginPath();
_context.moveTo(-this.platformWidth/2,0);
_context.lineTo(this.platformWidth/2,0);
_context.stroke();
_context.restore();
}
/*
var nCurPlayer = 0;
var nArms = players.length;
for ( var n=0; n < 2*Math.PI; n+=Math.PI*2/nArms )
{
// Save arm
_context.save();
_context.rotate( n );
// 2x width
_context.translate( this.radius*this.platformDistance/2, this.radius*this.platformDistance/2 );
_context.rotate( - n - this.rotation);
// Player tick and draw on each frame
players[nCurPlayer].tick( );
players[nCurPlayer].draw( _context );
nCurPlayer = (nCurPlayer+1)%players.length;
_context.beginPath();
_context.moveTo(-this.platformWidth/2,0);
_context.lineTo(this.platformWidth/2,0);
_context.stroke();
_context.restore();
}
*/
_context.restore();
_context.restore();
}
Wheel.prototype.getPosition = function( )
{
//
//return Math.PI * this.rotation * this.radius / 2;
// circle perimeter 2PI*r = PI*d
// 1 rotation = 2*PI
// part of circle means: PI*d*rotation/(2*PI) = d*rotation/2
return this.rotation * this.radius / 2;
}
// end Wheel class
///////////////////