Exemple #1
0
    XnaGeometry.Vector4 RotateVec2D(XnaGeometry.Vector4 vec_in, double angle)
    {
        var ret_vec = new XnaGeometry.Vector4(0, 0, 0, 1);

        ret_vec.X = vec_in.X * Math.Cos(angle) - vec_in.Y * Math.Sin(angle);
        ret_vec.Y = vec_in.Y * Math.Cos(angle) + vec_in.X * Math.Sin(angle);

        return(ret_vec);
    }
Exemple #2
0
    XnaGeometry.Vector2 getFromBasis(
        XnaGeometry.Vector4 pt_in,
        XnaGeometry.Vector4 base_vec_u,
        XnaGeometry.Vector4 base_vec_v)
    {
        XnaGeometry.Vector2 basis = new XnaGeometry.Vector2(0, 0);
        basis.X = XnaGeometry.Vector4.Dot(pt_in, base_vec_u);
        basis.Y = XnaGeometry.Vector4.Dot(pt_in, base_vec_v);

        return(basis);
    }
    public void debugDrawBones(MeshBone bone_in)
    {
        XnaGeometry.Vector4 pt1 = bone_in.world_start_pt;
        XnaGeometry.Vector4 pt2 = bone_in.world_end_pt;

        Debug.DrawLine(new Vector3((float)pt1.X, (float)pt1.Y, 0),
                       new Vector3((float)pt2.X, (float)pt2.Y, 0), Color.white);

        foreach (MeshBone cur_child in bone_in.children)
        {
            debugDrawBones(cur_child);
        }
    }
Exemple #4
0
    private void UpdateGameColliders()
    {
        int          i = 0;
        bool         is_dir_flipped = false;
        const double flip_cutoff    = 10.0;

        if (Math.Abs(creature_renderer.transform.rotation.eulerAngles.y) > flip_cutoff)
        {
            is_dir_flipped = true;
        }

        if (parentRBD)
        {
            parentRBD.isKinematic = noGravity;
        }

        foreach (var cur_body in child_bodies)
        {
            var cur_bone = runtime_bones[i];
            XnaGeometry.Vector4 spawn_center = (cur_bone.getWorldStartPt() + cur_bone.getWorldEndPt()) * 0.5f;
            XnaGeometry.Vector4 spawn_dir    = (cur_bone.getWorldEndPt() - cur_bone.getWorldStartPt());
            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 setAngle = -(float)Math.Atan2((double)u_spawn_dir.y, (double)u_spawn_dir.x) * Mathf.Rad2Deg;

            /*
             *          if(is_dir_flipped)
             *          {
             *                  setAngle = -setAngle;
             *          }
             *          else {
             *                  setAngle = -setAngle;
             *          }
             */

            if (cur_body != null)
            {
                cur_body.MoveRotation(setAngle);
                UnityEngine.Vector3 local_pos = new UnityEngine.Vector3((float)spawn_center.X, (float)spawn_center.Y, (float)0);
                cur_body.MovePosition(TransformToCreaturePt(local_pos));
            }

            i++;
        }
    }
Exemple #5
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);
        }
    }
Exemple #6
0
    public void poseCarryBones(MeshBone endeffector_bone)
    {
        int i = 0;

        foreach (var cur_bone in carry_bones)
        {
            var basis_pair = bones_basis[i];
            var tmp_vec    = endeffector_bone.getWorldEndPt() - endeffector_bone.getWorldStartPt();
            var base_vec_u = new XnaGeometry.Vector2(tmp_vec.X, tmp_vec.Y);
            base_vec_u.Normalize();

            var base_vec_v = new XnaGeometry.Vector2(0, 0);
            base_vec_v.X = -base_vec_u.Y;
            base_vec_v.Y = base_vec_u.X;

            var set_startpt = new XnaGeometry.Vector4(0, 0, 0, 1);
            var set_endpt   = new XnaGeometry.Vector4(0, 0, 0, 1);

            var calcVec = new XnaGeometry.Vector2(0, 0);
            calcVec = basis_pair.Item1.X * base_vec_u + basis_pair.Item1.Y * base_vec_v;

            set_startpt.X = calcVec.X;
            set_startpt.Y = calcVec.Y;
            set_startpt  += endeffector_bone.getWorldStartPt();
            set_startpt.W = 1;

            calcVec     = basis_pair.Item2.X * base_vec_u + basis_pair.Item2.Y * base_vec_v;
            set_endpt.X = calcVec.X;
            set_endpt.Y = calcVec.Y;
            set_endpt  += endeffector_bone.getWorldStartPt();
            set_endpt.W = 1;

            cur_bone.setWorldStartPt(set_startpt);
            cur_bone.setWorldEndPt(set_endpt);

            i++;
        }
    }
        public void AlterBonesByAnchor(Dictionary<string, MeshBoneUtil.MeshBone> bones_map, String animation_name_in)
        {
            if(target_creature.anchor_points_active == false)
            {
                return;
            }

            var anchor_point = target_creature.GetAnchorPoint(animation_name_in);
            var anchor_vector =  new XnaGeometry.Vector4(anchor_point.X, anchor_point.Y, 0, 0);
            foreach(var cur_data in bones_map)
            {
                var cur_bone = cur_data.Value;
                var start_pt = cur_bone.getWorldStartPt();
                var end_pt = cur_bone.getWorldEndPt();

                start_pt -= anchor_vector;
                end_pt -= anchor_vector;

                cur_bone.setWorldStartPt(start_pt);
                cur_bone.setWorldEndPt(end_pt);
            }
        }
Exemple #8
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;
        }
    }