Add pivot offset for rotations.

This commit is contained in:
2023-09-25 18:44:35 +02:00
parent ea2733f594
commit 8aab1132b6
8 changed files with 27 additions and 17 deletions

View File

@@ -78,9 +78,10 @@ namespace DaggerFramework.Rendering
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, DaggerColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0)
{
_position = position;
_offset = offset;
_rotation = rotation;
}
@@ -153,7 +154,7 @@ namespace DaggerFramework.Rendering
y = _position.Y,
width = size.X,
height = size.Y
}, Vector2.Zero, _rotation, DaggerColorToRaylibColor(color));
}, _offset, _rotation, DaggerColorToRaylibColor(color));
}
public override void DrawDebugText(string text, int fontSize, Color color)
@@ -203,7 +204,7 @@ namespace DaggerFramework.Rendering
private List<Texture2D> _texturePool = new();
private List<Raylib_cs.Font> _fontPool = new();
private Vector2 _position;
private Vector2 _position, _offset;
private float _rotation;
private Vector2 _windowSize;
}

View File

@@ -82,8 +82,9 @@ namespace DaggerFramework.Rendering
/// Sets transforms for the next draw operation.
/// </summary>
/// <param name="position">Global transform position.</param>
/// <param name="offset">Local offset point around which shapes will rotate.</param>
/// <param name="rotation">Rotation.</param>
public abstract void SetTransform(Vector2 position, float rotation = 0.0f);
public abstract void SetTransform(Vector2 position, Vector2 offset, float rotation = 0.0f);
/// <summary>
/// Sets the transform for the next draw operation.
/// </summary>

View File

@@ -102,7 +102,7 @@ namespace DaggerFramework.Rendering
}
/// <inheritdoc />
public override void SetTransform(Vector2 position, float rotation = 0)
public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0)
{
throw new NotImplementedException();
}

View File

@@ -1,12 +1,14 @@
using System.Numerics;
using DaggerFramework.Rendering;
namespace DaggerFramework.SceneGraph
{
public abstract class Drawable2d : Entity2d, IDrawable
{
public Vector2 PivotOffset { get; set; }
public void Draw(Renderer renderer)
{
renderer.SetTransform(Position);
renderer.SetTransform(Position, PivotOffset, Rotation);
OnDraw(renderer);
}

View File

@@ -5,5 +5,6 @@ namespace DaggerFramework.SceneGraph
public class Entity2d : Entity
{
public Vector2 Position { get; set; }
public float Rotation { get; set; }
}
}

View File

@@ -31,7 +31,7 @@ namespace DaggerFramework.SceneGraph
var scale = MathUtils.Lerp(_settings.ScaleEnd, _settings.ScaleBegin, t);
var color = MathUtils.LerpColor(_settings.ColorEnd, _settings.ColorBegin, t);
renderer.SetTransform(particle.Position, particle.Rotation);
renderer.SetTransform(particle.Position, Vector2.Zero, particle.Rotation);
renderer.DrawRectangle(Vector2.One * scale, color);
}
}

View File

@@ -9,6 +9,7 @@ public class RectangleShape2d : Drawable2d
public Color Color { get; set; } = Color.White;
public override void OnDraw(Renderer renderer)
{
PivotOffset = Size / 2;
renderer.DrawRectangle(Size, Color);
}
}

View File

@@ -10,25 +10,29 @@ public class TestPlayer : RectangleShape2d
Color = Color.Cyan;
_logger.Echo("OnStart");
_camera = new Camera2d()
{
Current = true,
};
// _camera = new Camera2d()
// {
// Current = true,
// };
Layer.AddEntity(_camera);
// Layer.AddEntity(_camera);
}
protected override void OnUpdate(double dt)
{
base.OnUpdate(dt);
var sprinting = Input.IsActionDown("sprint");
_speed = sprinting ? 400f : 200f;
// var sprinting = Input.IsActionDown("sprint");
// _speed = sprinting ? 400f : 200f;
var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed;
Position += velocity * (float)dt;
// var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed;
// Position += velocity * (float)dt;
_camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f);
// _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f);
var mousePos = Input.GetMousePosition();
Position = MathUtils.LerpVector2(Position, mousePos, dt * 5f);
Rotation += (float)dt * 100f;
}
private Logger _logger = new(nameof(TestPlayer));