Пример #1
0
        private void WriteInstanceData(StringWriter sw)
        {
            sw.WriteLine("");
            sw.WriteLine("        public var _instances:Array = ");
            sw.WriteLine("        [");

            Body2D[] ba = new Body2D[allBodies.Count];
            allBodies.CopyTo(ba, 0);
            List <Body2D> b2d = new List <Body2D>(ba);

            b2d.Sort();

            string comma = "";

            foreach (Body2D b in b2d)
            {
                if (b.InstanceName != V2D.ROOT_NAME)
                {
                    sw.Write(comma);
                    Dictionary <string, string> dict = null;
                    if (v2d.codeData.ContainsKey(b.InstanceName))
                    {
                        dict = v2d.codeData[b.InstanceName];
                    }
                    b.DumpInstance(sw, dict);
                    comma = ",\n";
                }
            }

            sw.WriteLine("\n        ];");
        }
Пример #2
0
 private void ParseBodyImage(Body2D b2d, IDefinition sy, Instance inst)
 {
     if (V2D.OUTPUT_TYPE == OutputType.Swf)
     {
         string path = VexTo2DPhysics.OutputDirectory + "/" + curVo.Name + ".swf";
         path = path.Replace('\\', '/');
         b2d.SetSymbol(sy.Name, path);
     }
     else if (V2D.OUTPUT_TYPE == OutputType.Xna)
     {
         string bmpPath = "";
         if (sy is Timeline)
         {
             Timeline tl = (Timeline)sy;
             bmpPath = paths[tl.Id];
         }
         else if (sy is Symbol)
         {
             bmpPath = paths[sy.Id];
         }
         string path = VexTo2DPhysics.OutputDirectory + "/" + bmpPath;
         path = path.Replace('\\', '/');
         b2d.SetSymbol(sy.Name, path);
     }
 }
Пример #3
0
        private void ParseTimeline(Timeline t)
        {
            List <IInstance> ris = t.Instances;

            foreach (IInstance instance in ris)
            {
                if (!(instance is Instance))
                {
                    continue;
                }
                Instance    inst = (Instance)instance;
                IDefinition def  = curVo.Definitions[inst.DefinitionId];
                if (inst.Name == null)
                {
                    inst.Name = "inst" + inst.Depth;
                }
                curName = def.Name;

                if (jointKinds.Contains(curName))
                {
                    // joint object
                    JointKind jointKind = jointKindMap[jointKinds.IndexOf(curName)];
                    ParseJoint(jointKind, inst);
                }
                else if (def is Timeline)
                {
                    // body object
                    Timeline tl  = (Timeline)def;
                    Body2D   b2d = new Body2D(inst.Name, def.Name, (int)inst.Depth, def.StrokeBounds);
                    bodies.Add(inst.Name, b2d);

                    b2d.Transforms = inst.Transformations;
                    if (HasShape(curVo, tl))
                    {
                        foreach (IInstance tlInst in tl.Instances)
                        {
                            IDefinition def2 = curVo.Definitions[tlInst.DefinitionId];

                            string nm2        = def2.Name;
                            bool   isShapeDef = IsShapeDef(def2);
                            if (isShapeDef)
                            {
                                b2d.AddShapes(curVo, def2, (Instance)tlInst);
                            }
                            else
                            {
                                ParseBodyImage(b2d, def2, inst);
                            }
                        }
                    }
                    else
                    {
                        // bkg, not box2D element
                        ParseBodyImage(b2d, def, inst);
                    }
                    parsedTypes.Add(curName);
                }
            }
        }
Пример #4
0
        public void Init(DDW.Swf.SwfCompilationUnit scu)
        {
            Body2D ground = new Body2D(V2D.ROOT_NAME, "_root", -1, Rectangle.Empty);

            ground.Transforms = new List <Transform>();
            ground.Transforms.Add(new Transform(0, (uint)(scu.Header.FrameCount * (1 / scu.Header.FrameRate)), new Matrix(1, 0, 0, 1, 0, 0), 1, ColorTransform.Identity));
            bodies.Add(V2D.ROOT_NAME, ground);
        }
