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
  1. Home
  2. General Programming
  3. C#
  4. Collision

Collision

Scheduled Pinned Locked Moved C#
learning
21 Posts 6 Posters 0 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.
  • M MasterSharp

    Wait, but when I run the program, you can't see the rectangles. It's probably supposed to do that, but how do you use both collision and drawing? I get them seperate, but... Thnks for the help so far though.

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #8

    Like I said, Invalidate() forces a paint. And what Pete said, I was gong to say, too. You should have a collection of objects that know how to draw themselves. Your paint event should step over that collection, and pass the drawing to them. This lets you easily add as many rectangles as you like, and your paint event stays clean, no matter how many shapes you add in the future.

    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    P 1 Reply Last reply
    0
    • C Christian Graus

      Like I said, Invalidate() forces a paint. And what Pete said, I was gong to say, too. You should have a collection of objects that know how to draw themselves. Your paint event should step over that collection, and pass the drawing to them. This lets you easily add as many rectangles as you like, and your paint event stays clean, no matter how many shapes you add in the future.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #9

      I only said it because you are busy racking up about 1000 posts a day now (at least that's how it seems to us mortals).:)

      Deja View - the feeling that you've seen this post before.

      C M 2 Replies Last reply
      0
      • P Pete OHanlon

        I only said it because you are busy racking up about 1000 posts a day now (at least that's how it seems to us mortals).:)

        Deja View - the feeling that you've seen this post before.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #10

        LOL - either way, I thought to say it and didn't, so it was a fair addition to what I posted, I should have mentioned it. It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code. BTW, Nish pointed out to me that I've gone over the 40,000 post mark. That's got to be a sign that I need to get a life.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        P P 2 Replies Last reply
        0
        • C Christian Graus

          LOL - either way, I thought to say it and didn't, so it was a fair addition to what I posted, I should have mentioned it. It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code. BTW, Nish pointed out to me that I've gone over the 40,000 post mark. That's got to be a sign that I need to get a life.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #11

          Christian Graus wrote:

          It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code.

          I know - it's always nice to see them grow up. BTW - I haven't forgotten your CD, it's just that we're stuck in the middle of a damn postal strike here in the UK. I don't want it to "get lost" in the post.

          Deja View - the feeling that you've seen this post before.

          C 1 Reply Last reply
          0
          • P Pete OHanlon

            Christian Graus wrote:

            It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code.

            I know - it's always nice to see them grow up. BTW - I haven't forgotten your CD, it's just that we're stuck in the middle of a damn postal strike here in the UK. I don't want it to "get lost" in the post.

            Deja View - the feeling that you've seen this post before.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #12

            OK, cool. LMK when that resolves itself.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • M MasterSharp

              I've been reading this Dummies book for starters, (that's to the same people, they KNOW who they are...), but anyways, I'm trying to figure out some simple kind of collision detection for 2 squares, not per pixel. I've got some to work, but I need to know the correct way to do this, so if anyone would teach me a little on collision, I'd be very happy. Thanks in advance.

              M Offline
              M Offline
              Matthew Cuba
              wrote on last edited by
              #13

              FYI - If you are interested in theory with a little code - mind you it is C++ but that's not important, check out the book "Beginning Math and Physics for Game Programmers" by Wendy Stahler. It is pretty good and might help you with game development. ISBN is 0-7357-1390-1. Anyhow, I found it to be invaluable in my game/physics programming. Good Luck!

              “You can't teach people to be lazy - either they have it, or they don't.” -Dagwood Bumstead

              M 1 Reply Last reply
              0
              • C Christian Graus

                LOL - either way, I thought to say it and didn't, so it was a fair addition to what I posted, I should have mentioned it. It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code. BTW, Nish pointed out to me that I've gone over the 40,000 post mark. That's got to be a sign that I need to get a life.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                P Offline
                P Offline
                Patrick Etc
                wrote on last edited by
                #14

                Christian Graus wrote:

                It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code.

                Definitely a nice change :)

                Christian Graus wrote:

                Nish pointed out to me that I've gone over the 40,000 post mark

                :omg: :omg: :omg: :omg: :omg: :wtf: :omg: :wtf: :wtf: :beer:


                The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

                1 Reply Last reply
                0
                • P Pete OHanlon

                  I only said it because you are busy racking up about 1000 posts a day now (at least that's how it seems to us mortals).:)

                  Deja View - the feeling that you've seen this post before.

                  M Offline
                  M Offline
                  MasterSharp
                  wrote on last edited by
                  #15

                  What are you talking about 100 posts a day. I know I'm VERY questioning, but not that many even for exagerations (probably killed that), and I've calmed down a bit and took to a book!

                  P 1 Reply Last reply
                  0
                  • M Matthew Cuba

                    FYI - If you are interested in theory with a little code - mind you it is C++ but that's not important, check out the book "Beginning Math and Physics for Game Programmers" by Wendy Stahler. It is pretty good and might help you with game development. ISBN is 0-7357-1390-1. Anyhow, I found it to be invaluable in my game/physics programming. Good Luck!

                    “You can't teach people to be lazy - either they have it, or they don't.” -Dagwood Bumstead

                    M Offline
                    M Offline
                    MasterSharp
                    wrote on last edited by
                    #16

                    Thanks for the tip.

                    M 1 Reply Last reply
                    0
                    • M MasterSharp

                      Thanks for the tip.

                      M Offline
                      M Offline
                      MasterSharp
                      wrote on last edited by
                      #17

                      Okay, I have just one more question about the this whole mess. After you assign the graphics... private void Form1_Paint(object sender, PaintEventArgs e) { Graphics G = e.Graphics; G.DrawRectangle(new Pen(Color.Black, 3), 15, 15, 200, 150); G.DrawRectangle(new Pen(Color.Red, 3), 15, 15, 200, 150); (for example) ... how do you name those rectangles?

                      M 1 Reply Last reply
                      0
                      • M MasterSharp

                        What are you talking about 100 posts a day. I know I'm VERY questioning, but not that many even for exagerations (probably killed that), and I've calmed down a bit and took to a book!

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #18

                        MasterSharp wrote:

                        What are you talking about 100 posts a day

                        Not you - Chris. He's the most prolific poster on CodeProject.

                        MasterSharp wrote:

                        I've calmed down a bit and took to a book!

                        That's the spirit. I buy (and read) 3 or 4 computer texts every other month. Hopefully that helps to keep me sharp.

                        Deja View - the feeling that you've seen this post before.

                        1 Reply Last reply
                        0
                        • M MasterSharp

                          Okay, I have just one more question about the this whole mess. After you assign the graphics... private void Form1_Paint(object sender, PaintEventArgs e) { Graphics G = e.Graphics; G.DrawRectangle(new Pen(Color.Black, 3), 15, 15, 200, 150); G.DrawRectangle(new Pen(Color.Red, 3), 15, 15, 200, 150); (for example) ... how do you name those rectangles?

                          M Offline
                          M Offline
                          Martin 0
                          wrote on last edited by
                          #19

                          Hello, You have to be carefull with the GDI objects here (memory leak)! If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class. Same to Rectangles. But you have to dispose (free the resourcess) them in the dispose method of your class (Form). System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty; System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty; System.Drawing.Pen penBlack = new Pen(Color.Black, 3); System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need! private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } if(penBlack !=null) penBlack.Dispose(); if(penRed !=null) penRed.Dispose(); } base.Dispose( disposing ); } If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly. private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3)) using(System.Drawing.Pen penRed = new Pen(Color.Red, 3)) { rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } } Hope it helps!

                          All the best, Martin

                          M 2 Replies Last reply
                          0
                          • M Martin 0

                            Hello, You have to be carefull with the GDI objects here (memory leak)! If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class. Same to Rectangles. But you have to dispose (free the resourcess) them in the dispose method of your class (Form). System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty; System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty; System.Drawing.Pen penBlack = new Pen(Color.Black, 3); System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need! private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } if(penBlack !=null) penBlack.Dispose(); if(penRed !=null) penRed.Dispose(); } base.Dispose( disposing ); } If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly. private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3)) using(System.Drawing.Pen penRed = new Pen(Color.Red, 3)) { rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } } Hope it helps!

                            All the best, Martin

                            M Offline
                            M Offline
                            MasterSharp
                            wrote on last edited by
                            #20

                            Hey thanks, that's just what I needed!

                            1 Reply Last reply
                            0
                            • M Martin 0

                              Hello, You have to be carefull with the GDI objects here (memory leak)! If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class. Same to Rectangles. But you have to dispose (free the resourcess) them in the dispose method of your class (Form). System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty; System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty; System.Drawing.Pen penBlack = new Pen(Color.Black, 3); System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need! private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } if(penBlack !=null) penBlack.Dispose(); if(penRed !=null) penRed.Dispose(); } base.Dispose( disposing ); } If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly. private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3)) using(System.Drawing.Pen penRed = new Pen(Color.Red, 3)) { rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } } Hope it helps!

                              All the best, Martin

                              M Offline
                              M Offline
                              MasterSharp
                              wrote on last edited by
                              #21

                              That just helped a lot, and there's SIMPLE collision detection! All I fond was 50 lines of code that was... extremely confusing and hard, but thatnks again to everyone who replied... Time to continue reading.

                              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