Correct sprites rotation

This commit is contained in:
Marco 2025-07-01 14:56:50 +02:00
commit e061ad9570
2 changed files with 59 additions and 10 deletions

View file

@ -32,7 +32,7 @@ public partial class Bullet3D : Area3D, IBullet
public bool IsFrozen { get; private set; } = false; public bool IsFrozen { get; private set; } = false;
public bool Enabled { 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] [Signal]
public delegate void OnDestroyEventHandler(); public delegate void OnDestroyEventHandler();
@ -52,6 +52,24 @@ public partial class Bullet3D : Area3D, IBullet
Mathf.DegToRad(45f), Mathf.DegToRad(45f),
0f 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() public override void _Ready()
{ {
@ -71,8 +89,8 @@ public partial class Bullet3D : Area3D, IBullet
this.Speed = bulletInfo.Speed; this.Speed = bulletInfo.Speed;
_sprite?.SetRotation(_defaultRotation); SetSpriteRotationToDirection();
SpriteRotation = 0f; //SpriteRotation = 0f;
if (_sprite is not null && bulletInfo.OriginalBulletResource.BulletSprite is not null) if (_sprite is not null && bulletInfo.OriginalBulletResource.BulletSprite is not null)
{ {
@ -97,6 +115,29 @@ public partial class Bullet3D : Area3D, IBullet
EmitSignalInitialized(); 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);
}
/// <summary> /// <summary>
/// Enables the bullet, shows the sprite and activates collisions /// Enables the bullet, shows the sprite and activates collisions
/// </summary> /// </summary>
@ -173,7 +214,7 @@ public partial class Bullet3D : Area3D, IBullet
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction _direction = _direction.Rotated(radians).Normalized(); // Rotate direction
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
RotateSprite(SpriteRotation + radians + Mathf.DegToRad(45)); SetSpriteRotationToDirection();
//SetRotation(Rotation + radians); //SetRotation(Rotation + radians);
} }
@ -181,7 +222,11 @@ public partial class Bullet3D : Area3D, IBullet
{ {
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; 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); //SetRotationDegrees(RotationDegrees + degrees);
} }
@ -189,10 +234,12 @@ public partial class Bullet3D : Area3D, IBullet
public virtual void RotateSprite(float radians) public virtual void RotateSprite(float radians)
{ {
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
SpriteRotation += radians;
var currentRotation = _sprite.GetRotation().Z;
var axis = Basis.FromEuler(_defaultRotation).Z; var axis = Basis.FromEuler(_defaultRotation).Z;
_sprite?.Rotate(axis, radians); _sprite?.Rotate(axis, currentRotation + radians);
//_sprite.SetRotation(new Vector3()); //_sprite.SetRotation(new Vector3());
//Rotate(axis, radians); //Rotate(axis, radians);
//_sprite?.Rotate(Vector3.Forward, radians); //_sprite?.Rotate(Vector3.Forward, radians);
@ -219,8 +266,10 @@ public partial class Bullet3D : Area3D, IBullet
_direction = normalized; _direction = normalized;
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return; if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
SetSpriteRotationToDirection();
//SetRotation(Mathf.Atan2(normalized.Y, normalized.X) + Mathf.Pi / 2); //SetRotation(Mathf.Atan2(normalized.Y, normalized.X) + Mathf.Pi / 2);
} }

BIN
Sprites/Bullets/rice_bullet_blue_small.png (Stored with Git LFS)

Binary file not shown.