6 Replies - 1390 Views - Last Post: 19 October 2013 - 02:34 PM

#1 XNAHelpme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Help with wall collision code for 2d Car object

Posted 16 October 2013 - 05:16 AM

I have the following excerpt from my player object code
  public void Collision(Rectangle newRectangle, int xOffset, int yOffset)
        {

            
            if (rectangle.TouchLeftOf(newRectangle))
            {
                position.X = newRectangle.Left - newRectangle.Width+ rectangle.Width/2;
                

            }
            if (rectangle.TouchRightOf(newRectangle))
            {
                position.X = newRectangle.Right + newRectangle.Width;

            }
            if (rectangle.TouchBottomOf(newRectangle))
            {

               position.Y = newRectangle.Bottom + newRectangle.Height - rectangle.Height / 2;
            }
            if (rectangle.TouchTopOf(newRectangle))
            {
               position.Y = newRectangle.Top -  rectangle.Height ;
               
            }



It is not working as I would hope and I have been messing with it for a while now. The Position.Y and Position.X variables belong to the player object. I want the player(rectangle) to collide with the wall (newRectangle) and not get stuck 1/2 way through or being shot off in one direction...just collide and be stopped in its tracks. Help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with wall collision code for 2d Car object

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Help with wall collision code for 2d Car object

Posted 16 October 2013 - 05:59 AM

position.X = newRectangle.Left - newRectangle.Width+ rectangle.Width/2;
position.Y = newRectangle.Bottom + newRectangle.Height - rectangle.Height / 2;


I'd say that math is wrong. It is probably causing the being stuck half way through the wall.

Also, depending on how your TouchXXOf() methods work, you might want to use else if statements, because it's possible that a given rectangle can overlap both sides, and thus both sets of code will be executed.
Was This Post Helpful? 1
  • +
  • -

#3 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 591
  • View blog
  • Posts: 1,317
  • Joined: 24-April 12

Re: Help with wall collision code for 2d Car object

Posted 16 October 2013 - 06:50 AM

Also, I generally test before I move. In other words, the movement is a "request" that is either granted or denied by the collision system. If no collision will occur it grants the request. If a collision will result, it denies the request and the object can't move.

The next step after that is to grant a partial request. So, the request is to move 4 units through the wall that is 2.6 units away. So, a collision is detected as a result of this request and the "answer" to the move request is "You can move 1.4 units in that direction". But the math can be a little tricky on this. Basically, it becomes a trigonometry problem at that point: "What is the angular distance to the collision point?"
Was This Post Helpful? 1
  • +
  • -

#4 XNAHelpme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Re: Help with wall collision code for 2d Car object

Posted 16 October 2013 - 10:24 AM

Thanks for both of you who replied. Mylo, I cant step through it at the moment but I would wager that you are correct and both sets of code is executing..resulting in the player shooting off in one direction. Looks like im likely going to have to spend some time learning some trig as well! Thanks for giving me a starting point either way!
Was This Post Helpful? 0
  • +
  • -

#5 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 591
  • View blog
  • Posts: 1,317
  • Joined: 24-April 12

Re: Help with wall collision code for 2d Car object

Posted 16 October 2013 - 08:53 PM

Yes. Trig gets used a LOT in game programming, even at the beginning level. Getting solid in trig will definitely help you.

I don't know how much trig you know, but the whole thing is built off of a hand full of rules/truths.

First, "trigon"s are three sided objects. We generally prefer to call them "triangles" instead, but 3 sides or 3 angles is 6 of one and half a dozen of the other. And anything that meters something measures it. Thermo-meters measure temperature. Speedo-meters measure speed. And so forth. So, tri-gon-o-metry is all about measuring the world using triangles. That's the whole thing in a nut shell. Except it turns out to be unbelievably useful for measuring things you can't touch like distances to planets we've never traveled to, things at the atomic scale, or things inside of imaginary computer generated worlds.

The first principle of trigonometry is rules regarding measuring things with right triangles. And it turns out that no matter how big or small a triangle is for a given angle of one of the two non-90 degree angles, the relationship between the sides is constant. So, if you know that angle you can know the ratio of the length of the sides. If you know the angle and one of the lengths, then you can know the exact length of the other and then can calculate all the angles and lengths of all the sides of the right triangle.

This is all based on the rule SOH-CAH-TOA.
Sine of an angle gives the Opposite side length divided by the Hypotenuse side length. Sine = Opposite/Hypotenuse
Cosine of an angle gives the Adjacent side length divided by the Hypotenuse side length. Cos = Adjacent/Hypotenuse
Tangent of an angle gives the Opposite side length divided by the Adjacent side length. Tan = Opposite/Adjacent

The Hypotenuse is the side of the triangle opposite the 90 degree angle. The side Adjacent is the other side (not the hypotenuse) touching the angle. And the side Opposite is the remaining side that is on the opposite side of the angle.

If you know that, you know the foundations of pretty much all of trigonometry.

You later learn that circles are just infinite triangles and so circles and triangles have these common relationships.

And so then you start measuring using circles and partial circles based on the idea that they are just a series of triangles.

And a key concept there is the radian which is based off the concept of Pi. No matter how large or small a circle is (whether it is the size of an atom or the size of a galaxy) the relationship between the distance across the circle (known as the diameter) and the distance around its outer edge (known as the circumference) is always the same. It is exactly Pi. Pi is an interesting number for a couple of reasons, but one is that it has an infinite number of decimal places. It never ends. You could spend the rest of eternity calculating Pi. So, we round off some place like 3.14. 2 times Pi is the relationship of the radius (or half the diameter) and the outer edge. So you can hopefully see that the distance of the circumference is (2)R(Pi) or double the radius and multiply by Pi. Usually it is written as 2PiR but if you know what it is you know its more accurate to write it as 2RPi since the 2 turns the radius back into a diameter.

Anyway, a radian is basically just a radius that is used as an angle of measure. Because the radius has this relationship to the circumference of a circle, we always know that a full circle is 2 times Pi radiuses. It doesn't matter how big the circle is or how big the radius is. 2Pi radiuses or radians is always a full circle. Or assuming Pi is 3.14 then a full circle is always 6.24 radiuses or radians. Just multiply the 6.24 times the radius length and you can know exactly how long that circumference is. But even without doing that you always know the full circle is 6.24 (or 2Pi) radiuses or radians how ever big a radius is.

That leads you to using 2Pi as an angle of measurement that equals 360 degrees. So, Pi is half a circle. Pi/2 is a quarter circle or 90 degrees. And Pi/4 is an eighth of a circle or 45 degrees. Combine them and 3 quarters of a circle is 3(Pi/2) or Pi+Pi/2 or 270 degrees.

Radians are something you will see all the time in computer games.

Anyway, that should get you off to a good start if you don't know trig. :-)

This post has been edited by BBeck: 16 October 2013 - 09:01 PM

Was This Post Helpful? 2
  • +
  • -

#6 tHc  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 63
  • Joined: 12-October 13

Re: Help with wall collision code for 2d Car object

Posted 19 October 2013 - 01:14 AM

What kind of wall do you mean ?
The screen bounds or a wall you have in the game screen or both ?
Is this game code from your head or are you following a tutorial ?

Sorry for all the questions just trying to get a better idea of what you actually want to do.

For screen bounds i use this in my pong game to stop the paddles moving off screen.
I have not tried the X direction i added that for you to see, as my paddle only moves in the Y direction

            // Here we set the paddle positons so they do not exit the screen            
            // if the top of the paddle hits the top of the screen stop
            if (paddlePosition.Y <= 0) paddlePosition.Y = 0;
            // if the top of the paddle hits the top of the screen stop
            if (paddlePosition.Y >= screenHeight - paddleHeight) paddlePosition.Y = screenHeight - paddleHeight;
            // if the top of the paddle hits the left side of the screen stop
            if (paddlePosition.X <= 0) paddlePosition.X = 0;
            // if the top of the paddle hits the right side of the screen stop
            // What going on here ? we take the screen width 1280 (Public property set in Game.cs) and simply take away 
            // the width of the paddle to determine our stoping point.
            if (paddlePosition.X >= screenWidth - paddleWidth) paddlePosition.X = screenWidth - paddleWidth;




For an in game wall i would just use basic bounding box collision like this

//  If rectangle 1 collides with rectangle 2
if (rectangle1.Intersects(rectangle2))
  {
   // Yes we have a collision do something
   // pseudo code stop player moving
      player.posX = player.posX;
      player.posY = player.posY;
  }



Hope that makes sense i have only started myself this month.

Regards

tHc

Sorry line 4 of the 1st code block was meant to say if the top of the paddle hits the bottom of the screen stop.
Was This Post Helpful? 0
  • +
  • -

#7 XNAHelpme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Re: Help with wall collision code for 2d Car object

Posted 19 October 2013 - 02:34 PM

View PosttHc, on 19 October 2013 - 01:14 AM, said:

What kind of wall do you mean ?
The screen bounds or a wall you have in the game screen or both ?
Is this game code from your head or are you following a tutorial ?

Sorry for all the questions just trying to get a better idea of what you actually want to do.

For screen bounds i use this in my pong game to stop the paddles moving off screen.
I have not tried the X direction i added that for you to see, as my paddle only moves in the Y direction

            // Here we set the paddle positons so they do not exit the screen            
            // if the top of the paddle hits the top of the screen stop
            if (paddlePosition.Y <= 0) paddlePosition.Y = 0;
            // if the top of the paddle hits the top of the screen stop
            if (paddlePosition.Y >= screenHeight - paddleHeight) paddlePosition.Y = screenHeight - paddleHeight;
            // if the top of the paddle hits the left side of the screen stop
            if (paddlePosition.X <= 0) paddlePosition.X = 0;
            // if the top of the paddle hits the right side of the screen stop
            // What going on here ? we take the screen width 1280 (Public property set in Game.cs) and simply take away 
            // the width of the paddle to determine our stoping point.
            if (paddlePosition.X >= screenWidth - paddleWidth) paddlePosition.X = screenWidth - paddleWidth;




For an in game wall i would just use basic bounding box collision like this

//  If rectangle 1 collides with rectangle 2
if (rectangle1.Intersects(rectangle2))
  {
   // Yes we have a collision do something
   // pseudo code stop player moving
      player.posX = player.posX;
      player.posY = player.posY;
  }



Hope that makes sense i have only started myself this month.

Regards

tHc

Sorry line 4 of the 1st code block was meant to say if the top of the paddle hits the bottom of the screen stop.



I did follow a tutorial from oyyou on YouTube, and it was a big help. This should help check it out http://www.youtube.c...h?v=TlHSNjeND9s
I am "clamping" the player object so that it cannot move outside of the playable area..thats not the issue. The collision code does seem to work but the real issue was what happens once we do have a collision. I have built my game world out of 2d tiles, and the player object could strike one of my walls (an instance of my tile class with the solid property set to true) from any angle, and could set off .touchTopof and touchLeft at the same time....anyways Ive added a flag so at least only one segment of the code fires at a time...but the math is still wrong and the player is not being placed where I would expect. Im going to have to come back to this one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1