From ea3639806a4dc0646b49f4beeda185696c7d6bd7 Mon Sep 17 00:00:00 2001 From: Richard Marcus <richard.marcus@fau.de> Date: Fri, 14 Feb 2020 02:04:05 +0100 Subject: [PATCH] enabled tree trunk collider, added only partly working onLine prototype --- Assets/FactManager.cs | 15 +++-- Assets/InteractionEngine/Fact.cs | 50 ++++++++++++----- Assets/InteractionEngine/WorldCursor.cs | 73 ++++++++++++++++++++++--- Assets/Resources/Prefabs/Line.prefab | 2 +- Assets/TreeWorld.unity | 2 +- 5 files changed, 112 insertions(+), 30 deletions(-) diff --git a/Assets/FactManager.cs b/Assets/FactManager.cs index 4f6c0f7d..9d5d6007 100644 --- a/Assets/FactManager.cs +++ b/Assets/FactManager.cs @@ -56,10 +56,12 @@ RayFact AddRayFact(int pid1, int pid2, int id) { Facts.Insert(id, new RayFact(id, pid1, pid2)); - var oPid = GetFirstEmptyID(); - Facts.Insert(oPid, new RayFact(id, pid1, pid2)); - oPid = GetFirstEmptyID(); - Facts.Insert(oPid, new RayFact(id, pid1, pid2)); + var oLid = GetFirstEmptyID(); + Facts.Insert(oLid, new OnLineFact(oLid, pid1, id)); + oLid = GetFirstEmptyID(); + Facts.Insert(oLid, new OnLineFact(oLid, pid2, id)); + + //TODO: check for more points, question: should MMT do this instead? return Facts.Find(x => x.Id == id) as RayFact; } @@ -105,6 +107,7 @@ public int GetFirstEmptyID() } + public void OnToolModeChanged(ToolMode ActiveToolMode) { //We need to do this somehwere... @@ -117,8 +120,10 @@ public void OnToolModeChanged(ToolMode ActiveToolMode) //everywhere, independent of already existing facts foreach (Fact fact in Facts) { + GameObject gO = fact.Representation; - gO.GetComponentInChildren<Collider>().enabled = false; + if ((gO.layer == LayerMask.NameToLayer("Line"))) + gO.GetComponentInChildren<Collider>().enabled = true; } break; diff --git a/Assets/InteractionEngine/Fact.cs b/Assets/InteractionEngine/Fact.cs index 5c050764..05572ecc 100644 --- a/Assets/InteractionEngine/Fact.cs +++ b/Assets/InteractionEngine/Fact.cs @@ -71,12 +71,7 @@ public PointFact(int i, float a, float b, float c, string uri) } -public class OpenLineFact : Fact -{ - //R: this is called RayFact for now (see below), feel free to change - //an infinite Line through the Points Pid1 and Pid2 - public int Pid1, Pid2; -} + public class LineFact : Fact { @@ -112,6 +107,13 @@ public LineFact(int i, int pid1, int pid2, string uri, string valuri) { } +public class OpenLineFact : Fact +{ + //R: this is called RayFact for now (see below), feel free to change + //an infinite Line through the Points Pid1 and Pid2 + public int Pid1, Pid2; +} + public class RayFact : Fact { //Id's of the 2 Point-Facts that are connected @@ -129,9 +131,9 @@ public RayFact(int i, int pid1, int pid2) PointFact pf2 = CommunicationEvents.Facts.Find((x => x.Id == pid2)) as PointFact; string p1URI = pf1.backendURI; string p2URI = pf2.backendURI; - float v = (pf1.Point - pf2.Point).magnitude; - string body = @"{ ""pointA"":""" + p1URI + @"""," + @"""pointB"":""" + p2URI + @"""," + @"""value"":" + format(v) + "}"; - AddFactResponse res = AddFactResponse.sendAdd("localhost:8081/fact/add/distance", body); + //TODO: fix body + string body = @"{ ""base"":""" + p1URI + @"""," + @"""dir"":""" + p2URI + @"""," + "}"; + AddFactResponse res = AddFactResponse.sendAdd("localhost:8081/fact/add/line", body); this.backendURI = res.factUri; this.backendValueURI = res.factValUri; } @@ -149,6 +151,30 @@ public RayFact(int i, int pid1, int pid2, string uri, string valuri) } +public class OnLineFact : Fact +{ + //Id's of the Point , and the Id of the Line it sits on + public int Pid, Lid; + + public OnLineFact(int i, int pid, int lid) + { + this.Id = i; + this.Pid = pid; + this.Lid = lid; + PointFact pf = CommunicationEvents.Facts.Find((x => x.Id == pid)) as PointFact; + RayFact lf = CommunicationEvents.Facts.Find((x => x.Id == lid)) as RayFact; + string pURI = pf.backendURI; + string lURI = lf.backendURI; + string body = @"{ ""vector"":""" + pURI + @"""," + @"""line"":""" + lURI + @"""," + "}"; + AddFactResponse res = AddFactResponse.sendAdd("localhost:8081/fact/add/onLine", body); + this.backendURI = res.factUri; + this.backendValueURI = res.factValUri; + } + + +} + + public class AngleFact : Fact { //Id's of the 3 Point-Facts, where Pid2 is the point, where the angle is @@ -192,9 +218,5 @@ public AngleFact(int i, int pid1, int pid2, int pid3, string uri, string valuri) this.backendValueURI = valuri; } } -public class OnLineFact : Fact -{ - //Id's of the Point , and the Id of the Line it sits on - public int Pid1, Lid2; -} + diff --git a/Assets/InteractionEngine/WorldCursor.cs b/Assets/InteractionEngine/WorldCursor.cs index fb14f1c9..79388502 100644 --- a/Assets/InteractionEngine/WorldCursor.cs +++ b/Assets/InteractionEngine/WorldCursor.cs @@ -21,7 +21,38 @@ void Start() } - // Update is called once per frame + + /// <summary> + /// Gets the coordinates of the intersection point of two lines. + /// </summary> + /// <param name="A1">A point on the first line.</param> + /// <param name="A2">Another point on the first line.</param> + /// <param name="B1">A point on the second line.</param> + /// <param name="B2">Another point on the second line.</param> + /// <param name="found">Is set to false of there are no solution. true otherwise.</param> + /// <returns>The intersection point coordinates. Returns Vector2.zero if there is no solution.</returns> + public Vector2 GetIntersectionPointCoordinates(Vector2 A1, Vector2 A2, Vector2 B1, Vector2 B2, out bool found) + { + float tmp = (B2.x - B1.x) * (A2.y - A1.y) - (B2.y - B1.y) * (A2.x - A1.x); + + if (tmp == 0) + { + // No solution! + found = false; + return Vector2.zero; + } + + float mu = ((A1.x - B1.x) * (A2.y - A1.y) - (A1.y - B1.y) * (A2.x - A1.x)) / tmp; + + found = true; + + return new Vector2( + B1.x + (B2.x - B1.x) * mu, + B1.y + (B2.y - B1.y) * mu + ); + } + + void Update() { Ray ray = Cam.ScreenPointToRay(Input.mousePosition); @@ -37,9 +68,36 @@ void Update() // Debug.Log(Hit.transform.tag); if (Hit.collider.transform.CompareTag("SnapZone")) { - Hit.point = Hit.collider.transform.position; - Hit.normal = Vector3.up; - CheckMouseButtons(true); + if(Hit.collider.gameObject.layer == LayerMask.NameToLayer("Line")){ + + int id = Hit.collider.gameObject.GetComponent<FactObject>().Id; + LineFact lineFact = CommunicationEvents.Facts.Find((x => x.Id == id)) as LineFact; + PointFact p1 = CommunicationEvents.Facts.Find((x => x.Id == lineFact.Pid1)) as PointFact; + PointFact p2 = CommunicationEvents.Facts.Find((x => x.Id == lineFact.Pid2)) as PointFact; + + Vector3 lineDir = p2.Point - p1.Point; + Plane plane = new Plane(lineDir, Hit.point); + + Ray intersectionRay = new Ray(p1.Point, lineDir ); + + if(plane.Raycast(intersectionRay, out float enter)){ + + Hit.point = p1.Point + lineDir.normalized * enter; + } + else Debug.LogError("something wrong with linesnapzone"); + CheckMouseButtons(true,true); + + + + } + else + { + Hit.point = Hit.collider.transform.position; + Hit.normal = Vector3.up; + CheckMouseButtons(true); + } + + transform.position = Hit.point; transform.up = Hit.normal; @@ -52,9 +110,6 @@ void Update() CheckMouseButtons(); } - - - } else { @@ -70,7 +125,7 @@ void Update() } //Check if left Mouse-Button was pressed and handle it - void CheckMouseButtons(bool OnSnap=false) + void CheckMouseButtons(bool OnSnap=false, bool onLine = false) { if (Input.GetMouseButtonDown(0)) @@ -82,7 +137,7 @@ void CheckMouseButtons(bool OnSnap=false) CommunicationEvents.TriggerEvent.Invoke(Hit); } else if(CommunicationEvents.ActiveToolMode==ToolMode.MarkPointMode){ - Hit.collider.enabled = false; + if(!onLine)Hit.collider.enabled = false; CommunicationEvents.TriggerEvent.Invoke(Hit); // CommunicationEvents.SnapEvent.Invoke(Hit); } diff --git a/Assets/Resources/Prefabs/Line.prefab b/Assets/Resources/Prefabs/Line.prefab index 68aabfc1..907f3cd0 100644 --- a/Assets/Resources/Prefabs/Line.prefab +++ b/Assets/Resources/Prefabs/Line.prefab @@ -263,7 +263,7 @@ GameObject: - component: {fileID: 6179051021116985022} m_Layer: 11 m_Name: Line - m_TagString: Selectable + m_TagString: SnapZone m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 diff --git a/Assets/TreeWorld.unity b/Assets/TreeWorld.unity index 097207b3..40e01671 100644 --- a/Assets/TreeWorld.unity +++ b/Assets/TreeWorld.unity @@ -275,7 +275,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 100010, guid: c91c1eb85782d5748ace27eb4d7415bb, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 100012, guid: c91c1eb85782d5748ace27eb4d7415bb, type: 3} propertyPath: m_IsActive -- GitLab