コード例 #1
0
        public static void CollideBox(GameObject physObject1, GameObject physObject2)
        {
            PointFloat vel1 = physObject1.GetVelocity();
            PointFloat vel2 = physObject2.GetVelocity();
            float      h1   = -(float)Math.Atan2(vel1.x, vel1.y);
            float      h2   = -(float)Math.Atan2(vel2.x, vel2.y);

            if (h2 > h1 + (_pi / 2.0f) && h2 < h1 - (_pi / 2.0f))
            {
                return;
            }
            PointFloat pos1 = physObject1.GetPosition();
            PointFloat pos2 = physObject2.GetPosition();
            float      x    = pos1.x - pos2.x;
            float      y    = pos1.y - pos2.y;
            float      h    = -(float)Math.Atan2(x, y);
            PointFloat dir  = GetDirection(h + (float)Math.PI / 2.0f);
            float      e1   = (Math.Abs(vel1.x) + Math.Abs(vel2.x)) * dir.x;
            float      e2   = (Math.Abs(vel1.y) + Math.Abs(vel2.y)) * dir.y;

            e1 = e1 / 2;
            e2 = e2 / 2;
            if (!physObject1.OnColision(physObject2, e1 + e2))
            {
                return;
            }
            int count = 0;

            while (count <= 10 && IsInBox(physObject1.GetPosition(), physObject2.GetPosition(), physObject1.GetSize(), physObject2.GetSize()))
            {
                if (vel1.GetSpeed() < vel2.GetSpeed())
                {
                    physObject2.SetPosAdd(vel2 / -10);
                }
                else
                {
                    physObject1.SetPosAdd(vel1 / 10);
                }
                count++;
            }
            if (physObject2.HasMass() == true)
            {
                physObject1.SetVelocity(0, 0);
            }
            if (physObject1.HasMass() == true)
            {
                physObject2.SetVelocity(0, 0);
            }

            PhysInteractions++;
        }
コード例 #2
0
ファイル: PhysEngine.cs プロジェクト: Vinic/TomatoEngine
        public static void CollideBox(GameObject physObject1, GameObject physObject2)
        {
            PointFloat vel1 = physObject1.GetVelocity();
            PointFloat vel2 = physObject2.GetVelocity();
            float h1 = -(float)Math.Atan2(vel1.x, vel1.y);
            float h2 = -(float)Math.Atan2(vel2.x, vel2.y);
            if (h2 > h1 + (_pi / 2.0f) && h2 < h1 - (_pi / 2.0f))
            {
                return;
            }
            PointFloat pos1 = physObject1.GetPosition();
            PointFloat pos2 = physObject2.GetPosition();
            float x = pos1.x - pos2.x;
            float y = pos1.y - pos2.y;
            float h = -(float)Math.Atan2(x, y);
            PointFloat dir = GetDirection(h + (float)Math.PI / 2.0f);
            float e1 = (Math.Abs(vel1.x) + Math.Abs(vel2.x)) * dir.x;
            float e2 = (Math.Abs(vel1.y) + Math.Abs(vel2.y)) * dir.y;
            e1 = e1 / 2;
            e2 = e2 / 2;
            if (!physObject1.OnColision(physObject2, e1 + e2))
            {
                return;
            }
            int count = 0;
            while (count <= 10 && IsInBox(physObject1.GetPosition(), physObject2.GetPosition(), physObject1.GetSize(), physObject2.GetSize()))
            {
                if (vel1.GetSpeed() < vel2.GetSpeed())
                {
                    physObject2.SetPosAdd(vel2 / -10);
                }
                else
                {
                    physObject1.SetPosAdd(vel1 / 10);
                }
                count++;
            }
            if (physObject2.HasMass() == true)
            {
                physObject1.SetVelocity(0, 0);
            }
            if (physObject1.HasMass() == true)
            {
                physObject2.SetVelocity(0, 0);
            }

            PhysInteractions++;
        }
