Example #1
0
        private V2DInstance GetV2DInstance(Instance2D inst)
        {
            V2DInstance result = new V2DInstance();

            result.DefinitionName = inst.DefinitionName;
            result.Density        = inst.Density;
            result.Depth          = inst.Depth;
            result.Friction       = inst.Friction;
            result.InstanceName   = inst.InstanceName;
            //result.Joints = inst.Joints;
            DDW.Vex.Matrix m = inst.Matrix;
            //result.Matrix = new V2DMatrix(m.ScaleX, m.Rotate0, m.Rotate1, m.ScaleY, m.TranslateX, m.TranslateY);
            result.Restitution = inst.Restitution;
            result.StartFrame  = inst.StartFrame;
            result.EndFrame    = inst.TotalFrames + inst.StartFrame;
            result.Visible     = inst.Visible;

            result.Alpha      = inst.Alpha;
            result.X          = inst.X;
            result.Y          = inst.Y;
            result.Rotation   = inst.Rotation;
            result.ScaleX     = inst.ScaleX;
            result.ScaleY     = inst.ScaleY;
            result.Transforms = TransformsConversion(result, inst.Transforms);

            return(result);

            //Dictionary<string, string> dict = null;
            //if (v2d.codeData.ContainsKey(inst.InstanceName))
            //{
            //    dict = v2d.codeData[inst.InstanceName];
            //}
            //return inst.GetV2DInstance(dict);
        }
Example #2
0
        private Instance2D CreateInstance2D(Instance inst, IDefinition def)
        {
            //MatrixComponents mc = inst.Transformations[0].Matrix.GetMatrixComponents();
            //Instance2D result = new Instance2D(inst.Name, def.Name, mc.TranslateX, mc.TranslateY, (float)(mc.Rotation * Math.PI / 180), mc.ScaleX, mc.ScaleY);
            Instance2D result = new Instance2D(inst.Name, def.Name, 0, 0, 0, 1, 1);

            result.Depth       = inst.Depth;
            result.Transforms  = inst.Transformations;
            result.Definition  = definitions.Find(d => d.Id == inst.DefinitionId);
            result.StartFrame  = curVo.GetFrameFromMilliseconds(inst.StartTime);
            result.TotalFrames = curVo.GetFrameFromMilliseconds(inst.EndTime) - result.StartFrame - 1; // end frame is last ms of frame, so -1
            return(result);
        }
Example #3
0
        private Instance2D CreateInstance2D(IDefinition def)
        {
            //MatrixComponents mc = Matrix.Identitiy.GetMatrixComponents();
            //Instance2D result = new Instance2D(def.Name, def.Name, mc.TranslateX, mc.TranslateY, (float)(mc.Rotation * Math.PI / 180), mc.ScaleX, mc.ScaleY);
            Instance2D result = new Instance2D(def.Name, def.Name, 0, 0, 0, 1, 1);

            result.Depth      = 0;
            result.Transforms = new List <Transform>();
            result.Transforms.Add(new Transform(0, 0, Matrix.Identity, 1, ColorTransform.Identity));
            result.Definition  = definitions.Find(d => d.Id == def.Id);
            result.StartFrame  = 0;
            result.TotalFrames = 0;
            return(result);
        }
Example #4
0
        private Instance2D AddInstance(Instance inst, IDefinition def)
        {
            Instance2D result = null;

            if (inst != null)
            {
                result = CreateInstance2D(inst, def);
                if (!parentStack.Peek().Definition.IsDefined)
                {
                    parentStack.Peek().Definition.Children.Add(result);
                }
            }
            return(result);
        }
Example #5
0
        public void Init(DDW.Swf.SwfCompilationUnit scu)
        {
            root = new Definition2D();
            root.DefinitionName = V2D.ROOT_NAME;
            root.LinkageName    = V2D.ROOT_NAME;
            root.Bounds         = Rectangle.Empty;
            root.FrameCount     = 1;
            definitions.Add(root);

            rootInstance            = new Instance2D(V2D.ROOT_NAME, root.DefinitionName, 0, 0, 0, 1, 1);
            rootInstance.Definition = root;
            rootInstance.Transforms.Add(new Transform(
                                            0,
                                            (uint)(scu.Header.FrameCount * (1000 / scu.Header.FrameRate)),
                                            new Matrix(1, 0, 0, 1, 0, 0),
                                            1,
                                            ColorTransform.Identity));

            parentStack.Push(rootInstance);
        }
Example #6
0
        private void GenerateJointData(Definition2D def)
        {
            if (!def.IsDefined)             // todo: really really need to do this isDefined crap right.
            {
                foreach (Joint2D j in def.Joints.Values)
                {
                    string jName = j.Name;
                    EnsureBodyTags(j);
                    string     b1Name = j.data["body1"];
                    string     b2Name = j.data["body2"];
                    Instance2D b1Body = (b1Name == V2D.ROOT_NAME) ? rootInstance : parentStack.Peek().Definition.GetChildByName(b1Name);
                    Instance2D b2Body = (b2Name == V2D.ROOT_NAME) ? rootInstance : parentStack.Peek().Definition.GetChildByName(b2Name);

                    V2DJointKind jointKind = j.JointKind;

                    if (!j.data.ContainsKey("location"))
                    {
                        if (V2D.OUTPUT_TYPE == OutputType.Swf)
                        {
                            j.data.Add("location", "#" + j.Locations[0].ToString());
                        }
                        else
                        {
                            j.data.Add("X", j.Locations[0].X.ToString());
                            j.data.Add("Y", j.Locations[0].Y.ToString());
                        }
                    }
                    if ((j.Locations.Count > 1) && (jointKind == V2DJointKind.Distance || jointKind == V2DJointKind.Pully))
                    {
                        if (!j.data.ContainsKey("location2"))
                        {
                            if (V2D.OUTPUT_TYPE == OutputType.Swf)
                            {
                                j.data.Add("location2", "#" + j.Locations[1].ToString());
                            }
                            else
                            {
                                j.data.Add("X2", j.Locations[1].X.ToString());
                                j.data.Add("Y2", j.Locations[1].Y.ToString());
                            }
                        }
                        if (jointKind == V2DJointKind.Pully)
                        {
                            Point groundAnchor1;
                            Point groundAnchor2;
                            if (b1Body.Transforms.Count > 1)
                            {
                                groundAnchor1 = new Point(
                                    b1Body.Transforms[1].Matrix.TranslateX,
                                    b1Body.Transforms[1].Matrix.TranslateY);
                                groundAnchor2 = new Point(
                                    b2Body.Transforms[1].Matrix.TranslateX,
                                    b2Body.Transforms[1].Matrix.TranslateY);
                            }
                            else
                            {
                                groundAnchor1 = j.Transforms[0];
                                groundAnchor2 = j.Transforms[1];
                            }

                            float d1x        = groundAnchor1.X - j.Locations[0].X;                     // m1a.TranslateX;
                            float d1y        = groundAnchor1.Y - j.Locations[0].Y;                     //m1a.TranslateY;
                            float d2x        = groundAnchor2.X - j.Locations[1].X;                     //m2a.TranslateX;
                            float d2y        = groundAnchor2.Y - j.Locations[1].Y;                     //m2a.TranslateY;
                            float maxLength1 = (float)Math.Sqrt(d1x * d1x + d1y * d1y);
                            float maxLength2 = (float)Math.Sqrt(d2x * d2x + d2y * d2y);
                            //j.data.Add("groundAnchor1", groundAnchor1.ToString());
                            //j.data.Add("groundAnchor2", groundAnchor2.ToString());
                            j.data.Add("groundAnchor1X", groundAnchor1.X.ToString());
                            j.data.Add("groundAnchor1Y", groundAnchor1.Y.ToString());
                            j.data.Add("groundAnchor2X", groundAnchor2.X.ToString());
                            j.data.Add("groundAnchor2Y", groundAnchor2.Y.ToString());
                            j.data.Add("maxLength1", maxLength1.ToString());
                            j.data.Add("maxLength2", maxLength2.ToString());
                        }
                    }
                    else if (b1Body.Transforms.Count > 1 || b2Body.Transforms.Count > 1)
                    {
                        List <Transform> tr = (b1Body.Transforms.Count > 1) ? b1Body.Transforms : b2Body.Transforms;
                        if (jointKind == V2DJointKind.Revolute)
                        {
                            float start = (float)(tr[0].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            float rmin  = (float)(tr[1].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            float rmax  = (float)(tr[2].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            rmin = rmin - start;
                            rmax = rmax - start;
                            if (rmin > 0)
                            {
                                rmin = (float)(Math.PI * -2 + rmin);
                            }
                            j.data.Add("min", rmin.ToString());
                            j.data.Add("max", rmax.ToString());
                        }
                        else if (jointKind == V2DJointKind.Prismatic)
                        {
                            Point a = new Point(
                                tr[0].Matrix.TranslateX,
                                tr[0].Matrix.TranslateY);
                            Point p0 = new Point(
                                tr[1].Matrix.TranslateX,
                                tr[1].Matrix.TranslateY);
                            Point p1 = new Point(
                                tr[2].Matrix.TranslateX,
                                tr[2].Matrix.TranslateY);

                            double len = Math.Sqrt((p1.Y - p0.Y) * (p1.Y - p0.Y) + (p1.X - p0.X) * (p1.X - p0.X));
                            double r   = ((p0.Y - a.Y) * (p0.Y - p1.Y) - (p0.X - a.X) * (p0.X - p1.X)) / (len * len);

                            float axisX   = p1.X - p0.X;
                            float axisY   = p1.Y - p0.Y;
                            float maxAxis = Math.Abs(axisX) > Math.Abs(axisY) ? axisX : axisY;
                            axisX /= maxAxis;
                            axisY /= maxAxis;

                            Point ap0 = new Point(p0.X - a.X, p0.Y - a.Y);
                            Point ap1 = new Point(p1.X - a.X, p1.Y - a.Y);
                            float min = (float)-(Math.Abs(r * len));
                            float max = (float)((1 - Math.Abs(r)) * len);
                            // Point ab = new Point(b.X - a.X, b.Y - a.Y);

                            // float r0 = (ap0.X * ab.X + ap0.Y * ab.Y) / (w * w); // dot product
                            //float r1 = (ap1.X * ab.X + ap1.Y * ab.Y) / (w * w); // dot product

                            j.data.Add("axisX", axisX.ToString());
                            j.data.Add("axisY", axisY.ToString());
                            j.data.Add("min", min.ToString());
                            j.data.Add("max", max.ToString());
                        }
                    }
                }
            }
        }
Example #7
0
        private void EnsureBodyTags(Joint2D j)
        {
            bool hasB1      = j.data.ContainsKey("body1");
            bool hasB2      = j.data.ContainsKey("body2");
            bool b1FromCode = hasB1;

            if (!hasB1 || !hasB2)
            {
                List <Instance2D> hits = FindTargetUnderPoint(j.Locations[0]);
                if (!hasB1 && hits.Count > 0)
                {
                    j.data.Add("body1", hits[0].InstanceName);
                    hasB1 = true;
                }

                if (!hasB2)
                {
                    Instance2D b2 = null;
                    if (j.Locations.Count > 1)// && j.JointKind == JointKind.Distance)
                    {
                        List <Instance2D> b2L = FindTargetUnderPoint(j.Locations[1]);

                        if (b2L.Count > 0)
                        {
                            b2 = b2L[0];
                        }
                        else
                        {
                            // ground
                        }
                    }
                    else if (hits.Count > 1)
                    {
                        b2 = hits[1];
                    }
                    else if (b1FromCode && hits.Count > 0)
                    {
                        b2 = hits[0];
                    }

                    if (!hasB1)
                    {
                        j.data.Add("body1", V2D.ROOT_NAME);
                    }

                    if (b2 != null)
                    {
                        j.data.Add("body2", b2.InstanceName);
                        hasB2 = true;
                    }
                    else
                    {
                        j.data.Add("body2", V2D.ROOT_NAME);
                    }
                }
            }
            GenerateJointActionData(j, parentStack.Peek().Definition);

            // flip if a ground ref (I think all joints to ground use ground on body 1..?)
            if (j.data["body2"] == V2D.ROOT_NAME)
            {
                string temp = j.data["body2"];
                j.data["body2"] = j.data["body1"];
                j.data["body1"] = temp;
                if (j.Locations.Count > 1)
                {
                    Point temp2 = j.Locations[0];
                    j.Locations[0] = j.Locations[1];
                    j.Locations[1] = temp2;
                }
            }
        }