-
perceived center
I want to make an object move toward the center of all the other objects of the same type (instances). I know how to get the average position of all the objects (loop, add up all positions and then divide by NObjects) but how would I go about averaging the positions of all instances bar one? Also I want to do this for all instances so each one moves toward the average center of all the others.
Basically:
for each object:
if object != another instance of itself:
--> add x/ypos to SumPos
AvgPos = SumPos / (NObjects - 1)
I tried -
On loop:
+ id <> loopindex
--> add xpos to xsum
--> add ypos to ysum
and with ForEach -
fixedObj <> LoopFV
--> do stuff
But with this it seems to think that the center is an average between the left of the frame and the objects!
Also tried nested loops and all that but still no luck. Hope this makes sense, really would appreciate the help.
-
You're more or less there - not sure what the problems is?
http://skydrive.live.com/redir?resid...094271BBDA!528
+ Always
-> Set SumX to 0
-> Set SumY to 0
-> ForEach: Start Foreach loop "GetPos" for "Object"
+ ForEach: On ForEach loop "GetPos" for "Object"
-> Add X("Object") to SumX
-> Add Y("Object") to SumY
+ Always
-> Object: Set TargetX to (SumX-X( "Object" ))/(NObjects( "Object" )-1)
-> Object: Set TargetY to (SumY-Y( "Object" ))/(NObjects( "Object" )-1)
-
Can I just confirm that this figures out to the same as:
Object is not other objects
--> Add OTHER Objects to the count
or :
perceived center of other objects) -
pcQ = (b1.position + b2.position + ... + bQ-1.position +
bQ+1.position + ... + bN.position) / (N-1)
where (1 <= Q <= N)
rather than :
center -
c = (b1.position + b2.position + ... + bN.position) / N
Is this what's basically happening?
Cheers
-
Note this part:
-> Object: Set TargetX to (SumX-X( "Object" ))/(NObjects( "Object" )-1)
-> Object: Set TargetY to (SumY-Y( "Object" ))/(NObjects( "Object" )-1)
We're adding up the X and Y coordinates of every instance, but then each instance subtracts its own X and Y coordinates from those totals, and we divide by 1 less than the total number of objects - so yes, each instance is looking only at the others, and not itself.
-
Cool, I think I realised that but I wanted to be sure.
Can this be generalised to, say, finding distance? If object is not other objects (but still an instance), find the distance to the closest? I'm still having trouble with getting distances to other instances.
-
No, to find the distance to the closest instance you really some kind of nested loop, like in the examples I gave you previously.
-
Ok cool, thanks Muddy you're a legend.
-
1 Attachment(s)
Didn't want to start a new thread but just on the distance thing - why does this example not work with ForEach (I did your nested loop thing, but I read somewhere that it doesn't work for the same object)? Also why does the fastloop one only select one of the objects when it should select two that are close to each other?
Attachment 6631
Thanks!
-
Re: The fastloop version:
I don't know what you're expecting to happen, but this is what's actually going to happen:
It picks the newest object, and then highlights* all objects within 50 pixels of it.
It then moves on to the next newest object, de-highlights it, and then highlights all objects within 50 pixels of it.
It continues like this until it has highlighted all objects within 50 pixels of the oldest object - but some other objects will also have remained highlighted if they are within 50 pixels of an object older than themselves.
* By "highlighted", I mean that its animation frame is set to 1.
Re: The ForEach version:
You run a ForEach loop called "pos2", which will deselect* all except one object at a time. However, you also compare the object's fixed value against the LoopFV of a different ForEach loop called "pos", which will deselect all other objects except for the one that matches.
What that means is that the only time any objects at all will be selected, is when the same single object is selected by both ForEach loops, and in that situation nothing will happen because the X and Y positions will equal X1 and Y1.
* By "selected", I mean that it's included in the object scope - ie. actions will apply to it (see: http://www.create-games.com/article.asp?id=1942 ).
-
Fastloop: this is well and good, I'm testing the distance of each active against all other actives and highlighting any within a 50px radius. But I thought it would also select the one doing the testing, seeing as it is also being tested by another one? Is this not like running a nested for loop that compares each object in a list to each object in another list?
ForEach: ah, yep, but I don't know any way of nesting loops for the same object with ForEach, and I read here somewhere that perhaps it's not possible, but the reasons were unclear.
EDIT: Got the fastloop version to work setting to frame 0 before running the loop, but in a separate event before it. Seems you have to set initial conditions separately from running the fastloop. So... why can't foreach do this?