Beispiel #1
0
        private void Collision()
        {
            //Check if its out of bounds
            if (!CollisionFuncs.PointAABBcolliding(global.point, GameManager.screenBox))
            {
                //Destroy the bullet
                FreeMemory();
                return;
            }


            //Go through all core objects and check for collision with tanks
            foreach (GameObject obj in GameManager.coreObjects)
            {
                if (obj.tag != "Tank")
                {
                    //We only want to collide with tanks
                    continue;
                }

                //Cast to tank and get AABB
                TankClass tank     = obj as TankClass;
                AABB      tankAABB = CollisionFuncs.OBBtoAABB(tank.collider);

                //Check AABB first, then OBB
                if (CollisionFuncs.PointAABBcolliding(global.point, tankAABB))
                {
                    //If they are colliding, check OBB collision
                    if (CollisionFuncs.PointOBBcolliding(global.point, tank.collider))
                    {
                        tank.Hit();

                        //Enter 'smoke mode' to display an explosion for a short time before destroying itself
                        hasHit = true;
                        image  = rl.Raylib.LoadTexture(GameManager.imageDir + @"Smoke\smokeOrange0.png");
                        //Update image based values
                        imgSize   = new rl.Vector2(image.width, image.height);
                        sourceRec = new rl.Rectangle(0f, 0f, imgSize.x, imgSize.y);
                        origin    = imgSize / 2;

                        return;
                    }
                }
            }
        }
Beispiel #2
0
        private static void StartGame()
        {
            //Create tank object and set its location
            rl.Image   img  = rl.Raylib.LoadImage(imageDir + @"Tanks\tankBlue.png");
            GameObject tank = new TankClass(null, img, 5, 25, 80, "p1", new rl.KeyboardKey[4] {
                rl.KeyboardKey.KEY_W, rl.KeyboardKey.KEY_S, rl.KeyboardKey.KEY_A, rl.KeyboardKey.KEY_D
            });

            rl.Raylib.UnloadImage(img);
            tank.SetLocation(rl.Raylib.GetScreenWidth() * 0.3f, rl.Raylib.GetScreenHeight() * 0.5f);

            //Create turret object as a child of tank
            img = rl.Raylib.LoadImage(imageDir + @"fish_blue.png");
            rl.Raylib.ImageRotateCCW(ref img);
            new TurretClass(tank, img, 100, @"Bullets\bulletBlue.png", new rl.KeyboardKey[3] {
                rl.KeyboardKey.KEY_Q, rl.KeyboardKey.KEY_E, rl.KeyboardKey.KEY_SPACE
            });
            rl.Raylib.UnloadImage(img);


            //Create a second tank with different controls
            img  = rl.Raylib.LoadImage(imageDir + @"Tanks\tankRed.png");
            tank = new TankClass(null, img, 5, 25, 80, "p2", new rl.KeyboardKey[4] {
                rl.KeyboardKey.KEY_I, rl.KeyboardKey.KEY_K, rl.KeyboardKey.KEY_J, rl.KeyboardKey.KEY_L
            });
            rl.Raylib.UnloadImage(img);
            tank.SetLocation(rl.Raylib.GetScreenWidth() * 0.7f, rl.Raylib.GetScreenHeight() * 0.5f);

            img = rl.Raylib.LoadImage(imageDir + @"fish_orange.png");
            rl.Raylib.ImageRotateCCW(ref img);
            new TurretClass(tank, img, 100, @"Bullets\bulletRed.png", new rl.KeyboardKey[3] {
                rl.KeyboardKey.KEY_U, rl.KeyboardKey.KEY_O, rl.KeyboardKey.KEY_N
            });
            rl.Raylib.UnloadImage(img);


            //Display controls
            Console.WriteLine("\n\n");
            Console.WriteLine("Player 1 moves with WASD, aims with QE and fires with SPACE");
            Console.WriteLine("Player 2 moves with IJKL, aims with UO and fires with N");
        }
Beispiel #3
0
        private void Collision()
        {
            //Update collider location and rotation
            UpdateColliderLoc();
            //Rotation matrix of the difference in rotation
            MthLib.Matrix3 rotMatrix = new MthLib.Matrix3();
            rotMatrix.SetRotateZ((local.rotation - global.rotation) * (float)(Math.PI / 180f));
            //Rotate the extents, then put them back in the matrix
            MthLib.Vector3 x = new MthLib.Vector3(collider.m1, collider.m4, 0);
            x           = rotMatrix * x;
            collider.m1 = x.x;
            collider.m4 = x.y;
            MthLib.Vector3 y = new MthLib.Vector3(collider.m2, collider.m5, 0);
            y           = rotMatrix * y;
            collider.m2 = y.x;
            collider.m5 = y.y;


            //Get an AABB for rough collision before using OBB
            AABB aabb = CollisionFuncs.OBBtoAABB(collider);

            //Check if going out of bounds. Because the tank needs to stay inside, just AABB needs to be tested
            if (!CollisionFuncs.AABBwithin(aabb, GameManager.screenBox))
            {
                //Tank is out of bounds; reset transform
                local = global;
                return;
            }

            //Go through all core objects and check for collision with other tanks
            foreach (GameObject obj in GameManager.coreObjects)
            {
                //If it isnt a tank, move on
                if (obj.tag != "Tank")
                {
                    continue;
                }

                TankClass tank = obj as TankClass;
                //If the tank is this tank, move on
                if (tank == this)
                {
                    continue;
                }


                //At this point, the tank must be a different tank, so it can be collided with
                //The collider is OBB, so it needs to be made an AABB
                AABB otherAABB = CollisionFuncs.OBBtoAABB(tank.collider);

                if (CollisionFuncs.AABBcolliding(aabb, otherAABB))
                {
                    //If they are colliding, check OBB collision
                    if (CollisionFuncs.OBBcolliding(collider, tank.collider))
                    {
                        //They are colliding, so reset transform
                        local = global;
                        return;
                    }
                }
            }
        }