[Unity] changes the vector direction without changing its size.

Source: Internet
Author: User

[Unity] changes the vector direction without changing its size.

Recently, when I was playing a brick-and-mortar game, I encountered a small problem, that is, the ball may perform round-robin between the left and right sides of the boundary, resulting in the game being unable to continue, so I want to change the direction of the ball when it hits the boundary vertically, but the speed remains unchanged. I tried some methods but did not achieve the effect. Later I thought that the speed is a vector, and the problem becomes to change the direction of a vector without changing its size. Google found the revolving matrix Wiki page.
Multiply the original vector matrix [x, y] by the rotation matrix
/// <Summary> // rotate the vector to change its direction, the size remains unchanged /// </summary> /// <param name = "v"> angle of rotation of the vector to be rotated </param> /// <param name = "angle"> </param> // <returns> the Rotated Vector </returns> private Vector2 RotationMatrix (Vector2 v, float angle) {var x = v. x; var y = v. y; var sin = Math. sin (Math. PI * angle/180); var cos = Math. cos (Math. PI * angle/180); var newX = x * cos + y * sin; var newY = x *-sin + y * cos; return new Vector2 (float) newX, (float) newY );}

Related Article

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.