@@ -49,7 +49,7 @@ var css3d = (function(document)
49
49
* @memberof ! css3d
50
50
* @instance
51
51
*/
52
- this . version = '0.9.1 ' ;
52
+ this . version = '0.9.2 ' ;
53
53
54
54
/**
55
55
* Browser supports CSS 3D
@@ -307,8 +307,9 @@ var css3d = (function(document)
307
307
308
308
// shading
309
309
if ( element . shading ) {
310
- var projected = element . normal . transform ( element . getTotalRotation ( ) ) ;
311
- projected = projected . toVector3 ( ) . normalize ( ) ;
310
+ //var projected = element.normal.transform(element.getTotalRotation());
311
+ //projected = projected.toVector3().normalize();
312
+ var projected = element . normalWorld ;
312
313
var dot = Math . abs ( projected . dot ( this . _scene . getLight ( ) ) ) . toFixed ( 10 ) ;
313
314
dot = dot * this . _scene . getShadingIntensity ( ) + ( 1 - this . _scene . getShadingIntensity ( ) ) ;
314
315
if ( this . _hasFilter ) {
@@ -759,6 +760,17 @@ css3d.camera = (function()
759
760
{
760
761
return css3d . matrix4 . back ( this . _rotation ) . normalize ( ) ;
761
762
} ;
763
+
764
+ /**
765
+ *
766
+ * @memberof ! css3d.camera
767
+ * @instance
768
+ * @returns {css3d.vector3 }
769
+ */
770
+ camera . prototype . forwardVector = function ( )
771
+ {
772
+ return css3d . matrix4 . forward ( this . _rotation ) . normalize ( ) ;
773
+ } ;
762
774
763
775
/**
764
776
* Move camera forward
@@ -770,11 +782,11 @@ css3d.camera = (function()
770
782
*/
771
783
camera . prototype . forward = function ( steps )
772
784
{
773
- var backVector = this . backVector ( ) ;
785
+ var forwardVector = this . forwardVector ( ) ;
774
786
this . setTranslation (
775
- this . _translation . x - ( backVector . x * steps ) ,
776
- this . _translation . y - ( backVector . y * steps ) ,
777
- this . _translation . z - ( backVector . z * steps )
787
+ this . _translation . x + ( forwardVector . x * steps ) ,
788
+ this . _translation . y + ( forwardVector . y * steps ) ,
789
+ this . _translation . z + ( forwardVector . z * steps )
778
790
) ;
779
791
return this ;
780
792
} ;
@@ -1028,6 +1040,90 @@ css3d.camera = (function()
1028
1040
return camera ;
1029
1041
1030
1042
} ( ) ) ;
1043
+ /**
1044
+ * CSS 3D engine
1045
+ *
1046
+ * @category css3d
1047
+ * @package css3d.collision
1048
+ * @author Jan Fischer, bitWorking <info@bitworking.de>
1049
+ * @copyright 2014 Jan Fischer
1050
+ * @license http://www.opensource.org/licenses/mit-license.html MIT License
1051
+ */
1052
+
1053
+ /**
1054
+ *
1055
+ * @name css3d.collision
1056
+ * @class
1057
+ * @param {Array } elements
1058
+ * @returns {css3d.collision }
1059
+ */
1060
+ css3d . collision = ( function ( )
1061
+ {
1062
+ /**
1063
+ *
1064
+ * @param {Array } elements
1065
+ * @returns {css3d.collision }
1066
+ */
1067
+ var collision = function ( elements )
1068
+ {
1069
+ this . _elements = elements ;
1070
+ }
1071
+
1072
+ /**
1073
+ *
1074
+ * @memberof ! css3d.collision
1075
+ * @instance
1076
+ * @param {css3d.vector3 } position
1077
+ * @param {css3d.vector3 } normal
1078
+ * @param {Number } distance
1079
+ * @returns {Array }
1080
+ */
1081
+ collision . prototype . getCollisions = function ( position , normal , distance )
1082
+ {
1083
+ var collisionPoint = new css3d . vector3 (
1084
+ position . x + ( normal . x * distance ) ,
1085
+ position . y + ( normal . y * distance ) ,
1086
+ position . z + ( normal . z * distance )
1087
+ ) ;
1088
+
1089
+ var elementPosition , elementDistance , elementSize , planeDistance ;
1090
+ var collisionElements = [ ] ;
1091
+
1092
+ for ( var i = 0 ; i < this . _elements . length ; i ++ ) {
1093
+ if ( null == this . _elements [ i ] . _domElement ) {
1094
+ continue ;
1095
+ }
1096
+
1097
+ elementPosition = this . _elements [ i ] . getTotalTranslation ( ) ;
1098
+ elementDistance = css3d . vector3 . prototype . distance ( elementPosition , collisionPoint ) ;
1099
+ elementSize = Math . max (
1100
+ this . _elements [ i ] . _domElement . offsetWidth / 2 ,
1101
+ this . _elements [ i ] . _domElement . offsetHeight / 2
1102
+ ) ;
1103
+
1104
+ if ( elementDistance < elementSize ) {
1105
+ elementPosition . sub ( collisionPoint ) ;
1106
+ planeDistance = css3d . vector3 . prototype . dot2 ( this . _elements [ i ] . normalWorld , elementPosition ) ;
1107
+ //console.log(planeDistance);
1108
+
1109
+ //if (planeDistance <= 0) {
1110
+ if ( Math . abs ( planeDistance ) < distance ) { // normally <= 0, but if the steps are too large the collision is missed
1111
+ //this._elements[i]._domElement.style.border = '1px solid red';
1112
+ collisionElements . push ( this . _elements [ i ] ) ;
1113
+ }
1114
+ }
1115
+ else {
1116
+ //this._elements[i]._domElement.style.border = 'none';
1117
+ }
1118
+ }
1119
+ return collisionElements ;
1120
+ }
1121
+
1122
+ return collision ;
1123
+ } ( ) ) ;
1124
+
1125
+
1126
+
1031
1127
/**
1032
1128
* CSS 3D engine
1033
1129
*
@@ -1084,6 +1180,7 @@ css3d.element = (function()
1084
1180
this . backfaceCullingDirty = false ;
1085
1181
this . worldView = null ;
1086
1182
this . normal = new css3d . vector3 ( 0 , 0 , 1 ) ;
1183
+ this . normalWorld = new css3d . vector3 ( 0 , 0 , 1 ) ;
1087
1184
1088
1185
/**
1089
1186
* Indicates if the element inherits the scaling from an parent element.
@@ -1478,6 +1575,17 @@ css3d.element = (function()
1478
1575
{
1479
1576
return this . _translation ;
1480
1577
} ;
1578
+
1579
+ /**
1580
+ *
1581
+ * @memberof ! css3d.element
1582
+ * @instance
1583
+ * @returns {css3d.vector3 }
1584
+ */
1585
+ element . prototype . getTotalTranslation = function ( )
1586
+ {
1587
+ return new css3d . vector3 ( this . _world [ 3 ] , this . _world [ 7 ] , this . _world [ 11 ] ) ;
1588
+ } ;
1481
1589
1482
1590
/**
1483
1591
*
@@ -1489,6 +1597,17 @@ css3d.element = (function()
1489
1597
{
1490
1598
return css3d . matrix4 . back ( this . _world ) . normalize ( ) ;
1491
1599
} ;
1600
+
1601
+ /**
1602
+ *
1603
+ * @memberof ! css3d.element
1604
+ * @instance
1605
+ * @returns {css3d.vector3 }
1606
+ */
1607
+ element . prototype . forwardVector = function ( )
1608
+ {
1609
+ return css3d . matrix4 . forward ( this . _world ) . normalize ( ) ;
1610
+ } ;
1492
1611
1493
1612
/**
1494
1613
* Move forward
@@ -1500,11 +1619,11 @@ css3d.element = (function()
1500
1619
*/
1501
1620
element . prototype . forward = function ( steps )
1502
1621
{
1503
- var backVector = this . backVector ( ) ;
1622
+ var forwardVector = this . forwardVector ( ) ;
1504
1623
this . setTranslation (
1505
- this . _translation . x - ( backVector . x * steps ) ,
1506
- this . _translation . y - ( backVector . y * steps ) ,
1507
- this . _translation . z - ( backVector . z * steps )
1624
+ this . _translation . x + ( forwardVector . x * steps ) ,
1625
+ this . _translation . y + ( forwardVector . y * steps ) ,
1626
+ this . _translation . z + ( forwardVector . z * steps )
1508
1627
) ;
1509
1628
return this ;
1510
1629
} ;
@@ -1750,6 +1869,11 @@ css3d.element = (function()
1750
1869
this . _parent . update ( ) ; // this seems to be needed if you only call engine.update().render()
1751
1870
this . _world = css3d . matrix4 . multiply ( this . _parent . getWorldMatrix ( ) , this . _world ) ;
1752
1871
}
1872
+
1873
+ // transform normal
1874
+ // isn't it always the forward vector?
1875
+ this . normalWorld = this . normal . transform ( this . getTotalRotation ( ) ) ;
1876
+ this . normalWorld = this . normalWorld . toVector3 ( ) . normalize ( ) ;
1753
1877
1754
1878
this . _isDirty = false ;
1755
1879
@@ -2948,6 +3072,16 @@ css3d.matrix4 = {
2948
3072
{
2949
3073
return new css3d . vector3 ( matrix [ 2 ] , matrix [ 6 ] , matrix [ 10 ] ) ;
2950
3074
} ,
3075
+
3076
+ /**
3077
+ *
3078
+ * @param {Array } matrix
3079
+ * @returns {css3d.vector3 }
3080
+ */
3081
+ forward : function ( matrix )
3082
+ {
3083
+ return new css3d . vector3 ( - matrix [ 2 ] , - matrix [ 6 ] , - matrix [ 10 ] ) ;
3084
+ } ,
2951
3085
2952
3086
/**
2953
3087
*
@@ -3843,6 +3977,11 @@ css3d.vector3 = (function(css3d)
3843
3977
{
3844
3978
return ( this . x == 0 && this . y == 0 && this . z == 0 ) ;
3845
3979
} ;
3980
+
3981
+ vector3 . prototype . distance = function ( a , b )
3982
+ {
3983
+ return Math . sqrt ( Math . pow ( a . x - b . x , 2 ) + Math . pow ( a . y - b . y , 2 ) + Math . pow ( a . z - b . z , 2 ) ) ;
3984
+ } ;
3846
3985
3847
3986
return vector3 ;
3848
3987
0 commit comments