Home   Help Search Login Register  

Author Topic: How to check if object inside defined area  (Read 2298 times)

0 Members and 1 Guest are viewing this topic.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
How to check if object inside defined area
« on: 07 Aug 2009, 18:37:02 »
I want to know whether an object is inside a undefined sized rectangular area.

For example four road cones are set to form the 4 corners of the area.
I want to know if a soldier enters that area. How would I do that?

Triggers can't be used, I can only use positions, as in positions of the cones and position of the soldier..


I'm a complete and utter math retard so please explain this like to a four-year-old   :-[
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: How to check if object inside defined area
« Reply #1 on: 07 Aug 2009, 21:06:30 »
Not really thinking straight today (have my head in some annoying script right now...) - so this will be messy.

For something like this:
Code: [Select]
[[coneTL, coneBR], mySoldier] execVM "checkSoldierInCones.sqf";Where TL = top left, and BR = bottom right.  You don't need any more to get the dimensions of the rectangle/square.

Try:
Code: (checkSoldierInCones.sqf) [Select]
_minx = (getPos ((_this select 0) select 0)) select 0; // X-coord of top-left cone
_miny = (getPos ((_this select 0) select 0)) select 1; // Y-coord of top-left cone
_maxx = (getPos ((_this select 0) select 1)) select 0; // X-coord of bottom-right cone
_maxy = (getPos ((_this select 0) select 1)) select 1; // Y-coord of bottom-right cone

_spos = getPos (_this select 1); // Soldier position
_sx = _soldier select 0;
_sy = _soldier select 1;

// Wait until soldier is in the area.
waitUntil { (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) };

hint format["%1 is in the area!", _soldier];

Of course, this probably isn't the easiest way to do it... but it is the simplest for 'explanatory purposes'.  You could add a sleep to the waitUntil to do checking less frequently.  However, using "&&" means that it'll just fail at the first failed condition, anyway... so it's hardly going to put any real burden on you :)

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: How to check if object inside defined area
« Reply #2 on: 07 Aug 2009, 21:18:25 »
Triggers can't be used, I can only use positions.

What do you mean? Theoretically, it can be done with with triggers, you just need to get the center of the area and half of each length of the trigger.

Alternative to what you are saying, that is,
it can be done with triggers, you just don't want to use them, that is a little different,
though, you could do something like what James said.

Hope that helps.

Luke
Pesky Human!!
Wort Wort Wort.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: How to check if object inside defined area
« Reply #3 on: 07 Aug 2009, 21:33:19 »
Quote
What do you mean? Theoretically, it can be done with with triggers
The example I gave is only an example, the purposes this will be used won't work with triggers.. :P

@JamesF1
Thanx, I'll give that a try.. :)
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: How to check if object inside defined area
« Reply #4 on: 11 Aug 2009, 09:35:33 »
@JamesF1
Hmm, that doesn't seem to work, 1 to 3 of the 4 checks return false no matter whether I'm inside or outside the "coned area".  :(

Which ones are true and which ones are false depends on which side of the "coned area" I am..
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: How to check if object inside defined area
« Reply #5 on: 11 Aug 2009, 13:09:51 »
It appears I made a number of errors in the script above... wow...  Here's a version I've just tested working.

Code: [Select]
_minx = (getPos ((_this select 0) select 0)) select 0; // X-coord of top-left cone
_miny = (getPos ((_this select 0) select 1)) select 1; // Y-coord of bottom-right cone
_maxx = (getPos ((_this select 0) select 1)) select 0; // X-coord of bottom-right cone
_maxy = (getPos ((_this select 0) select 0)) select 1; // Y-coord of top-left cone

_soldier = _this select 1; // Soldier position

// Wait until soldier is in the area.
while {true} do {
    scopeName "loop";
    _sx = (getPos _soldier) select 0;
    _sy = (getPos _soldier) select 1;

    if( (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) ) exitWith {
        hint format["%1 is in the area!", _soldier];
    };
};

While I'm at it, I'll just mention what was probably obvious, this will only work for square areas on a square angle (e.g. 0, 90, 180, etc.) - other angles need a more complex approach.
« Last Edit: 11 Aug 2009, 13:12:19 by JamesF1 »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: How to check if object inside defined area
« Reply #6 on: 11 Aug 2009, 21:34:22 »
Thanks, that works fine.. :)

Glad you mentioned that angle stuff, due to my math retardness I always forget that those play a part in these things too..  :confused:

I would indeed need this to work in any angle (the area itself stays rectangle, only the orientation changes).
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: How to check if object inside defined area
« Reply #7 on: 12 Aug 2009, 12:30:22 »
Alas, my own maths isn't spectacular enough to make this work on an angle without some significant time spent on it... Unfortunately, I don't have a significant amount of time free at the moment :(  I'll see if I can do something about it, but I really can't make any promises.

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: How to check if object inside defined area
« Reply #8 on: 13 Aug 2009, 16:46:33 »
I think there might be a way using trig(onomotry),

but this complex math would be why I suggested using trig(ger)s.  :P

h- I'm sure it can be done, I don't know how... But I'm sure it can be done.  :confused:

Luke
Pesky Human!!
Wort Wort Wort.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: How to check if object inside defined area
« Reply #9 on: 19 Aug 2009, 16:16:29 »
Quote
    if( (_sx>_minx) && (_sx<_maxx) && (_sy>_miny) && (_sy<_maxy) ) exitWith {
        hint format["%1 is in the area!", _soldier];

Ur.....isn't that
hint format ["%1 is in the area!",name _soldier]  ?

Or am i wrong????
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: How to check if object inside defined area
« Reply #10 on: 19 Aug 2009, 16:26:40 »
Nope, using _soldier by itself will yield the name when using a format statement ;)

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: How to check if object inside defined area
« Reply #11 on: 19 Aug 2009, 16:45:33 »
Ohh..................
At least i got a source of knowledge from you....
Thanks for that....
Haroon1992.................
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(