By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

picture collision

sgrec7
P: 59
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private sub form1_keypress (keyascii as integer)
  3.  
  4. if image1.top <= image2.top + 105 and image1.top >= image2.top - 105 then
  5. msgbox ("crashed")
  6. endif
  7.  
  8.  
(im using VB 6 this time BTW)

thanks
Sep 5 '07 #1
Share this Question
Share on Google+
12 Replies


sgrec7
P: 59
ohia guys... don't mean to rush but its an assignment... so any time soon would be nice
Sep 5 '07 #2

Expert 5K+
P: 8,434
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.

Expand|Select|Wrap|Line Numbers
  1. If B1X2 >= B2X1 AND B1X1 <= B2X2
  2.   Then they are "touching" horizontally.
  3.  
  4. If B1Y2 >= B2Y1 AND B1Y1 <= B2Y2
  5.   Then they are "touching" vertically.
  6.  
  7. If they're both true
  8.   Then BINGO!
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.
Sep 5 '07 #3

sgrec7
P: 59
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.

Expand|Select|Wrap|Line Numbers
  1. If B1X2 >= B2X1 AND B1X1 <= B2X2
  2.   Then they are "touching" horizontally.
  3.  
  4. If B1Y2 >= B2Y1 AND B1Y1 <= B2Y2
  5.   Then they are "touching" vertically.
  6.  
  7. If they're both true
  8.   Then BINGO!
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
Sep 5 '07 #4

Expert 5K+
P: 8,434
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 :)
Sep 5 '07 #5

sgrec7
P: 59
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
Sep 6 '07 #6

Expert 5K+
P: 8,434
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.
Sep 6 '07 #7

pbmods
Expert 5K+
P: 5,821
That's Mr. Internet to you.

:P
Sep 6 '07 #8

Expert 5K+
P: 8,434
That's Mr. Internet to you.
We're all friends here - you can just call me "The". :)
Sep 6 '07 #9

pureenhanoi
100+
P: 175
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private sub form1_keypress (keyascii as integer)
  3.  
  4. if image1.top <= image2.top + 105 and image1.top >= image2.top - 105 then
  5. msgbox ("crashed")
  6. endif
  7.  
  8.  
(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.
Expand|Select|Wrap|Line Numbers
  1. If (Image1.Top+Image1.Height)>=Image2.Top _     'move Image1 from top downto image2
  2.  Or Image1.Top<=(Image2.Top+Image2.Height) _  'move image1 from bottom upto image2
  3.  Or (Image1.Left+Image1.Width)>=Image2.Left _ ' move Image1 from Left  to Image2
  4.  Or Image1.Left<=(Image2.Left+Image2.Width) _ 'move Image1 from Right to Image2
  5.  Then
  6. Msg "Crashed"
  7. End If
  8.  
Sep 6 '07 #10

sgrec7
P: 59
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.
Expand|Select|Wrap|Line Numbers
  1. If (Image1.Top+Image1.Height)>=Image2.Top _     'move Image1 from top downto image2
  2.  Or Image1.Top<=(Image2.Top+Image2.Height) _  'move image1 from bottom upto image2
  3.  Or (Image1.Left+Image1.Width)>=Image2.Left _ ' move Image1 from Left  to Image2
  4.  Or Image1.Left<=(Image2.Left+Image2.Width) _ 'move Image1 from Right to Image2
  5.  Then
  6. Msg "Crashed"
  7. End If
  8.  
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
Sep 6 '07 #11

sgrec7
P: 59
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.)
Sep 6 '07 #12

sgrec7
P: 59
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
Sep 6 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.