コード例 #1
0
ファイル: Window.cs プロジェクト: dali2333/Physics_Engine
 public static bool Add_SpliceObj(string name, SpliceOBJ obj)
 {
     try
     {
         obj.Name = name;
         obj.Judge_Visible();
         All_SpliceOBJ.Add(name, obj);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
コード例 #2
0
        private static void Equal_Interval_Callback(object state) //等时间间隔计算
        {
            //物理算法
            Parallel.ForEach(Window.All_Obj.Values, (rc1) =>
            {
                //组合体
                if (rc1.Is_Spliced)
                {
                    SpliceOBJ so = Window.All_SpliceOBJ[rc1.Splice_Name];
                }
                //非组合体
                else
                {
                    if (rc1.Movable)
                    {
                        //位移-速度计算
                        rc1.NX = rc1.X + rc1.Speed_X * Physics_Interval_Time;
                        rc1.NY = rc1.Y + rc1.Speed_Y * Physics_Interval_Time;

                        rc1.Speed_X += (rc1.F_X / rc1.Weight + Global_GX) * Physics_Interval_Time;
                        rc1.Speed_Y += (rc1.F_Y / rc1.Weight + Global_GY) * Physics_Interval_Time;

                        //碰撞-摩擦计算
                        if (rc1.Collisible)
                        {
                            //遍历其余物体
                            Parallel.ForEach(Window.All_Obj.Values, (rc2) =>
                            {
                                //找出符合标准的物体
                                if (rc2.Collisible && rc1.Name != rc2.Name)
                                {
                                    //判断是否碰撞 Graph.Add_String(Graph.All_Graphs["V"], "AAA");
                                    if (rc1.Speed_X > 0)
                                    {
                                        if (rc1.Speed_Y > 0)
                                        {
                                            if (rc1.NX + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.NY + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                if (rc2.X < rc1.X && rc1.X < rc2.PX && rc1.Y < rc2.Y && rc2.Y < rc1.NPY)
                                                {
                                                    rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NY = rc2.Y - rc1.SY;
                                                }

                                                if (rc1.PX < rc2.X && rc2.X < rc1.NPX && rc2.Y < rc1.Y && rc1.Y < rc2.PY)
                                                {
                                                    rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NX = rc2.X - rc1.SX;
                                                }
                                            }
                                        }
                                        else if (rc1.Speed_Y < 0)
                                        {
                                            if (rc1.NX + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.NY)
                                            {
                                                if (rc2.X < rc1.X && rc1.X < rc2.PX && rc1.NY < rc2.PY && rc2.PY < rc1.Y)
                                                {
                                                    rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NY = rc2.Y + rc2.SY;
                                                }

                                                if (rc1.PX < rc2.X && rc2.X < rc1.NPX && rc2.Y < rc1.Y && rc1.Y < rc2.PY)
                                                {
                                                    rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NX = rc2.X - rc1.SX;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (rc1.NX + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                rc1.NX = rc2.X - rc1.SX;
                                            }
                                        }
                                    }
                                    else if (rc1.Speed_X < 0)
                                    {
                                        if (rc1.Speed_Y > 0)
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.NX &&
                                                rc1.NY + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                if (rc2.X < rc1.X && rc1.X < rc2.PX && rc1.Y < rc2.Y && rc2.Y < rc1.NPY)
                                                {
                                                    rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NY = rc2.Y - rc1.SY;
                                                }

                                                if (rc1.NX < rc2.PX && rc2.PX < rc1.X && rc2.Y < rc1.Y && rc1.Y < rc2.PY)
                                                {
                                                    rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NX = rc2.X + rc2.SX;
                                                }
                                            }
                                        }
                                        else if (rc1.Speed_Y < 0)
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.NX &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.NY)
                                            {
                                                if (rc2.X < rc1.X && rc1.X < rc2.PX && rc1.NY < rc2.PY && rc2.PY < rc1.Y)
                                                {
                                                    rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NY = rc2.Y + rc2.SY;
                                                }

                                                if (rc1.NX < rc2.PX && rc2.PX < rc1.X && rc2.Y < rc1.Y && rc1.Y < rc2.PY)
                                                {
                                                    rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                    rc1.NX = rc2.X + rc2.SX;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.NX &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                rc1.Speed_X *= -(rc1.Elastic + rc2.Elastic);

                                                rc1.NX = rc2.X + rc2.SX;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (rc1.Speed_Y > 0)
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.NY + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                rc1.NY = rc2.Y - rc1.SY;
                                            }
                                        }
                                        else if (rc1.Speed_Y < 0)
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.NY)
                                            {
                                                rc1.Speed_Y *= -(rc1.Elastic + rc2.Elastic);

                                                rc1.NY = rc2.Y + rc2.SY;
                                            }
                                        }
                                        else
                                        {
                                            if (rc1.X + rc1.SX > rc2.X &&
                                                rc2.X + rc2.SX > rc1.X &&
                                                rc1.Y + rc1.SY > rc2.Y &&
                                                rc2.Y + rc2.SY > rc1.Y)
                                            {
                                                //静止状态被碰到
                                            }
                                        }
                                    }
                                }
                            });

                            rc1.X = rc1.NX;
                            rc1.Y = rc1.NY;
                        }

                        //
                        rc1.Judge_Speed();
                        //rc1.Judge_Visible();
                        if (rc1.X + rc1.SX >= Window.Size_X - 1 || rc1.X < 0 || rc1.Y + rc1.SY >= Window.Size_Y || rc1.Y < 0)
                        {
                            Out_Of_Bounds.Invoke(rc1);

                            rc1.Visible = false;
                        }
                        else
                        {
                            rc1.Visible = true;
                        }

                        ///////////////////////////////////
                    }
                    else
                    {
                        //rc1.Speed_X = 0; rc1.Speed_Y = 0;
                    }
                }
            });

            //信息显示
            Graph.Add_String(Graph.All_Graphs["V"], "        "); Graph.Add_String(Graph.All_Graphs["V"], Physics_Time_Watch.ElapsedMilliseconds.ToString() + " " + Game_Time_Watch.ElapsedMilliseconds.ToString());

            //动态延迟时间
            Physics_Interval_Time = (float)Physics_Time_Watch.ElapsedMilliseconds / 1000;

            Physics_Time_Watch.Restart();
        }