picture collision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgrec7
    New Member
    • Aug 2007
    • 58

    picture collision

    hey guys, guess what, i've got another problem for you!!

    i want it so that when image1 touches image2, a "crashed message" appears

    this dosen't work:

    Code:
    private sub form1_keypress (keyascii as integer)
    
    if image1.top <= image2.top + 105 and image1.top >= image2.top - 105 then
    msgbox ("crashed")
    endif
    (im using VB 6 this time BTW)

    thanks
  • sgrec7
    New Member
    • Aug 2007
    • 58

    #2
    ohia guys... don't mean to rush but its an assignment... so any time soon would be nice

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Hm... not so sure we should be helping with this, as we have a fairly strict anti-homework policy. However, in general terms, to detect the collision between two rectangular objects I imagine you could do something like this...

      (Thinking out loud here)

      Let's call the top-left coordinates of box 1 B1X1 / B1Y1 and the bottom right B1X2 / B1Y2.
      For box 2, call the top-left coordinates B2X1 / B2Y1 and the bottom right B2X2 / B2Y2.

      Code:
      If B1X2 >= B2X1 AND B1X1 <= B2X2
        Then they are "touching" horizontally.
      
      If B1Y2 >= B2Y1 AND B1Y1 <= B2Y2
        Then they are "touching" vertically.
      
      If they're [I]both[/I] true
        Then [B]BINGO![/B]
      Does that make any sense?

      If not, draw or cut out some squares (or play in a graphics program or something), move them around and have a look at how the corners relate to each other. That's what I did.

      Comment

      • sgrec7
        New Member
        • Aug 2007
        • 58

        #4
        Originally posted by Killer42
        Hm... not so sure we should be helping with this, as we have a fairly strict anti-homework policy. However, in general terms, to detect the collision between two rectangular objects I imagine you could do something like this...

        (Thinking out loud here)

        Let's call the top-left coordinates of box 1 B1X1 / B1Y1 and the bottom right B1X2 / B1Y2.
        For box 2, call the top-left coordinates B2X1 / B2Y1 and the bottom right B2X2 / B2Y2.

        Code:
        If B1X2 >= B2X1 AND B1X1 <= B2X2
          Then they are "touching" horizontally.
        
        If B1Y2 >= B2Y1 AND B1Y1 <= B2Y2
          Then they are "touching" vertically.
        
        If they're [I]both[/I] true
          Then [B]BINGO![/B]
        Does that make any sense?

        If not, draw or cut out some squares (or play in a graphics program or something), move them around and have a look at how the corners relate to each other. That's what I did.
        yea thanks killer, you've saved the day again, umm i know what my problem was.. i only catered for the vertical collision.

        but yea thanks for breaking the rules for little old me, and if its any help, i've okayed it with my teacher

        he said "I don't mind you getting help from the internet, but not from the year 12's"

        so yea my teacher refers to you as "the internet"

        thanks the internet

        sgrec7

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          We don't mind helping you to understand a subject. After all, that's pretty much what we're here for.

          Where we run into problems is a lot of students come here, paste in their homework questions and add something along the lines of "plz plz plz give me the code - urgent!". This is not only unethical, but plain old stupid. If we hand out homework answers, it doesn't help anyone, including the student.

          Signed: The Internet :)

          Comment

          • sgrec7
            New Member
            • Aug 2007
            • 58

            #6
            Hey there the internet

            I know what you mean, I have a little brother that always asks me how to get a note (he just started trumpet, I've been doing it for 5 years) there is no point telling him, I've got to give him strategies to help the situation.

            So yea thanks again

            sgrec7

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              No problem.

              Just one thing. When you get to differently-shaped objects, you'll have to go find some real collision-detection algorithms (which I know nothing about). That simple X/Y check only works or square/rectangular objects.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                That's Mr. Internet to you.

                :P

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by pbmods
                  That's Mr. Internet to you.
                  We're all friends here - you can just call me "The". :)

                  Comment

                  • pureenhanoi
                    New Member
                    • Mar 2007
                    • 175

                    #10
                    Originally posted by sgrec7
                    hey guys, guess what, i've got another problem for you!!

                    i want it so that when image1 touches image2, a "crashed message" appears

                    this dosen't work:

                    Code:
                    private sub form1_keypress (keyascii as integer)
                    
                    if image1.top <= image2.top + 105 and image1.top >= image2.top - 105 then
                    msgbox ("crashed")
                    endif
                    (im using VB 6 this time BTW)

                    thanks
                    Are you moving the Image1?
                    Do you want a crasshed message appear when they have the collision?

                    I dont know the method you've used to move the picture1. But the condition for collision seems incorrect.
                    Code:
                    If (Image1.Top+Image1.Height)>=Image2.Top _     'move Image1 from top downto image2
                     Or Image1.Top<=(Image2.Top+Image2.Height) _  'move image1 from bottom upto image2
                     Or (Image1.Left+Image1.Width)>=Image2.Left _ ' move Image1 from Left  to Image2
                     Or Image1.Left<=(Image2.Left+Image2.Width) _ 'move Image1 from Right to Image2
                     Then
                    Msg "Crashed"
                    End If

                    Comment

                    • sgrec7
                      New Member
                      • Aug 2007
                      • 58

                      #11
                      Originally posted by pureenhanoi
                      Are you moving the Image1?
                      Do you want a crasshed message appear when they have the collision?

                      I dont know the method you've used to move the picture1. But the condition for collision seems incorrect.
                      Code:
                      If (Image1.Top+Image1.Height)>=Image2.Top _     'move Image1 from top downto image2
                       Or Image1.Top<=(Image2.Top+Image2.Height) _  'move image1 from bottom upto image2
                       Or (Image1.Left+Image1.Width)>=Image2.Left _ ' move Image1 from Left  to Image2
                       Or Image1.Left<=(Image2.Left+Image2.Width) _ 'move Image1 from Right to Image2
                       Then
                      Msg "Crashed"
                      End If
                      thanks that looks better

                      i don't know if anyone has realized but this thread has turned into a big lol thread

                      but srsly, thanks for all the help

                      sgrec7

                      Comment

                      • sgrec7
                        New Member
                        • Aug 2007
                        • 58

                        #12
                        So, I'm guessing the pbmods is AMERICAN (no offense, but you're a little bit power hungry) (and [I hope you get this American joke] I give you an inch and you take a mile!!) (BTW I can take a joke) (when it's funny !!! :P)

                        But seriously, if you read correctly, it was my teacher that called KILLER42 (not you so there !!) the internet

                        So there again !!! :-)

                        Keep up the good work guys

                        sgrec7

                        (If you thought that that post made no sense, then you share my thoughts exactly.)

                        Comment

                        • sgrec7
                          New Member
                          • Aug 2007
                          • 58

                          #13
                          i think the poor old Aussie Kille... i mean the needs an Aussie thankyou from an Aussie

                          *sgrec7 looks for an Aussie*

                          *sgrec7 realizes that he is an Aussie*

                          thanks the


                          yae i made 50 posts !!!!

                          yae 4 me

                          Comment

                          Working...