- Home /
Vector rotation
I have 3 values which I want to represent on a bar graph like display. The catch is I want the bars to come out rotated around a central point, to form a triangular shape. The values are variable, but the total is limited. Something like this.
The values are a, b and c, along vectors o-e, o-g, o-f.
My issue is the math behind it. I can follow the formula easy enough, but have yet to figure out how to apply it correctly in this instance. I'm trying to place point a at the correct distance, then rotate it by 120 degrees(circle/3) to get my three vectors. However, they all shoot off at random angles.
I assume my problem is a) Not getting the direction vectors correct and b) not extending along these correctly. I've looked at wolfram, even the wolfire blog linked in another answer, but can't wrap my head around where I'm going wrong.
Relevant code attached.
var oPos : Vector2 = Vector2(5, 5); var oAng : Vector2;
var currentAng : float = 0; var theta : float = 120.0;
var aPos : Vector2; var aAng : Vector2; var aDist : int = 7;
var bPos : Vector2; var bAng : Vector2; var bDist : int = 7;
var cPos : Vector2; var cAng : Vector2; var cDist : int = 7;
private var newAng : Vector2; private var newPos : Vector2;
function Start() {
oAng = Vector2(0, 0); aPos = oPos + Vector2(aDist, aDist);
bPos = oPos + Vector2(bDist, bDist);
bPos.x = (bPos.x * Mathf.Cos(theta)) - (bPos.y * Mathf.Sin(theta));
bPos.y = (bPos.x * Mathf.Sin(theta)) + (bPos.y * Mathf.Cos(theta));
cPos = oPos + Vector2(cDist, cDist);
cPos.x = (cPos.x * Mathf.Cos(theta*2)) - (cPos.y * Mathf.Sin(theta*2));
cPos.y = (cPos.x * Mathf.Sin(theta*2)) + (cPos.y * Mathf.Cos(theta*2));
}
Answer by Peter G · Apr 24, 2011 at 11:59 AM
Unity has a convenient operator for rotating Vector's.
var distFromCenter : float;
var firstPoint : Vector3 = Vector3.up; var secondPoint : Vector3 = Vector3.up; var thirdPoint : Vector3 = Vector3.up;
var rotationAngle : float = 120.0;
function Start() { var rotation : Quaternion = Quaternion.AngleAxis(rotationAngle, Vector3.forward);
firstPoint *= rotation;
firstPoint *= distFromCenter;
secondPoint *= rotation;
secondPoint *= distFromCenter;
thirdPoint *= rotation;
thirdPoint *= distFromCenter;
}
Sorry it took so long to reply.
The Point *= rotation; lines don't work, took me this long to get the google search right to find this thread: http://forum.unity3d.com/threads/36737-Vector3-rotation
Worked once I changed "Point = rot" to "Point = rot Point".
Otherwise perfect - ta muchly.