Пример #5
0
        private void WriteInstanceData(XmlWriter xw, Body2D b)
        {
            Dictionary <string, string> dict = null;

            if (v2d.codeData.ContainsKey(b.InstanceName))
            {
                dict = v2d.codeData[b.InstanceName];
            }

            b.DumpInstance(xw, dict);
        }
Пример #6
0
 private void GenBodyList(Body2D b)
 {
     if (!allBodies.Contains(b))
     {
         allBodies.Add(b);
         for (int i = 0; i < b.Children.Count; i++)
         {
             GenBodyList(b.Children[i]);
         }
     }
 }
Пример #7
0
 private void GenBodyList(Body2D b)
 {
     if (!allBodies.Contains(b))
     {
         allBodies.Add(b);
         for (int i = 0; i < b.Children.Count; i++)
         {
             GenBodyList(b.Children[i]);
         }
     }
 }
Пример #8
0
        private void WriteJointData(XmlWriter xw, Body2D body)
        {
            Joint2D[] jts = new Joint2D[body.Joints.Count];
            body.Joints.Values.CopyTo(jts, 0);
            List <Joint2D> jList = new List <Joint2D>(jts);

            jList.Sort();

            foreach (Joint2D j in jList)
            {
                j.Dump(xw);
            }
        }
Пример #9
0
        private void RunSimLoop()
        {
            Task.Run(() => {
                long ct;
                long lt   = DateTime.Now.Ticks;
                double dt = 0;
                double c  = 0;

                while (!isClosing)
                {
#if WINFORMS
                    evt.WaitOne();
#else
                    evt.WaitOne(5);
#endif

                    ct = DateTime.Now.Ticks;
                    dt = (ct - lt);
                    lt = ct;
                    c += dt;

                    dt /= dtFactor;
                    dt *= simSpeed;

                    if (c >= fpsDelay)
                    {
                        c = 0;
#if WINFORMS
                        parent.Invalidate();
#else
                        Application.Instance.Invoke(() => parent.Invalidate());
#endif
                    }

                    switch (mode)
                    {
                    case Modes.Standard:
                        SimStandard(dt);
                        break;

                    case Modes.Planetarium:
                        SimPlanetarium(dt);
                        break;
                    }

                    Body2D.CheckBodiesCollisions(bodies, forces, dt);
                }
            });
        }
Пример #10
0
        private void WriteJointData(StringWriter sw, Body2D body)
        {
            sw.WriteLine("");
            sw.WriteLine("        public var _joints:Array = ");
            sw.WriteLine("        [");

            Joint2D[] jts = new Joint2D[body.Joints.Count];
            body.Joints.Values.CopyTo(jts, 0);
            List <Joint2D> jList = new List <Joint2D>(jts);

            jList.Sort();

            string comma = "";

            foreach (Joint2D j in jList)
            {
                sw.Write(comma);
                j.Dump(sw);
                comma = ",\n";
            }

            sw.WriteLine("\n        ];");
        }
Пример #11
0
        private void WriteDefinitions(XmlWriter xw, Body2D b)
        {
            for (int i = 0; i < b.Children.Count; i++)
            {
                WriteDefinitions(xw, b.Children[i]);
            }

            if (b.Symbol != null && !parsedDefs.Contains(b.Symbol.SymbolName))
            {
                parsedDefs.Add(b.Symbol.SymbolName);

                xw.WriteStartElement("V2DDefinition");
                xw.WriteAttributeString("Name", b.TypeName);
                xw.WriteAttributeString("LinkageName", b.Symbol.SymbolName);
                xw.WriteAttributeString("OffsetX", b.Bounds.Point.X.ToString());
                xw.WriteAttributeString("OffsetY", b.Bounds.Point.Y.ToString());
                b.DumpShapeData(xw);

                if (b.Children.Count > 0)
                {
                    xw.WriteStartElement("Instances");
                    for (int i = 0; i < b.Children.Count; i++)
                    {
                        WriteInstanceData(xw, b.Children[i]);
                    }
                    xw.WriteEndElement(); // Instances
                }
                if (b.Joints.Count > 0)
                {
                    xw.WriteStartElement("Joints");
                    WriteJointData(xw, b);
                    xw.WriteEndElement(); // Joints
                }

                xw.WriteEndElement(); // V2DDefinition
            }
        }
