Vec2
Class
Extends ValueType
Representation of 2D vectors and points.
Index
Properties
ONE
Vec2
return a Vec2 object with x = 1 and y = 1.ZERO
Vec2
return a Vec2 object with x = 0 and y = 0.ZERO_R
Vec2
return a readonly Vec2 object with x = 0 and y = 0.UP
Vec2
return a Vec2 object with x = 0 and y = 1.UP_R
Vec2
return a readonly Vec2 object with x = 0 and y = 1.RIGHT
Vec2
return a readonly Vec2 object with x = 1 and y = 0.RIGHT_R
Vec2
return a Vec2 object with x = 1 and y = 0.x
Number
y
Number
Methods
mag
Returns the length of this vector.magSqr
Returns the squared length of this vector.subSelf
Subtracts one vector from this.sub
Subtracts one vector from this, and returns the new result.mulSelf
Multiplies this by a number.mul
Multiplies by a number, and returns the new result.divSelf
Divides by a number.div
Divides by a number, and returns the new result.scaleSelf
Multiplies two vectors.scale
Multiplies two vectors, and returns the new result.negSelf
Negates the components.neg
Negates the components, and returns the new result.clone
copy
set
add
subtract
multiply
divide
ceil
floor
min
max
round
multiplyScalar
scaleAndAdd
distance
squaredDistance
len
lengthSqr
negate
inverse
inverseSafe
normalize
dot
cross
lerp
random
transformMat3
transformMat4
strictEquals
equals
angle
toArray
fromArray
constructor
Constructorclone
clone a Vec2 objectset
Sets vector with another's valueequals
Check whether two vector equalfuzzyEquals
Check whether two vector equal with some degree of variance.toString
Transform to string with vector informationslerp
Calculate linear interpolation result between this vector and another one with given ratioclampf
Clamp the vector between from float and to float.add
Adds this vector.addSelf
Adds this vector.subtract
Subtracts one vector from this.multiplyScalar
Multiplies this by a number.multiply
Multiplies two vectors.divide
Divides by a number.negate
Negates the components.dot
Dot productcross
Cross productlen
Returns the length of this vector.lengthSqr
Returns the squared length of this vector.normalizeSelf
Make the length of this vector to 1.normalize
Note that the current vector is unchanged and a new normalized vector is returned.angle
Get angle in radian between this and vector.signAngle
Get angle in radian between this and vector with direction.rotate
rotaterotateSelf
rotate selfproject
Calculates the projection of the current vector over the given vector.transformMat4
Transforms the vec2 with a mat4.maxAxis
Returns the maximum value in x, y.
Details
Properties
ONE
return a Vec2 object with x = 1 and y = 1.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:213 |
ZERO
return a Vec2 object with x = 0 and y = 0.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:223 |
ZERO_R
return a readonly Vec2 object with x = 0 and y = 0.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:230 |
UP
return a Vec2 object with x = 0 and y = 1.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:239 |
UP_R
return a readonly Vec2 object with x = 0 and y = 1.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:246 |
RIGHT
return a readonly Vec2 object with x = 1 and y = 0.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:255 |
RIGHT_R
return a Vec2 object with x = 1 and y = 0.
meta | description |
---|---|
Type | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:262 |
x
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/value-types/vec2.ts:734 |
y
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/value-types/vec2.ts:739 |
Methods
mag
Returns the length of this vector.
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:55 |
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.ts:65 |
Examples
var v = cc.v2(10, 10);
v.magSqr(); // return 200;
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.ts:75 |
Parameters
vector
Vec2
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.ts:87 |
Parameters
vector
Vec2out
Vec2 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 = new Vec2;
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.ts:103 |
Parameters
num
number
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.ts:115 |
Parameters
num
numberout
Vec2 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 = new Vec2;
v.mul(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.ts:131 |
Parameters
num
number
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.ts:143 |
Parameters
num
numberout
Vec2 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 = new Vec2;
v.div(5, v1); // return Vec2 {x: 2, y: 2};
scaleSelf
Multiplies two vectors.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:159 |
Parameters
vector
Vec2
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.ts:171 |
Parameters
vector
Vec2out
Vec2 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 = new Vec2;
v.scale(cc.v2(5, 5), v1); // return Vec2 {x: 50, y: 50};
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.ts:187 |
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.ts:198 |
Parameters
out
Vec2 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 = new Vec2;
v.neg(v1); // return Vec2 {x: -10, y: -10};
clone
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:271 |
copy
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:282 |
set
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:295 |
add
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:308 |
subtract
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:321 |
multiply
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:334 |
divide
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:347 |
ceil
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:360 |
floor
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:373 |
min
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:386 |
max
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:400 |
round
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:413 |
multiplyScalar
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:426 |
scaleAndAdd
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:439 |
distance
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:452 |
squaredDistance
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:465 |
len
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:478 |
lengthSqr
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:491 |
negate
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:504 |
inverse
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:517 |
inverseSafe
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:530 |
normalize
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:556 |
dot
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:575 |
cross
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:586 |
lerp
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:599 |
random
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:614 |
transformMat3
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:629 |
transformMat4
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:645 |
strictEquals
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:661 |
equals
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:672 |
angle
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:688 |
toArray
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:708 |
fromArray
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:721 |
constructor
Constructor see Cc/vec2:method or Cc/p:method
meta | description |
---|---|
Defined in | cocos2d/core/value-types/vec2.ts:747 |
Parameters
clone
clone a Vec2 object
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:769 |
set
Sets vector with another's value
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:779 |
Parameters
newValue
Vec2 !#en new value to set. !#zh 要设置的新值
equals
Check whether two vector equal
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/value-types/vec2.ts:793 |
Parameters
other
Vec2
fuzzyEquals
Check whether two vector equal with some degree of variance.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/value-types/vec2.ts:804 |
Parameters
toString
Transform to string with vector informations
meta | description |
---|---|
Returns | string |
Defined in | cocos2d/core/value-types/vec2.ts:822 |
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.ts:835 |
Parameters
to
Vec2ratio
Number the interpolation coefficientout
Vec2 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.ts:853 |
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};
add
Adds this vector.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:877 |
Parameters
Examples
var v = cc.v2(10, 10);
v.add(cc.v2(5, 5));// return Vec2 {x: 15, y: 15};
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.ts:896 |
Parameters
vector
Vec2
subtract
Subtracts one vector from this.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:910 |
Parameters
vector
Vec2
Examples
var v = cc.v2(10, 10);
v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};
multiplyScalar
Multiplies this by a number.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:927 |
Parameters
num
number
Examples
var v = cc.v2(10, 10);
v.multiply(5);// return Vec2 {x: 50, y: 50};
multiply
Multiplies two vectors.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:944 |
Parameters
vector
Vec2
Examples
var v = cc.v2(10, 10);
v.multiply(cc.v2(5, 5));// return Vec2 {x: 50, y: 50};
divide
Divides by a number.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:961 |
Parameters
num
number
Examples
var v = cc.v2(10, 10);
v.divide(5); // return Vec2 {x: 2, y: 2};
negate
Negates the components.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:978 |
Examples
var v = cc.v2(10, 10);
v.negate(); // return Vec2 {x: -10, y: -10};
dot
Dot product
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:994 |
Parameters
vector
Vec2
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.ts:1008 |
Parameters
vector
Vec2
Examples
var v = cc.v2(10, 10);
v.cross(cc.v2(5, 5)); // return 0;
len
Returns the length of this vector.
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:1022 |
Examples
var v = cc.v2(10, 10);
v.len(); // return 14.142135623730951;
lengthSqr
Returns the squared length of this vector.
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:1035 |
Examples
var v = cc.v2(10, 10);
v.lengthSqr(); // return 200;
normalizeSelf
Make the length of this vector to 1.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:1048 |
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.ts:1074 |
Parameters
out
Vec2 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.ts:1097 |
Parameters
vector
Vec2
signAngle
Get angle in radian between this and vector with direction.
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:1119 |
Parameters
vector
Vec2
rotate
rotate
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:1131 |
Parameters
radians
numberout
Vec2 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.ts:1146 |
Parameters
radians
number
project
Calculates the projection of the current vector over the given vector.
meta | description |
---|---|
Returns | Vec2 |
Defined in | cocos2d/core/value-types/vec2.ts:1163 |
Parameters
vector
Vec2
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.ts:1178 |
Parameters
m
Mat4 matrix to transform without
Vec2 the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
maxAxis
Returns the maximum value in x, y.
meta | description |
---|---|
Returns | number |
Defined in | cocos2d/core/value-types/vec2.ts:1191 |