1
- import { Body , Engine , Render , Bodies , World , Vector , Runner , Events , Mouse , MouseConstraint } from "matter-js" ;
1
+ import {
2
+ Body ,
3
+ Engine ,
4
+ Render ,
5
+ Bodies ,
6
+ World ,
7
+ Vector ,
8
+ Runner ,
9
+ Events ,
10
+ Mouse ,
11
+ MouseConstraint ,
12
+ Composite
13
+ } from "matter-js" ;
2
14
import { createPartCircle } from "./utils/CustomBodies"
3
15
import { getTranformedPoint , findMinimumDistanceToObstacle } from "./utils/utils" ;
4
16
5
17
type event = ( robot : TwoWheelRobot ) => any ;
6
- export type sensorPosition = 'L1' | 'L2' | 'L3' | 'F1' | 'F2' | 'F3' | 'R1' | 'R2' | 'R3' | 'B1' | 'B2' | 'B3' ;
18
+ export type sensorPosition = 'L1' | 'L2' | 'L3' | 'F1' | 'F2' | 'F3' | 'R1' | 'R2' | 'R3' | 'B1' | 'B2' | 'B3' | 'CFL' | 'CFR' | 'CBL' | 'CBR' ;
7
19
export class TwoWheelRobot {
8
20
private _canvas : any ;
9
21
private _engine : Engine ;
@@ -17,8 +29,8 @@ export class TwoWheelRobot {
17
29
robotInitialAngle : number ;
18
30
19
31
robotMass : number = 1000 ;
20
- robotFrictionAir :number = 0.5 ;
21
- static readonly forceMultiplier = 0.0015 ;
32
+ robotFrictionAir :number = 1 ;
33
+ static readonly forceMultiplier = 0.5 ;
22
34
23
35
ultrasonicSensor : Body ;
24
36
static readonly maxUltrasonicDistance = 400 ;
@@ -34,7 +46,12 @@ export class TwoWheelRobot {
34
46
'F3' : { x : 14 , y : 9 , angle : 0 } ,
35
47
'B1' : { x : - 14 , y :- 9 , angle : - Math . PI } ,
36
48
'B2' : { x : - 14 , y : 0 , angle : - Math . PI } ,
37
- 'B3' : { x : - 14 , y : 9 , angle : - Math . PI }
49
+ 'B3' : { x : - 14 , y : 9 , angle : - Math . PI } ,
50
+ 'CFL' : { x : 14 , y :- 9 , angle : Math . PI / 4 } ,
51
+ 'CFR' : { x : 14 , y : 9 , angle : - Math . PI / 4 } ,
52
+ 'CBL' : { x : - 14 , y :- 9 , angle : - 5 * Math . PI / 4 } ,
53
+ 'CBR' : { x : - 14 , y : 9 , angle : 5 * Math . PI / 4 } ,
54
+
38
55
}
39
56
ultrasonicSensorDistances : { [ position : string ] : number ; } = { } ;
40
57
@@ -99,6 +116,19 @@ export class TwoWheelRobot {
99
116
100
117
World . add ( this . _engine . world , [ this . robot , ] ) ; //obstacle]);
101
118
119
+ /*
120
+ // add mouse control
121
+ const mouse = Mouse.create(this._canvas);
122
+
123
+ const mouseConstraint = MouseConstraint.create(this._engine, {
124
+ mouse: mouse
125
+ });
126
+
127
+ World.add(this._engine.world, mouseConstraint);
128
+ // keep the mouse in sync with rendering
129
+ this._render.mouse = mouse;
130
+ */
131
+
102
132
Render . run ( this . _render ) ;
103
133
this . reset ( ) ;
104
134
@@ -130,8 +160,17 @@ export class TwoWheelRobot {
130
160
131
161
}
132
162
}
163
+ /*
164
+ //Add event with 'mousemove'
165
+ Events.on(mouseConstraint, 'mousemove', function (event) {
166
+ //For Matter.Query.point pass "array of bodies" and "mouse position"
167
+
168
+ //Your custom code here
169
+ console.log(event); //returns a shape corrisponding to the mouse position
133
170
171
+ });
134
172
173
+ */
135
174
self . updateUltrasonicSensor ( ) ;
136
175
137
176
@@ -208,24 +247,30 @@ export class TwoWheelRobot {
208
247
this . leftWheelSpeed = left ;
209
248
this . rightWheelSpeed = right ;
210
249
}
211
- private once = 0 ;
250
+ //TODO review if this is actually solving a bug or not
251
+ private starting = true ;
212
252
applyForces ( ) : void {
213
-
253
+ if ( ! this . starting ) {
214
254
const leftForcePosition = getTranformedPoint ( this . robotBody . position , this . robot . angle , 0 , - 10 ) ;
215
255
const rightForcePosition = getTranformedPoint ( this . robotBody . position , this . robot . angle , 0 , 10 ) ;
216
- let leftWheelForce = Vector . create ( TwoWheelRobot . forceMultiplier * Math . abs ( this . leftWheelSpeed ) , 0 ) ;
256
+ let leftWheelForce = Vector . create ( TwoWheelRobot . forceMultiplier * Math . abs ( this . leftWheelSpeed ) , 0 ) ;
217
257
leftWheelForce = Vector . rotate ( leftWheelForce , this . robot . angle ) ;
218
- if ( this . leftWheelSpeed < 0 )
258
+ if ( this . leftWheelSpeed < 0 )
219
259
leftWheelForce = Vector . neg ( leftWheelForce ) ;
220
260
221
- let rightWheelForce = Vector . create ( TwoWheelRobot . forceMultiplier * Math . abs ( this . rightWheelSpeed ) , 0 ) ;
261
+ let rightWheelForce = Vector . create ( TwoWheelRobot . forceMultiplier * Math . abs ( this . rightWheelSpeed ) , 0 ) ;
222
262
rightWheelForce = Vector . rotate ( rightWheelForce , this . robot . angle ) ;
223
- if ( this . rightWheelSpeed < 0 )
263
+ if ( this . rightWheelSpeed < 0 )
224
264
rightWheelForce = Vector . neg ( rightWheelForce ) ;
225
265
226
- Body . applyForce ( this . robot , rightForcePosition , rightWheelForce ) ;
227
266
Body . applyForce ( this . robot , leftForcePosition , leftWheelForce ) ;
267
+ Body . applyForce ( this . robot , rightForcePosition , rightWheelForce ) ;
268
+ }
228
269
270
+ else
271
+ {
272
+ this . starting = false ;
273
+ }
229
274
230
275
}
231
276
@@ -253,6 +298,7 @@ export class TwoWheelRobot {
253
298
Body . setAngle ( this . robot , this . robotInitialAngle ) ;
254
299
Body . setVelocity ( this . robot , { x :0 , y :0 } ) ;
255
300
Body . setAngularVelocity ( this . robot , 0 ) ;
301
+ Body . update ( this . robot , 10 , 1 , 1 ) ;
256
302
this . leftWheelSpeed = 0 ;
257
303
this . rightWheelSpeed = 0 ;
258
304
for ( const coin of this . removedCoins )
@@ -261,7 +307,7 @@ export class TwoWheelRobot {
261
307
}
262
308
this . removedCoins = [ ] ;
263
309
264
- this . tick ( 10 ) ;
310
+ // this.tick(10);
265
311
this . updateUltrasonicSensor ( ) ;
266
312
267
313
}
0 commit comments