tsuikit159 / IERG3080-group-project

This is a group project from course IERG3080 group 12
0 stars 0 forks source link

Collisions #1

Closed tsuikit159 closed 10 months ago

tsuikit159 commented 10 months ago

TODO: make the collision work. Then Follow the project instructions. When the collision occurs, The player wins when its size is larger than the threshold specified in the stage. The player loses when it is completely absorbed by the other orbs.

Reference video : https://www.youtube.com/watch?v=VYXuGTefEFs&t=10s&ab_channel=FriendlyNeighborhoodProgrammer

Zappeddy commented 10 months ago

I have tried to amend the code in the video u provided in order to make it work.

Currently when the player collide with another object, the object will disappear and the player will slowly grow bigger.

I will publish the code after I have figured out all the mechanisms.

private async void Collide(string Dir) { foreach(var x in GameScreen.Children.OfType().ToList()) { if((string)x.Tag == "Collide") { Rect PlayerHB = new Rect(Canvas.GetLeft(Player), Canvas.GetTop(Player), Player.Width, Player.Height); Rect ToCollide = new Rect (Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);

        if(PlayerHB.IntersectsWith(ToCollide))
        {
            Canvas.SetTop(Player, Canvas.GetTop(Player) + SpeedY);
            SpeedY = 0;
            GameScreen.Children.Remove(x);
            for (int i = 0; i < 10; i++)
            {
                Player.Width += 2;
                Player.Height += 2;
                await Task.Delay(50);
            }

        }
    }
}

}

tsuikit159 commented 10 months ago

Great, I have also improved the precision with this function, which calculates the position and size of the circle and checks if they are overlapping.

public static bool CheckCollision(Ellipse e1, Ellipse e2) { var r1 = e1.ActualWidth / 2; var x1 = Canvas.GetLeft(e1) + r1; var y1 = Canvas.GetTop(e1) + r1; var r2 = e2.ActualWidth / 2; var x2 = Canvas.GetLeft(e2) + r2; var y2 = Canvas.GetTop(e2) + r2; var d = new Vector(x2 - x1, y2 - y1); return d.Length <= r1 + r2; } here is my version of collide().

public void collide() { foreach (var x in GameScreen.Children.OfType().ToList()) { if ((string)x.Tag == "circle") { if (CheckCollision(Player, x)) { if (Player.Width > x.Width) { GameScreen.Children.Remove(x); Player.Width += Player.Width / 10; Player.Height += Player.Width / 10; } else { MessageBox.Show("Game Over"); this.Close(); } } else if (CheckCollision(Sun, x)) { GameScreen.Children.Remove(x); Sun.Width += 2; Sun.Height += 2; }

    }
    else if (CheckCollision(Player, Sun))
    {
        MessageBox.Show("Game Over");
        this.Close();
    }

}

}