Пример #12
0
        private void WriteDefinitions(XmlWriter xw, Body2D b)
        {
            for (int i = 0; i < b.Children.Count; i++)
            {
                WriteDefinitions(xw, b.Children[i]);
            }

            if (b.Symbol != null && !parsedDefs.Contains(b.Symbol.SymbolName))
            {
                parsedDefs.Add(b.Symbol.SymbolName);

                xw.WriteStartElement("V2DDefinition");
                xw.WriteAttributeString("Name", b.TypeName);
                xw.WriteAttributeString("LinkageName", b.Symbol.SymbolName);
                xw.WriteAttributeString("OffsetX", b.Bounds.Point.X.ToString());
                xw.WriteAttributeString("OffsetY", b.Bounds.Point.Y.ToString());
                b.DumpShapeData(xw);

                if (b.Children.Count > 0)
                {
                    xw.WriteStartElement("Instances");
                    for (int i = 0; i < b.Children.Count; i++)
                    {
                        WriteInstanceData(xw, b.Children[i]);
                    }
                    xw.WriteEndElement(); // Instances
                }
                if (b.Joints.Count > 0)
                {
                    xw.WriteStartElement("Joints");
                    WriteJointData(xw, b);
                    xw.WriteEndElement(); // Joints
                }

                xw.WriteEndElement(); // V2DDefinition
            }
        }
Пример #13
0
        private void ParseTimeline(Timeline t)
        {
            List <IInstance> insts = t.Instances;

            foreach (IInstance instance in insts)
            {
                if (!(instance is Instance))
                {
                    continue;
                }
                Instance inst = (Instance)instance;
                if (inst.Name == null)
                {
                    inst.Name = "$inst_" + instAutoNumber++;
                }

                IDefinition    def = curVo.Definitions[inst.DefinitionId];
                DefinitionKind dk  = (DefinitionKind)def.UserData;

                if ((dk & DefinitionKind.Ignore) != 0)
                {
                    continue;
                }

                if ((dk & DefinitionKind.JointMarker) != 0)
                {
                    V2DJointKind jointKind = Enum.Parse(V2DJointKind, def.Name, true);
                    ParseJoint(jointKind, inst);
                }

                if ((dk & DefinitionKind.ShapeMarker) != 0)
                {
                }

                if ((dk & DefinitionKind.TextField) != 0)
                {
                    Text txt = (Text)def;
                }

                if ((dk & DefinitionKind.Timeline) != 0)
                {
                    V2DDefinition vd = CreateDefinition(inst, def);
                    V2DInstance   vi = CreateInstance(inst, vd);

                    parentStack.Push(vi);
                    ParseTimeline((Timeline)def);
                    parentStack.Pop();
                }

                if ((dk & DefinitionKind.Symbol) != 0)
                {
                    V2DInstance vi = CreateInstance(inst, def);
                }

                if ((dk & DefinitionKind.Vex2D) != 0)
                {
                    // todo: this is just adding images and/or instances
                    Body2D b2d = CreateDefinition(inst, def);
                    b2d.AddShapes(curVo, def, inst);
                }
            }
            GenerateJointData();
        }
