Vec2
Representation of 2D vectors and points.
Properties
ONE Vec2 static
return a Vec2 object with x = 1 and y = 1.
ZERO Vec2 static
return a Vec2 object with x = 0 and y = 0.
up Vec2 static
return a Vec2 object with x = 0 and y = 1.
RIGHT Vec2 static
return a Vec2 object with x = 1 and y = 0.
Methods
Vec2
(
-
[x
=0]
-
[y
=0]
)
Vec2
- [x =0]
- [y =0]
set
(
-
newValue
)
Vec2
- newValue
equals
(
-
other
)
Boolean
- other
lerp
(
-
to
-
ratio
-
[out
]
)
Vec2
- to
- ratio
- [out ]
addSelf
(
-
vector
)
Vec2
- vector
add
(
-
vector
-
[out
]
)
Vec2
- vector
- [out ]
Adds two vectors, and returns the new result.
name | type | description |
---|---|---|
vector
|
Vec2 |
!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p |
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
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
(
-
vector
)
Vec2
- vector
Subtracts one vector from this. If you want to save result to another vector, use sub() instead.
name | type | description |
---|---|---|
vector
|
Vec2 |
!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p |
returns:
examples:
var v = cc.v2(10, 10);
v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};
sub
(
-
vector
-
[out
]
)
Vec2
- vector
- [out ]
Subtracts one vector from this, and returns the new result.
name | type | description |
---|---|---|
vector
|
Vec2 |
!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p |
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
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
(
-
num
)
Vec2
- num
mul
(
-
num
-
[out
]
)
Vec2
- num
- [out ]
scaleSelf
(
-
vector
)
Vec2
- vector
scale
(
-
vector
-
[out
]
)
Vec2
- vector
- [out ]
Multiplies two vectors, and returns the new result.
name | type | description |
---|---|---|
vector
|
Vec2 |
!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p |
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
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
(
-
vector
)
Vec2
- vector
div
(
-
vector
-
[out
]
)
Vec2
- vector
- [out ]
Divides by a number, and returns the new result.
name | type | description |
---|---|---|
vector
|
Vec2 |
!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p |
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
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 ( ) Vec2
Negates the components. If you want to save result to another vector, use neg() instead.
returns:
examples:
var v = cc.v2(10, 10);
v.negSelf(); // return Vec2 {x: -10, y: -10};
neg
(
-
[out
]
)
Vec2
- [out ]
Negates the components, and returns the new result.
name | type | description |
---|---|---|
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
examples:
var v = cc.v2(10, 10);
var v1;
v.neg(v1); // return Vec2 {x: -10, y: -10};
dot
(
-
[vector
]
)
number
- [vector ]
cross
(
-
[vector
]
)
number
- [vector ]
mag ( ) number
Returns the length of this vector.
returns:
examples:
var v = cc.v2(10, 10);
v.mag(); // return 14.142135623730951;
magSqr ( ) number
Returns the squared length of this vector.
returns:
examples:
var v = cc.v2(10, 10);
v.magSqr(); // return 200;
normalizeSelf ( ) Vec2
Make the length of this vector to 1.
returns:
examples:
var v = cc.v2(10, 10);
v.normalizeSelf(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
normalize
(
-
[out
]
)
Vec2
- [out ]
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.
name | type | description |
---|---|---|
out
optional
|
Vec2 |
optional, the receiving vector |
returns:
result var v = cc.v2(10, 10); v.normalize(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
angle
(
-
vector
)
number
- vector
signAngle
(
-
vector
)
number
- vector
rotate
(
-
radians
-
[out
]
)
Vec2
- radians
- [out ]