Jump to content

Need help from the code wizards in uderstanding this


Sabre

Recommended Posts

Hello. Long story short, I'm learning the basics of games programing and ran into a problem with my brain.

While learning basic bounding box collisions, I was given this example.

 if (this.position.X + this.size.X > otherSprite.position.X &&
                this.position.X < otherSprite.position.X + otherSprite.size.X &&
                this.position.Y + this.size.Y > otherSprite.position.Y &&
                this.position.Y < otherSprite.position.Y + otherSprite.size.Y)
                return true;
            else
                return false;

So, what it's doing, checking the position (in pixels) and the size (120 pixels in this case) and if the other sprite enters this space, the it returns true and then stuff happens.

What I can't get my head around is that if the other prite is less then position + size, doesn't that mean that the whole top left area would also make them collide? I typed in and ran the code and it works fine, so how does it know that?

Link to comment
Share on other sites

K i think i got what is happening here in this code, but i gotta few questions.

Is postition y the center of the sprite or the bottum,(for my own personal learning of how this works)

So, Imma not sure what the question is, is it:

you have your sprite, but the top left is left blank, because it is not part of your drawing.

Link to comment
Share on other sites

It messures from the top left point. To the top left is 0 0, and the bottom right is 120 120.

What I'm asking is, if it works out the position of B is less then possition and size of A, then they collide. How does it know not to have them collide when B is behind A.

For example. If A is at 20, 20 and B is at 10, 10, then how does the program know that they are not touching? Because 10 is less then 20.

Link to comment
Share on other sites

K, i may not know what i'm talking about, but imma try.(I haven't gotten to any programming with pictures..)

if (this.position.X + this.size.X > otherSprite.position.X &&

                this.position.X < otherSprite.position.X + otherSprite.size.X &&

                this.position.Y + this.size.Y > otherSprite.position.Y &&

                this.position.Y < otherSprite.position.Y + otherSprite.size.Y)

                return true;

            else

                return false;

so, it will only return true if, both this, //this.position.X + this.size.X > otherSprite.position.X// and this this.position.X < otherSprite.position.X + otherSprite.size.X, are true, so the enemy sprite has to be less then your sprites position plus it's length, and the enemy sprites position plus it's length has to be more then your sprites position, to be true, so that is between to points, and your picture is imbetween those two points

bla, i dunno if imma typing this right so imma draw a pic.

http://img32.imageshack.us/i/help1pc.jpg/

seems like a not good way to do it if you ask me,i think it would be better if it where from the center position, to the outside, rather then the length, from one side to the other, in yours im guessing size is the length from one side to the other, and not the middle, plus half it's size.

http://img12.imageshack.us/img12/8748/help2o.jpg