コード例 #3
0
        //handles collision
        public static void CollideCircleBox(GameObject physObject1, GameObject physObject2)
        {
            PointFloat vel1 = physObject1.GetVelocity();
            PointFloat vel2 = physObject2.GetVelocity();
            float      h1   = -(float)Math.Atan2(vel1.x, vel1.y);
            float      h2   = -(float)Math.Atan2(vel2.x, vel2.y);

            if (h2 > h1 + (_pi / 2.0f) && h2 < h1 - (_pi / 2.0f))
            {
                return;
            }
            PointFloat pos1 = physObject1.GetPosition();
            PointFloat pos2 = physObject2.GetPosition();
            float      x    = pos1.x - pos2.x;
            float      y    = pos1.y - pos2.y;
            float      h    = -(float)Math.Atan2(x, y);
            PointFloat dir  = GetDirection(h + (float)Math.PI / 2.0f);
            //while ( IsOverlappingInCircle(physObject1, physObject2) )
            //{
            //physObject1.SetPosAdd(dir / 1);
            //physObject2.SetPosAdd(dir / -1);
            //}
            float e1 = (Math.Abs(vel1.x) + Math.Abs(vel2.x)) * dir.x;
            float e2 = (Math.Abs(vel1.y) + Math.Abs(vel2.y)) * dir.y;

            e1 = e1 / 2;
            e2 = e2 / 2;
            if (!physObject1.OnColision(physObject2, e1 + e2))
            {
                return;
            }

            if (physObject2.HasMass() == true)
            {
                physObject1.SetPosAdd(vel1 / -1);
                physObject1.SetVelocity(e1, e2);
                physObject1.SetRotationVelocity(((float)Math.Atan2(vel1.x + vel2.x, vel1.y + vel2.y) - (float)Math.Atan2(x, y)) / 100f);
            }
            if (physObject1.HasMass() == true)
            {
                physObject2.SetVelocity(-e1, -e2);
                physObject2.SetRotationVelocity(((float)Math.Atan2(vel1.x + vel2.x, vel1.y + vel2.y) - (float)Math.Atan2(x, y)) / 100f);
            }

            PhysInteractions++;
        }
コード例 #4
0
ファイル: PhysEngine.cs プロジェクト: Vinic/TomatoEngine
        //handles collision
        public static void CollideCircleBox(GameObject physObject1, GameObject physObject2)
        {
            PointFloat vel1 = physObject1.GetVelocity();
            PointFloat vel2 = physObject2.GetVelocity();
            float h1 = -(float)Math.Atan2(vel1.x, vel1.y);
            float h2 = -(float)Math.Atan2(vel2.x, vel2.y);
            if ( h2 > h1 + (_pi/2.0f) && h2 < h1 - (_pi/2.0f) )
            {
                return;
            }
            PointFloat pos1 = physObject1.GetPosition();
            PointFloat pos2 = physObject2.GetPosition();
            float x = pos1.x - pos2.x;
            float y = pos1.y - pos2.y;
            float h = -(float)Math.Atan2(x, y);
            PointFloat dir = GetDirection(h + (float)Math.PI / 2.0f);
            //while ( IsOverlappingInCircle(physObject1, physObject2) )
            //{
                //physObject1.SetPosAdd(dir / 1);
                //physObject2.SetPosAdd(dir / -1);
            //}
            float e1 = ( Math.Abs(vel1.x) + Math.Abs(vel2.x) ) * dir.x;
            float e2 = ( Math.Abs(vel1.y)+ Math.Abs(vel2.y) ) * dir.y;
            e1 = e1 / 2;
            e2 = e2 / 2;
            if(!physObject1.OnColision(physObject2, e1 + e2)){
                return;
            }

            if ( physObject2.HasMass()  == true )
            {
                physObject1.SetPosAdd(vel1 / -1);
                physObject1.SetVelocity(e1, e2);
                physObject1.SetRotationVelocity(( (float)Math.Atan2(vel1.x + vel2.x, vel1.y + vel2.y) - (float)Math.Atan2(x, y) ) / 100f);
            }
            if ( physObject1.HasMass() == true )
            {
                physObject2.SetVelocity(-e1, -e2);
                physObject2.SetRotationVelocity(( (float)Math.Atan2(vel1.x + vel2.x, vel1.y + vel2.y) - (float)Math.Atan2(x, y) ) / 100f);
            }

            PhysInteractions++;
        }