私は現在、カメラを含むより良いエンティティ システムのために 3D エンジンを拡張しています。これにより、カメラを親エンティティに配置できますが、これは別のエンティティにも存在する可能性があります (など...)。
--エンティティ
----エンティティ
------カメラ
今、カメラの視線方向を設定したいのですが、エンティティの視線を設定するためにも使用される次のメソッドを使用してこれを行っています。
public void LookAt(Vector3 target, Vector3 up)
{
Matrix4x4 oldValue = _Matrix;
_Matrix = Matrix4x4.LookAt(_Position, target, up) * Matrix4x4.Scale(_Scale);
Vector3 p, s;
Quaternion r;
Quaternion oldRotation = _Rotation;
_Matrix.Decompose(out p, out s, out r);
_Rotation = r;
// Update dependency properties
ForceUpdate(RotationDeclaration, _Rotation, oldRotation);
ForceUpdate(MatrixDeclaration, _Matrix, oldValue);
}
ただし、コードはカメラに対してのみ機能し、他のエンティティに対しては機能しません。他のエンティティに対してこのメソッドを使用すると、オブジェクトはその位置で回転します (エンティティはルート ノードにあるため、親はありません)。マトリックスの look at メソッドは次のようになります。
public static Matrix4x4 LookAt(Vector3 position, Vector3 target, Vector3 up)
{
// Calculate and normalize forward vector
Vector3 forward = position - target;
forward.Normalize();
// Calculate and normalie side vector ( side = forward x up )
Vector3 side = Vector3.Cross(up, forward);
side.Normalize();
// Recompute up as: up = side x forward
up = Vector3.Cross(forward, side);
up.Normalize();
//------------------
Matrix4x4 result = new Matrix4x4(false)
{
M11 = side.X,
M21 = side.Y,
M31 = side.Z,
M41 = 0,
M12 = up.X,
M22 = up.Y,
M32 = up.Z,
M42 = 0,
M13 = forward.X,
M23 = forward.Y,
M33 = forward.Z,
M43 = 0,
M14 = 0,
M24 = 0,
M34 = 0,
M44 = 1
};
result.Multiply(Matrix4x4.Translation(-position.X, -position.Y, -position.Z));
return result;
}
decompose メソッドも、位置変数に対して間違った値を返しますp
。では、なぜカメラは機能しているのに実体は機能していないのでしょうか?