5 Replies - 1249 Views - Last Post: 19 February 2017 - 03:46 PM

#1 ViktorCoder96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-January 17

My RightCollision doens't work

Posted 17 January 2017 - 03:38 PM

Hi im just new with monogame/xna and i followed some tutorials for collision but my right Collision doesn't work.
My Sprite just walks through the tile! Can someone help me out please.
     static class CollisionKlasse
    {
        public static bool BovenCollision(this Rectangle Rectangel1, Rectangle Rectangel2)
        {
            return (Rectangel1.Bottom > Rectangel2.Top - 1 &&
                    Rectangel1.Bottom < Rectangel2.Top + (Rectangel2.Height / 2) &&
                    Rectangel1.Right > Rectangel2.Left &&
                    Rectangel1.Left < Rectangel2.Right);
        }

        public static bool BodemCollision(this Rectangle Rectangel1, Rectangle Rectangel2)
        {
            return (Rectangel1.Top < Rectangel2.Bottom + (Rectangel2.Height / 2) &&
                    Rectangel1.Bottom > Rectangel2.Bottom - 1 &&
                    Rectangel1.Right > Rectangel2.Left &&
                    Rectangel1.Left < Rectangel2.Right);
        }

        public static bool LinkerKantCollision(this Rectangle Rectangel1, Rectangle Rectangel2)
        {
            return (Rectangel1.Right < Rectangel2.Right &&
                    Rectangel1.Right > Rectangel2.Left &&
                    Rectangel1.Top < Rectangel2.Bottom &&
                    Rectangel1.Bottom > Rectangel2.Top);
        }
                                            //My Collision for Right
        [color="#A0522D"]public static bool RechterKantCollision(this Rectangle Rectangel1, Rectangle Rectangel2)[/color]
        {
            return (Rectangel1.Left > Rectangel2.Left &&
                    Rectangel1.Left < Rectangel2.Right &&
                    Rectangel1.Top < Rectangel2.Bottom &&
                    Rectangel1.Bottom > Rectangel2.Top);
        }

    } 


Is This A Good Question/Topic? 0
  • +

Replies To: My RightCollision doens't work

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: My RightCollision doens't work

Posted 01 February 2017 - 01:26 PM

I know this post is a couple weeks old at this point, but wanted to throw out my suggestion anyway:

I think the issue you're having is that you're comparing the left side of Rectangel1, when you probably want to compare the right side of Rectangel1 for right side collisions.

return (Rectangel1.Right > Rectangel2.Left &&
        Rectangel1.Left < Rectangel2.Right &&
        Rectangel1.Top < Rectangel2.Bottom &&
        Rectangel1.Bottom > Rectangel2.Top);


Was This Post Helpful? 0
  • +
  • -

#3 ViktorCoder96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-January 17

Re: My RightCollision doens't work

Posted 02 February 2017 - 03:01 AM

View PostKilorn, on 01 February 2017 - 01:26 PM, said:

I know this post is a couple weeks old at this point, but wanted to throw out my suggestion anyway:

I think the issue you're having is that you're comparing the left side of Rectangel1, when you probably want to compare the right side of Rectangel1 for right side collisions.

return (Rectangel1.Right > Rectangel2.Left &&
        Rectangel1.Left < Rectangel2.Right &&
        Rectangel1.Top < Rectangel2.Bottom &&
        Rectangel1.Bottom > Rectangel2.Top);



Thank you for your replay but i still have the same problem should i post my whole project in the post ?
Was This Post Helpful? 0
  • +
  • -

#4 basteagui  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 60
  • Joined: 08-September 13

Re: My RightCollision doens't work

Posted 19 February 2017 - 02:17 PM

why are you using the operation &&

that implies you want all of those things to be true.. shouldn't you be using this symbol instead:


||
Was This Post Helpful? 0
  • +
  • -

#5 ViktorCoder96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-January 17

Re: My RightCollision doens't work

Posted 19 February 2017 - 02:24 PM

View Postbasteagui, on 19 February 2017 - 02:17 PM, said:

why are you using the operation &&

that implies you want all of those things to be true.. shouldn't you be using this symbol instead:


||


I followed an tutorial on YouTube and he used the "&". I'm still an beginner in programming.
Was This Post Helpful? 0
  • +
  • -

#6 basteagui  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 60
  • Joined: 08-September 13

Re: My RightCollision doens't work

Posted 19 February 2017 - 03:46 PM

try to change your code to only check 1 side at a time

instead of checking 4 conditions only check left > right.
and go checking each corner to try to identify the problem friend.

it's not easy to know what is wrong when you have 4 conditionals you need to check for and you are wondering if you wrote the whole thing correct.

if you try and see what happens with each of the sides 1 by one i am sure you can fix it by yourself and understand better what the code is doing!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1