Vector patterns commonly used in 2d games

Source: Internet
Author: User

function Cc.exports.VectorRotateByAngle (vector,angle)-Calculates the vector after the rotation of the vectors, angle: positive counterclockwise, negative input clockwise

Angle = angle*math.pi/180

Local sinangle = Math.sin (angle)

Local cosangle = Math.Cos (angle)

Return CC.P (vector.x * cosangle-vector.y * sinangle,vector.x * sinangle + vector.y * cosangle)

End

function Cc.exports.AngleBetweenVector (V1,V2)-Calculates the angle of two vectors with a positive value

Local n = v1.x*v2.x + v1.y*v2.y

Local m = Vector2tolen (v1) *vector2tolen (v2)

Return Math.acos (n/m) * (180/MATH.PI)

End

function Cc.exports.NomalizeVector (vector)--unit vector

Local Veclen = math.sqrt (vector.x*vector.x + vector.y*vector.y)--unit vector

Return CC.P (Vector.x/veclen,vector.y/veclen)

End

function Cc.exports.VectorToLen (VEC2)--calculating the length of a vector

Return Math.sqrt (VEC2.X*VEC2.X+VEC2.Y*VEC2.Y)

End

Vector patterns commonly used in 2d games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.