private void InitControllers()
    {
        if (creature_renderer == null)
        {
            return;
        }

        MeshBone root_bone = GetRootBone();

        runtime_bones = root_bone.getAllChildren();
        child_bodies  = new List <Rigidbody2D>();
        List <Collider2D> bone_colliders = new List <Collider2D>();
        var        parent_obj            = creature_renderer.gameObject;
        Collider2D parent_collider       = parent_obj.GetComponent <Collider2D>();

        if (parent_collider != null)
        {
            if (noCollisions)
            {
                parent_collider.enabled = false;
            }

            foreach (Transform child in transform)
            {
                var cur_body     = child.GetComponent <Rigidbody2D>();
                var cur_collider = child.GetComponent <Collider2D>();

                if ((cur_body != null) && (cur_collider != null))
                {
                    child_bodies.Add(cur_body);
                    bone_colliders.Add(cur_collider);
                    // Turn off collision between bone collider and parent collider
                    Physics2D.IgnoreCollision(cur_collider, parent_collider);
                    if (noCollisions)
                    {
                        cur_collider.enabled = false;
                    }
                }
            }

            // Turn off collisions between bone colliders
            foreach (Collider2D cur_collider1 in bone_colliders)
            {
                foreach (Collider2D cur_collider2 in bone_colliders)
                {
                    if (cur_collider1 != cur_collider2)
                    {
                        Physics2D.IgnoreCollision(cur_collider1, cur_collider2);
                    }
                }
            }
        }
    }
Example #2
0
    public void initCarryBones(MeshBone endeffector_bone)
    {
        if (bones_basis != null)
        {
            // Already bound
            return;
        }

        bones_basis = new List <MeshBoneUtil.Tuple <XnaGeometry.Vector2, XnaGeometry.Vector2> >();
        carry_bones = endeffector_bone.getAllChildren();
        carry_bones.RemoveAt(0); // Remove first end_effector bone, we do not want to carry that

        var base_vec_u = endeffector_bone.getWorldEndPt() - endeffector_bone.getWorldStartPt();

        base_vec_u.Normalize();

        var base_vec_v = new XnaGeometry.Vector4(0, 0, 0, 0);

        base_vec_v.X = -base_vec_u.Y;
        base_vec_v.Y = base_vec_u.X;

        foreach (var cur_bone in carry_bones)
        {
            var basis_start = getFromBasis(
                cur_bone.getWorldStartPt() - endeffector_bone.getWorldStartPt(),
                base_vec_u,
                base_vec_v);

            var basis_end = getFromBasis(
                cur_bone.getWorldEndPt() - endeffector_bone.getWorldStartPt(),
                base_vec_u,
                base_vec_v);

            var basis_pair =
                new MeshBoneUtil.Tuple <XnaGeometry.Vector2, XnaGeometry.Vector2>(basis_start, basis_end);

            bones_basis.Add(basis_pair);
        }
    }
Example #3
0
    public void CreateGameColliders()
    {
        if (creature_renderer == null)
        {
            return;
        }

        if (transform.childCount > 0)
        {
            Debug.Log("Please remove all children game colliders before running CreateGameColliders!");
            return;
        }

        creature_renderer.InitData();
        MeshBone root_bone = GetRootBone();

        // Create a base rigid body that actually participates in simulation for the creature_renderer
        var         parent_obj = creature_renderer.gameObject;
        Rigidbody2D parent_rbd = parent_obj.AddComponent <Rigidbody2D>();

        parentRBD = parent_rbd;
        parent_rbd.isKinematic = false;

        BoxCollider2D parent_collider = parent_obj.AddComponent <BoxCollider2D>();

        parent_collider.size = new UnityEngine.Vector2(simColliderWidth, simColliderHeight);

        // Create rigid bodies for all the bones
        List <MeshBone> all_children = root_bone.getAllChildren();

        foreach (MeshBone cur_child in all_children)
        {
            // Create new GameObject
            GameObject new_obj = new GameObject(cur_child.key);

            // Add components
            XnaGeometry.Vector4 spawn_center = (cur_child.getWorldStartPt() + cur_child.getWorldEndPt()) * 0.5f;
            float colliderWidth           = (float)(cur_child.getWorldRestEndPt() - cur_child.getWorldRestStartPt()).Length();
            XnaGeometry.Vector4 spawn_dir = (cur_child.getWorldRestEndPt() - cur_child.getWorldRestStartPt());
            spawn_dir.Normalize();

            // Get the direction in the renderer's space
            UnityEngine.Vector3 u_spawn_dir = new UnityEngine.Vector3((float)spawn_dir.X, (float)spawn_dir.Y, (float)0);
            u_spawn_dir = TransformToCreatureDir(u_spawn_dir);

            float startAngle = (float)Math.Atan2((double)u_spawn_dir.y, (double)u_spawn_dir.x) * Mathf.Rad2Deg;
            UnityEngine.Vector3 local_pos = new UnityEngine.Vector3((float)spawn_center.X, (float)spawn_center.Y, (float)0);

            Rigidbody2D new_rbd = new_obj.AddComponent <Rigidbody2D>();
            new_rbd.isKinematic = true;
            BoxCollider2D new_collider = new_obj.AddComponent <BoxCollider2D>();
            new_collider.size = new UnityEngine.Vector2(colliderWidth, colliderHeight);

            // set the position using the renderer's parent transform
            new_obj.transform.position = TransformToCreaturePt(local_pos);

            new_obj.transform.rotation = UnityEngine.Quaternion.AngleAxis(startAngle, new UnityEngine.Vector3(0, 0, 1));

            // Make new object child of the parent
            new_obj.transform.parent = gameObject.transform;
        }
    }