- Home /
Calculate third Vector3 with two Vector3s and an Angle
Greetings;
I'm trying to accomplish the finding myPointC, see comments to see where I need help with. The rest of the code works as intended. The function I'm having a problem with is how to find using an angle. The line length does not even need to be here as it's not really an issue because I know that value.
the basic is find pointC relative to pointA using pointA, pointB and an Angle.
Here is a image for a visual question if this can help you any. I'm pulling my hair out over this, even though I'm already bald:
private void DrawLineUsingDistanceAtAngle(){
Vector3 myPointA;
Vector3 myPointB;
Vector3 myPointC;
float lineAngle = 15f;
float lineLength = 10f;
myPointB = LerpByDistance(myPointA, myPointB, lineLength);
myPointB = GetThirdVector3UsingAngle(
myPointA, myPointB, lineAngle, "x");
DoSomethingGreat(myPointA, myPointB);
}
private Vector3 GetThirdVector3UsingAngle(
Vector3 pointA, Vector3 pointB,
float angle, string zeroAxis = "z")
{
Vector3 pointC;
if (zeroAxis == "x"){
pointA.x = 0;
pointB.x = 0;
}else if(zeroAxis == "y"){
pointA.y = 0;
pointA.y = 0;
}else{
pointA.z = 0;
pointA.z = 0;
}
// do calculation to get pointC here
return pointC;
}
public Vector3 LerpByDistance(Vector3 A, Vector3 B, float x)
{
Vector3 P = x * Vector3.Normalize(B - A) + A;
return P;
}
Answer by Owen-Reynolds · Dec 09, 2012 at 05:58 PM
Take that top line AB, rotate it by the angle, then rescale for the correct length. I'm going to assume this is a top view, so the rotation is over Y. I think you have it all except the actual rotate, but including it all for context:
Vector3 B2 = B-A; // move line AB to come out of (0,0,0) so rotate is easier
Quaternion angleSpin = Quaternion.Euler(0,angle,0); // create a rotation out of angle
Vector3 C2 = angleSpin * B2; // rotate AB by the angle
// C2 is now facing the correct way, but coming out of (0,0,0) and the wrong len:
C = C2.normalized * cLen + B; // rescale, recenter coming out of A
The answer is correct (except you should add A, not B) but as you said you need two more things bedside the angle and the other two points:
the rotation axis
the length of AC
otherwise with only the information in the original post, point C could be anywhere on a cone surface.
This still isnt working for me, can you kindly review my edited code below? I'm still getting a straight line to the original B point. Also; I already have a LerpByDistance function which moves the point down a line between two points so I believe that solved the .Normalized section of the original answer.
private Vector3 GetThirdVector3UsingAngle(Vector3 A, Vector3 B, float angle, float cLen, string rotateOnAxis = "y")
{
Vector3 B2 = B - A; // move line AB to come out of (0,0,0) so rotate is easier
Quaternion angleSpin;
if (rotateOnAxis == "x"){
angleSpin = Quaternion.Euler(angle,0,0); // create a rotation out of angle on x axis
}else if(rotateOnAxis == "z"){
angleSpin = Quaternion.Euler(0,0,angle); // create a rotation out of angle on z axis
}else{
angleSpin = Quaternion.Euler(0,angle,0); // create a rotation out of angle on y axis default
}
Vector3 C2 = angleSpin * B2; // rotate AB by the angle
C2 = LerpByDistance(A, B, cLen); // rescale, recenter co$$anonymous$$g out of A
return C2;
}
public Vector3 LerpByDistance(Vector3 A, Vector3 B, float x)
{
// x is distance from point A that you want to travel
Vector3 P = x * Vector3.Normalize(B - A) + A;
return P;
}
$$anonymous$$y apologies... The above code is working as the question asks. Some other portion of my program was off so I prematurely stated it wasn't working. Both functions do as intended as I wrote them above. Thank you both for your help in this. Now my 2 day search on solving this matter while pulling my hair out is closed.
Thanks!
Your answer
Follow this Question
Related Questions
How do I find the angle between the players forward vector and the mouse position? 1 Answer
Get Direction from 2 Vectors - and Apply to Transform 0 Answers
How to use Quaternion to rotate an object in only one axis? 1 Answer
Firing a projectile in 2D 0 Answers
Moving by rotation of objects 0 Answers