Checking Multiple Objects with IsColliding

Started by Snake, Tue 19/05/2009 02:20:37

Previous topic - Next topic

GarageGothic

Oh yeah, there's a spelling mistake. "secondbjectcount++;" should be "secondobjectcount++;" of course. I fixed the above code.

Snake

Quote from: GarageGothic on Wed 20/05/2009 17:20:25
Oh yeah, there's a spelling mistake. "secondbjectcount++;" should be "secondobjectcount++;" of course. I fixed the above code.
Heh - We both have to pay a little more attention me thinks. You had misspelled RandomizePosition everytime too ;)

But anyway, man, I got it working and I think I've finally cracked the case with the over-lapping issue (since it ignored your new code too).

Besides your original code I had put in a check to see if any objects were out of bounds. No matter where I put any of the code, some objects were still overlapping.

Like you had brought up in one of your earlier posts, it didn't check to see whether any of the objects that were moved got placed on top of another one or not.

I noticed that in your new code, the boundries for the objects was already uncluded.
This finally sunk in my head and so I removed my code for checking the boundries, adjusted the boundries within your code and wa-la. So far after a bunch of testing I haven't seen any overlapping.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

Yeah, that's what happens when you write code directly on the forums instead of using the script editor. Next time I should probably test it before posting :)

Anyway, great to hear that it works now. Are you experiencing any slowdown when changing rooms? I was a bit worried about how many iterations it would take for the objects to find a non-overlapping position.

Snake

lol

I'm experiencing 0 slowdown so far and I expect that it'll stay that way.

I'm excited that it is working as well. Thank you, GG.

Here's one more conundrum for you:

I need to do the same thing for the bottom half of the screen (366-750) with ten more objects.
With your old code all I did was copy and paste it and change all the INTs slightly to make it work.

But with your new code it seems to be a bit more complicated than that. I'm finding it hard to understand how you conected you integers to those ten objects...

The bottom objects are objects11-20.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Khris

If I may, all you need to do is adjust the function a bit:

Code: ags
function PositionObjects(int first, int last, int x, int y, int w, int h) {
  int objectcount = first;
  while (objectcount <= last) {
    randomizeposition(objectcount, x, y, w, h); //Just some random numbers for the bounding box
    while (checkprevcollision(objectcount) == true) {
      randomizeposition(objectcount, x, y, w, h);
      }
    objectcount++;
    }
  }


Then call PositionObjects(0, 10, x, y, w, h); to position objects 0-10 in a bounding box @ x,y with width w and height h.
For the bottom objects, use PositionObjects(11, 20, x, y, w, h);

GarageGothic

#25
If the two screen areas don't intersect, you could optimize a bit (not that it's going to make much of a difference as the objects will never overlap) by adding a parameter for the first object to check against in checkprevcollision:

Code: ags
bool checkprevcollision(int objectid, int first) {
  int secondobjectcount = first;
  while (secondobjectcount < objectid) {
    if (object[objectid].IsCollidingWithObject(object[secondobjectcount]) == true) return true;
    secondobjectcount++;
    }
  }


and update KhrisMUC's PositonObjects to:

Code: ags
function PositionObjects(int first, int last, int x, int y, int w, int h) {
  int objectcount = first;
  while (objectcount <= last) {
    randomizeposition(objectcount, x, y, w, h); //Just some random numbers for the bounding box
    while (checkprevcollision(objectcount, first) == true) {
      randomizeposition(objectcount, x, y, w, h);
      }
    objectcount++;
    }
  }

Snake

#26
This is what it gave me:

GlobalScript.asc(536): Error (line 536): Function declaration has wrong number of arguments to prototype
Which is this line:
function PositionObjects(int first, int last, int x, int y, int w, int h) {


\edit/
Should I be replacing the code I have with this, or should I be making this one seperate?

Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

Did you remember to change the import in the header to the updated function? And yeah, just replace your code (and put in the first and last values into the function calls in your room script).

Snake

#28
No, I didn't, which I figured out a few minutes ago. It worked.


Right now I'm adjusting the boundries... the top objects seem to be clustering together, though not toughing, and the bottom objects are more spread out but some are off screen. I'll get back in a few minutes...

//--EDIT--//
I got the top half adjusted perfectly, couldn't get any better, but I can't seem to get the numbers right for the bottom half.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

Object coordinates are defined as the lower left corner of the object, so you should make sure to have at least an object's width of margin on the right hand side of the screen.

Snake

I don't know, I can't get right.

PositionObjects(11, 20, 50, 350, 690, 590); is what I came up with and either nothing shows up or there's only 3 or 4 objects on. I don't understand where I 'm going wrong.

Should I be doing fancy-dancy stuff like this, (200)+350?
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

I think you're mistaking the height/width values for coordinates. Looking at the numbers I assume the game is 800x600 resolution, but if your y-coordinate starts at 350 and the box is 590 pixels tall, that would mean 340 of those pixels are offscreen. If you want the box to end at y coordinate 590 it should only have a height value of 240.

Snake

Finally got it:
12, 366, 690, 230

Thanks for all your help and putting up with my lack of scripting ability. You have no idea how much I appreciate it.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk