- Home /
Molecule simulation: make child objects attract each other so parents will stay close together?
Hey guys, I'm making a simple molecule simulation game. So far, I have single atoms wander around until they come across another single atom, then approach each other and form a bond at their respective bonding points, meaning that from this moment on, their bonds (which are child objects to the atom game object) should stick together (attract each other) so the bond won't be broken by the atoms drifting too far away from each other. This would also stabilize molecule chains. And I cannot for the life of me make this work.
The atom game objects have rigid bodies, the bond game objects don't. the Bonding radius and Action radius objects are simply colliders which act as triggers. The bonding point game objects were me trying to make this work by having a transform at which the two bonds should act out their attraction forces after the FormBond() method had completed. Any help is greatly appreciated!
This is my Bond Script in C#:
using UnityEngine;
using System.Collections;
public class Bond : MonoBehaviour {
public Element myElement;
public GameObject myPartnerElement;
public GameObject myPartnerBond;
public GameObject bondingPoint;
public bool hasPartner;
private BondingRadius bondingR;
private Rigidbody rigidBody;
// Use this for initialization
void Start () {
myElement = GetComponentInParent <Element> ();
rigidBody = myElement.GetComponent <Rigidbody> ();
bondingR = GetComponentInChildren <BondingRadius> ();
}
// Update is called once per frame
void Update () {
if (hasPartner) {
AttractPartner ();
}
}
public void FormBond (GameObject otherBond) {
// Debug.Log (this.gameObject.name + " FormBond called");
Bond partnerBond = otherBond.GetComponent<Bond> ();
if (myPartnerBond == null && partnerBond.myPartnerBond == null || myPartnerBond == null && partnerBond.myPartnerBond == this.gameObject) {
myPartnerBond = otherBond;
myPartnerElement = otherBond.GetComponentInParent<Element> ().gameObject;
hasPartner = true;
bondingR.enabled = false;
Debug.Log (this.gameObject.name + " bonded with " + otherBond.name);
// rigidBody.freezeRotation = true;
myElement.UpdateBonds ();
}
}
public void BreakBond () {
Debug.Log (this.gameObject.name + " broke up with " + myPartnerBond.name);
myPartnerBond = null;
myPartnerElement = null;
hasPartner = false;
bondingR.enabled = true;
rigidBody.freezeRotation = false;
myElement.UpdateBonds ();
}
public void AttractPartner () {
if (Vector3.Distance (transform.position, myPartnerBond.transform.position) > 0.3f) {
Debug.Log ("Attracting partner");
// transform.LookAt (myPartnerBond.transform);
// myElement.rigidBody.AddForce ((myPartnerBond.transform.position - transform.position).normalized * 0.005f, ForceMode.Impulse);
rigidBody.AddForceAtPosition ((myPartnerBond.GetComponent<Bond> ().bondingPoint.transform.position - bondingPoint.transform.position).normalized * 0.005f, bondingPoint.transform.position);
// rigidBody.MovePosition (myPartnerBond.transform.position);
}
// this.transform.position = this.transform.position + ((myPartnerBond.transform.position - this.transform.position).normalized * myElement.attractionForce);
}
}
Answer by FortisVenaliter · Apr 11, 2017 at 08:10 PM
I would recommend instantiating a FixedJoint component for the bonds to make the atom rigidbodies stick together. That way, you're not fighting the physics engine, you're working in it's rules, so the two rigidbodies will start working in concert.
thanks for the tip, it did solve the problem I had with the bonds and atoms getting separated from each other :)) However, it didn't quite work out for joining two bonds. I tried a spring joint for that but it's still buggy and does not behave as I expected... Could it be a problem that each bond instantiates a spring joint onto itself at the moment of bonding and connects it to its partner bond? Because for the connection between atom and bond all I used was a fixed joint on the atom referencing the bond, and leaving the bond itself untouched...
Your answer
Follow this Question
Related Questions
Unity 2D - How to calculate weight/strain? 0 Answers
Ignore Force Imparted By Object 0 Answers
How to calculate force to apply to move object to a certain distance. 1 Answer
Model tips over for no reason 1 Answer
Problems making a glider 3 Answers