![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 2
Rep Power: 0
![]() |
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 =)
|
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
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 |
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2006
Posts: 2
Rep Power: 0
![]() |
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
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#6 | |||
|
Hobbyist Programmer
|
Quote:
what is making them move?? are you using some sorta game scrippting engine? Quote:
Quote:
__________________
i dont know much about programming but i try to help |
|||
|
|
|
|
|
#7 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
The theory behind this is pretty simple, and based on several points:
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 |
|
|
|
|
|
#8 |
|
Programmer
|
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 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|