Skip to content

Commit

Permalink
fix(VirtualObject): don't return matrix as quaternion
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Jan 3, 2024
1 parent d4eb346 commit f431af2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 37 deletions.
30 changes: 21 additions & 9 deletions ZenKit.Test/Vobs/TestVirtualObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Numerics;
using NUnit.Framework;
using ZenKit.Util;
using ZenKit.Vobs;

namespace ZenKit.Test.Vobs;
Expand Down Expand Up @@ -37,10 +38,15 @@ public void TestLoadG1()
Assert.Multiple(() => CheckVec3(vob.BoundingBox.Max, -18772.623f, -42.7076874f, 4567.23486f));
Assert.Multiple(() => CheckVec3(vob.Position, -18869.623f, -139.707687f, 4470.23486f));

Assert.That(vob.Rotation.X, Is.EqualTo(0));
Assert.That(vob.Rotation.Y, Is.EqualTo(0));
Assert.That(vob.Rotation.Z, Is.EqualTo(0));
Assert.That(vob.Rotation.W, Is.EqualTo(1.0f));
Assert.That(vob.Rotation.M11, Is.EqualTo(1));
Assert.That(vob.Rotation.M21, Is.EqualTo(0));
Assert.That(vob.Rotation.M31, Is.EqualTo(0));
Assert.That(vob.Rotation.M12, Is.EqualTo(0));
Assert.That(vob.Rotation.M22, Is.EqualTo(1));
Assert.That(vob.Rotation.M32, Is.EqualTo(0));
Assert.That(vob.Rotation.M13, Is.EqualTo(0));
Assert.That(vob.Rotation.M23, Is.EqualTo(0));
Assert.That(vob.Rotation.M33, Is.EqualTo(1));
Assert.That(vob.ShowVisual, Is.True);
Assert.That(vob.SpriteCameraFacingMode, Is.EqualTo(SpriteAlignment.None));
Assert.That(vob.CdStatic, Is.False);
Expand Down Expand Up @@ -68,10 +74,16 @@ public void TestLoadG2()
Assert.Multiple(() => CheckVec3(vob.BoundingBox.Max, 30929.8301f, 4836.17529f, -14817.3135f));
Assert.Multiple(() => CheckVec3(vob.Position, 30913.4668f, 4798.9751f, -14841.4434f));

Assert.That(vob.Rotation.X, Is.EqualTo(0));
Assert.That(vob.Rotation.Y, Is.EqualTo(-0.199367985f));
Assert.That(vob.Rotation.Z, Is.EqualTo(0));
Assert.That(vob.Rotation.W, Is.EqualTo(0.979924798f));
Assert.That(vob.Rotation.M11, Is.EqualTo(0.920505285f));
Assert.That(vob.Rotation.M21, Is.EqualTo(0));
Assert.That(vob.Rotation.M31, Is.EqualTo(-0.390731275f));
Assert.That(vob.Rotation.M12, Is.EqualTo(0));
Assert.That(vob.Rotation.M22, Is.EqualTo(1));
Assert.That(vob.Rotation.M32, Is.EqualTo(0));
Assert.That(vob.Rotation.M13, Is.EqualTo(0.390731275f));
Assert.That(vob.Rotation.M23, Is.EqualTo(0));
Assert.That(vob.Rotation.M33, Is.EqualTo(0.920505285f));

Assert.That(vob.ShowVisual, Is.True);
Assert.That(vob.SpriteCameraFacingMode, Is.EqualTo(SpriteAlignment.None));
Assert.That(vob.CdStatic, Is.False);
Expand Down Expand Up @@ -102,7 +114,7 @@ public void TestSetters()
};

