Collision detection in c#

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Collision detection in c#

Post by mattykins »

So i'm working on a roguelike and it is coming along pretty good. However something extremely odd happened.

Essentially the way I have map generation setup is the game will generate a series of random numbers, and based on those numbers It will turn certain holes or monsters on or off. So lets say the first number it generates is 14.
It will then go through a series of if statements to pick which hole tile to turn on, and since the number is less than 20 it will turn on a hole at 205, 105.

So in order to get the player to not be able to walk through the hole, I had a collision detection code setup like this

Code: Select all

 private void btnDown_Click(object sender, EventArgs e)
        {
            if (HeroLocation.Y != 455)
            {
                if (HeroLocation.Y == 55 & HeroLocation.X == 205 & HoleX205Y105.Visible == true)
                {
                    
                }
                else
                 
                {
                    HeroLocation.Y = HeroLocation.Y + 50;
                    Hero.Location = new Point(HeroLocation.X, HeroLocation.Y);
                }
            }
So when the player presses the up button it checks to see if the player is already at the top of the map, Then if the player isn't it checks to see if its below a Hole at X205 and Y105, and if that hole is on (The maps get randomly generated so a hole in that location can either be on or off) The player will not move up.

That code worked perfectly until I added another if statement to check for a different hole.

Code: Select all

 private void btnDown_Click(object sender, EventArgs e)
        {
            if (HeroLocation.Y != 455)
            {
                if (HeroLocation.Y == 55 & HeroLocation.X == 205 & HoleX205Y105.Visible == true)
                {
                    
                }
                 if(HoleX505Y205.Visible == true & HeroLocation.X == 505 & HeroLocation.Y ==  155)
                {

                }
                else
                 
                {
                    HeroLocation.Y = HeroLocation.Y + 50;
                    Hero.Location = new Point(HeroLocation.X, HeroLocation.Y);
                }
            }
Now The user cannot pass through the second hole, but can walk right through the first hole.

This is a screenshot of the game thus far if it helps
Image
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Collision detection in c#

Post by Xaleph »

Whoaah! Looking good so far, but it seems you do it the long way, which is OK but it can be shortened a lot.

If you have a grid system ( which you appear to have.. ) you can just use math to do all the calculations.

Let`s say each square is 50 points ( or pixels, i don`t know ) you can then calculate every tile, incuding the hero and the holes. That way, you don`t have to keep variables for each individual tile, saves a lot of time.

Ok, back on topic, you have some logic errors, you should do the moving logic in a different method, the btn_click() event should allways be triggered otherwise you can get deadlocks ( very, VERY important to avoid those ) so inside the btn_click() method, you should check the movement, if the movement is equal to left, call a method, going right? Same method! Why? Because the btn_click() should be handled regardless of the movement.

Anyway, inside your new method you should get the movement direction, calculate whether or not you are allowed on that tile and if not, stop movement. That`s about it.

Offtopic once more: if you handle the grid differently, I recommend using an ArrayList. Inside that Array, add the Tiles ( new class ). Each tile has 3 or 4 properties, the X, the Y, the Visibility and a boolean whether movement is allowed. That means your array is going to get 100 Tile objects if you have a 10x10 grid. Now, on the updateMovement() method, you know the hero`s X and Y, you know the direction ( X+1, X-1, Y+1, Y-1) and all you have to do is loop trough the ArrayList and check if that particular Tile is accessible.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Collision detection in c#

Post by mattykins »

Hm. Thanks man! That sounds a lot easier. I will definantly use arrays for this, however I have to leave for school and there's finals this week so I dunno when I'll get around to it.

EDIT: So when I got home i started looking into arrays and arraylists in C#. And i'm extremely confused haha, How do I get an array to hold an x, y, and a boolean.
Post Reply

Return to “Beginner Help and Support”