Skip to content

Commit dfa1b8b

Browse files
committed
Issue cocos2d#2698: refactor vec2, vec3 and aabb
1 parent e1e2931 commit dfa1b8b

File tree

13 files changed

+494
-591
lines changed

13 files changed

+494
-591
lines changed

cocos2d/core/CCCamera.js

+6-14
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,21 @@ cc.Camera = cc.Class.extend({
113113
*/
114114
locate:function () {
115115
if (this._dirty) {
116-
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();
117-
118-
cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
119-
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);
120-
121-
cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
116+
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
117+
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
118+
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
122119
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
123-
124120
this._dirty = false;
125121
}
126122
cc.kmGLMultMatrix( this._lookupMatrix);
127123
},
128124

129125
_locateForRenderer: function(matrix){
130126
if (this._dirty) {
131-
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();
132-
133-
cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
134-
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);
135-
136-
cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
127+
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
128+
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
129+
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
137130
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
138-
139131
this._dirty = false;
140132
}
141133
cc.kmMat4Multiply(matrix, matrix, this._lookupMatrix);

cocos2d/core/CCDirectorWebGL.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
8282

8383
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
8484
cc.kmGLLoadIdentity();
85-
var eye = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, zeye);
86-
var center = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, 0.0);
87-
var up = cc.kmVec3Fill(null, 0.0, 1.0, 0.0);
85+
var eye = new cc.math.Vec3(-ox + size.width / 2, -oy + size.height / 2, zeye);
86+
var center = new cc.math.Vec3( -ox + size.width / 2, -oy + size.height / 2, 0.0);
87+
var up = new cc.math.Vec3( 0.0, 1.0, 0.0);
8888
cc.kmMat4LookAt(matrixLookup, eye, center, up);
8989
cc.kmGLMultMatrix(matrixLookup);
9090
break;
@@ -246,30 +246,24 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
246246

247247
// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
248248
var zClip = transform.mat[14] / transform.mat[15];
249-
250249
var glSize = this._openGLView.getDesignResolutionSize();
251-
var clipCoord = new cc.kmVec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
252-
253-
var glCoord = new cc.kmVec3();
254-
cc.kmVec3TransformCoord(glCoord, clipCoord, transformInv);
255-
250+
var glCoord = new cc.math.Vec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
251+
glCoord.transformCoord(transformInv);
256252
return cc.p(glCoord.x, glCoord.y);
257253
};
258254

259255
_p.convertToUI = function (glPoint) {
260256
var transform = new cc.kmMat4();
261257
cc.GLToClipTransform(transform);
262258

263-
var clipCoord = new cc.kmVec3();
259+
var clipCoord = new cc.math.Vec3(glPoint.x, glPoint.y, 0.0);
264260
// Need to calculate the zero depth from the transform.
265-
var glCoord = new cc.kmVec3(glPoint.x, glPoint.y, 0.0);
266-
cc.kmVec3TransformCoord(clipCoord, glCoord, transform);
261+
clipCoord.transformCoord(transform);
267262

268263
var glSize = this._openGLView.getDesignResolutionSize();
269264
return cc.p(glSize.width * (clipCoord.x * 0.5 + 0.5), glSize.height * (-clipCoord.y * 0.5 + 0.5));
270265
};
271266

