Page 1 of 1
How to use Intersect detect a grc collide with many graphics
Posted: Wed Apr 23, 2008 10:37 am
by alex298
Hello,
I am making a very simple game.
1. A graphic "Ball" will be moving randomly in a card.
2. There are 20 same small graphics laying on the card too. The name of the small graphics may be all call grc "Brick" or grc "Brick 1", grc "Brick 2", ...... grc "Brick 20". I don't know which naming is suitable.
3. I am thinking to use Intersect () to detect when the grc "Ball" collide with ANY of the grc Bricks", for example:
if intersect (grc"Ball", ANY grc Brick) then ........
I don't know how to use Intersect function correctly to detect the grc "Ball" with ANY of the grc Bricks.
Thanks and best regards
Posted: Wed Apr 23, 2008 1:15 pm
by BvG
You need repeat to do an action on many targets. Untested Example:
Code: Select all
function anyBrickIntersect
repeat with x = 1 to 20
if intersect(graphic "Ball, graphic ("Brick" && x)) then
return "Brick" && x
end if
end repeat
return false
end anyBrickIntersect
Posted: Wed Apr 23, 2008 2:22 pm
by alex298
Dear BvG,
Thanks for your help.
Actually the 20 Bricks are just an example. The Bricks will be cloned and disappeared when some conditions meet. Therefore I really don't know exactly the number of the Bricks.
Is it possible that the number of Bricks are unknown?
Thanks and best regards
Posted: Wed Apr 23, 2008 3:01 pm
by malte
Hi alex,
my approach would be to keep a list of bricks that have been created in a variable. Then you could
Code: Select all
repeat for each line theGraphic in gAllBricks
if intersect(grc "ball",theGraphic) then
-- intersection occured
end if
end repeat
the variable gAllBrics needs to contain valid references to a control, each control on a line. E.g.:
grc "brick 1"
grc "brick 2"
...
grc "brick n"
Hope that helps,
Malte
Posted: Wed Apr 23, 2008 4:51 pm
by Nonsanity
The fastest method for this particular example would be to create a math formula that takes an x,y as input and returns a brick index. You can do this since the bricks are arranged in a gridded layout and don't overlap. So if the bricks were 100 pixels wide by 50 tall, 8 per row, you might check with:
get trunc( x / 100 ) + (8 * trunc( y / 50 )) + 1
if item it of gBrickList is 0 then return 0
put 0 into item it of gBrickList
return it
Where gBrickList is a global list initialized to "1,1,1,1," etc for however many bricks there are. So the grid would be numbered like so:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19... etc
If the bricks are arranged in the staggered layout that real bricks are, you'd just have to change the formula based on the even/oddness of trunc( y / 50 ), modding the x input by half a brick-width. If there are gaps in the wall at startup, just make those items in the global list 0.
The nice feature of this method is that it is very fast and pretty much the same speed regardless of the number of bricks.
With a larger ball, you may want to test against four x,y points to get a better simulation: the center of the four edges of the ball's rect. For more improvement, test them in an order so that the leading edge of the moving ball is tested first, the trailing edges tested last, or not at all.
Posted: Wed Apr 23, 2008 4:59 pm
by Nonsanity
I should say that in my last post I'm assuming the game is a version of the classic Brick Out, as depicted in this image I found in Google's image search:

Posted: Thu Apr 24, 2008 8:42 am
by alex298
Hi all,
Thanks for your help. I got it

I have one more question:
The Bricks will be divided into three portions - Left portion, Central portion and Right portion. There will be different effects when the Ball hits different portion of the Bricks.
It seems that Intersection do not allow to "detect" which portions of the grahics are intersected. How can I do that?
Thanks and best regards
Posted: Thu Apr 24, 2008 11:08 pm
by Nonsanity
If you know where the ball is and where the brick is you can use their X positions to figure out which third is hit (regardless of which method you're using for collision detection).
Code: Select all
____________________
| 1 | 2 | 3 | <-- MyBrick
div1 div2
put left of MyBrick + (width of MyBrick / 3) into div1
put left of MyBrick + ((width of MyBrick / 3) * 2) into div2
if item 1 of loc of MyBall < div1 then put 1 into WhichPartHit
else if item 1 of loc of MyBall < div2 then put 2 into WhichPartHit
else put 3 into WhichPartHit
You can make that more logically compact, but there's the wordy version for clarity.