From 62c633fecb4a995884401717353499b2400a04f5 Mon Sep 17 00:00:00 2001 From: Niklas Ulf Anders Cullberg Date: Mon, 1 Oct 2012 17:35:07 +0200 Subject: [PATCH] Looking in direction pointing line --- .../DepthsBelow/Component/Transform.cs | 6 ++++ DepthsBelow/DepthsBelow/MouseInput.cs | 30 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/DepthsBelow/DepthsBelow/Component/Transform.cs b/DepthsBelow/DepthsBelow/Component/Transform.cs index 6385688..c7a451c 100755 --- a/DepthsBelow/DepthsBelow/Component/Transform.cs +++ b/DepthsBelow/DepthsBelow/Component/Transform.cs @@ -97,6 +97,12 @@ namespace DepthsBelow.Component } public float Rotation; + + public void Rotate(int directionX, int directionY) + { + Rotation = (float)Math.Atan(directionY/directionX); + } + public Vector2 Origin; public static implicit operator Vector2(WorldTransform worldTransform) diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 92ff766..dcdfc2c 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -87,15 +87,17 @@ public MouseInput(Core core) if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) { foreach (var unit in core.Squad) - if (unit.Selected) + if (unit.Selected) + { unit.GetComponent().Goal = Grid.WorldToGrid(mouseWorldPos); + } } if (ms.RightButton == ButtonState.Pressed) { foreach (var unit in core.Squad) { - if (unit.Selected) + if (unit.Selected && gridRectangle.Intersects(unit.GetComponent().Rectangle)) { checkingDirection = true; directionStart = unit.Transform.World + unit.Transform.World.Origin; @@ -104,8 +106,28 @@ public MouseInput(Core core) } if (checkingDirection == true && ms.RightButton == ButtonState.Pressed) { - directionNow.X = ms.X; - directionNow.Y = ms.Y; + foreach (var unit in core.Squad) + if (unit.Selected) + { + directionNow.X = mouseWorldPos.X; + directionNow.Y = mouseWorldPos.Y; + float directionX = mouseWorldPos.X - unit.Transform.World.X; + float directionY = mouseWorldPos.Y - unit.Transform.World.Y; + var mouseDirection = mouseWorldPos; + mouseDirection.Normalize(); + //float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi; + /*if (directionX < 0) + { + directionX *= -1; + unit.GetComponent().World.Rotation = (float)Math.Atan(directionY / directionX); + } + else + { + */unit.GetComponent().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y, mouseWorldPos.X - unit.Transform.World.X);/* + }*/ + //Console.WriteLine(angle); + //unit.GetComponent().World.Rotation = angle; + } } else {