// Referring to the children of the link. private void DeleteObject() { GameObject selected = GameObject.Find("Plane").GetComponent <ButtonStateManager>().SelectedObject; if (selected.GetComponent <RobotLink>() != null) { List <Transform> childrenTransforms = new List <Transform>(); foreach (Transform child in selected.transform) { childrenTransforms.Add(child); } // If it's a leaf and no children if (childrenTransforms.Count == 0) { selected.GetComponent <RobotLink>().ParentJoint.GetComponent <ObjectJoint>().ChildLink = null; Destroy(selected); } // If it has children else { ObjectJoint parentJoint = selected.GetComponent <RobotLink>().ParentJoint.GetComponent <ObjectJoint>(); parentJoint.ChildLink = null; parentJoint.ChildJoints = null; Destroy(selected); } } // If it's a joint. // Technically, it's just detaching, but atm I don't know how to or where to place the new "joint" so I'm just going to leave as is. else { selected.transform.parent = null; selected.GetComponent <ObjectJoint>().ParentJoint = null; } }
/// <summary> /// GenerateJoint: Helper method to recreate joints by: /// 1. Creating the appropriate GameObject /// 2. Setting its position properties. /// 3. Setting misc. properties as well as removing the collider. /// /// <param name="jointConfig"> A protobuf object holding all of the necessary information to recreate a joint. </param> /// /// <returns> A GameObject representing a joint on a robot. </returns> /// </summary> public static GameObject GenerateJoint(JointStorage jointConfig) { // Creating actual joint GameObject newJoint = GameObject.CreatePrimitive(PrimitiveType.Sphere); ObjectJoint newObjectJoint = newJoint.AddComponent <ObjectJoint>(); // Putting into right position newJoint.transform.position = new Vector3(jointConfig.PositionParams[0], jointConfig.PositionParams[1], jointConfig.PositionParams[2]); newJoint.transform.rotation = new Quaternion(jointConfig.RotationParams[0], jointConfig.RotationParams[1], jointConfig.RotationParams[2], jointConfig.RotationParams[3]); // Setting ObjectJoint Configs newObjectJoint.RotateAxis = new Vector3(jointConfig.AxisParams[0], jointConfig.AxisParams[1], jointConfig.AxisParams[2]); newObjectJoint.AxisRotation = jointConfig.AxisParams[3]; newObjectJoint.ChildJointIDs = jointConfig.ChildrenJoints; newObjectJoint.ChildLinkID = jointConfig.ChildrenLink; newObjectJoint.ParentLinkID = jointConfig.ParentLink; newObjectJoint.ChildJoints = new List <GameObject>(); Rigidbody newRigid = newJoint.AddComponent <Rigidbody>(); newRigid.isKinematic = true; newRigid.useGravity = false; Destroy(newJoint.GetComponent <Collider>()); return(newJoint); }
/// <summary> /// Attach: Attaches the currently selected objet to the nearest available link/joint depending on what is selected. /// </summary> private void Attach() { GameObject selected = GameObject.Find("Plane").GetComponent <SelectorManagerScript>().selected; // Checking if object is selected if (selected != null) { // If selected object is a link if (selected.GetComponent <RobotLink>() != null && !selected.GetComponent <ClickerTest>().IsLocked) { GameObject closestJoint = GetClosestJoint(selected); // If there a joint exists, link it to the closest one. if (closestJoint != null) { // ObjectJoint/RobotLink procedures closestJoint.GetComponent <ObjectJoint>().ChildLink = selected; selected.GetComponent <RobotLink>().ParentJoint = closestJoint; selected.transform.parent = closestJoint.transform; // "Locking" the object selected.GetComponent <ClickerTest>().IsLocked = true; closestJoint.GetComponent <ClickerTest>().IsLocked = true; } else { Debug.Log("You need a Joint to attach the Link to!"); } } // If selected object is a joint else if (selected.GetComponent <ObjectJoint>() != null) { GameObject closestLink = GetClosestLink(selected); // If there is a link, attach it to the closest one. if (closestLink != null) { ObjectJoint thisJoint = selected.GetComponent <ObjectJoint>(); // Doing ObjectJoint/RobotLink stuff thisJoint.ParentJoint = closestLink.GetComponent <RobotLink>().ParentJoint; thisJoint.ParentJoint.GetComponent <ObjectJoint>().ChildJoints.Add(selected); thisJoint.ParentLink = closestLink; selected.transform.parent = closestLink.transform; // "Locking" the object selected.GetComponent <ClickerTest>().IsRotationLocked = true; } else { Debug.Log("You need a Link to attach the Joint to!"); } } } }
/// <summary> /// SetJoints: Sets the new positions for all joints of the robot. /// /// <param name="newJoints"> A PositionList holding all of the new positions of the joints. </param> /// <param name="rootObject"> The root joint GameObject. </param> /// </summary> public static void SetJoints(PositionList newJoints, GameObject rootObject) { foreach (KeyValuePair <int, PositionStorage> pair in newJoints.PList) { GameObject joint = rootObject.GetComponent <ObjectJoint>().ReferenceDict[pair.Key]; ObjectJoint jointComponent = joint.GetComponent <ObjectJoint>(); jointComponent.transform.Rotate(jointComponent.RotateAxis, pair.Value.Rotation); jointComponent.AxisRotation = pair.Value.Rotation; jointComponent.AxisRotation = pair.Value.Velocity; } }
/// <summary> /// CreateDict: Given the root joint, assigns all of the positions of the joints in the joint's dictionary. /// /// <param name="rootJoint"> The joint to store the dictioary in. </param> /// <param name="defaultDict"> The dictionary to store the PositionStorages in. </param> /// /// <returns> The newly generated ObjectSpecs object. </returns> /// </summary> public static void CreateDict(GameObject rootJoint, IDictionary <int, PositionStorage> defaultDict) { ObjectJoint JointObject = rootJoint.GetComponent <ObjectJoint>(); foreach (KeyValuePair <int, GameObject> pair in JointObject.ReferenceDict) { PositionStorage newStorage = new PositionStorage(); newStorage.Rotation = pair.Value.GetComponent <ObjectJoint>().AxisRotation; newStorage.Velocity = pair.Value.GetComponent <ObjectJoint>().LocalVelocity; defaultDict[pair.Key] = newStorage; } }
private void Update() { //For testing purposes-- > sends joint information then gets updated joint information, updates the information. if (Input.GetKeyDown(KeyCode.Space)) { testList.PList[0].Rotation = 30; testList.PList[1].Rotation = 60; ObjectJoint.GetJoints(rootObject.GetComponent <ObjectJoint>().ChildObjectJoints, rootObject); ObjectJoint.SetJoints(testList, rootObject); Debug.Log("Complete!"); } }
private void AttachObjects() { GameObject selected = GameObject.Find("Plane").GetComponent <ButtonStateManager>().SelectedObject; if (selected != null) { if (selected.GetComponent <RobotLink>() != null) { GameObject closestJoint = GetClosestJoint(selected); if (closestJoint != null) { closestJoint.GetComponent <ObjectJoint>().ChildLink = selected; selected.GetComponent <RobotLink>().ParentJoint = closestJoint; selected.transform.parent = closestJoint.transform; foreach (Transform child in selected.transform) { if (child.name == "Transform Tool(Clone)") { child.GetComponent <TransformTool>().ObjectConnected = true; } } } else { Debug.Log("You need a Joint to attach the Link to!"); } } else if (selected.GetComponent <ObjectJoint>() != null) { GameObject closestLink = GetClosestLink(selected); if (closestLink != null) { ObjectJoint thisJoint = selected.GetComponent <ObjectJoint>(); thisJoint.ParentJoint = closestLink.GetComponent <RobotLink>().ParentJoint; thisJoint.ParentJoint.GetComponent <ObjectJoint>().ChildJoints.Add(selected); thisJoint.ParentLink = closestLink; selected.transform.parent = closestLink.transform; } else { Debug.Log("You need a Link to attach the Joint to!"); } } } }
/// <summary> /// SpawnJoint: Spawns a new joint object. /// </summary> public static GameObject SpawnJoint() { Vector3 SpawnPosition = new Vector3(0.05f, 0f, -5.8f); GameObject newJoint = GameObject.CreatePrimitive(PrimitiveType.Sphere); newJoint.transform.position = SpawnPosition; ObjectJoint JointComponent = newJoint.AddComponent <ObjectJoint>(); JointComponent.IsRoot = true; return(newJoint); // Also need to set the rotation axis somehow? }
// Given the root joint, creates a position list to be sent over via protobuf. public void CreateList(GameObject rootJoint, List <PositionStorage> defaultList = null, bool isRoot = true) { List <GameObject> children = rootJoint.GetComponent <ObjectJoint>().ChildJoints; ObjectJoint thisJoint = rootJoint.GetComponent <ObjectJoint>(); PositionStorage newStorage = new PositionStorage(); newStorage.Rotation = thisJoint.AxisRotation; defaultList.Add(newStorage); foreach (GameObject joint in children) { CreateList(joint, defaultList, false); } }
/// <summary> /// GenerateRobot: Recursively sets transform parent/child properties. /// /// <param name="root"> A protobuf object holding all of the necessary information to recreate a robot. </param> /// <param name="jointDict"> A dictionary mapping jointIDs to their joint GameObjects</param> /// <param name="linkDict"> A dictionary mapping linkIDs to their link GameObjects</param> /// <param name="_isRoot"> An optional boolean indicating whether an object is the root joint or not</param> /// </summary> public static void SetTransforms(GameObject root, Dictionary <int, GameObject> jointDict, Dictionary <int, GameObject> linkDict, bool _isRoot = false) { // Setting up the reference dict here, should only be run in the beginning. if (_isRoot) { foreach (KeyValuePair <int, GameObject> pair in jointDict) { root.GetComponent <ObjectJoint>().ReferenceDict.Add(pair.Key, pair.Value); } } ObjectJoint rootJoint = root.GetComponent <ObjectJoint>(); if (rootJoint.ChildLinkID != 0) { rootJoint.ChildLink = linkDict[rootJoint.ChildLinkID]; rootJoint.ChildLink.transform.parent = rootJoint.transform; } foreach (int ID in rootJoint.ChildJointIDs) { if (ID != 0) { rootJoint.ChildJoints.Add(jointDict[ID]); jointDict[ID].transform.parent = rootJoint.transform; } } if (rootJoint.ParentLinkID != 0) { GameObject ScaleProtection = new GameObject(); ScaleProtection.name = "ScaleProtection"; rootJoint.ParentLink = linkDict[rootJoint.ParentLinkID]; ScaleProtection.transform.parent = rootJoint.ParentLink.transform; rootJoint.transform.parent = ScaleProtection.transform; } foreach (int ID in rootJoint.ChildJointIDs) { if (ID != 0) { SetTransforms(jointDict[ID], jointDict, linkDict); } } }
private void Update() { // This is for first movement in the sequence if (Input.GetKeyDown(KeyCode.A)) { testList.PList[4].Rotation = -45; testList.PList[5].Rotation = -45; testList.PList[8].Rotation = 90; testList.PList[9].Rotation = 90; ObjectJoint.SetJoints(testList, GameObject.Find("Sphere")); } // This is for the second movement in the sequence (repeate this and 3) if (Input.GetKeyDown(KeyCode.S)) { testList.PList[4].Rotation = 45; testList.PList[5].Rotation = 45; testList.PList[8].Rotation = -90; testList.PList[9].Rotation = -90; testList.PList[2].Rotation = -45; testList.PList[3].Rotation = -45; testList.PList[6].Rotation = 90; testList.PList[7].Rotation = 90; ObjectJoint.SetJoints(testList, GameObject.Find("Sphere")); } // Third movement in the sequence if (Input.GetKeyDown(KeyCode.D)) { testList.PList[4].Rotation = -45; testList.PList[5].Rotation = -45; testList.PList[8].Rotation = 90; testList.PList[9].Rotation = 90; testList.PList[2].Rotation = 45; testList.PList[3].Rotation = 45; testList.PList[6].Rotation = -90; testList.PList[7].Rotation = -90; GameObject root = GameObject.Find("Sphere"); root.transform.Translate(Vector3.left * Time.deltaTime * 100); ObjectJoint.SetJoints(testList, GameObject.Find("Sphere")); } #if ServerTesting // Configuring the robot with server test if (Input.GetKeyDown(KeyCode.A)) { RobotStructure test = ClientUDP <RobotStructure> .UDPSend(8888, testStructure); ConstructionManager.GenerateRobot(test); ObjectJoint.GetJoints(GameObject.Find("Sphere").GetComponent <ObjectJoint>().ChildObjectJoints, GameObject.Find("Sphere")); } // Moving the thing if (Input.GetKeyDown(KeyCode.Space)) { PositionListCreator.CreateList(GameObject.Find("Sphere"), testList.PList); PositionList newList = ClientUDP <PositionList> .UDPSend(7777, testList); ObjectJoint.SetJoints(newList, GameObject.Find("Sphere")); } #endif }
void Update() { if (_material != null) { // The target color for the Interaction object will be determined by various simple state checks. Color targetColor = defaultColor; if (_intObj.isHovered && useHover) { float glow = _intObj.closestHoveringControllerDistance.Map(0F, 0.2F, 1F, 0.0F); targetColor = Color.Lerp(defaultColor, hoverColor, glow); } // On press if (_intObj is InteractionButton && (_intObj as InteractionButton).isPressed) { if (!_isPushed) { _isPushed = true; switch (ButtonChoice) { case ButtonType.SpawnLink: GameObject newLink = RobotLink.SpawnLink(); GameObject Linktool = SpawnTransformTool(newLink); LinkIDCount += 1; newLink.GetComponent <RobotLink>().SelfID = LinkIDCount; newLink.transform.localScale = spawnScale; newLink.transform.position = spawnLocation; break; case ButtonType.SpawnJoint: GameObject newJoint = ObjectJoint.SpawnJoint(); GameObject Jointtool = SpawnTransformTool(newJoint); JointIDCount += 1; newJoint.GetComponent <ObjectJoint>().SelfID = JointIDCount; newJoint.transform.localScale = spawnScale; newJoint.transform.position = spawnLocation; break; case ButtonType.Attach: AttachObjects(); break; case ButtonType.Delete: DeleteObject(); break; } targetColor = pressedColor; } } // For the button cooldown if (_intObj is InteractionButton && !(_intObj as InteractionButton).isPressed) { _isPushed = false; } // Lerp actual material color to the target color. _material.color = Color.Lerp(_material.color, targetColor, 30F * Time.deltaTime); } }
private void SpawnJoint() { GameObject newJoint = ObjectJoint.SpawnJoint(); newJoint.AddComponent <ClickerTest>(); }