Jump to content


Photo

find objects in a rectangle


  • Please log in to reply
5 replies to this topic

#1 krymly

krymly

    GMC Member

  • New Member
  • 244 posts

Posted 24 August 2012 - 06:47 AM

I am making a selection square, rts style. However I run into the following problem: I have a drawn selection square, but i want an array with the unique ID's for the units in that square, and with in I do not mean the origin of the object, but rather the sprite (or part of it) so that even if the origin lies out of the square and the sprite is partly in it it is still selected.

Now, I have thought about accomplishing this by searching for all unit ID's of all selectable objects and check object for object if it lies within the region of selection, however this sounds like a really inefficient way to do it, and it still does not tackle the problem with the object origin.

Does anyone here have some suggestions on this?
  • 0

#2 Blazingocean

Blazingocean

    GMC Member

  • New Member
  • 27 posts
  • Version:GM8

Posted 24 August 2012 - 06:59 AM

Make a rectangle object and just use collisions?
  • 0

#3 krymly

krymly

    GMC Member

  • New Member
  • 244 posts

Posted 24 August 2012 - 07:14 AM

How did you have that in mind? using collision_rectangle? as I do not see how that one gives me the unique unit ID's, or making an invisible stretched object that checks for collissions with selectable objects? even if that last is the case, I cannot figure how it could grant me an array of unit ID's
  • 0

#4 Blake

Blake

    GMC Member

  • GMC Member
  • 503 posts
  • Version:GM:Studio

Posted 24 August 2012 - 07:30 AM

What you can do is call collision_rectangle(), take the id of the one instance it finds, move that instance out of the way and repeat. Then afterwards, move them all back to their proper positions. Something like this should do it:

//Define a ds_list in the create event. I prefer to use these over arrays as you can easily get the size of them.
list_selection = ds_list_create();

//Run this to get the unit id's in the left mouse released event or similar.
ds_list_clear(list_select);
repeat(1000)
{
    c = collision_rectangle(x1,y1,x2,y2,obj_unit,1,0);
    if c != noone
    {
        ds_list_add(list_select,c);
        c.x_prev = c.x; c.y_prev = c.y; //store x and y positions in variables so we can move the instances back afterwards
        c.x = -1000; c.y = -1000; //move the instance out of the way
    }
    else
    break; //break the loop if no more instances are found within the rectangle
}
    
for(i = 0; i < ds_list_size(list_select); i += 1;)
{
    a = ds_list_find_value(list_select,i);
    a.x = a.x_prev; a.y = a.y_prev; //move all the instances back to where they were
}

Edited by Blake, 24 August 2012 - 07:33 AM.

  • 0

#5 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 24 August 2012 - 08:48 AM

Or just make each instance check if itself is in the rectangle.
var list; // Needed to use the variable inside the with statement.
list = ds_list_create();
with (obj_unit) { // Loop through all instances
    // Check if it is in the rectangle. Don't forget to set "notme" to false!
    if (collision_rectangle(x1, y1, x2, y2, id, true, false)) {
        ds_list_add(list, id);
    }
}
// Now do stuff with the list

Edited by torigara, 24 August 2012 - 08:49 AM.

  • 0

#6 krymly

krymly

    GMC Member

  • New Member
  • 244 posts

Posted 24 August 2012 - 09:31 AM

Thanks for the relplies,
I tried using blakes method, and it works, sort of... However there is still one problem. When checking for collissions with the rectangle I notice that it does not only select the units, it also selects walls and floors around it. Any idea where I am doing it wrong?

Edited by krymly, 24 August 2012 - 09:32 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users