Пример #14
0
        private void SetEventsHandlers()
        {
            canvas.Paint       += (object s, PaintEventArgs e) => Paint(e);
            parent.Closing     += (_, __) => isClosing = true;
            parent.SizeChanged += (_, __) => SetBounds();
            parent.MouseDown   += (_, __) => {
                if (overBody != null)
                {
                    isDragging       = true;
                    overBodyCanMove  = overBody.Movable;
                    overBody.Movable = false;
                }
                else
                {
                    isDragging = false;
                }
            };
            parent.MouseUp += (_, __) => {
                isDragging = false;
                if (overBody != null)
                {
                    overBody.Movable = overBodyCanMove;
                }
            };
            parent.MouseMove += (object o, MouseEventArgs e) => {
                PointD p = e.Location;
                p.X = e.Location.X / scale - bounds.Width / 2;
                p.Y = bounds.Height / 2 - e.Location.Y / scale;

                if (isDragging)
                {
                    overBody.TranslateAbs(p.X, p.Y);
                }
                else
                {
                    Cursor c = Cursors.Default;
                    bodies.ForEach((b) => {
                        if (b.Intersects(p))
                        {
#if WINFORMS
                            c = Cursors.Hand;
#else
                            c = Cursors.Pointer;
#endif
                            overBody = b;
                            return;
                        }
                    });
                    parent.Cursor = c;
                }
            };
            canvas.KeyDown += (object s, KeyEventArgs e) => {
#if WINFORMS
                switch (e.KeyCode)
                {
#else
                switch (e.Key)
                {
#endif
                case Keys.Add:
                    if (scale < 2)
                    {
                        scale += 0.1f;
                        SetBounds();
                        UpdateTitlebarText();
                    }
                    break;

                case Keys.Subtract:
                    if (scale > 0.1f)
                    {
                        scale -= 0.1f;
                        SetBounds();
                        UpdateTitlebarText();
                    }
                    break;

                case Keys.Enter:
                    CreateRandomObjects();
                    break;

                case Keys.Space:
                    if (mode == Modes.Standard)
                    {
                        mode = Modes.Planetarium;
                    }
                    else
                    {
                        mode = Modes.Standard;
                    }
                    CreateRandomObjects();
                    break;

#if WINFORMS
                case Keys.OemPeriod:
#else
                case Keys.Period:
#endif
                    if (simSpeed < 10)
                    {
                        simSpeed += 0.1f;
                    }
                    UpdateTitlebarText();
                    break;

#if WINFORMS
                case Keys.Oemcomma:
#else
                case Keys.Comma:
#endif
                    if (simSpeed > 0.1)
                    {
                        simSpeed -= 0.1f;
                    }
                    UpdateTitlebarText();
                    break;

                case Keys.F1:
                    showHelp = !showHelp;
                    break;

                case Keys.Escape:
                    parent.Close();
                    break;
                }
            };
        }
Пример #15
0
        private void WriteInstanceData(XmlWriter xw, Body2D b)
        {
            Dictionary<string, string> dict = null;
            if (v2d.codeData.ContainsKey(b.InstanceName))
            {
                dict = v2d.codeData[b.InstanceName];
            }

            b.DumpInstance(xw, dict);
        }
Пример #16
0
        private void GenerateJointData()
        {
            foreach (Joint2D j in joints.Values)
            {
                string jName = j.Name;
                EnsureBodyTags(j);
                string b1Name = j.data["body1"];
                string b2Name = j.data["body2"];
                Body2D b1Body = bodies[b1Name];
                Body2D b2Body = bodies[b2Name];

                JointKind 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 == JointKind.Distance || jointKind == JointKind.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 == JointKind.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 == JointKind.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 == JointKind.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());
                    }
                }
            }
        }
Пример #17
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 <Body2D> hits = FindTargetUnderPoint(j.Locations[0]);
                if (!hasB1 && hits.Count > 0)
                {
                    j.data.Add("body1", hits[0].InstanceName);
                    hasB1 = true;
                }

                if (!hasB2)
                {
                    Body2D b2 = null;
                    if (j.Locations.Count > 1)// && j.JointKind == JointKind.Distance)
                    {
                        List <Body2D> 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);
                    }
                }
            }

            // 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;
                }
            }
        }
Пример #18
0
        private void WriteJointData(XmlWriter xw, Body2D body)
        {
            Joint2D[] jts = new Joint2D[body.Joints.Count];
            body.Joints.Values.CopyTo(jts, 0);
            List<Joint2D> jList = new List<Joint2D>(jts);
            jList.Sort();

            foreach (Joint2D j in jList)
            {
                j.Dump(xw);
            }
        }