1
- const type6 = require ( "../../dist/type6.cjs" ) ;
1
+ const Type6 = require ( "../../dist/type6.cjs" ) ;
2
2
3
- let vector1 = new type6 . Vector2 ( 2.0 , 2.2 ) ;
4
- let vector2 = new type6 . Vector2 ( 1.0 , 1.0 ) ;
3
+ let vector1 = new Type6 . Vector2 ( 2.0 , 2.2 ) ;
4
+ let vector2 = new Type6 . Vector2 ( 1.0 , 1.0 ) ;
5
5
console . log ( vector1 . toString ( ) ) ;
6
6
console . log ( vector2 . toString ( ) ) ;
7
7
vector1 . add ( vector2 ) ;
8
8
console . log ( vector1 . toString ( ) ) ;
9
- console . log ( type6 . Utils . round ( 6.7 , 0 ) ) ;
10
- console . log ( type6 . Random . float ( 4 , 10 ) ) ;
11
- console . log ( type6 . Trigonometry . twopi ) ;
9
+ console . log ( Type6 . Utils . round ( 6.7 , 0 ) ) ;
10
+ console . log ( Type6 . Random . float ( 4 , 10 ) ) ;
11
+ console . log ( Type6 . Trigonometry . twopi ) ;
12
12
13
- let cosine = type6 . Trigonometry . cosineEquation ( 1 , 3.14 , 0 , 0 ) ;
14
- let sine = type6 . Trigonometry . sineEquation ( 1 , 3.14 , 0 , 0 ) ;
13
+ let cosine = Type6 . Trigonometry . cosineEquation ( 1 , 3.14 , 0 , 0 ) ;
14
+ let sine = Type6 . Trigonometry . sineEquation ( 1 , 3.14 , 0 , 0 ) ;
15
15
console . log ( cosine ) ;
16
16
console . log ( sine ) ;
17
17
18
- let circle = new type6 . Circle ( 0 , 0 , type6 . Random . integer ( 100 , 200 ) ) ;
18
+ let circle = new Type6 . Circle ( 0 , 0 , Type6 . Random . integer ( 100 , 200 ) ) ;
19
19
circle . position . set ( 2 , 3 ) . add ( vector2 ) ;
20
- console . log ( circle ) ;
20
+ console . log ( circle ) ;
21
+
22
+
23
+ function AabbVSAabbCollision ( apos , ahs , bpos , bhs ) {
24
+ let ab = new Type6 . Vector2 ( ) . copy ( apos ) . subtract ( bpos ) ; //(-3,-2)
25
+ let penetration = ab . clone ( ) . absolute ( ) . opposite ( ) . add ( ahs ) . add ( bhs ) ; //(0,1)
26
+ if ( penetration . isPositive ( ) ) {
27
+ return getProjection ( penetration , ab ) ;
28
+ }
29
+ return penetration . origin ( ) ;
30
+ }
31
+
32
+ function getProjection ( penetration , ab ) {
33
+ //pick the projection axis
34
+ let minAxis = penetration . getMinAxis ( ) ; //x
35
+ penetration . setOppositeAxis ( minAxis , 0 ) ; //(0,0)
36
+ if ( penetration [ minAxis ] && ab [ minAxis ] < 0 ) {
37
+ penetration [ minAxis ] = - penetration [ minAxis ] ;
38
+ }
39
+ return penetration ;
40
+ }
41
+
42
+ let apos = new Type6 . Vector2 ( 2 , 2 ) ;
43
+ let ahs = new Type6 . Vector2 ( 1 , 1 ) ;
44
+ let bpos = new Type6 . Vector2 ( 5 , 4 ) ;
45
+ let bhs = new Type6 . Vector2 ( 2 , 2 ) ;
46
+
47
+ let collisionDetection = AabbVSAabbCollision ( apos , ahs , bpos , bhs ) ;
48
+ console . log ( 'collision' , collisionDetection ) ;
0 commit comments