void OnMouseUp() { BallUpdate script = gameObject.GetComponent <BallUpdate>(); MDDriver mddriverScript = GameObject.FindObjectOfType <MDDriver>(); mddriverScript.applyForces(new int[] { (int)script.number }, new float[] { 0.0f, 0.0f, 0.0f }); maxCamera.cameraStop = false; stopCamera = false; if (arrowParent != null) { GameObject.DestroyImmediate(arrowParent); arrowParent = null; } }
// Use this for initialization // Does something as a response // to the user clicking on the object void OnMouseDrag() { if (arrowParent == null) { arrowParent = new GameObject("Arrow"); GameObject arrow; arrow = GameObject.CreatePrimitive(PrimitiveType.Cube); arrow.transform.parent = arrowParent.transform; arrow.transform.localScale = new Vector3(0.4f, 3.5f, 0.4f); arrow.transform.Translate(arrow.transform.up * 2.0f); arrow.renderer.enabled = true; arrowParent.transform.position = transform.position; arrowParent.transform.parent = transform; } MDDriver mddriverScript = GameObject.FindObjectOfType <MDDriver>(); BallUpdate script = gameObject.GetComponent <BallUpdate>(); if (Input.GetMouseButton(0)) { maxCamera.cameraStop = true; stopCamera = true; Vector3 p = Input.mousePosition; p.z = Camera.main.WorldToScreenPoint(transform.position).z; Vector3 worldCoords = Camera.main.ScreenToWorldPoint(p); Vector3 force = worldCoords - transform.position; mddriverScript.applyForces(new int[] { (int)script.number }, new float[] { force.x, force.y, force.z }); // transform.position = Camera.main.ScreenToWorldPoint(p); float distance = Vector3.Distance(worldCoords, transform.position); float arrowZScale = distance / 8.0f; float arrowScale = distance / 12.0f; arrowParent.transform.up = force; arrowParent.transform.localScale = new Vector3(arrowScale, arrowZScale, arrowScale); } }
void Update() { if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript == null) { mddriverScript = GameObject.FindObjectOfType <MDDriver>(); } if (!listen) { VRPNButtonController btn_ctrl = gameObject.GetComponent <VRPNButtonController>() as VRPNButtonController; if (btn_ctrl != null) { VRPNButtonController.OnButton += OnVRPNButtonEvent; listen = true; } } ffScript = gameObject.GetComponent <VRPNForceFeedback>() as VRPNForceFeedback; if (checkTime) { double diffSecs = System.DateTime.Now.Subtract(lastPressedTime).TotalSeconds; if (diffSecs >= 1.0f) { applyForce = true; checkTime = false; } } // Vector3 direction = -transform.forward; GameObject closestAtom = MoleculeModel.atoms[0] as GameObject; float minDist = Vector3.Distance(closestAtom.transform.position, transform.position); //Find closest atom for (int i = 1; i < MoleculeModel.atoms.Count; i++) { GameObject go = MoleculeModel.atoms[i] as GameObject; float dist = Vector3.Distance(go.transform.position, transform.position); if (dist < minDist) { minDist = dist; closestAtom = go; } } float[] forces = new float[atomNumberList.Count * 3]; Vector3 diff = atomList[0].transform.position - atomList[1].transform.position; Vector3 currentForce = diff.normalized; Vector3 clampedCurrentForce = currentForce * 5.0f; forces[0] = clampedCurrentForce.x; forces[1] = clampedCurrentForce.y; forces[2] = clampedCurrentForce.z; forces[3] = -clampedCurrentForce.x; forces[4] = -clampedCurrentForce.y; forces[5] = -clampedCurrentForce.z; string filename = "umol_2K96_data.txt"; StreamWriter fpsLog = new StreamWriter(filename, true); fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position)); fpsLog.Close(); fpsLog.Dispose(); mddriverScript.applyForces(atomNumberList.ToArray(), forces); for (int i = 0; i < arrowList.Count; i++) { arrowList[i].transform.up = diff; arrowList[i].transform.localScale = new Vector3(1.0f, 10.0f, 1.0f); } if (applyForce) { if (atomList.Count > 0) { Vector3 barycenter = (atomList[0].transform.position + atomList[1].transform.position) / 2; diff = transform.position - barycenter; float distance = diff.magnitude; // Vector3 d2 = GameObject.FindGameObjectWithTag("LoadBox").transform.worldToLocalMatrix * diff; // // Interactive mode // for (int i = 0; i < atomList.Count; i++) { // if (atomList[i].rigidbody != null) { // atomList[i].rigidbody.AddForce(diff, ForceMode.Impulse); // } // } ffScript.setLinearForceForVector(-diff); if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript != null) { forces = new float[atomNumberList.Count * 3]; diff = atomList[0].transform.position - atomList[1].transform.position; currentForce = diff.normalized; // currentForce *= Mathf.Sqrt(distance); currentForce *= 1 / Mathf.Pow((1 + 0.5f * Mathf.Exp(-3 * distance)), 2); // currentForce *= 1 / Mathf.Pow(1 + Mathf.Exp(-(distance - 4)), 2); //Vector3 clampedCurrentForce = currentForce * distance / 4.0f; clampedCurrentForce = currentForce * 10.0f; for (int i = 0; i < atomNumberList.Count; i++) { forces[i] = clampedCurrentForce.x; forces[i + 1] = clampedCurrentForce.y; forces[i + 2] = clampedCurrentForce.z; } // forces[0] = clampedCurrentForce.x; // forces[1] = clampedCurrentForce.y; // forces[2] = clampedCurrentForce.z; // // forces[3] = -clampedCurrentForce.x; // forces[4] = -clampedCurrentForce.y; // forces[5] = -clampedCurrentForce.z; // filename = "umol_2K96_data.txt"; // fpsLog = new StreamWriter(filename, true); // fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position)); // fpsLog.Close(); // fpsLog.Dispose(); if (forceApplied == false) { for (int i = 0; i < arrowList.Count; i++) { arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = true; } forceApplied = true; } float arrowZScale = distance / 10.0f; float arrowScale = distance / 20.0f; for (int i = 0; i < arrowList.Count; i++) { arrowList[i].transform.up = diff; arrowList[i].transform.localScale = new Vector3(arrowScale, arrowZScale, arrowScale); } mddriverScript.applyForces(atomNumberList.ToArray(), forces); } } } else { if (forceApplied == true) { for (int i = 0; i < arrowList.Count; i++) { arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = false; arrowList[i].transform.localScale = new Vector3(0.4f, 3.5f, 0.4f); } forces = new float[atomNumberList.Count * 3]; for (int i = 0; i < atomNumberList.Count; i++) { forces[i] = 0.0f; forces[i + 1] = 0.0f; forces[i + 2] = 0.0f; } mddriverScript.applyForces(atomNumberList.ToArray(), forces); mddriverScript.resetForces(); forceApplied = false; } // GameObject obj = CastRayFromPicker (distance); GameObject obj = closestAtom; // If there is no raycasted object or the object is not an atom, we reset the state variables and display // if (obj == null || (obj.GetComponent<BallUpdateHB>() == null)) { if (minDist > 8.0f) { Reset(); ffScript.resetForce(); } else { // The user pressed the button, so we perform the selection and set se selection variable to false if (selectCurrentAtom) { Select(); selectCurrentAtom = false; } // Set currently pointed atom and associated halo vars Set(obj); } if (ffScript != null) { ffScript.setForceForAtomPosition(closestAtom.transform.position); } // Debug.Log ("Calling setForceForAtomPosition"); // ffScript.setForceForAtomPosition(closestAtom.transform.position); } }