Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 12th, 2006, 7:59 AM   #1
f00dz5734
Newbie
 
Join Date: Jun 2006
Posts: 2
Rep Power: 0 f00dz5734 is on a distinguished road
Object Collision

Hi, im making a game, or what is soon to be a game, and i cant seem to get the object collision right. I have to image boxes, one on the left side of the screen named Toon at (0, 3120) which is 180x480 and on on the ride side of the screen named Buwwet at (6000, 3240) which is 90X45. The objects are both heading towards the middle of the screen at a constant variable speed of 30, i need it to pop up a messagebox whenever these two (toon and Buwwet) collide with each other. Can anyone please help me? Thank you very much in advance =)
f00dz5734 is offline   Reply With Quote
Old Jun 12th, 2006, 8:24 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
constant variable

Have you thought about what coordinate conditions constitute an intersection? You need a mental perception before you start writing code. I'd suggest a few minutes with a pencil and graph paper.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 12th, 2006, 8:39 AM   #3
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
first:

WTF??

secnd: if they are moving in a line or any linar fasion then you can use some linear algbra with your constant rate of motion to predict at what time they will collide.

OR

IF obj1(x,y) = obj2(x,y) THEN msgBox("BOOM");


third: www.google.com it. i know that inst fun and is slow at times but it will help a lot.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jun 12th, 2006, 8:40 AM   #4
f00dz5734
Newbie
 
Join Date: Jun 2006
Posts: 2
Rep Power: 0 f00dz5734 is on a distinguished road
yes indeed i have, the reply u gave dosnt help me though, its already in progress and everything is working except for the collision, i gave the position of the two boxs, and what they are doing, they are not moving up and down, just left and right, and whenver they just happen to intersect with each other i need it to display a message box, but i dont understand, nor do i know how to get the message box to dislpay when they intersect. Writing with a pencil and graph paper wont help me when i dont know how to do it in the long run, tis why i asked this question earlier
f00dz5734 is offline   Reply With Quote
Old Jun 12th, 2006, 8:52 AM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
get a timer going. for each loop get the boxes to move, then check if the co-ordinates meet or overlap, do this by using the .left and .width of the images. this can be easily done using a single if statement in this case.
Seif is online now   Reply With Quote
Old Jun 12th, 2006, 8:53 AM   #6
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Quote:
Originally Posted by f00dz5734
i gave the position of the two boxs, and what they are doing, they are not moving up and down, just left and right,
show code
what is making them move??
are you using some sorta game scrippting engine?

Quote:
Originally Posted by f00dz5734
and whenver they just happen to intersect with each other i need it to display a message box, but i dont understand, nor do i know how to get the message box to dislpay when they intersect.
collion occurse when two object "touch" eachother or when their edges share the same space( x,y,z chords) like obj1(x,y) = obj2(x,y). so you need some sorta function that looks at every bodies x,y,z and make's sure they are not over lapping or tuching. how you do this depends on how you want to structure the whole program.

Quote:
Originally Posted by f00dz5734
Writing with a pencil and graph paper wont help me when i dont know how to do it in the long run, tis why i asked this question earlier
think out what is going on and what you are trying to do.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jun 12th, 2006, 9:23 AM   #7
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,195
Rep Power: 5 lectricpharaoh will become famous soon enough
The theory behind this is pretty simple, and based on several points:
  • Each object is represented as a rectangle
  • The coordinates of an object are expressed as the coordinates of its enclosing rectangle's upper-left corner
  • You know the dimensions of each object
  • From the coordinates and dimensions, you can determine the area occupied by each object
Now you decide on an algorithm. One approach is to check the edges of the area occupied by each object, and see if the edges of another object lie within it, like so:
Imagine A and B are rectangles. A is 10x10 pixels, and B is 20x20 pixels. A is located at 0,0 while B is located at 5,5. Thus, A occupies a horizontal span of 0 to 9, and a vertical span of 0 to 9, while B occupies a horizontal span of 5 to 24, and a vertical span of 5 to 24 (all ranges are inclusive).

Now we check if the leftmost edge of A (x coordinate of 0) lies within the horizontal range of B (5 to 24). Nope. No potential collision there. Now we check the right edge (x of 9), which does lie in the 5 to 24 range, so we have a possible collision. When we do the same for the vertical range checks, we get another potential collision, and by doing a logical AND of the two values (true AND true), we determine there was a collision.

For this to work, you need to check it from the point of view of both objects. For example, imagine the smaller A is entirely contained within the larger B. Checking to see if any of B's edges are within the range of A's edges will not yield a collision, though it's obvious they are in a state of collision. If you want to optimize this, you actually only need to check to see if the object with the smaller range has an edge within the larger object's range. In our case, we would check to see if A had edges within B's range, but in some cases, you will check one way for vertical, and another for horizontal (imagine if A was 5x10 pixels, and B was 10x5 pixels; it would be impossible to contain one entirely within the other). Thus, when checking the horizontal, you'd check if A's edges were in B's range, and for vertical, you'd do it the other way around. Then you AND the result, and viola, you've determined if they are colliding.

Another way to look at it is testing the corners, but it amounts to the same thing.

Anyways, hopefully I've shown you how you can think about the problem; you should know be able to code a function that tests two objects for collision, and returns true if they are in a collision state. Simply call this function every time you update the object locations, and you'll detect when they collide.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 16th, 2006, 2:46 AM   #8
brownhead
Programmer
 
Join Date: Mar 2006
Location: California
Posts: 37
Rep Power: 0 brownhead is on a distinguished road
Send a message via AIM to brownhead Send a message via MSN to brownhead
If you want to learn, look at the above post, if you want to leech, look at this post... I'd reccomend reading and understanding the above post then using this function... anyways, here ya go. Hope it helps
Private Function IsIntersect(Rect1 As RECT, Rect2 As RECT) As Boolean
Dim RectHold As RECT
IsIntersect = CBool(IntersectRect(RectHold, Rect1, Rect2))
End Function
And this is the RECT type:
Private Type RECT
	Left As Long
	Top As Long
	Right As Long
	Bottom As Long
End Type
HOpe that helped ya some
brownhead is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC