/// <summary> /// 全オブジェクトを取得して、"Atoms"tag を抜き出す /// </summary> /// <param name="Parent">親とするオブジェクト</param> public void SetAtomsList(GameObject Parent) { List <GameObject> AtomsList = new List <GameObject>(); // 格納する配列 foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) { if (obj.tag == "Atoms") { AtomsList.Add(obj); // Atomsタグがついてるやつ入れる } } // Bondのために2つの分子を比較 for (int i = 0; i < AtomsList.Count; i++) { for (int j = 0; j < i; j++) { // 結合数を取得 atom1 = AtomsList[j].GetComponent <AtomsInfo>(); atom2 = AtomsList[i].GetComponent <AtomsInfo>(); // 結合数が1未満もしくは2つのオブジェクトの親が異なるとき(距離は考慮しない) if (atom1.bondsNum < 1 || atom2.bondsNum < 1) { // 結合させる CalcBond(Parent, AtomsList[j], AtomsList[i]); } } } }
/// <summary> /// 親オブジェクトを渡しているので、子オブジェクトを検索して使う /// </summary> /// <param name="Parent">Moleculeオブジェクト</param> /// <param name="FuncGroup">官能基オブジェクト</param> void BondsFuncGroup(GameObject Parent, GameObject FuncGroup) { AtomsInfo ParentInfo = Parent.GetComponent <AtomsInfo>(); //AtomsInfo FuncInfo = FuncGroup.GetComponent<AtomsInfo>(); //* bool flag = false; // Molecule内の原子分ループ for (int i = 0; i < Parent.transform.childCount; i++) { for (int j = 0; j < FuncGroup.transform.childCount; j++) { if ("Atoms" == Parent.transform.GetChild(i).gameObject.tag&& "Atoms" == FuncGroup.transform.GetChild(j).gameObject.tag) { flag = CalcFuncGroup(Parent.transform.GetChild(i).gameObject, FuncGroup.transform.GetChild(j).gameObject); } if (flag) { CalcBond(Parent, Parent.transform.GetChild(i).gameObject, FuncGroup.transform.GetChild(j).gameObject); break; } } if (flag) { break; } } if (flag) { //ここでFuncGroup内の全てのオブジェクトをParentに移動させる // Parent内の全ての子オブジェクトを取得 // ループごとにFuncGroup.transform.childCountの値が変わってしまったのでこの書き方 for (int i = FuncGroup.transform.childCount - 1; i >= 0; i--) { //Debug.Log(FuncGroup.transform.GetChild(i).gameObject); if ("Atoms" == FuncGroup.transform.GetChild(i).tag) { ParentInfo.childName.Add(FuncGroup.transform.GetChild(i).name); //ParentList.Add(FuncGroup.transform.GetChild(i).gameObject); } FixedJoint fixJoint = FuncGroup.transform.GetChild(i).gameObject.GetComponent <FixedJoint>(); fixJoint.connectedBody = Parent.GetComponent <Rigidbody>(); FuncGroup.transform.GetChild(i).gameObject.transform.parent = Parent.transform; if (0 == i) { Destroy(FuncGroup); } } } //Debug.Log(flag); }
/// <summary> /// 原子と結合の親を作る /// </summary> /// <param name="ParentName">親の名前</param> public void CreateParents(string ParentName) { Parent = new GameObject(ParentName); Parent.tag = "Parent"; DontDestroyOnLoad(Parent); rigidParent = Parent.AddComponent<Rigidbody>(); // 親にRididbody rigidParent.useGravity = false; rigidParent.isKinematic = false; rigidParent.mass = 1.0f; rigidParent.drag = 10.0f; rigidParent.angularDrag = 10.0f; atom = Parent.AddComponent<AtomsInfo>(); }
/// <summary> /// 原子と結合の親を作る /// </summary> /// <param name="ParentName">親の名前</param> public void CreateParents(string ParentName) { Parent = new GameObject(ParentName); Parent.tag = "Parent"; DontDestroyOnLoad(Parent); rigidParent = Parent.AddComponent <Rigidbody>(); // 親にRididbody rigidParent.useGravity = false; rigidParent.isKinematic = false; rigidParent.mass = 1.0f; rigidParent.drag = 10.0f; rigidParent.angularDrag = 10.0f; atom = Parent.AddComponent <AtomsInfo>(); }
public void CreateAtoms(GameObject Parent, string AtomName, Vector3 Vec3) { switch (AtomName) { case "H": obj = Instantiate(H, Vec3, Quaternion.identity) as GameObject; break; case "O": obj = Instantiate(O, Vec3, Quaternion.identity) as GameObject; break; case "C": obj = Instantiate(C, Vec3, Quaternion.identity) as GameObject; break; } //switch (key) //{ // case KeyCode.O: // break; //} obj.name = AtomName; // 名前を変更 obj.transform.parent = null; // 親オブジェクト無しで初期化 DontDestroyOnLoad(obj); // Scene を切り替えても Object を保持 rigid = obj.AddComponent <Rigidbody>(); // Rigidbodyコンポーネントを追加 rigid.isKinematic = false; // 物理計算しない rigid.useGravity = false; // 重力使用しない rigid.mass = 0.1f; // 物体の重さ rigid.drag = 10.0f; // 空気抵抗の大きさ rigid.angularDrag = 10.0f; // 回転の空気抵抗 atom = obj.AddComponent <AtomsInfo>(); atom.bondsNum = 0; // 初期化 // 親子関係付け処理 if (null != Parent) { rigid.mass = 0.0f; FixedJoint fixJoint = obj.AddComponent <FixedJoint>(); fixJoint.connectedBody = Parent.GetComponent <Rigidbody>(); obj.transform.parent = Parent.transform; } }
public void CreateAtoms(GameObject Parent, string AtomName, Vector3 Vec3) { switch (AtomName) { case "H": obj = Instantiate(H, Vec3, Quaternion.identity) as GameObject; break; case "O": obj = Instantiate(O, Vec3, Quaternion.identity) as GameObject; break; case "C": obj = Instantiate(C, Vec3, Quaternion.identity) as GameObject; break; } //switch (key) //{ // case KeyCode.O: // break; //} obj.name = AtomName; // 名前を変更 obj.transform.parent = null; // 親オブジェクト無しで初期化 DontDestroyOnLoad(obj); // Scene を切り替えても Object を保持 rigid = obj.AddComponent<Rigidbody>(); // Rigidbodyコンポーネントを追加 rigid.isKinematic = false; // 物理計算しない rigid.useGravity = false; // 重力使用しない rigid.mass = 0.1f; // 物体の重さ rigid.drag = 10.0f; // 空気抵抗の大きさ rigid.angularDrag = 10.0f; // 回転の空気抵抗 atom = obj.AddComponent<AtomsInfo>(); atom.bondsNum = 0; // 初期化 // 親子関係付け処理 if (null != Parent) { rigid.mass = 0.0f; FixedJoint fixJoint = obj.AddComponent<FixedJoint>(); fixJoint.connectedBody = Parent.GetComponent<Rigidbody>(); obj.transform.parent = Parent.transform; } }
// 向きなどの計算 /// <summary> /// 2つの球オブジェクトの距離を比較し、距離が定数値を下回っていれば、 /// 棒オブジェクトの向きや長さを計算する /// </summary> /// <param name="Parent">親とするオブジェクト</param> /// <param name="obj1">比較する球1つめ</param> /// <param name="obj2">比較する球2つめ</param> public void CalcBond(GameObject Parent, GameObject obj1, GameObject obj2) { // 2つの座標ベクトルの比較 float distance = Vector3.Distance(obj1.transform.position, obj2.transform.position); if (obj1.activeInHierarchy && obj2.activeInHierarchy && // シーン上に存在し、 BOND_JUDGMENT > distance) // 距離が定数値を下回っていれば { //Debug.Log(obj1.name + ", " + obj2.name); //向きを定義 float x, y, z, r, //差分 rad_x, rad_y, rad_z; //3点の角度 Vector3 position; //座標 Quaternion rotation; //回転 //まず向きを決めてから座標の位置を2座標の中心に変更する //http://qiita.com/2dgames_jp/items/60274efb7b90fa6f986a x = obj1.transform.position.x - obj2.transform.position.x; y = obj1.transform.position.y - obj2.transform.position.y; z = obj1.transform.position.z - obj2.transform.position.z; r = Mathf.Sqrt(x * x + y * y + z * z); rad_y = Mathf.Atan2(z, x) * Mathf.Rad2Deg; rad_x = Mathf.Acos(y / r) * Mathf.Rad2Deg; rad_z = 0; position = new Vector3((obj2.transform.position.x + obj1.transform.position.x) / 2, (obj2.transform.position.y + obj1.transform.position.y) / 2, (obj2.transform.position.z + obj1.transform.position.z) / 2); rotation = Quaternion.Euler(rad_z, -rad_y, -rad_x); //rad_xもマイナスにしたら動いた //rotation = Quaternion.Euler(rad_z, -rad_y, rad_x); //動かない CreateBonds(Parent, position, rotation, distance); // 相互に結合数を追加 atom1 = obj1.GetComponent <AtomsInfo>(); atom1.bondsNum += 1; atom2 = obj2.GetComponent <AtomsInfo>(); atom2.bondsNum += 1; } }
// 向きなどの計算 /// <summary> /// 2つの球オブジェクトの距離を比較し、距離が定数値を下回っていれば、 /// 棒オブジェクトの向きや長さを計算する /// </summary> /// <param name="Parent">親とするオブジェクト</param> /// <param name="obj1">比較する球1つめ</param> /// <param name="obj2">比較する球2つめ</param> public void CalcBond(GameObject Parent, GameObject obj1, GameObject obj2) { // 2つの座標ベクトルの比較 float distance = Vector3.Distance(obj1.transform.position, obj2.transform.position); if (obj1.activeInHierarchy && obj2.activeInHierarchy && // シーン上に存在し、 BOND_JUDGMENT > distance) // 距離が定数値を下回っていれば { //Debug.Log(obj1.name + ", " + obj2.name); //向きを定義 float x, y, z, r, //差分 rad_x, rad_y, rad_z; //3点の角度 Vector3 position; //座標 Quaternion rotation; //回転 //まず向きを決めてから座標の位置を2座標の中心に変更する //http://qiita.com/2dgames_jp/items/60274efb7b90fa6f986a x = obj1.transform.position.x - obj2.transform.position.x; y = obj1.transform.position.y - obj2.transform.position.y; z = obj1.transform.position.z - obj2.transform.position.z; r = Mathf.Sqrt(x * x + y * y + z * z); rad_y = Mathf.Atan2(z, x) * Mathf.Rad2Deg; rad_x = Mathf.Acos(y / r) * Mathf.Rad2Deg; rad_z = 0; position = new Vector3((obj2.transform.position.x + obj1.transform.position.x) / 2, (obj2.transform.position.y + obj1.transform.position.y) / 2, (obj2.transform.position.z + obj1.transform.position.z) / 2); rotation = Quaternion.Euler(rad_z, -rad_y, -rad_x); //rad_xもマイナスにしたら動いた //rotation = Quaternion.Euler(rad_z, -rad_y, rad_x); //動かない CreateBonds(Parent, position, rotation, distance); // 相互に結合数を追加 atom1 = obj1.GetComponent<AtomsInfo>(); atom1.bondsNum += 1; atom2 = obj2.GetComponent<AtomsInfo>(); atom2.bondsNum += 1; } }
/// <summary> /// 全オブジェクトを取得して、"Atoms"tag を抜き出す /// </summary> /// <param name="Parent">親とするオブジェクト</param> public void SetAtomsList(GameObject Parent) { List<GameObject> AtomsList = new List<GameObject>(); // 格納する配列 foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) { if (obj.tag == "Atoms") AtomsList.Add(obj); // Atomsタグがついてるやつ入れる } // Bondのために2つの分子を比較 for (int i = 0; i < AtomsList.Count; i++) { for (int j = 0; j < i; j++) { // 結合数を取得 atom1 = AtomsList[j].GetComponent<AtomsInfo>(); atom2 = AtomsList[i].GetComponent<AtomsInfo>(); // 結合数が1未満もしくは2つのオブジェクトの親が異なるとき(距離は考慮しない) if (atom1.bondsNum < 1 || atom2.bondsNum < 1) { // 結合させる CalcBond(Parent, AtomsList[j], AtomsList[i]); } } } }