vob.Position = new Vector3(30913.4668f, 4798.9751f, -14841.4434f);
vob.Rotation = new Quaternion(0, -0.199367985f, 0, 0.979924798f);
vob.Rotation = new Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
vob.ShowVisual = false;
vob.SpriteCameraFacingMode = SpriteAlignment.None;
vob.CdStatic = true;
Expand Down
40 changes: 17 additions & 23 deletions ZenKit/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using System.Text;
using ZenKit.Daedalus;
using ZenKit.Util;
using ZenKit.Vobs;
using static ZenKit.Native.Callbacks;
using static ZenKit.Native.Structs;
Expand Down Expand Up @@ -6744,42 +6745,35 @@ public struct ZkMat3x3
m21,
m22;

public ZkMat3x3(Quaternion quat)
public static ZkMat3x3 FromPublicMatrix(Matrix3x3 mat)
{
var mat = Matrix4x4.CreateFromQuaternion(quat);
m00 = mat.M11;
m10 = mat.M12;
m20 = mat.M13;
m01 = mat.M21;
m11 = mat.M22;
m21 = mat.M23;
m02 = mat.M31;
m12 = mat.M32;
m22 = mat.M33;
return new ZkMat3x3
{
m00 = mat.M11,
m10 = mat.M12,
m20 = mat.M13,
m01 = mat.M21,
m11 = mat.M22,
m21 = mat.M23,
m02 = mat.M31,
m12 = mat.M32,
m22 = mat.M33,
};
}

public Quaternion ToQuaternion()
public Matrix3x3 ToPublicMatrix()
{
// TODO(lmichaelis): Make this faster.
var mat = new Matrix4x4(
return new Matrix3x3(
m00,
m10,
m20,
0,
m01,
m11,
m21,
0,
m02,
m12,
m22,
0,
0,
0,
0,
1
m22
);
return Quaternion.CreateFromRotationMatrix(mat);
}
}

Expand Down
20 changes: 20 additions & 0 deletions ZenKit/Util/Matrix3x3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ZenKit.Util
{
public class Matrix3x3
{
public float M11, M12, M13, M21, M22, M23, M31, M32, M33;

public Matrix3x3(float m11, float m12, float m13, float m21, float m22, float m23, float m31, float m32, float m33)
{
M11 = m11;
M12 = m12;
M13 = m13;
M21 = m21;
M22 = m22;
M23 = m23;
M31 = m31;
M32 = m32;
M33 = m33;
}
}
}
10 changes: 5 additions & 5 deletions ZenKit/Vobs/VirtualObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public interface IVirtualObject : ICacheable<IVirtualObject>
public bool PhysicsEnabled { get; set; }
public Vector3 Position { get; set; }
public string PresetName { get; set; }
public Quaternion Rotation { get; set; }
public Matrix3x3 Rotation { get; set; }
public bool ShowVisual { get; set; }
public SpriteAlignment SpriteCameraFacingMode { get; set; }
public bool Static { get; set; }
Expand Down Expand Up @@ -366,7 +366,7 @@ public class CachedVirtualObject : IVirtualObject
public bool PhysicsEnabled { get; set; }
public Vector3 Position { get; set; }
public string PresetName { get; set; }
public Quaternion Rotation { get; set; }
public Matrix3x3 Rotation { get; set; }
public bool ShowVisual { get; set; }
public SpriteAlignment SpriteCameraFacingMode { get; set; }
public bool Static { get; set; }
Expand Down Expand Up @@ -479,10 +479,10 @@ public Vector3 Position
set => Native.ZkVirtualObject_setPosition(Handle, value);
}

public Quaternion Rotation
public Matrix3x3 Rotation
{
get => Native.ZkVirtualObject_getRotation(Handle).ToQuaternion();
set => Native.ZkVirtualObject_setRotation(Handle, new Native.Structs.ZkMat3x3(value));
get => Native.ZkVirtualObject_getRotation(Handle).ToPublicMatrix();
set => Native.ZkVirtualObject_setRotation(Handle, Native.Structs.ZkMat3x3.FromPublicMatrix(value));
}

public bool ShowVisual
Expand Down

0 comments on commit f431af2

Please sign in to comment.