Vec2 Class
Extends ValueType
Module: cc
Representation of 2D vectors and points.
Index
Properties
xNumberyNumberONEVec2return a Vec2 object with x = 1 and y = 1.ZEROVec2return a Vec2 object with x = 0 and y = 0.UPVec2return a Vec2 object with x = 0 and y = 1.RIGHTVec2return a Vec2 object with x = 1 and y = 0.
Methods
constructorConstructorcloneclone a Vec2 objectsetSets vector with another's valueequalsCheck whether two vector equalfuzzyEqualsCheck whether two vector equal with some degree of variance.toStringTransform to string with vector informationslerpCalculate linear interpolation result between this vector and another one with given ratioclampfClamp the vector between from float and to float.addSelfAdds this vector.addAdds two vectors, and returns the new result.subSelfSubtracts one vector from this.subSubtracts one vector from this, and returns the new result.mulSelfMultiplies this by a number.mulMultiplies by a number, and returns the new result.scaleSelfMultiplies two vectors.scaleMultiplies two vectors, and returns the new result.divSelfDivides by a number.divDivides by a number, and returns the new result.negSelfNegates the components.negNegates the components, and returns the new result.dotDot productcrossCross productmagReturns the length of this vector.magSqrReturns the squared length of this vector.normalizeSelfMake the length of this vector to 1.normalizeNote that the current vector is unchanged and a new normalized vector is returned.angleGet angle in radian between this and vector.signAngleGet angle in radian between this and vector with direction.rotaterotaterotateSelfrotate selfprojectCalculates the projection of the current vector over the given vector.transformMat4Transforms the vec2 with a mat4.
Details
Properties
x
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/value-types/vec2.js:66 |
y
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/value-types/vec2.js:69 |
ONE
return a Vec2 object with x = 1 and y = 1.
| meta | description |
|---|---|
| Type | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:611 |
ZERO
return a Vec2 object with x = 0 and y = 0.
| meta | description |
|---|---|
| Type | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:622 |
UP
return a Vec2 object with x = 0 and y = 1.
| meta | description |
|---|---|
| Type | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:633 |
RIGHT
return a Vec2 object with x = 1 and y = 0.
| meta | description |
|---|---|
| Type | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:644 |
Methods
constructor
Constructor see Cc/vec2:method or cc.p
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/vec2.js:45 |
Parameters
clone
clone a Vec2 object
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:78 |
set
Sets vector with another's value
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:88 |
Parameters
newValueVec2 !#en new value to set. !#zh 要设置的新值
equals
Check whether two vector equal
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/value-types/vec2.js:102 |
Parameters
otherVec2
fuzzyEquals
Check whether two vector equal with some degree of variance.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/value-types/vec2.js:113 |
Parameters
toString
Transform to string with vector informations
| meta | description |
|---|---|
| Returns | string |
| Defined in | cocos2d/core/value-types/vec2.js:131 |
lerp
Calculate linear interpolation result between this vector and another one with given ratio
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:144 |
Parameters
toVec2rationumber the interpolation coefficientoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
clampf
Clamp the vector between from float and to float.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:162 |
Parameters
Examples
var min_inclusive = cc.v2(0, 0);
var max_inclusive = cc.v2(20, 20);
var v1 = cc.v2(20, 20).clampf(min_inclusive, max_inclusive); // Vec2 {x: 20, y: 20};
var v2 = cc.v2(0, 0).clampf(min_inclusive, max_inclusive); // Vec2 {x: 0, y: 0};
var v3 = cc.v2(10, 10).clampf(min_inclusive, max_inclusive); // Vec2 {x: 10, y: 10};
addSelf
Adds this vector. If you want to save result to another vector, use add() instead.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:186 |
Parameters
vectorVec2
Examples
var v = cc.v2(10, 10);
v.addSelf(cc.v2(5, 5));// return Vec2 {x: 15, y: 15};
add
Adds two vectors, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:203 |
Parameters
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
v.add(cc.v2(5, 5)); // return Vec2 {x: 15, y: 15};
var v1;
v.add(cc.v2(5, 5), v1); // return Vec2 {x: 15, y: 15};
subSelf
Subtracts one vector from this. If you want to save result to another vector, use sub() instead.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:223 |
Parameters
vectorVec2
Examples
var v = cc.v2(10, 10);
v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};
sub
Subtracts one vector from this, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:240 |
Parameters
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
v.sub(cc.v2(5, 5)); // return Vec2 {x: 5, y: 5};
var v1;
v.sub(cc.v2(5, 5), v1); // return Vec2 {x: 5, y: 5};
mulSelf
Multiplies this by a number. If you want to save result to another vector, use mul() instead.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:260 |
Parameters
numnumber
Examples
var v = cc.v2(10, 10);
v.mulSelf(5);// return Vec2 {x: 50, y: 50};
mul
Multiplies by a number, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:277 |
Parameters
numnumberoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
v.mul(5); // return Vec2 {x: 50, y: 50};
var v1;
v.mul(5, v1); // return Vec2 {x: 50, y: 50};
scaleSelf
Multiplies two vectors.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:297 |
Parameters
vectorVec2
Examples
var v = cc.v2(10, 10);
v.scaleSelf(cc.v2(5, 5));// return Vec2 {x: 50, y: 50};
scale
Multiplies two vectors, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:314 |
Parameters
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
v.scale(cc.v2(5, 5)); // return Vec2 {x: 50, y: 50};
var v1;
v.scale(cc.v2(5, 5), v1); // return Vec2 {x: 50, y: 50};
divSelf
Divides by a number. If you want to save result to another vector, use div() instead.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:334 |
Parameters
numnumber
Examples
var v = cc.v2(10, 10);
v.divSelf(5); // return Vec2 {x: 2, y: 2};
div
Divides by a number, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:351 |
Parameters
numnumberoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
v.div(5); // return Vec2 {x: 2, y: 2};
var v1;
v.div(5, v1); // return Vec2 {x: 2, y: 2};
negSelf
Negates the components. If you want to save result to another vector, use neg() instead.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:371 |
Examples
var v = cc.v2(10, 10);
v.negSelf(); // return Vec2 {x: -10, y: -10};
neg
Negates the components, and returns the new result.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:387 |
Parameters
outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
Examples
var v = cc.v2(10, 10);
var v1;
v.neg(v1); // return Vec2 {x: -10, y: -10};
dot
Dot product
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:405 |
Parameters
vectorVec2
Examples
var v = cc.v2(10, 10);
v.dot(cc.v2(5, 5)); // return 100;
cross
Cross product
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:419 |
Parameters
vectorVec2
Examples
var v = cc.v2(10, 10);
v.cross(cc.v2(5, 5)); // return 0;
mag
Returns the length of this vector.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:433 |
Examples
var v = cc.v2(10, 10);
v.mag(); // return 14.142135623730951;
magSqr
Returns the squared length of this vector.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:446 |
Examples
var v = cc.v2(10, 10);
v.magSqr(); // return 200;
normalizeSelf
Make the length of this vector to 1.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:459 |
Examples
var v = cc.v2(10, 10);
v.normalizeSelf(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
normalize
Returns this vector with a magnitude of 1.
Note that the current vector is unchanged and a new normalized vector is returned. If you want to normalize the current vector, use normalizeSelf function.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:485 |
Parameters
outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
angle
Get angle in radian between this and vector.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:508 |
Parameters
vectorVec2
signAngle
Get angle in radian between this and vector with direction.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/value-types/vec2.js:530 |
Parameters
vectorVec2
rotate
rotate
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:542 |
Parameters
radiansnumberoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
rotateSelf
rotate self
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:557 |
Parameters
radiansnumber
project
Calculates the projection of the current vector over the given vector.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:574 |
Parameters
vectorVec2
Examples
var v1 = cc.v2(20, 20);
var v2 = cc.v2(5, 5);
v1.project(v2); // Vec2 {x: 20, y: 20};
transformMat4
Transforms the vec2 with a mat4. 3rd vector component is implicitly '0', 4th vector component is implicitly '1'
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/value-types/vec2.js:589 |