i’m trying to make 2D characters with 2D rigidbodies and having hard time with defining real center of mass of all body parts.
I dont use polygon colliders but i know if you use them, they set automatically the center of mass on the correct spot of the sprite. i assume center of mass is calculated with the values of points (vertex) of polygon collider.
Can anyone give me some tips about how can i calculate and assign center of mass of the sprites without using polygon collider?
okay, i discover that there is something called Sprite.vertices . It came with Unity5 and theoretically i should be able to calculate the “real” center of mass of the sprites with that array.
I think i have to add the vertices together and divide them with the total amount of vertices.
when im done, i will post the code.
okay, here is my workaround:
public class CoM : MonoBehaviour {
public Rigidbody2D rb;
public PolygonCollider2D pc;
public Vector2 com;
public Vector2 p_com;
void Awake ()
{
rb = gameObject.GetComponent<Rigidbody2D> ();
com = rb.centerOfMass;
pc = gameObject.AddComponent<PolygonCollider2D> ();
pc =gameObject.GetComponent<PolygonCollider2D>();
p_com = rb.centerOfMass;
Destroy (pc);
rb.centerOfMass = p_com;
}
void Update ()
{
Debug.Log ("before: " + com);
Debug.Log ("with pc: " + p_com);
Debug.Log ("after: " + rb.centerOfMass);
}
}