1

Currently, I can draw two line segments and have them intersect.

What I want to do:

I would like to be able to rotate either of the two intersecting line segments about the intersection point by clicking on the endpoint of the line or the line itself and dragging it left or right.

Here's what I have so far:

   //Shift coordinate system so that origin is at the point of rotation, do the rotation and then shift the coordinate system
   //back to original intersection poiint
   public Vector2 RotateLineAboutIntersectionPoint(Vector2 intersectionPoint, Line line, Vector2 mousePosition)
   {
       double angle = Math.Atan2(mousePosition.y - intersectionPoint.y, mousePosition.x - intersectionPoint.x);
       double newPointOne = (line.point1.x - intersectionPoint.x) * Math.Cos(angle) - (line.point1.y - intersectionPoint.y) * Math.Sin(angle) + intersectionPoint.x;
       double newPointTwo = (line.point1.x - intersectionPoint.x) * Math.Sin(angle) + (line.point1.y - intersectionPoint.y) * Math.Cos(angle) + intersectionPoint.y;

       return new Vector2((float)newPointOne,(float)newPointTwo);
   }

I think the formula I have is correct, the problem is the mouse position doesn't get updated when dragging/rotating/scaling my line. The line is stuck in the same place.

That is, I need to track where the mouse was at the "click" event, and then subtract the angle of that point from the angle I'm computing for the new mouse position. How do I do that?

EDIT:

I have my mouse position in Update.

Update() {

   Vector3 mousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
}
Kala J
  • 2,040
  • 4
  • 45
  • 85
  • if you need just to handle the mouse then look here http://stackoverflow.com/a/20924609/2521214 and look how the common mouse event is called `edit.mouse(X-x0,Y-y0,Shift);` ... so create single mouse handler with the code of yours and call it from all mouse events. – Spektre Mar 20 '15 at 07:54
  • I think I just need to track the position of the first mouse click and the position of the last mouse click. – Kala J Mar 20 '15 at 13:08

0 Answers0