Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Advanced Collision Detection with GDI+

Advanced Collision Detection with GDI+

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpgraphicshelplounge
6 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    david mindplay com
    wrote on last edited by
    #1

    Got a problem guys, I am writing a vector game with .net and gdi+. I've run into a bit of a brick wall and I need some advice. I am writing the game in vb.net but I have a general understanding of C# so if you reply to this message with C# code I will have no problem. The game map consists of many randomly sized rectangular obsacles placed randomly around at random rotations. In the game you pilot a ship around the map. When you apply forward force the ship starts accelerating in the direction it is pointing. If you rotate the ship it continues along it's original path until you apply force in a different direction. The "Ship" has a circular shield that surrounds it. Now, I am able to detect when the ship's shield collides with a rectangular obstacle by checking wheather or not the intersection of the ship's shield region and the rectangular obstacle's region has a size. My problem is... I want the ship to bounce off the rectangular obstacle at the appropriate angle. Anyone have any ideas on how to acomplish this? I wish I could tack on some screenshots to help explain. Feel free to ask me any questions if you need more info. Thanks dB dB

    P 1 Reply Last reply
    0
    • D david mindplay com

      Got a problem guys, I am writing a vector game with .net and gdi+. I've run into a bit of a brick wall and I need some advice. I am writing the game in vb.net but I have a general understanding of C# so if you reply to this message with C# code I will have no problem. The game map consists of many randomly sized rectangular obsacles placed randomly around at random rotations. In the game you pilot a ship around the map. When you apply forward force the ship starts accelerating in the direction it is pointing. If you rotate the ship it continues along it's original path until you apply force in a different direction. The "Ship" has a circular shield that surrounds it. Now, I am able to detect when the ship's shield collides with a rectangular obstacle by checking wheather or not the intersection of the ship's shield region and the rectangular obstacle's region has a size. My problem is... I want the ship to bounce off the rectangular obstacle at the appropriate angle. Anyone have any ideas on how to acomplish this? I wish I could tack on some screenshots to help explain. Feel free to ask me any questions if you need more info. Thanks dB dB

      P Offline
      P Offline
      pwinant
      wrote on last edited by
      #2

      Hi David, I don't pretend to understand collision detection/response well at all, but I was looking into this kind of thing a few months back and I ran across an article that might help. This may be more than you need since it seems like you're talking about a 2D problem as opposed to a 3D one but I think it will work out the same. In the formulas below, V is the velocity vector of your ship, N is the surface normal of the plane you are colliding with, Vn is the velocity in the normal direction, Vt is the velocity in the direction tangential to the surface normal, and V1 is the new velocity vector you're looking for. Vn = (N . V) * N <-- supposed to mean "the dot product of N and V, multiplied by N" Vt = V - Vn V1 = Vt - Vn I got this from the following gamasutra article (and there's a picture in there explaining what the different vectors represent): http://www.gamasutra.com/features/20000208/lander\_01.htm Hope this helps! Phil

      D 1 Reply Last reply
      0
      • P pwinant

        Hi David, I don't pretend to understand collision detection/response well at all, but I was looking into this kind of thing a few months back and I ran across an article that might help. This may be more than you need since it seems like you're talking about a 2D problem as opposed to a 3D one but I think it will work out the same. In the formulas below, V is the velocity vector of your ship, N is the surface normal of the plane you are colliding with, Vn is the velocity in the normal direction, Vt is the velocity in the direction tangential to the surface normal, and V1 is the new velocity vector you're looking for. Vn = (N . V) * N <-- supposed to mean "the dot product of N and V, multiplied by N" Vt = V - Vn V1 = Vt - Vn I got this from the following gamasutra article (and there's a picture in there explaining what the different vectors represent): http://www.gamasutra.com/features/20000208/lander\_01.htm Hope this helps! Phil

        D Offline
        D Offline
        david mindplay com
        wrote on last edited by
        #3

        Phil, Well I don't remember much of the math I lerned in High School, which is really my problem. Perhaps you can enlighten me. What exactly do you mean by "Velocity Vector"? I know the velocity and the bering, how do these relate to a "Velocity Vector"? Also, what in the world is a "dot product"? I actually figured out how to do it after staring at the screen for several hours last night, smoking cigarette after cigarette, but my method is quite a bit more complex than the one you describe seems to be. Thanks dB

        P 1 Reply Last reply
        0
        • D david mindplay com

          Phil, Well I don't remember much of the math I lerned in High School, which is really my problem. Perhaps you can enlighten me. What exactly do you mean by "Velocity Vector"? I know the velocity and the bering, how do these relate to a "Velocity Vector"? Also, what in the world is a "dot product"? I actually figured out how to do it after staring at the screen for several hours last night, smoking cigarette after cigarette, but my method is quite a bit more complex than the one you describe seems to be. Thanks dB

          P Offline
          P Offline
          pwinant
          wrote on last edited by
          #4

          I was afraid you'd ask that. ;P You seem to be in a 2D world with a bearing (presumably in degrees) and a velocity, so my answer will be based off that assumption. A 2D vector can be thought of as a line going from the origin of a cartesian coordinate system <0,0> to the point , so the vector can be represented with 2 scalar values x and y. To calculate the length of a vector we simply calculate the distance between the origin and the point at : length = sqrt( (x-0)*(x-0) + (y-0)*(y-0) ) = sqrt(x*x + y*y) You can think of the "length" of the velocity vector as the actual velocity of the ship, and the coordinates as the bearing. So the velocity vector contains both the direction and the velocity. If, for example, the ship is traveling at a 45 degree angle, the velocity in vector notation might be <1, 1> or <2, 2>, or <3, 3>, etc., depending on how fast it's going. If the vector is <1, 1> it means the length (i.e., velocity) is sqrt(2), if the vector is <2, 2>, the velocity is sqrt(8), etc. In your case you need to take the sine of the bearing as the X coordinate for the velocity vector, and the cosine of the bearing becomes the Y coordinate for the vector. Then you scale that vector by your velocity amount by multiplying the x and y component of the vector by that amount. Rather than get into vector dot products and cross products and other scary stuff, I think it would be better if I just refer you to another document that I think does what you're looking for. http://www.gamedev.net/reference/articles/article1019.asp This shows an implementation of 2D vectors and also a function for computing the angle to adjust a ball striking a pool table edge, which sound like the kind of thing you're doing. Look for the function "CBall::HitBoundary". If you want to read up on 2D vector math, etc. there's a million places you can go on the web. Here's a page that explains unit vectors and vector dot products: http://www.geocities.com/SiliconValley/2151/math2d.html Good luck! Phil

          D 1 Reply Last reply
          0
          • P pwinant

            I was afraid you'd ask that. ;P You seem to be in a 2D world with a bearing (presumably in degrees) and a velocity, so my answer will be based off that assumption. A 2D vector can be thought of as a line going from the origin of a cartesian coordinate system <0,0> to the point , so the vector can be represented with 2 scalar values x and y. To calculate the length of a vector we simply calculate the distance between the origin and the point at : length = sqrt( (x-0)*(x-0) + (y-0)*(y-0) ) = sqrt(x*x + y*y) You can think of the "length" of the velocity vector as the actual velocity of the ship, and the coordinates as the bearing. So the velocity vector contains both the direction and the velocity. If, for example, the ship is traveling at a 45 degree angle, the velocity in vector notation might be <1, 1> or <2, 2>, or <3, 3>, etc., depending on how fast it's going. If the vector is <1, 1> it means the length (i.e., velocity) is sqrt(2), if the vector is <2, 2>, the velocity is sqrt(8), etc. In your case you need to take the sine of the bearing as the X coordinate for the velocity vector, and the cosine of the bearing becomes the Y coordinate for the vector. Then you scale that vector by your velocity amount by multiplying the x and y component of the vector by that amount. Rather than get into vector dot products and cross products and other scary stuff, I think it would be better if I just refer you to another document that I think does what you're looking for. http://www.gamedev.net/reference/articles/article1019.asp This shows an implementation of 2D vectors and also a function for computing the angle to adjust a ball striking a pool table edge, which sound like the kind of thing you're doing. Look for the function "CBall::HitBoundary". If you want to read up on 2D vector math, etc. there's a million places you can go on the web. Here's a page that explains unit vectors and vector dot products: http://www.geocities.com/SiliconValley/2151/math2d.html Good luck! Phil

            D Offline
            D Offline
            david mindplay com
            wrote on last edited by
            #5

            Oh Yea, well that aught to make things a bit easier. Thanks!

            D 1 Reply Last reply
            0
            • D david mindplay com

              Oh Yea, well that aught to make things a bit easier. Thanks!

              D Offline
              D Offline
              david mindplay com
              wrote on last edited by
              #6

              Hey Phil, I've got one more problem. I've been taken the data you gave me and assembled it into a 2DVector class. I am almost finished but I am stuck on one thing. Is there an equasion to determine the bering in degrees from the vector coords? I can't seem to find the answer to this question anywere on the internet. I've been through pages and pages of search results for hours today and I'm starting to get a little pissed off. Thanks, dB

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups