0
\$\begingroup\$

I have been trying to come up with a function that allows me to take in an origin vector, angle, and a length I want to be between the old and new vector, but I can't wrap my head around it.

The function requires the angle input to be a 3D vector that is normalized and needs to look something like this.

glm::vec3 CreateAngledVector(glm::vec3 origin, glm::vec3 angle, float length)
{
    angle = glm::normalize(angle);

    ...
}

If anyone has any suggestions on how I can achieve this, I'd be happy to hear them.

Edit I've managed to make it so I can create angles on a 2D plane on all 3 dimension but I can't figure out how to rotate it on two planes at the same time.

glm::vec3 CreateAngledVector(glm::vec3 start, glm::vec3 axis, float angle, float length)
{
    axis = glm::normalize(axis);

    glm::vec3 output = axis;
    output = glm::rotate(output, (angle*DEG2RAD) * axis.z, glm::vec3(1.0f, 0.0f, 0.0f));
    output = glm::rotate(output, (angle*DEG2RAD) * axis.x, glm::vec3(0.0f, 1.0f, 0.0f));
    output = glm::rotate(output, (angle*DEG2RAD) * axis.y, glm::vec3(0.0f, 0.0f, 1.0f));

    output *= length;
    output += start;
    return output;
}

Using this function I can create circles using the following.

int loops = 180;
float angle_part = 360.0f / loops;
glm::vec3 axis = glm::vec3(1.0f, 0.0f, 0.0f);
for (int i = 0; i < loops; i++)
{
    glm::vec3 new_vector = CreateAngledVector(l1.position, axis, angle_part*i, 1.0f) ;
    ...
}

The state before the function works fine creating angled vectors on a 2d plane (end goal is to make circles) but when I attempt to turn the axis of the function to go into 3D such as glm::vec3 axis = glm::vec3(1.0f, 1.0f, 0.0f); It breaks the function (see example)

axis = glm::vec3(0.0f, 1.0f, 0.0f) enter image description here

axis = glm::vec3(0.0f, 0.0f, 1.0f) enter image description here

"Broken one" axis = glm::vec3(1.0f, 1.0f, 0.0f) enter image description here

\$\endgroup\$
  • \$\begingroup\$ The naming here is a bit confusing. By "origin" do you mean a center point or do you mean an "original" input vector that you then want to rotate? Have you looked at "glm::lerp" or looked into Linear Interpolation? \$\endgroup\$ – Honeybunch Jul 21 '17 at 20:37
  • \$\begingroup\$ @Honeybunch The origin would be the input vector that I want to rotate and get a new vector at x distance from the original \$\endgroup\$ – 0xen Jul 21 '17 at 20:55
  • \$\begingroup\$ Origin is a confusing name. When I see origin I think <0,0,0>. The "angle" and "length" looks sort of like an axis-angle rotation model. If you converted that to a quaternion you could multiply that against your input vector and I think get the result that you want. Here's an example in Unity but the math calls in GLM are pretty similar. answers.unity3d.com/questions/46770/… \$\endgroup\$ – Honeybunch Jul 21 '17 at 21:05
  • 1
    \$\begingroup\$ 3D rotations are a bit tricky. There's a number of different ways to represent them, from quaternions to matrices to Axis-angle. Your system doesn't seem to be one of the ones in standard use. Can you give some examples of desired input/output values you're looking for? \$\endgroup\$ – Jimmy Jul 21 '17 at 21:58
  • \$\begingroup\$ @Jimmy An example of what I need is this, When a vector is passed into the function (the origin) I want the output vector to be rotated on the x,y and z axis based on a number of degrees passed in the angle input and have x distance between the origin vector and the output vector \$\endgroup\$ – 0xen Jul 22 '17 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.