Rotate#
Math / Rotate
Can Read
Cannot Write
Description#
Rotates a vector counterclockwise by an angle in degrees
Parameters#
| Name | Description |
|---|---|
| Vector | The vector that is rotated |
| Degrees | How much the vector turns counterclockwise |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Rotate")]
[Category("Math/Rotate")]
[Description("Rotates a vector counterclockwise by an angle in degrees")]
[Image(typeof(IconRotation), ColorTheme.Type.Yellow, typeof(Badge2D))]
[Parameter("Vector", "The vector that is rotated")]
[Parameter("Degrees", "How much the vector turns counterclockwise")]
[Keywords("Vector", "Turn", "Spin", "Angle", "Degrees", "Direction")]
[Serializable]
public class TypeVector2MathRotate : TypeVector2
{
[SerializeField] private GetVector2 m_Vector = new GetVector2();
[SerializeField] private GetNumber m_Degrees = new GetNumber(90f);
public override Vector2 GetPreview(GameObject source) => Rotate(
this.m_Vector.GetPreview(source),
this.m_Degrees.GetPreview(source)
);
public override Vector2 Get(Args args) => Rotate(this.m_Vector.Get(args), this.m_Degrees.Get(args));
private static Vector2 Rotate(Vector2 vector, double degrees)
{
float radians = (float) degrees * Mathf.Deg2Rad;
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);
return new Vector2(
vector.x * cos - vector.y * sin,
vector.x * sin + vector.y * cos
);
}
public override string ToString()
{
return $"Rotate {this.m_Vector} by {this.m_Degrees}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}