giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

CreateStare function to Matrix4.cs #48

Closed ghost closed 4 years ago

ghost commented 4 years ago

I added a function to Matrix4.cs that will let you rotate a vector to look at another vector, like a look at matrix, but not for cameras. I found this useful in my projects, so I was thinking it could be added to the library.

TheAIBot commented 4 years ago

I thought CreateStare and LookAt looked similair so i took at stab at recreating CreateStare from LookAt. Assuming you are rotating a Vector3, then Matrix4.LookAt(eye, target, up).Transpose() is the same as Matrix4.CreateStare(target, eye, up).

I wrote a little example.

static void Main(string[] args)
{
    var eye = new Vector3(7, 2, -3);
    var target = new Vector3(3, -4, -1);
    var up = new Vector3(9, 8, 7);

    var a = Matrix4.LookAt(eye, target, up).Transpose();
    var b = Matrix4.CreateStare(target, eye, up);

    var test = new Vector3(27, 3, -33);
    Console.WriteLine(a * test); //<-27,89182. -19,767666. 25,657078>
    Console.WriteLine(b * test); //<-27,89182. -19,767666. 25,657078>
}

With that in mind, I don't see the new methods as too useful.

ghost commented 4 years ago

I thought CreateStare and LookAt looked similair so i took at stab at recreating CreateStare from LookAt. Assuming you are rotating a Vector3, then Matrix4.LookAt(eye, target, up).Transpose() is the same as Matrix4.CreateStare(target, eye, up).

I wrote a little example.

static void Main(string[] args)
{
  var eye = new Vector3(7, 2, -3);
  var target = new Vector3(3, -4, -1);
  var up = new Vector3(9, 8, 7);

  var a = Matrix4.LookAt(eye, target, up).Transpose();
  var b = Matrix4.CreateStare(target, eye, up);

  var test = new Vector3(27, 3, -33);
  Console.WriteLine(a * test); //<-27,89182. -19,767666. 25,657078>
  Console.WriteLine(b * test); //<-27,89182. -19,767666. 25,657078>
}

With that in mind, I don't see the new methods as too useful.

I just found the function online, and converted it to C#. I didn't know you could just transpose LookAt to do the same thing.

giawa commented 4 years ago

Hey there, thanks for the contribution to the project! It's awesome to see other people using this code. As TheAIBot mentioned, it looks like similar functionality exists by taking the transpose of the LookAt matrix. Is CreateStare a commonly used function (do engines like Unreal, Godot, Unity, etc have similar functionality)? If so, I think it's worth including, but perhaps include it by simply calling LootAt(eye, target, up).Transpose() instead of producing duplicate code. Let me know what you think.

ghost commented 4 years ago

Hey there, thanks for the contribution to the project! It's awesome to see other people using this code. As TheAIBot mentioned, it looks like similar functionality exists by taking the transpose of the LookAt matrix. Is CreateStare a commonly used function (do engines like Unreal, Godot, Unity, etc have similar functionality)? If so, I think it's worth including, but perhaps include it by simply calling LootAt(eye, target, up).Transpose() instead of producing duplicate code. Let me know what you think.

Unity as a LookAt function for objects, and I think Unreal has one also. Yeah, I didn't know it was the same result as LootAt(eye, target, up).Transpose().

giawa commented 4 years ago

Does that mean this PR can be closed for now? I was unable to find examples of a Stare matrix in Unity/Unreal, but that doesn't mean we can't add one.

ghost commented 4 years ago

Does that mean this PR can be closed for now? I was unable to find examples of a Stare matrix in Unity/Unreal, but that doesn't mean we can't add one.

CreateStare would do the same as LookAt for objects in Unity.

giawa commented 4 years ago

Sounds good, closing this as duplicate functionality.