/* * Sets a joint point as the new start position. Removes all saved virtual paths and intersections. * */ public void setStartPosition(JointPoint joint) { startJoint = joint; intersections = new List <VirtualIntersection>(); paths = new List <VirtualPath>(); VirtualIntersection intersection = CreateInstance <VirtualIntersection>(); intersection.init(joint.getPosition(), joint, "" + intersections.Count); intersections.Add(intersection); intersection.setWalkingStartPosition(0, joint.getWalkingStartPosition(0)); intersection.setWalkingStartPosition(1, joint.getWalkingStartPosition(1)); intersection.setWalkingStartPosition(2, joint.getWalkingStartPosition(2)); intersection.setWalkingStartPosition(3, joint.getWalkingStartPosition(3)); #if UNITY_EDITOR SceneView.RepaintAll(); AssetDatabase.AddObjectToAsset(intersection, this); EditorUtility.SetDirty(this); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); #endif Debug.Log("Set new start position"); }
/* * Checks if user choosed to walk on the specified path. * */ private bool isPathChosen(VirtualPath path) { int direction = getRedirectionDirection(currentJoint, path.getCurve()); int pathIndex = data.getPathIndex(currentJoint, path.getCurve()); Vector3 directionToCurveCircleCenter = (path.getCurve().getCircleCenter() - currentJoint.getWalkingStartPosition(pathIndex)).normalized; Vector3 rotatedDirectionVector = Quaternion.AngleAxis(-direction * 90f, Vector3.up) * directionToCurveCircleCenter; Plane plane = new Plane(rotatedDirectionVector.normalized, currentJoint.getWalkingStartPosition(pathIndex)); if (plane.GetSide(Camera.main.transform.localPosition)) { return(true); } return(false); }
/* * Calculates walking start positions for new intersection * */ private void calculateWalkingStartPositions(int pathIndex, JointPoint endJointPoint, VirtualIntersection endIntersection, Vector3 circleCenter, Vector3 directionToEndPath) { endIntersection.setWalkingStartPosition(pathIndex, circleCenter + directionToEndPath); Vector3 directionToCurve0 = endJointPoint.getWalkingStartPosition(0) - endJointPoint.getPosition(); Vector3 directionToCurve1 = endJointPoint.getWalkingStartPosition(1) - endJointPoint.getPosition(); Vector3 directionToCurve2 = endJointPoint.getWalkingStartPosition(2) - endJointPoint.getPosition(); Vector3 directionToCurve3 = endJointPoint.getWalkingStartPosition(3) - endJointPoint.getPosition(); float angleBetweenCurves01 = angle360(directionToCurve0, directionToCurve1); float angleBetweenCurves02 = angle360(directionToCurve0, directionToCurve2); float angleBetweenCurves03 = angle360(directionToCurve0, directionToCurve3); float angleBetweenCurves12 = angle360(directionToCurve1, directionToCurve2); float angleBetweenCurves13 = angle360(directionToCurve1, directionToCurve3); float angleBetweenCurves10 = angle360(directionToCurve1, directionToCurve0); float angleBetweenCurves23 = angle360(directionToCurve2, directionToCurve3); float angleBetweenCurves20 = angle360(directionToCurve2, directionToCurve0); float angleBetweenCurves21 = angle360(directionToCurve2, directionToCurve1); float angleBetweenCurves30 = angle360(directionToCurve3, directionToCurve0); float angleBetweenCurves31 = angle360(directionToCurve3, directionToCurve1); float angleBetweenCurves32 = angle360(directionToCurve3, directionToCurve2); Vector3 directionToFirstStartPosition = (circleCenter + directionToEndPath) - endIntersection.getPosition(); Vector3 directionToOtherStartPosition = new Vector3(); switch (pathIndex) { case 0: endIntersection.setWalkingStartPosition(0, circleCenter + directionToEndPath); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves01, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(1, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves02, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(2, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves03, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(3, endIntersection.getPosition() + directionToOtherStartPosition); break; case 1: endIntersection.setWalkingStartPosition(1, circleCenter + directionToEndPath); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves12, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(2, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves13, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(3, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves10, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(0, endIntersection.getPosition() + directionToOtherStartPosition); break; case 2: endIntersection.setWalkingStartPosition(2, circleCenter + directionToEndPath); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves23, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(3, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves20, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(0, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves21, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(1, endIntersection.getPosition() + directionToOtherStartPosition); break; case 3: endIntersection.setWalkingStartPosition(3, circleCenter + directionToEndPath); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves30, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(0, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves31, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(1, endIntersection.getPosition() + directionToOtherStartPosition); directionToOtherStartPosition = Quaternion.AngleAxis(angleBetweenCurves32, Vector3.up) * directionToFirstStartPosition; endIntersection.setWalkingStartPosition(2, endIntersection.getPosition() + directionToOtherStartPosition); break; } }