Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/pong/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Init()
public override void Start()
{
World world = new();
Entity player = new(new Vector3(456, 228, 1));
Entity player = new(new Vector3(400, 200, 1));
Entity enemy = new();

Sprite spr = new(456, 456, andrei);
Expand Down
3 changes: 2 additions & 1 deletion samples/pong/Soldier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public override void Start()
public override void Update(double deltaTime)
{
base.Update(deltaTime);

}

public override void FrameUpdate(double deltaTime)
Expand All @@ -25,7 +26,7 @@ public override void FrameUpdate(double deltaTime)

if (Input.IsKeyPressed(Keys.V))
{
Log.Info("V pressed");
Entity.Transform.RotateFromPoint(new Vector2(800, 200), 90);
}

PhysicsBody? body = Entity?.GetComponent<PhysicsBody>();
Expand Down
29 changes: 29 additions & 0 deletions src/rasdaq/Transformations/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ public class Transform(Vector3 spawnPosition)
/// </summary>
public float rotation = 0.0f;

/// <summary>
/// Rotate the entity from a point in the world space.
///
/// Note that this does not rotate the entity itself.
/// Instead, it moves the entity in a circular arc, with `point` being the centre of the arc.
///
/// </summary>
/// <param name="point">centre of rotation</param>
/// <param name="rotation">rotation in degrees</param>
public void RotateFromPoint(Vector2 point, float rotation)
{
// get radius
Vector2 entityPos = new(position.X, position.Y);
Vector2 radius = (OVector2)entityPos - (OVector2)point;

// get trig
var sin = Math.Sin(MathHelper.DegreesToRadians(rotation));
var cos = Math.Cos(MathHelper.DegreesToRadians(rotation));

// get x,y from origin
var xFromOrigin = radius.X * cos - radius.Y * sin;
var yFromOrigin = radius.X * sin + radius.Y * cos;

// set position and round to 4 dp
position.X = Math.Round(point.X + xFromOrigin, 4);
position.Y = Math.Round(point.Y + yFromOrigin, 4);
}

internal void CoordUpdate(OVector2 delta)
{
position.X += delta.X;
Expand Down Expand Up @@ -65,5 +93,6 @@ internal Matrix4 GetRenderedTransform(float imageWidth, float imageHeight)
var scale = ScaleFromNDC(imageWidth, imageHeight);

return scale * rotation * translation;
// return scale * rotation * translation;
}
}
26 changes: 26 additions & 0 deletions tests/TransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,30 @@ public void GetRenderedTransform_SetPositionScale_ScaleThenTranslate()
// need to transpose back since openTK uses row major
AssertMatrixEqual(transform.GetRenderedTransform(1, 1), correctMatrix.Transposed());
}

[Test]
public void RotateFromPoint_At90CCW_Rotate90CCW()
{
// (x: 2, y: 0)
transform.position = new Vector3(2, 0, 0);

Vector2 center = new(0, 0);
transform.RotateFromPoint(center, 90);
var actual = new Vector3(0, 2, 0);

Assert.That(transform.position, Is.EqualTo(actual));
}

[Test]
public void RotateFromPoint_AtMinus90WithCenterOnRight_Rotate90CW()
{
// (x: 2, y: 0)
transform.position = new Vector3(-2, 0, 0);

Vector2 center = new(0, 0);
transform.RotateFromPoint(center, -90);
var actual = new Vector3(0, 2, 0);

Assert.That(transform.position, Is.EqualTo(actual));
}
}