We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have an array of narrow vertical rectangles. Each rectangle moves randomly back and forth (left to right) across the screen.
When *any of these overlap I want an event to happen, eg. println "overlapping". So far this works fine with 2 rectangles, but only by referring directly to their number and not array indices.
When I try indexing, I am running into the issue that, when a rectangle is checking if it overlaps another, it is also checking if it overlaps itself, and thus always returns "overlapping". I get what is happening but how to solve it is a little beyond my reasoning abilities.
Would anyone care to offer some tips? Many thanks.
Answers
In theory, for loop i over all rects and for each of them for loop j over all i+1 till the end of the array so that each pair of rect is checked
Chrisir
Thanks for the reply, Chrisir. I've been trying to put this in to practice but failing so far.
post your entire code here as text (not as image)
j++...
I corrected it above
That's my code. I guess line 58 is the issue:
if (many_Rects[i].overlaps(many_Rects[j])
looks ok to me.
what is the issue?
Sorry, I was using Sublime as my text editor and it was not building properly. Yes it works. Thanks very much for those tips, Chrisir (and koogs) . Much obliged :)
Sorry to bother you guys again.
Turning the screen white when two lines overlapped, (line 60 in code above), was just a test. My ultimate goal here was sound related.
I have a very long sample in the buffer which is mapped to the width of the screen. My desire is that, when any two lines intersect, the sample will trigger from that point in the buffer where they collide ie. if two lines collide in the far left of the screen the sound will trigger somewhere around the beginning of the sample and vice versa.
Sound triggers fine, but I am having two issues:
When the lines intersect, the sample is triggered continually in accordance with frame rate. I want it to trigger only once per intersection ie. the sound triggers on entry and not continually re-cueing for the duration that two lines overlap.
Despite my best efforts I am unable to get the sample to trigger at the point(s) of contact between the arrays of lines.
for (int i=0; i<many_Rects.length-1; i++) { for(int j=i+1; j<many_Rects.length; j++) { if (many_Rects[i].overlaps(many_Rects[j])) { println("OVERLAP"); int position = int(map( I_dont_know_What_To_Put_Here, 0, width, 0, groove.length() ) ); groove.cue( position ); } } }
Could I be a little cheeky and ask for some tips here?
Thank you!
For the first issue:
You could implement a variable „overlapped“ that initializes to false. If an overlap occurs set the variable to true. Else set it to false.
When your song is written from left to right and you have multiple rects moving up and down: the rects are colliding vertically in one column.
How many columns do we have from left to right? 100? How many rects are moving in each column, 2?
You need for loop over the columns and then use the nested for loop checking each rect in the current column against the others in that column.
Map: map(columnX or map(rectX....
Thanks for your help, Chrisir. Sorry for the delay. I am bouncing between forums for various language I have to learn this semester. did not realise I had not replied :)
;-)