Blockland Forums > Modification Help

Doing a container search in a rectangular prism with two given points.

Pages: (1/2) > >>

otto-san:

Pretty much, I'm wondering how I'd let a player set two corner points and have a container search inside of those two points. (Much like WorldEdit on Minecraft, I guess.)

The only way I can think of is doing some vector math to find the centre of the prism and making the box size the size it'd need to be to fill the area perfectly.


What I currently have is this:


--- Code: ---function VectorComp(%a, %b)
{
%a = getWord(%a, 0) + getWord(%a, 1) + getWord(%a, 2);
%b = getWord(%b, 0) + getWord(%b, 1) + getWord(%b, 2);
return (%a > %b);
}

function VectorRectPrismCentre(%a, %b)
{
%r1 = vectorSub((VectorComp(%a, %b) ? %a : %b), (VectorComp(%a, %b) ? %b : %a));
%r2 = vectorScale(%r1, 0.5);
return %r2;
}
--- End code ---

Except that doesn't work in all cases.

I'm not great with vector math.

Port:

I'm not quite certain what you mean. Could you explain in some other way?

What I'm understanding is, you're taking two vectors representing two corners of a rectangular area and you are trying to find all objects in between those points, correct?

otto-san:


--- Quote from: Port on February 10, 2012, 07:18:04 PM ---I'm not quite certain what you mean. Could you explain in some other way?

What I'm understanding is, you're taking two vectors representing two corners of a rectangular area and you are trying to find all objects in between those points, correct?

--- End quote ---
Exactly.

Port:


--- Code: ---function doStuff( %vec0, %vec1, %mask )
{
%center = vectorAdd( %vec0, vectorScale( vectorSub( %vec1, %vec0 ), 0.5 ) );
initContainerRadiusSearch( %center, vectorDist( %vec0, %vec1 ) * 4, %mask );

while ( isObject( %obj = containerSearchNext() )
{
%pos = %obj.getPosition();
%dif0 = vectorSub( %pos, %vec0 );
%dif1 = vectorSub( %pos, %vec1 );

if ( getWord( %dif0, 0 ) < 0 || getWord( %dif0, 1 ) < 0 || getWord( %dif1, 2 ) < 0 )
{
continue;
}

if ( getWord( %dif1, 0 ) < 0 || getWord( %dif1, 1 ) < 0 || getWord( %dif1, 2 ) < 0 )
{
continue;
}

// do stuff with %obj
}
}

--- End code ---

There is probably a more efficient way, but this should work. Also, this code assumes that the first point is on the negative axis (e.g. 2 2 2 and 4 4 4, not 4 4 4 and 2 2 2, but that's an easy fix anyway).

otto-san:

Alright, I'll test that out.

edit:

Didn't exactly work.


--- Code: ---function doStuff( %vec0, %vec1, %mask )
{
%center = vectorAdd( %vec0, vectorScale( vectorSub( %vec1, %vec0 ), 0.5 ) );
initContainerRadiusSearch( %center, vectorDist( %vec0, %vec1 ) * 4, %mask );

while ( isObject( %obj = containerSearchNext() ) )
{
%pos = %obj.getPosition();
%dif0 = vectorSub( %pos, %vec0 );
%dif1 = vectorSub( %pos, %vec1 );

if ( getWord( %dif0, 0 ) < 0 || getWord( %dif0, 1 ) < 0 || getWord( %dif1, 2 ) < 0 )
{
continue;
}

if ( getWord( %dif1, 0 ) < 0 || getWord( %dif1, 1 ) < 0 || getWord( %dif1, 2 ) < 0 )
{
continue;
}

%obj.setColor(0);
}
}
--- End code ---



edit 2:

Just realised you did this:

initContainerRadiusSearch( %center, vectorDist( %vec0, %vec1 ) * 4, %mask );

The second argument has to be a vector, doesn't it?

Pages: (1/2) > >>

Go to full version