Only calculate inverse transform

main
copygirl 12 months ago
parent 9b7cb3d2fd
commit 49d407f624
  1. 15
      objects/Grid.cs

@ -72,21 +72,20 @@ public partial class Grid : Area3D
/// </summary>
public Transform3D Snap(Transform3D transform, Vector3 normal, Item item)
{
var inverseTransform = GlobalTransform.AffineInverse();
var inverseBasis = GlobalBasis.Inverse();
var halfSize = (Vector3)item.Size * StepSize / 2;
var halfSize = (Vector3)item.Size * StepSize / 2;
// Get grid-local values of the global transform and normal.
var pos = inverseTransform * transform.Origin;
var basis = inverseBasis * transform.Basis;
normal = inverseBasis * normal;
var inverse = GlobalTransform.AffineInverse();
var pos = inverse * transform.Origin;
var basis = inverse.Basis * transform.Basis;
normal = inverse.Basis * normal;
// Snap rotation to nearest axis.
basis = Basis.FromEuler(basis.GetEuler().Snapped(Tau / 4));
// Offset / "push out" by half of the item's size.
var offsetAxis = (int)(normal * basis).Abs().MaxAxisIndex();
pos += halfSize[offsetAxis] * normal;
var axis = (normal * basis).Abs().MaxAxisIndex();
pos += halfSize[(int)axis] * normal;
// Snap the position to the grid.
var halfOff = basis * halfSize.PosMod(1.0f);

Loading…
Cancel
Save