272-
273267
_p.getVisibleSize = function () {
274268
//if (this._openGLView) {
275269
return this._openGLView.getVisibleSize();

cocos2d/kazmath/aabb.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,52 @@
2727
*/
2828

2929
/**
30-
* A struture that represents an axis-aligned
31-
* bounding box.
30+
* A structure that represents an axis-aligned bounding box.
31+
* cc.kmAABB => cc.math.AABB
3232
*/
33-
cc.kmAABB = function (min, max) {
33+
cc.math.AABB = function (min, max) {
3434
/** The max corner of the box */
35-
this.min = min || new cc.kmVec3();
35+
this.min = min || new cc.math.Vec3();
3636
/** The min corner of the box */
37-
this.max = max || new cc.kmVec3();
37+
this.max = max || new cc.math.Vec3();
3838
};
3939

4040
/**
41-
* Returns KM_TRUE if point is in the specified AABB, returns
42-
* KM_FALSE otherwise.
41+
* Returns true if point is in the specified AABB, returns false otherwise.
42+
* @param {cc.kmVec3} point
43+
* @returns {boolean}
4344
*/
44-
cc.kmAABBContainsPoint = function (pPoint, pBox) {
45-
if (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
45+
cc.math.AABB.prototype.containsPoint = function (point) {
46+
return (point.x >= this.min.x && point.x <= this.max.x &&
47+
point.y >= this.min.y && point.y <= this.max.y &&
48+
point.z >= this.min.z && point.z <= this.max.z);
49+
};
50+
51+
/**
52+
* Returns true if point is in the specified AABB, returns
53+
* false otherwise.
54+
*/
55+
cc.math.AABB.containsPoint = function (pPoint, pBox) {
56+
return (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
4657
pPoint.y >= pBox.min.y && pPoint.y <= pBox.max.y &&
47-
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z) {
48-
return cc.KM_TRUE;
49-
}
50-
return cc.KM_FALSE;
58+
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z);
5159
};
5260

5361
/**
54-
* Assigns pIn to pOut, returns pOut.
62+
* Assigns aabb to current AABB object
63+
* @param {cc.math.AABB} aabb
5564
*/
56-
cc.kmAABBAssign = function (pOut, pIn) {
57-
cc.kmVec3Assign(pOut.min, pIn.min);
58-
cc.kmVec3Assign(pOut.max, pIn.max);
59-
return pOut;
65+
cc.math.AABB.prototype.assign = function(aabb){
66+
this.min.assign(aabb.min);
67+
this.max.assign(aabb.max);
6068
};
6169

6270
/**
63-
* Scales pIn by s, stores the resulting AABB in pOut. Returns pOut
71+
* Assigns pIn to pOut, returns pOut.
6472
*/
65-
cc.kmAABBScale = function (pOut, pIn, s) {
66-
cc.log("cc.kmAABBScale hasn't been supported.");
73+
cc.math.AABB.assign = function (pOut, pIn) { //cc.kmAABBAssign
74+
pOut.min.assign(pIn.min);
75+
pOut.max.assign(pIn.max);
76+
return pOut;
6777
};
6878

cocos2d/kazmath/gl/matrix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ cc.kmGLTranslatef = function (x, y, z) {
133133
};
134134

135135
cc.kmGLRotatef = function (angle, x, y, z) {
136-
var axis = new cc.kmVec3(x, y, z);
136+
var axis = new cc.math.Vec3(x, y, z);
137137
var rotation = new cc.kmMat4();
138138

139139
//Create a rotation matrix using the axis and the angle
140-
cc.kmMat4RotationAxisAngle(rotation, axis, cc.kmDegreesToRadians(angle));
140+
cc.kmMat4RotationAxisAngle(rotation, axis, cc.degreesToRadians(angle));
141141

142142
//Multiply the rotation matrix by the current matrix
143143
cc.kmMat4Multiply(cc.current_stack.top, cc.current_stack.top, rotation);

cocos2d/kazmath/mat3.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ cc.kmMat3AreEqual = function (pMat1, pMat2) {
179179
return true;
180180

181181
for (var i = 0; i < 9; ++i) {
182-
if (!(pMat1.mat[i] + cc.kmEpsilon > pMat2.mat[i] &&
183-
pMat1.mat[i] - cc.kmEpsilon < pMat2.mat[i])) {
182+
if (!(pMat1.mat[i] + cc.math.EPSILON > pMat2.mat[i] &&
183+
pMat1.mat[i] - cc.math.EPSILON < pMat2.mat[i])) {
184184
return false;
185185
}
186186
}

cocos2d/kazmath/mat4.js

+33-35
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ cc.kmMat4._gaussj = function (a, b) {
122122
indxr[i] = irow;
123123
indxc[i] = icol;
124124
if (cc.kmMat4._get(a, icol, icol) == 0.0)
125-
return cc.KM_FALSE;
125+
return false;
126126

127127
pivinv = 1.0 / cc.kmMat4._get(a, icol, icol);
128128
cc.kmMat4._set(a, icol, icol, 1.0);
@@ -153,7 +153,7 @@ cc.kmMat4._gaussj = function (a, b) {
153153
cc.kmMat4._swap(a, k, indxr[l], k, indxc[l]);
154154
}
155155
}
156-
return cc.KM_TRUE;
156+
return true;
157157
};
158158

159159
cc.kmMat4._identity =
@@ -174,7 +174,7 @@ cc.kmMat4Inverse = function (pOut, pM) {
174174
cc.kmMat4Assign(inv, pM);
175175
cc.kmMat4Identity(tmp);
176176

177-
if (cc.kmMat4._gaussj(inv, tmp) == cc.KM_FALSE)
177+
if (cc.kmMat4._gaussj(inv, tmp) == false)
178178
return null;
179179

180180
cc.kmMat4Assign(pOut, inv);
@@ -338,8 +338,8 @@ cc.kmMat4AreEqual = function (pMat1, pMat2) {
338338
}
339339

340340
for (var i = 0; i < 16; i++) {
341-
if (!(pMat1.mat[i] + cc.kmEpsilon > pMat2.mat[i] &&
342-
pMat1.mat[i] - cc.kmEpsilon < pMat2.mat[i])) {
341+
if (!(pMat1.mat[i] + cc.math.EPSILON > pMat2.mat[i] &&
342+
pMat1.mat[i] - cc.math.EPSILON < pMat2.mat[i])) {
343343
return false;
344344
}
345345
}
@@ -573,43 +573,40 @@ cc.kmMat4Translation = function (pOut, x, y, z) {
573573
* wish to extract the vector from. pOut is a pointer to the
574574
* kmVec3 structure that should hold the resulting vector
575575
*/
576-
cc.kmMat4GetUpVec3 = function (pOut, pIn) {
577-
pOut.x = pIn.mat[4];
578-
pOut.y = pIn.mat[5];
579-
pOut.z = pIn.mat[6];
580-
cc.kmVec3Normalize(pOut, pOut);
581-
return pOut;
576+
cc.kmMat4GetUpVec3 = function (vec3, mat4) {
577+
vec3.x = mat4.mat[4];
578+
vec3.y = mat4.mat[5];
579+
vec3.z = mat4.mat[6];
580+
return vec3.normalize();
582581
};
583582

584583
/** Extract the right vector from a 4x4 matrix. The result is
585584
* stored in pOut. Returns pOut.
586585
*/
587-
cc.kmMat4GetRightVec3 = function (pOut, pIn) {
588-
pOut.x = pIn.mat[0];
589-
pOut.y = pIn.mat[1];
590-
pOut.z = pIn.mat[2];
591-
cc.kmVec3Normalize(pOut, pOut);
592-
return pOut;
586+
cc.kmMat4GetRightVec3 = function (vec3, mat4) {
587+
vec3.x = mat4.mat[0];
588+
vec3.y = mat4.mat[1];
589+
vec3.z = mat4.mat[2];
590+
return vec3.normalize();
593591
};
594592

595593
/**
596594
* Extract the forward vector from a 4x4 matrix. The result is
597595
* stored in pOut. Returns pOut.
598596
*/
599-
cc.kmMat4GetForwardVec3 = function (pOut, pIn) {
600-
pOut.x = pIn.mat[8];
601-
pOut.y = pIn.mat[9];
602-
pOut.z = pIn.mat[10];
603-
cc.kmVec3Normalize(pOut, pOut);
604-
return pOut;
597+
cc.kmMat4GetForwardVec3 = function (vec3, mat4) {
598+
vec3.x = mat4.mat[8];
599+
vec3.y = mat4.mat[9];
600+
vec3.z = mat4.mat[10];
601+
return vec3.normalize();
605602
};
606603

607604
/**
608605
* Creates a perspective projection matrix in the
609606
* same way as gluPerspective
610607
*/
611608
cc.kmMat4PerspectiveProjection = function (pOut, fovY, aspect, zNear, zFar) {
612-
var r = cc.kmDegreesToRadians(fovY / 2);
609+
var r = cc.degreesToRadians(fovY / 2);
613610
var deltaZ = zFar - zNear;
614611
var s = Math.sin(r);
615612

@@ -647,20 +644,21 @@ cc.kmMat4OrthographicProjection = function (pOut, left, right, bottom, top, near
647644
* the resulting matrix is stored in pOut. pOut is returned.
648645
*/
649646
cc.kmMat4LookAt = function (pOut, pEye, pCenter, pUp) {
650-
var f = new cc.kmVec3(), up = new cc.kmVec3(), s = new cc.kmVec3(), u = new cc.kmVec3();
647+
var f = new cc.math.Vec3(pCenter), up = new cc.math.Vec3(pUp);
651648
var translate = new cc.kmMat4();
652649

653-
cc.kmVec3Subtract(f, pCenter, pEye);
654-
cc.kmVec3Normalize(f, f);
650+
f.subtract(pEye);
651+
f.normalize();
655652

656-
cc.kmVec3Assign(up, pUp);
657-
cc.kmVec3Normalize(up, up);
653+
up.normalize();
658654

659-
cc.kmVec3Cross(s, f, up);
660-
cc.kmVec3Normalize(s, s);
655+
var s = new cc.math.Vec3(f);
656+
s.cross(up);
657+
s.normalize();
661658

662-
cc.kmVec3Cross(u, s, f);
663-
cc.kmVec3Normalize(s, s);
659+
var u = new cc.math.Vec3(s);
660+
u.cross(f);
661+
s.normalize();
664662

665663
cc.kmMat4Identity(pOut);
666664

@@ -690,8 +688,8 @@ cc.kmMat4RotationAxisAngle = function (pOut, axis, radians) {
690688
var rcos = Math.cos(radians);
691689
var rsin = Math.sin(radians);
692690

693-
var normalizedAxis = new cc.kmVec3();
694-
cc.kmVec3Normalize(normalizedAxis, axis);
691+
var normalizedAxis = new cc.math.Vec3(axis);
692+
normalizedAxis.normalize();
695693

696694
pOut.mat[0] = rcos + normalizedAxis.x * normalizedAxis.x * (1 - rcos);
697695
pOut.mat[1] = normalizedAxis.z * rsin + normalizedAxis.y * normalizedAxis.x * (1 - rcos);

0 commit comments

Comments
 (0)