// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; float dot = Vector3.Dot(v1, v2); float cosTheta = float.NaN; float theta = float.NaN; if (Mathf.Abs(dot) > float.Epsilon) // make sure dot is properly defined { cosTheta = dot / (v1.magnitude * v2.magnitude); theta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; } Debug.Log("Dot result=" + dot + " cosTheta=" + cosTheta + " angle=" + theta); #region For visualizing the vectors ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); ShowNV2.VectorAt = P1.transform.localPosition; ShowNV2.Direction = -ShowV2.Direction; ShowNV2.Magnitude = ShowV2.Magnitude; Vector3 v1_v2 = v1 - v2; ShowV1_V2.Direction = v1_v2; ShowV1_V2.Magnitude = v1_v2.magnitude; ShowV1_V2.VectorAt = P2.transform.localPosition; ShowV1_V2_Pos.Direction = ShowV1_V2.Direction; ShowV1_V2_Pos.Magnitude = ShowV1_V2.Magnitude; ShowV1_V2_Pos.VectorAt = P0.transform.localPosition; #endregion }
// Update is called once per frame void Update() { #region Visualization on/off: show or hide to avoid clutering AxisFrame.ShowAxisFrame = DrawAxisFrame; // Draw or Hide Axis Frame P1.SetActive(DrawPositionAsVector); // objects that suppor position as vector P2.SetActive(DrawPositionAsVector); Pd.SetActive(DrawPositionAsVector); Pi.SetActive(DrawVectorAsPosition); // objects that support vector as positions Pj.SetActive(DrawVectorAsPosition); Pe.SetActive(DrawVectorAsPosition); ShowVdAtP1.DrawVector = DrawPositionAsVector; // Display or hide the vectors ShowVd.DrawVector = DrawPositionAsVector; ShowVeAtPi.DrawVector = DrawVectorAsPosition; ShowVe.DrawVector = DrawVectorAsPosition; #endregion #region Position Vector: Show Pd as a vector at P1 if (DrawPositionAsVector) { // Use position of Pd as position vector Vector3 vectorVd = Pd.transform.localPosition; // Step 1: take care of visualization // for Vd ShowVd.Direction = vectorVd; ShowVd.Magnitude = vectorVd.magnitude; // apply Vd at P1 ShowVdAtP1.VectorAt = P1.transform.localPosition; // Always draw at P1 ShowVdAtP1.Magnitude = vectorVd.magnitude; // get from vectorVd ShowVdAtP1.Direction = vectorVd; // Step 2: demonstrate P2 is indeed Vd away from P1 P2.transform.localPosition = P1.transform.localPosition + vectorVd; } #endregion #region Vector from two points: Show Ve as the position Pe if (DrawVectorAsPosition) { // Use from Pi to Pj as vector for Ve Vector3 vectorVe = Pj.transform.localPosition - Pi.transform.localPosition; // Step 1: Take care of visualization // for Ve: from Pi to Pj ShowVeAtPi.VectorFromTo(Pi.transform.localPosition, Pj.transform.localPosition); // Show as Ve at the origin ShowVe.Direction = vectorVe; ShowVe.Magnitude = vectorVe.magnitude; // Step 2: demonstrate Pe is indeed Ve away from the origin Pe.transform.localPosition = vectorVe; } #endregion }
// Update is called once per frame void Update() { float distance = 0; // closest distance Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; float v1Len = v1.magnitude; if (v1Len > float.Epsilon) { Vector3 vt = Pt.transform.localPosition - P0.transform.localPosition; Vector3 v1n = (1f / v1Len) * v1; // <<-- what is going on here? float d = Vector3.Dot(vt, v1n); if (d < 0) { Pon.transform.localPosition = P0.transform.localPosition; distance = vt.magnitude; } else if (d > v1Len) { Pon.transform.localPosition = P1.transform.localPosition; distance = (Pt.transform.localPosition - P1.transform.localPosition).magnitude; } else { Pon.transform.localPosition = P0.transform.localPosition + d * v1n; Vector3 von = Pon.transform.localPosition - Pt.transform.localPosition; distance = von.magnitude; } float s = distance * kScaleFactor; Pon.transform.localScale = new Vector3(s, s, s); Debug.Log("v1Len=" + v1Len + " d=" + d + " Distance=" + distance); } #region For visualizing the lines bool visible = v1Len > float.Epsilon; ShowVc.DrawVector = visible; ShowLine.DrawVector = visible; ShowV1.DrawVector = visible; if (v1Len > float.Epsilon) { Vector3 vt = Pt.transform.localPosition - P0.transform.localPosition; Vector3 v1n = (1f / v1Len) * v1; // <<-- what am I doing here? float d = Vector3.Dot(v1n, vt); ShowLine.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowVc.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition); float after = 0.45f; float before = 0.15f; Vector3 pv0 = P0.transform.localPosition - before * v1;; Vector3 pv1 = P1.transform.localPosition + after * v1; ShowV1.VectorFromTo(pv0, pv1); } #endregion }
// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; #region For visualizing the vectors Vector3 pv0 = P0.transform.localPosition - 0.5f * v1; Vector3 pv1 = P1.transform.localPosition + 0.5f * v1; ShowV1.VectorFromTo(pv0, pv1); Pa.transform.localPosition = P0.transform.localPosition - 0.18f * v1; Pb.transform.localPosition = P0.transform.localPosition + 1.2f * v1; ShowLine.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); #endregion }
// Update is called once per frame void Update() { aVector.DrawVector = ShowA; sVector.DrawVector = ShowS; cutVector.DrawVector = ShowC; Vector3 d = Dst.transform.localPosition - Src.transform.localPosition; aVector.Direction = d; aVector.VectorAt = aPos.transform.localPosition; aVector.Magnitude = size; sVector.VectorAtDirLength(aVector.VectorAt + Vector3.right, d, aVector.Magnitude); cutVector.VectorFromTo(Src.transform.localPosition, Dst.transform.localPosition); }
// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; #region For visualizing the vectors ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); float r = 0.8f * v1.magnitude; Vector3 p0 = P0.transform.localPosition; float delta = 45; for (int i = 0; i < ShowV2.Length; i++) { float d = (i + 1) * delta * Mathf.Deg2Rad; float x = r * Mathf.Cos(d); float y = r * Mathf.Sin(d); Vector3 t = new Vector3(x, y, 0); ShowV2[i].VectorFromTo(p0, t); } #endregion }
// Update is called once per frame void Update() { float cosTheta = float.NaN; float theta = float.NaN; Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; float dot = Vector3.Dot(v1, v2); if ((v1.magnitude > float.Epsilon) && (v2.magnitude > float.Epsilon)) { cosTheta = dot / (v1.magnitude * v2.magnitude); // Alternatively, // costTheta = Vector3.Dot(v1.normalize, v2.normalize) theta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; } Debug.Log("Dot result=" + dot + " cosTheta=" + cosTheta + " angle=" + theta); #region For visualizing the vectors ShowPlane.DrawPlane = DrawThePlane; ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); Vector3 dotP = Vector3.zero; ShowPlane.DrawPlane &= cosTheta != float.NaN; ShowDot.DrawVector = cosTheta != float.NaN; if (cosTheta != float.NaN) { float r = 3f * Mathf.Abs(cosTheta); dotP.x = r * cosTheta; dotP.y = r * Mathf.Sin(theta * Mathf.Deg2Rad); ShowDot.VectorFromTo(Vector3.zero, dotP); if (Mathf.Abs(cosTheta) < (1f - float.Epsilon)) { // The plane Vector3 n = Vector3.Cross(v1, v2); if (Vector3.Dot(n, Vector3.forward) > 0) { n = -n; } float s = Mathf.Max(v1.magnitude, v2.magnitude); ShowPlane.XSize = ShowPlane.ZSize = s * 0.7f; ShowPlane.PlaneNormal = n; ShowPlane.Center = (P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition) / 3f; } else { ShowPlane.DrawPlane = false; } } if (dot > 0f) { ShowDot.VectorColor = PositiveColor; } else { ShowDot.VectorColor = NegativeColor; } #endregion }
// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; if ((v1.magnitude > float.Epsilon) && (v2.magnitude > float.Epsilon)) // make sure v1 and v2 are not zero vectors { switch (ProjChoice) { case ProjectionChoice.V1OntoV2: float v1LengthonV2 = Vector3.Dot(v1, v2.normalized); Debug.Log("Projection Result: Length of V1 along V2 = " + v1LengthonV2); break; case ProjectionChoice.V2OntoV1: float v2LengthonV1 = Vector3.Dot(v1.normalized, v2); Debug.Log("Projection Result: Length of V2 along V1 = " + v2LengthonV1); break; default: Debug.Log("Projection Result: no projection, dot=" + Vector3.Dot(v1, v2)); break; } } #region For visualizing the vectors ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); ShowProjected.DrawVector = (v1.magnitude > float.Epsilon) && (v2.magnitude > float.Epsilon) && (ProjChoice != ProjectionChoice.ProjectionOff); ShowProjectedAlone.DrawVector = ShowProjected.DrawVector; ShowProjected.VectorColor = PositiveColor; ShowProjectedAlone.VectorColor = PositiveColor; float length = 0f; if (ShowProjected.DrawVector) { if (ProjChoice == ProjectionChoice.V1OntoV2) { Vector3 nv = v2.normalized; length = Vector3.Dot(v1, nv); Vector3 pt = P0.transform.localPosition + length * nv; ShowProjected.VectorFromTo(P0.transform.localPosition, pt); Debug.DrawLine(P1.transform.localPosition, pt, Color.black); } else { Vector3 nv = v1.normalized; length = Vector3.Dot(v2, nv); Vector3 pt = P0.transform.localPosition + length * nv; ShowProjected.VectorFromTo(P0.transform.localPosition, pt); Debug.DrawLine(P2.transform.localPosition, pt, Color.black); } ShowProjectedAlone.VectorAt = P0.transform.localPosition - Vector3.right; ShowProjectedAlone.Magnitude = ShowProjected.Magnitude; if (length > 0) { ShowProjectedAlone.Direction = Vector3.up; ShowProjectedAlone.VectorColor = Color.black; ShowProjected.VectorColor = PositiveColor; } else { ShowProjectedAlone.Direction = -Vector3.up; ShowProjectedAlone.VectorColor = Color.red; ShowProjected.VectorColor = NegativeColor; } } #endregion }
// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; if (v1.magnitude > float.Epsilon) { Vector3 vt = Pt.transform.localPosition - P0.transform.localPosition; Vector3 v1n = v1.normalized; float d = Vector3.Dot(vt, v1n); Pon.transform.localPosition = P0.transform.localPosition + d * v1.normalized; if ((d >= 0) && (d <= v1.magnitude)) { Debug.Log("V1.mag=" + v1.magnitude + " Projected Length=" + d + " ==> Inside!"); } else { Debug.Log("V1.mag=" + v1.magnitude + " Projected Length=" + d + " ==> Outside!"); } } #region For visualizing the vectors ShowLine.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); bool visible = v1.magnitude > float.Epsilon; ShowV1.DrawVector = visible; ShowPv.DrawVector = visible; ShowPa.DrawVector = visible; if (visible) { Vector3 v1n = v1.normalized; Vector3 vt = Pt.transform.localPosition - P0.transform.localPosition; float d = Vector3.Dot(vt, v1n); if (d >= 0 && d <= v1.magnitude) { ShowLine.VectorColor = MyDrawObject.CollisionColor; } else { ShowLine.VectorColor = MyDrawObject.NoCollisionColor; } ShowPv.VectorFromTo(P0.transform.localPosition, Pt.transform.localPosition); ShowPa.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition); float after = 0.45f; float before = 0.15f; Vector3 pv0 = P0.transform.localPosition - before * v1;; Vector3 pv1 = P1.transform.localPosition + after * v1; if (d > (((1f + after) * v1.magnitude) - 1f)) { pv1 = Pon.transform.localPosition + v1n; } if (d < ((-before * v1.magnitude) + 1f)) { pv0 = Pon.transform.localPosition - v1n; } ShowV1.VectorFromTo(pv0, pv1); } #endregion }
// Update is called once per frame void Update() { #region Identical to EX_6_2 // Computes V1 and V2 Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; if ((v1.magnitude < float.Epsilon) || (v2.magnitude < float.Epsilon)) { return; } // Plane equation parameters Vector3 vn = Vector3.Cross(v1, v2); vn.Normalize(); // keep this vector normalized float D = Vector3.Dot(vn, P0.transform.localPosition); // Showing the plane equation is consistent Pn.transform.localPosition = D * vn; Ds.transform.localScale = new Vector3(D * 2f, D * 2f, D * 2f); // sphere expects diameter // Set up for displaying Pt and Pon Pt.SetActive(ShowPointOnPlane); Pon.SetActive(ShowPointOnPlane); float t = 0; bool almostParallel = false; if (ShowPointOnPlane) { float d = Vector3.Dot(vn, Pt.transform.localPosition); // distance almostParallel = (Mathf.Abs(d) < float.Epsilon); Pon.SetActive(!almostParallel); if (!almostParallel) { t = D / d; Pon.transform.localPosition = t * Pt.transform.localPosition; } } #endregion float l1 = v1.magnitude; float l2 = v2.magnitude; Vector3 v2p = l2 * Vector3.Cross(vn, v1).normalized; P2p.transform.localPosition = P0.transform.localPosition + v2p; bool inside = false; if (!almostParallel) { Vector3 von = Pon.transform.localPosition - P0.transform.localPosition; float d1 = Vector3.Dot(von, v1.normalized); float d2 = Vector3.Dot(von, v2p.normalized); inside = ((d1 >= 0) && (d1 <= l1)) && ((d2 >= 0) && (d2 <= l2)); if (inside) { Debug.Log("Inside: Pon is inside of the region defined by V1 and V2"); } else { Debug.Log("Outside: Pon is outside of the region defined by V1 and V2"); } } #region For visualizing the vectors ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); ShowV3.VectorFromTo(P0.transform.localPosition, P2p.transform.localPosition); ShowVn.VectorAt = P0.transform.localPosition; ShowVn.Direction = vn; ShowVn.Magnitude = 2f; ShowNormal.VectorAt = Vector3.zero; ShowNormal.Magnitude = Mathf.Abs(D) + 2f; ShowNormal.Direction = vn; ShowPlane.PlaneNormal = -vn; Vector3 at = P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition + Pn.transform.localPosition; int c = 4; float scale = 1.0f; ShowPtLine.DrawVector = ShowPointOnPlane; float da = v1.magnitude * scale; float db = v2.magnitude * scale; float du = Mathf.Max(da, db); if (ShowPointOnPlane && (!almostParallel)) { Pon.GetComponent <Renderer>().material.color = Color.white; float don = (Pon.transform.localPosition - P0.transform.localPosition).magnitude * scale; at += Pon.transform.localPosition; c++; du = Mathf.Max(du, don); // Now the line ShowPtLine.VectorColor = Color.black; if (Vector3.Dot(Pon.transform.localPosition, Pt.transform.localPosition) < 0) { ShowPtLine.VectorColor = Color.red; ShowPtLine.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition); } else { if (Pon.transform.localPosition.magnitude > Pt.transform.localPosition.magnitude) { ShowPtLine.VectorFromTo(Vector3.zero, Pon.transform.localPosition); } else { ShowPtLine.VectorFromTo(Vector3.zero, Pt.transform.localPosition); } } if (!inside) { Pon.GetComponent <Renderer>().material.color = Color.red; } } if (du < 0.5f) { du = 0.5f; } ShowPlane.XSize = ShowPlane.ZSize = du; ShowPlane.Center = at / c; #endregion }
// Update is called once per frame void Update() { // Computes V1 and V2 Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; if ((v1.magnitude < float.Epsilon) || (v2.magnitude < float.Epsilon)) { return; } // Plane equation parameters Vector3 vn = Vector3.Cross(v1, v2); vn.Normalize(); // keep this vector normalized float D = Vector3.Dot(vn, P0.transform.localPosition); // Showing the plane equation is consistent Pn.transform.localPosition = D * vn; Ds.transform.localScale = new Vector3(D * 2f, D * 2f, D * 2f); // sphere expects diameter // Set up for displaying Pt and Pon Pt.SetActive(ShowPointOnPlane); Pon.SetActive(ShowPointOnPlane); float t = 0; bool almostParallel = false; if (ShowPointOnPlane) { float d = Vector3.Dot(vn, Pt.transform.localPosition); // distance almostParallel = (Mathf.Abs(d) < float.Epsilon); Pon.SetActive(!almostParallel); if (!almostParallel) { t = D / d; Pon.transform.localPosition = t * Pt.transform.localPosition; } } #region For visualizing the vectors ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); ShowVn.VectorAt = P0.transform.localPosition; ShowVn.Direction = vn; ShowVn.Magnitude = 2f; ShowNormal.VectorAt = Vector3.zero; ShowNormal.Magnitude = Mathf.Abs(D) + 2f; ShowNormal.Direction = vn; ShowPlane.PlaneNormal = -vn; Vector3 at = P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition + Pn.transform.localPosition; int c = 4; float scale = 1.0f; ShowPtLine.DrawVector = ShowPointOnPlane; float d1 = v1.magnitude * scale; float d2 = v2.magnitude * scale; float du = Mathf.Max(d1, d2); if (ShowPointOnPlane && (!almostParallel)) { float don = (Pon.transform.localPosition - P0.transform.localPosition).magnitude * scale; at += Pon.transform.localPosition; c++; du = Mathf.Max(du, don); // Now the line ShowPtLine.VectorColor = Color.black; if (Vector3.Dot(Pon.transform.localPosition, Pt.transform.localPosition) < 0) { ShowPtLine.VectorColor = Color.red; ShowPtLine.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition); } else { if (Pon.transform.localPosition.magnitude > Pt.transform.localPosition.magnitude) { ShowPtLine.VectorFromTo(Vector3.zero, Pon.transform.localPosition); } else { ShowPtLine.VectorFromTo(Vector3.zero, Pt.transform.localPosition); } } } if (du < 0.5f) { du = 0.5f; } ShowPlane.XSize = ShowPlane.ZSize = du; ShowPlane.Center = at / c; #endregion }
// Update is called once per frame void Update() { Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition; Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition; Vector3 v1xv2 = Vector3.Cross(v1, v2); Vector3 v2xv1 = Vector3.Cross(v2, v1); float d = Vector3.Dot(v1.normalized, v2.normalized); bool notParallel = (Mathf.Abs(d) < (1.0f - float.Epsilon)); if (notParallel) { float theta = Mathf.Acos(d) * Mathf.Rad2Deg; float cd = Vector3.Dot(v1xv2.normalized, v2xv1.normalized); float dv1 = Vector3.Dot(v1xv2, v1); float dv2 = Vector3.Dot(v1xv2, v2); Debug.Log(" theta=" + theta + " v1xv2=" + v1xv2 + " v2xv1=" + v2xv1 + " v1xv2-dot-v2xv1=" + cd + " Dot with v1/v2=" + dv1 + " " + dv2); } else { Debug.Log("Two vectors are parallel, cross product is a zero vector"); } #region For visualizing the vectors ShowPlane.DrawPlane = DrawThePlane; ShowV1xV2.DrawVector = DrawV1xV2; ShowV2xV1.DrawVector = DrawV2xV1; ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition); ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition); ShowPlane.DrawPlane &= notParallel; ShowV1xV2.DrawVector &= notParallel; ShowV2xV1.DrawVector &= notParallel; if (notParallel) { ShowV1xV2.VectorAt = P0.transform.localPosition; ShowV1xV2.Magnitude = v1xv2.magnitude * Factor; ShowV1xV2.Direction = v1xv2; ShowV2xV1.VectorAt = P0.transform.localPosition; ShowV2xV1.Magnitude = v2xv1.magnitude * Factor; ShowV2xV1.Direction = v2xv1; // The plane Vector3 n = Vector3.Cross(v1, v2); if (Vector3.Dot(n, Vector3.forward) > 0) { n = -n; } float s = Mathf.Max(v1.magnitude, v2.magnitude); ShowPlane.XSize = ShowPlane.ZSize = s; ShowPlane.PlaneNormal = n; ShowPlane.Center = (P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition) / 3f; } #endregion }
// Update is called once per frame void Update() { #region Step 0: Initial error checking Debug.Assert((Pc.transform.localPosition - Pb.transform.localPosition).magnitude > float.Epsilon); Debug.Assert(Vn.magnitude > float.Epsilon); Debug.Assert(Aspeed > float.Epsilon); Debug.Assert(Tr > float.Epsilon); // recoveries from the errors if ((Pc.transform.localPosition - Pb.transform.localPosition).magnitude < float.Epsilon) { Pc.transform.localPosition = Pb.transform.localPosition - Vector3.forward; } if (Vn.magnitude < float.Epsilon) { Vn = Vector3.forward; } if (Aspeed < float.Epsilon) { Aspeed = 0.01f; } if (Tr < float.Epsilon) { Tr = 0.01f; } #endregion #region Step 1: The Aiming System Vector3 aDir = (Pc.transform.localPosition - Pb.transform.localPosition); aDir.Normalize(); // assuming the two are not located at the same point Pc.transform.localPosition = Pb.transform.localPosition + Aspeed * aDir; if (!MoveAgent) { // controls only when agent is not moving Pa.transform.localPosition = Pb.transform.localPosition + 2 * Aspeed * aDir; Adir = aDir; } #endregion #region Step 2: The Agent if (MoveAgent) { Pa.transform.localPosition += Aspeed * Time.deltaTime * Adir; AgentSinceTime += Time.deltaTime; if (AgentSinceTime > AgentSentInterval) { // Time to re-send the agent from base Pa.transform.localPosition = Pc.transform.localPosition; Adir = aDir; AgentSinceTime = 0f; } } if (ShowVelocity && ShowDebugLines) { Debug.DrawLine(Pa.transform.localPosition, Pa.transform.localPosition + 20f * Adir, Color.red); } #endregion #region Step 3: The Plane and infront/parellel checks // Plane equation Vn.Normalize(); Pn.transform.localPosition = D * Vn; // agent position checks float paDotVn = Vector3.Dot(Pa.transform.localPosition, Vn); bool infrontOfPlane = (paDotVn > D); // Agent motion direction checks float aDirDotVn = Vector3.Dot(Adir, Vn); bool isApproaching = (aDirDotVn < 0f); bool notParallel = (Mathf.Abs(aDirDotVn) > float.Epsilon); #endregion #region Step 4: The Shadow Ps.SetActive(CastShadow && infrontOfPlane); if (CastShadow && infrontOfPlane) { float h = Vector3.Dot(Pa.transform.localPosition, Vn); Ps.transform.localPosition = Pa.transform.localPosition - (h - D) * Vn; if (ShowDebugLines) { Debug.DrawLine(Pa.transform.localPosition, Ps.transform.localPosition, Color.black); } } #endregion #region Step 5: The Reflection Pon.SetActive(DoReflection && notParallel && infrontOfPlane && isApproaching); Pr.SetActive(DoReflection && notParallel && infrontOfPlane && isApproaching); Vector3 vr = Vector3.up; // Reflection vector bool vrIsValid = false; if (DoReflection && notParallel && isApproaching) { if (infrontOfPlane) { float d = (D - Vector3.Dot(Pa.transform.localPosition, Vn)) / aDirDotVn; Pon.transform.localPosition = Pa.transform.localPosition + d * Adir; Vector3 von = Pa.transform.localPosition - Pon.transform.localPosition; // von is simply -d*Adir Vector3 m = (Vector3.Dot(von, Vn) * Vn) - von; vr = 2 * m + von; Pr.transform.localPosition = Pon.transform.localPosition + vr; vrIsValid = true; if (ShowDebugLines) { Debug.DrawLine(Pa.transform.localPosition, Pon.transform.localPosition, Color.red); Debug.DrawLine(Pon.transform.localPosition, Pr.transform.localPosition, Color.red); } // if (von.magnitude < float.Epsilon) What will happen if you do this? if (von.magnitude < 0.1f) { // collision! Adir = vr.normalized; } } else { Debug.Log("Potential problem!: high speed Agent, missing the plane collision?"); // What can you do? } } #endregion #region Step 6: The collision with treasure Pt.SetActive(DoReflection && CollideTreasure); Pt.transform.localScale = new Vector3(2 * Tr, 2 * Tr, 2 * Tr); // this is the diameter Pt.GetComponent <Renderer>().material.color = MyDrawObject.NoCollisionColor; if (DoReflection && CollideTreasure && vrIsValid) { Vector3 vt = Pt.transform.localPosition - Pon.transform.localPosition; float dt = Vector3.Dot(vt, vr.normalized); if ((dt >= 0) && (dt <= vr.magnitude)) { Vector3 pdt = Pon.transform.localPosition + dt * vr.normalized; if ((pdt - Pt.transform.localPosition).magnitude <= Tr) { Pt.GetComponent <Renderer>().material.color = MyDrawObject.CollisionColor; } } } #endregion #region For visualation AxisFrame.ShowAxisFrame = ShowAxisFrame; if (ShowAxisFrame && ShowDebugLines) { Debug.DrawLine(Vector3.zero, Pn.transform.localPosition, Color.white); } // Aiming ShowAim.VectorFromTo(Pb.transform.localPosition, Pc.transform.localPosition); // Refleciton plane ShowVrN.VectorAt = Pn.transform.localPosition; ShowVrN.Magnitude = 2.0f; ShowVrN.Direction = Vn; ShowReflectionPlane.Center = Pn.transform.localPosition; ShowReflectionPlane.PlaneNormal = Vn; /* * Do not adjust the plane size, it is confusing. * const float S = 3f; * float s = S; * if (CastShadow || DoReflection) * { * if (CastShadow) { * s = (Ps.transform.localPosition - Pn.transform.localPosition).magnitude * 0.3f; * // ShowProj.VectorFromTo(Pa.transform.localPosition, Ps.transform.localPosition); * } * if (DoReflection) * { * float t = (Pon.transform.localPosition - Pn.transform.localPosition).magnitude * 0.3f; * if (t > s) * s *= 1.02f; * else if (t < s) * s *= 0.98f; * // ShowToPn.VectorFromTo(Pa.transform.localPosition, Pon.transform.localPosition); * // ShowFromPn.VectorFromTo(Pon.transform.localPosition, Pr.transform.localPosition); * } * } * ShowReflectionPlane.XSize = ShowReflectionPlane.ZSize = s; */ #endregion }