public override void UpdateContactPoints() { float cubeHalfEdge = 0.125f; mContactPointsInLocalSpace.Clear(); Vector3 direction1 = transform.position - mChildObjects[2].transform.position; direction1.Normalize(); Vector3 localspaceDirection1 = transform.InverseTransformDirection(direction1); ArithemeticObject childObject = mChildObjects[0]; Vector3 direction2 = childObject.mChildObjects[0].transform.position - childObject.mChildObjects[2].transform.position; direction2.Normalize(); Vector3 localspaceDirection2 = transform.InverseTransformDirection(direction2); if (Vector3.Dot(localspaceDirection1, Vector3.forward) == 0 && Vector3.Dot(localspaceDirection2, Vector3.forward) == 0) { mContactPointsInLocalSpace.Add(new Vector3(0, 0, cubeHalfEdge)); mContactPointsInLocalSpace.Add(new Vector3(0, 0, -cubeHalfEdge)); } if (Vector3.Dot(localspaceDirection1, Vector3.right) == 0 && Vector3.Dot(localspaceDirection2, Vector3.right) == 0) { mContactPointsInLocalSpace.Add(new Vector3(-cubeHalfEdge, 0, 0)); mContactPointsInLocalSpace.Add(new Vector3(cubeHalfEdge, 0, 0)); } if (Vector3.Dot(localspaceDirection1, Vector3.up) == 0 && Vector3.Dot(localspaceDirection2, Vector3.up) == 0) { mContactPointsInLocalSpace.Add(new Vector3(0, cubeHalfEdge, 0)); mContactPointsInLocalSpace.Add(new Vector3(0, -cubeHalfEdge, 0)); } }
void UpdateObjectSelected() { if (mSelectedObject == null && Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitinfo; if (Physics.Raycast(ray, out hitinfo, float.MaxValue, arithMeticObjectLayerMask)) { GameObject hitObj = hitinfo.transform.gameObject; Debug.LogWarning(" object is hit"); ArithemeticObject arObj = hitObj.GetComponent <ArithemeticObject>(); mSelectedObject = arObj.GetArithemeticObjectParent(); GameObject parentObject = mSelectedObject.gameObject; parentObject.layer = ignoreRaycastLayer; SetLayerRecursively(parentObject, ignoreRaycastLayer); Debug.LogWarning("Object has been selected"); PopulateAttachableObjectsForType(mSelectedObject); //populate attachable objects here as per the type selected. if (mSelectedObject.mIsComplete) { mIsDraggingAttachable = true; } else { mIsDraggingAttachable = false; } } } }
public virtual void Attach(ArithemeticObject obj, Vector3 contactPoint) { Vector3 direction = (contactPoint - transform.position); direction.Normalize(); Vector3 newPosition = contactPoint + direction * 0.125f; obj.transform.position = newPosition; obj.transform.parent = transform; mChildObjects.Add(obj); //now offset the parent and children so that the parent is in the middle again. Vector3 newParentPosition = GetCenterPositionForObject(); Vector3 positionOffset = newParentPosition - transform.position; OffsetAll(-positionOffset); transform.position = newParentPosition; if (mChildObjects.Count == 2) { Vector3 contactPointBehind = contactPoint + -direction * 2 * 0.125f; mContactPointsInLocalSpace.Add(transform.InverseTransformPoint(contactPointBehind)); Vector3 contactPointFront = contactPoint + direction * 2 * 0.125f; mContactPointsInLocalSpace.Add(transform.InverseTransformPoint(contactPointFront)); } else { Vector3 contactPointInLocalSpace = transform.InverseTransformPoint(contactPoint); int index = mContactPointsInLocalSpace.FindIndex((point) => point == contactPointInLocalSpace); Vector3 contactPointNew = contactPoint + direction * 2 * 0.125f; mContactPointsInLocalSpace[index] = transform.InverseTransformPoint(contactPointNew); } }
void PopulateAttachableObjectsForType(ArithemeticObject arObj) { mAttachableObjects.Clear(); if (arObj.GetType() == typeof(UnitsObject)) { mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(UnitsObject))); mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(TensObject) && obj.mIsComplete == false)); } else if (arObj.GetType() == typeof(TensObject)) { mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(TensObject))); mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(HundrededObject) && obj.mIsComplete == false)); } else if (arObj.GetType() == typeof(HundrededObject)) { mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(HundrededObject))); mAttachableObjects.AddRange(mAllObjects.FindAll((obj) => obj.GetType() == typeof(ThousandsObject) && obj.mIsComplete == false)); } mAttachableObjects.Remove(arObj); //make sure you don't do any proximity checks against the selected object itself. }
// public enum EArithemeticObjectType // [TODO] take this out and replace it with inheritance, there should be a different class for each type of object // { // UNIT, // UNITSROD, // TEN, // TENSPLATE, // HUNDRED, // HUNDREDSCUBE, // THOUSAND, // THOUSANDSROD // // } protected virtual void Start() { // if(mType == EArithemeticObjectType.UNIT) // { // float cubeHalfEdge = 0.5f; // mContactPointsInLocalSpace.Add(new Vector3(0,0,cubeHalfEdge)); // mContactPointsInLocalSpace.Add(new Vector3(0,0,-cubeHalfEdge)); // mContactPointsInLocalSpace.Add(new Vector3(-cubeHalfEdge,0,0)); // mContactPointsInLocalSpace.Add(new Vector3(cubeHalfEdge,0,0)); // mContactPointsInLocalSpace.Add(new Vector3(0,cubeHalfEdge,0)); // mContactPointsInLocalSpace.Add(new Vector3(0,-cubeHalfEdge,0)); // } // else if(mType == EArithemeticObjectType.TEN || mType == EArithemeticObjectType.HUNDRED) // { // ArithemeticObject[] childArray = transform.GetComponentsInChildren<ArithemeticObject> (); // mChildObjects.AddRange (childArray); // UpdateContactPoints (); // } if (transform.parent != null) { parentArithemticObject = transform.parent.gameObject.GetComponent <ArithemeticObject>(); } }
void UpdateObjectDropped() { if (mSelectedObject != null && Input.GetMouseButtonUp(0)) { Debug.LogWarning(" Releasing the selected object "); if (mIsDraggingAttachable) { mIsDraggingAttachable = false; //mSelectedObject.collider.enabled = true; List <Vector3> contactPointsInSelObj = mSelectedObject.GetContactPoints(); float leastDistance = float.MaxValue; ArithemeticObject nearestObj = null; Vector3 nearestContactPoint = Vector3.zero; foreach (ArithemeticObject arObj in mAttachableObjects) { List <Vector3> contactPoints = arObj.GetContactPoints(); foreach (Vector3 contactPoint in contactPoints) { foreach (Vector3 contactPointInSelObj in contactPointsInSelObj) { float distance = Vector3.Distance(contactPoint, contactPointInSelObj); if (distance <= THRESHOLD_FORATTACH && distance < leastDistance) { nearestObj = arObj; leastDistance = distance; nearestContactPoint = contactPoint; } } } } if (nearestObj != null && nearestObj.GetType() == typeof(UnitsObject)) { //create a parent, and attach units to it. GameObject unitsRod = new GameObject("TensObj", typeof(TensObject)); unitsRod.layer = LayerMask.NameToLayer("ArithemticObject"); ArithemeticObject unitsObj = unitsRod.GetComponent <ArithemeticObject> (); unitsRod.transform.parent = arithemeticObjectParent; unitsRod.transform.position = nearestObj.transform.position; unitsObj.Attach(nearestObj); unitsObj.Attach(mSelectedObject, nearestContactPoint); nearestObj.SetArithemeticObjectParent(unitsObj); mSelectedObject.SetArithemeticObjectParent(unitsObj); mAllObjects.Remove(nearestObj); mAllObjects.Remove(mSelectedObject); mAllObjects.Add(unitsObj); } else if (nearestObj != null && (nearestObj.GetType() == typeof(TensObject) || nearestObj.GetType() == typeof(HundrededObject)) && nearestObj.mIsComplete == false) { nearestObj.Attach(mSelectedObject, nearestContactPoint); mSelectedObject.SetArithemeticObjectParent(nearestObj); mAllObjects.Remove(mSelectedObject); if (nearestObj.GetChildCount() == 10) //this one is complete , make it into a next denomimation object { nearestObj.UpdateContactPoints(); nearestObj.mIsComplete = true; } } else if (nearestObj != null && nearestObj.GetType() == typeof(TensObject)) { GameObject tensPlate = new GameObject("HundredsObj", typeof(HundrededObject)); tensPlate.layer = LayerMask.NameToLayer("ArithemticObject"); ArithemeticObject tensObj = tensPlate.GetComponent <ArithemeticObject> (); tensPlate.transform.parent = arithemeticObjectParent; mAllObjects.Add(tensObj); tensPlate.transform.position = nearestObj.transform.position; tensObj.Attach(nearestObj); tensObj.Attach(mSelectedObject, nearestContactPoint); nearestObj.SetArithemeticObjectParent(tensObj); mSelectedObject.SetArithemeticObjectParent(tensObj); mAllObjects.Remove(nearestObj); mAllObjects.Remove(mSelectedObject); } else if (nearestObj != null && nearestObj.GetType() == typeof(HundrededObject)) { GameObject tensPlate = new GameObject("ThousandsObj", typeof(ThousandsObject)); tensPlate.layer = LayerMask.NameToLayer("ArithemticObject"); ArithemeticObject tensObj = tensPlate.GetComponent <ArithemeticObject> (); tensPlate.transform.parent = arithemeticObjectParent; mAllObjects.Add(tensObj); tensPlate.transform.position = nearestObj.transform.position; tensObj.Attach(nearestObj); tensObj.Attach(mSelectedObject, nearestContactPoint); nearestObj.SetArithemeticObjectParent(tensObj); mSelectedObject.SetArithemeticObjectParent(tensObj); mAllObjects.Remove(nearestObj); mAllObjects.Remove(mSelectedObject); } } mSelectedObject.gameObject.layer = arithMeticObjectLayer; SetLayerRecursively(mSelectedObject.gameObject, arithMeticObjectLayer); mSelectedObject = null; } }
public virtual void Attach(ArithemeticObject obj) { //this should be used only when this is the first child to be added to the parent mChildObjects.Add(obj); obj.transform.parent = transform; }
public void SetArithemeticObjectParent(ArithemeticObject parent) { parentArithemticObject = parent; }