if ((this.position.X + this.size.X.one > otherSprite.position.X. &&

                this.position.X < otherSprite.position.X + otherSprite.size.X.one &&

                this.position.Y + this.size.Y.one > otherSprite.position.Y &&

                this.position.Y < otherSprite.position.Y + otherSprite.size.Y)||(  //OR OPERATOR NOT &&

                this.position.X+this.size.two..... basically same thing but two replacing one, where one is the size from the center right, and two is the size from center left)

                return true;

            else

                return false;

mebbe just ignore my righting, cause im in a hurry and didn't check to make shure my logic was sound.

and again, i could just be mumbling stupid ideas with no point, but imma trying.

Link to comment
Share on other sites

k wait, that original code you had worked right, you where just wandering y that happened, right?

Link to comment
Share on other sites

Yes, it worked fine, but I don't see how. That's what I'm asking.

The circle example you showed was also included for circles. It works out the middle but halfing it's width and height, using that as the radius, then whenever a sprite comes within radius of the sprite centre, then they colide. That makes sence. The first example was for bounding boxes.

Link to comment
Share on other sites

I dunno what a bound box is...

Heck i dunno anything I'm talking about, just making some guesses with the little knowledge I have...

Link to comment
Share on other sites

I only know Lua and the scripting used in GZDoom.  Though I must admit, I kinda get how other scripting works.

Link to comment
Share on other sites

Okay, this this.position and other.position is confusing me.

To rephrase:

size 1 is v,w

position 1 is x,y

size 2 is a,b

position 2 c,d

Is this statement mathematically equivalent to:

x + v > c AND x < c + a AND y + w >  d AND y < d + v?

I don't understand what "size" is, as you phrased it as a coordinate pair. Is it the opposite corner of the box?

It is really hard to tell with this code snippet. Can you post it in context? I especially want to know how "size" is defined.

EDIT: I think I'm on to something:

I will add that opposite corner of box 1 is 25, 25 and opposite corner of box 2 is 15,15 since I did not see any size values in your description.

Let's assume that size is the opposite corner. In the case of box 1, it is position + 5.

Size 1: 25,25

Position 1: 20,20

Size 2: 15, 15

Position 2: 10,10

20+25 > 15 -> 45 > 15 = TRUE

20 < 10 + 15 -> 20 < 25 = TRUE

20+25 > 15 -> 45 > 15 = TRUE

20 < 10 + 15 -> 20 < 25 = TRUE

TRUE AND TRUE AND TRUE AND TRUE = TRUE

Now let's assume that size is the number of pixels to the opposite corner, in this case 5.

Size 1: 5,5

Position 1: 20,20

Size 2: 5, 5

Position 2: 10,10

20 + 5 > 15 -> 25 > 15 = TRUE

20 < 10 + 5 -> 20 < 15 = FALSE

20 + 5 > 15 -> 25 > 15 = TRUE

20 < 10 + 5 -> 20 < 15 = FALSE

TRUE AND FALSE AND TRUE AND FALSE = FALSE

You get trues with a size value of 11, but at that point you have a physical collision anyway, so you'd want it true.

Link to comment
Share on other sites

size is the size of the sprite in pixels, in this case a 120 square.

this.position is the position of the sprites top left corner.

other.position is the top left corner of any other sprite.

The full load sprite section complete with hard coded velocity and start possition. Both use the ball sprite atm. Also the names mySprite are kind of stupid, but it's just to help you understand it.

// Load 2D texture sprite
            mySprite1 = new clsSprite(Content.Load<Texture2D>("ball"),
                new Vector2(0f, 0f), new Vector2(120f, 120f), 
                graphics.PreferredBackBufferWidth, 
                graphics.PreferredBackBufferHeight);
            mySprite1.velocity = new Vector2(1, 1);

           mySprite2 = new clsSprite(Content.Load<Texture2D>("ball"),
                new Vector2(218f, 118f), new Vector2(120f, 120f),
                graphics.PreferredBackBufferWidth,
                graphics.PreferredBackBufferHeight);
            mySprite2.velocity = new Vector2(3, -3);


x + v > c AND x < c + a AND y + w >  d AND y < d + v?
Yep, pretty much. What I don't get is how that means it doesn't bounce when it goes under. or over for example. Also, here's the screen boundry test.

        {
            //If we move out of the screen invert velocity

            //Checking Right Boundary
            if (position.X + size.X + velocity.X > screenSize.X)
                velocity = new Vector2(-velocity.X, velocity.Y);

            //Checking Bottom Boundary
            if (position.Y + size.Y + velocity.Y > screenSize.Y)
                velocity = new Vector2(velocity.X, -velocity.Y);

            //Checking Left Boundary
            if (position.X + velocity.X < 0)
                velocity = new Vector2(-velocity.X, velocity.Y);

            //Checking Top Boundary
            if (position.Y + velocity.Y < 0)
                velocity = new Vector2(velocity.X, -velocity.Y);

            //since we have adjusted velocity, now we add it to the current position.
            position += velocity;
Link to comment
Share on other sites

Ok. Let's do the math:

Size 1: 120,120

Position 1: 0,0

Size 2: 120, 120

Position 2: 218,118

120 + 0 > 218 = 120 > 218 = FALSE

0 < 218 + 120 = 0 < 338 = TRUE

0 + 120 > 118 = 120 > 118 = TRUE

0 < 118+120 = 0 < 238 = TRUE

FALSE AND TRUE AND TRUE AND TRUE = FALSE, no collision.

EDIT: I missed the velocity... give me a bit...

The new position after one cycle:

Size 1: 120,120

Position 1: 1,1

Size 2: 120, 120

Position 2: 221,115

120 + 1 > 221 = 121 > 221 = FALSE

1 < 221 + 120 = 1 < 341 = TRUE

1 + 120 > 115 = 120 > 115 = TRUE

1 < 115+120 = 1 < 235 = TRUE

FALSE AND TRUE AND TRUE AND TRUE = FALSE, no collision.

I don't feel like continuing this out ad infinitum, but I see mathematically that it seems to work.

The first inequality asks if the X coordinate of the opposite corner of box one is larger than the X coordinate of the origin of box 2. In this case, it is not.

The second asks if the X coordinate of the origin of Box 1 is less than the X coordinate of opposite corner of box 2. I this case, it is.

The third asks if the Y coordinate of the opposite corner of box one is larger than the Y coordinate of the origin of box 2. In this case, it is.

The fourth asks if the Y coordinate of the origin of Box 1 is less than the Y coordinate of opposite corner of box 2. I this case, it is.

In order for there to be a collision, all four must be true.

Link to comment
Share on other sites

The first inequality asks if the X coordinate of the opposite corner of box one is larger than the X coordinate of the origin of box 2. In this case, it is not.

The second asks if the X coordinate of the origin of Box 1 is less than the X coordinate of opposite corner of box 2. I this case, it is.

The third asks if the Y coordinate of the opposite corner of box one is larger than the Y coordinate of the origin of box 2. In this case, it is.

The fourth asks if the Y coordinate of the origin of Box 1 is less than the Y coordinate of opposite corner of box 2. I this case, it is.

Ah right. So it's not asking for numbers of box 2 simpley to be less then numbers of box 1, it's also asking for them to higher in some cases?

Link to comment
Share on other sites

You're overthinking it. I find it helpful to not think of it as code, but rather a mathematical/logical question. One set is doing a greater than operation while the other is doing a less than operation.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...