diff --git a/Scripts/Weapons/Bullet3D.cs b/Scripts/Weapons/Bullet3D.cs index eba0772c..b46719b4 100644 --- a/Scripts/Weapons/Bullet3D.cs +++ b/Scripts/Weapons/Bullet3D.cs @@ -32,7 +32,7 @@ public partial class Bullet3D : Area3D, IBullet public bool IsFrozen { get; private set; } = false; public bool Enabled { get; private set; } = false; - public float SpriteRotation { get; private set; } = 0f; + //public float SpriteRotation { get; private set; } = 0f; [Signal] public delegate void OnDestroyEventHandler(); @@ -52,6 +52,24 @@ public partial class Bullet3D : Area3D, IBullet Mathf.DegToRad(45f), 0f ); + + private Vector3 MakeRotationVectorDegrees(float rotation) + { + return new( + Mathf.DegToRad(-45f), + Mathf.DegToRad(45f), + Mathf.DegToRad(rotation) + ); + } + + private Vector3 MakeRotationVectorRad(float rotation) + { + return new( + Mathf.DegToRad(-45f), + Mathf.DegToRad(45f), + rotation + ); + } public override void _Ready() { @@ -71,8 +89,8 @@ public partial class Bullet3D : Area3D, IBullet this.Speed = bulletInfo.Speed; - _sprite?.SetRotation(_defaultRotation); - SpriteRotation = 0f; + SetSpriteRotationToDirection(); + //SpriteRotation = 0f; if (_sprite is not null && bulletInfo.OriginalBulletResource.BulletSprite is not null) { @@ -97,6 +115,29 @@ public partial class Bullet3D : Area3D, IBullet EmitSignalInitialized(); } + private void SetSpriteRotationToDirection() + { + _sprite?.SetRotation(MakeRotationFromDirection(_direction)); + } + + private Vector3 MakeRotationFromDirection(Vector2 direction) + { + // Rotate input by -45 degrees to counter camera isometry + float cos = Mathf.Cos(-Mathf.Pi / 4f); + float sin = Mathf.Sin(-Mathf.Pi / 4f); + + Vector2 rotatedDir = new Vector2( + direction.X * cos - direction.Y * sin, + direction.X * sin + direction.Y * cos + ); + + //float zRotation = Mathf.Atan2(rotatedDir.Y, rotatedDir.X)/* - Mathf.Pi / 2f*/; + + float zRotation = Mathf.Atan2(-rotatedDir.Y, rotatedDir.X) + Mathf.Pi; + + return MakeRotationVectorRad(zRotation); + } + /// /// Enables the bullet, shows the sprite and activates collisions /// @@ -173,7 +214,7 @@ public partial class Bullet3D : Area3D, IBullet _direction = _direction.Rotated(radians).Normalized(); // Rotate direction if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; - RotateSprite(SpriteRotation + radians + Mathf.DegToRad(45)); + SetSpriteRotationToDirection(); //SetRotation(Rotation + radians); } @@ -181,7 +222,11 @@ public partial class Bullet3D : Area3D, IBullet { if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; - SpriteRotation = Mathf.DegToRad(Mathf.RadToDeg(SpriteRotation) + degrees); + var currentRotation = _sprite.GetRotationDegrees().Z; + + _sprite.RotateZ(Mathf.DegToRad(currentRotation + degrees)); + + // SpriteRotation = Mathf.DegToRad(Mathf.RadToDeg(SpriteRotation) + degrees); //SetRotationDegrees(RotationDegrees + degrees); } @@ -189,10 +234,12 @@ public partial class Bullet3D : Area3D, IBullet public virtual void RotateSprite(float radians) { if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; - SpriteRotation += radians; + + var currentRotation = _sprite.GetRotation().Z; var axis = Basis.FromEuler(_defaultRotation).Z; - _sprite?.Rotate(axis, radians); + _sprite?.Rotate(axis, currentRotation + radians); + //_sprite.SetRotation(new Vector3()); //Rotate(axis, radians); //_sprite?.Rotate(Vector3.Forward, radians); @@ -219,8 +266,10 @@ public partial class Bullet3D : Area3D, IBullet _direction = normalized; if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; + + SetSpriteRotationToDirection(); //SetRotation(Mathf.Atan2(normalized.Y, normalized.X) + Mathf.Pi / 2); - + } diff --git a/Sprites/Bullets/rice_bullet_blue_small.png b/Sprites/Bullets/rice_bullet_blue_small.png index 3f022965..08b00eb0 100644 --- a/Sprites/Bullets/rice_bullet_blue_small.png +++ b/Sprites/Bullets/rice_bullet_blue_small.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:735ec2be49727d1499c7835704e709b1a37b2dae286df6d993f40835460c65ac -size 121 +oid sha256:39298c7c540b3c59ff53fb66d2bb61cd518df9c93086cd7cf460893e30445238 +size 123