-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModulePhisic.cannon.js
217 lines (192 loc) · 6.26 KB
/
ModulePhisic.cannon.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
(function () {
"use strict";
}());
var PhisicModule = Module.extend({
init: function (game) {
"use strict";
//this will keep track of the object added to the scene
this.objects = [];
//an array full of objects to be loaded
this.loadQueque = [];
// Setup our world
this.world = new CANNON.World();
this.world.quatNormalizeSkip = 0;
this.world.quatNormalizeFast = false;
var solver = new CANNON.GSSolver();
this.world.defaultContactMaterial.contactEquationStiffness = 1e9;
this.world.defaultContactMaterial.contactEquationRegularizationTime = 4;
solver.iterations = 7;
solver.tolerance = 0.1;
var split = true;
if(split)
this.world.solver = new CANNON.SplitSolver(solver);
else
this.world.solver = solver;
this.world.gravity.set(0,1,0);//this.world.gravity.set(0,1,0);
this.world.broadphase = new CANNON.NaiveBroadphase();
// Create a slippery material (friction coefficient = 0.0)
var physicsMaterial = new CANNON.Material("slipperyMaterial");
var physicsContactMaterial = new CANNON.ContactMaterial(physicsMaterial,
physicsMaterial,
0.0, // friction coefficient
0.3 // restitution
);
// We must add the contact materials to the world
this.world.addContactMaterial(physicsContactMaterial);
/*
// Create a sphere
var mass = 5, radius = 1.3;
sphereShape = new CANNON.Sphere(radius);
sphereBody = new CANNON.RigidBody(mass,sphereShape,physicsMaterial);
sphereBody.position.set(0,5,0);
sphereBody.linearDamping = 0.9;
this.world.add(sphereBody);
*/
// Create a plane
/*
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape,physicsMaterial);
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
this.world.add(groundBody);
*/
},
tick: function () {
//load the needed meshes from the quequeList
if(this.loadQueque.length > 0){
//console.log('PSX need to load items:', this.loadQueque.length);
for (var i=this.loadQueque.length-1; i>=0; i--){
var obj = this.loadQueque[i];
this.add(obj);
//console.log('PSX loading',obj ,'list to load is', this.loadQueque);
//remove the object loaded from the queque list
var index = this.loadQueque.indexOf(obj);
this.loadQueque.splice(index, 1);
}
}
//console.log('updaating psx')
//do one step in our phisic world
var timeStep = 1.0 / 60.0; // seconds
this.world.step(timeStep);
//update.objects.positions from phisics
for (var i=0; i<this.objects.length; i++){
var obj = this.objects[i];
obj.position.x = obj.psx.position.x;
obj.position.y = obj.psx.position.y;
obj.position.z = obj.psx.position.z;
obj.rotation.x = obj.psx.quaternion.x;
obj.rotation.y = obj.psx.quaternion.y;
obj.rotation.z = obj.psx.quaternion.z;
obj.rotation.w = obj.psx.quaternion.w;
//console.dir(obj.position);
}
},
add: function (obj){
//console.log(obj);
switch(obj.type){
case 'car':
var car = obj.data;
//console.log('PSX data',obj.data);
/*
var mass = 0.0000001;
*/
var halfExtents = new CANNON.Vec3(
car['Car']['body width']['_val'],
car['Car']['body height']['_val'],
car['Car']['body length']['_val']
);
var boxShape = new CANNON.Box(halfExtents);
var carBody = new CANNON.RigidBody(car['Car']['mass']['_val']*1,boxShape);
obj.psx = carBody;
obj.psx.position.set(obj.position.x,
obj.position.y,
obj.position.z);
obj.psx.quaternion.set( obj.rotation.x,
obj.rotation.y,
obj.rotation.z,
0);
obj.psx.velocity.set( 0,
0,
0);
this.world.add(obj.psx);
//console.log('car body added')
break;
case 'wheel':
//add wheels
var wheel = obj;
var car = wheel.car;
var halfExtents = new CANNON.Vec3( 1,
1,
1);
//var wheelShape = new CANNON.Box(halfExtents);
var wheelShape = new CANNON.Sphere(1.2);
var wheelMass = 1;
var wheelBody = new CANNON.RigidBody(wheelMass,wheelShape);
var wheelPosition = new CANNON.Vec3(wheel.position.x,
wheel.position.y,
wheel.position.z)
wheel.psx = wheelBody;
// Hinge the wheels
var zero = new CANNON.Vec3();
var leftAxis = new CANNON.Vec3(0,1,0);
var rightAxis = new CANNON.Vec3(0,-1,0);
var leftFrontAxis = new CANNON.Vec3(0,1,0);
var rightFrontAxis = new CANNON.Vec3(0,-1,0);
/*
if(true){
leftFrontAxis = new CANNON.Vec3(0,0,0);
rightFrontAxis = new CANNON.Vec3(0,0,0);
leftFrontAxis.normalize();
rightFrontAxis.normalize();
}
*/
switch (wheel.location){
case 'FR':
var carAxis =rightAxis;
var wheelAxis =rightAxis;
break;
case 'FL':
var carAxis =leftAxis;
var wheelAxis =leftAxis;
break;
case 'RR':
var carAxis =rightAxis;
var wheelAxis =rightAxis;
break;
case 'RL':
var carAxis =leftAxis;
var wheelAxis =leftAxis;
break;
}
var constraint = new CANNON.HingeConstraint(car.psx,//body A
wheelPosition,//pivot A
carAxis,//axis A
wheelBody,//body B
zero,//pivot B = il punto di B sul quale ruotare
wheelAxis);//axis B = su quale asse B deve ruotare sul suo punto
this.world.add(wheelBody);
//this.world.addConstraint(constraint);
break;
case 'track':
// var groundBody = new CANNON.RigidBody(0,groundShape,physicsMaterial);
// groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
var mass = 0;
var shape = new CANNON.Box(new CANNON.Vec3(5,2,0.5));
var body = new CANNON.RigidBody(mass,shape);
obj.psx = body;
obj.psx.position.set(obj.position.x,
obj.position.y,
obj.position.z);
this.world.add(obj.psx);
//console.log('carbody added')
break;
case 'other':
break;
}
this.register(obj);
},
register: function (obj){
this.objects.push(obj)
},
remove: function () {
},
})