0 Favourites

picking closest object

This forum is currently in read-only mode.
• 13 posts
• I'm having some trouble trying to figure out how to make my player object pick the closest instance of another object.

<img src="http://farm3.static.flickr.com/2287/2425640857_df5752fc31.jpg?v=0">

I want the orange box (the player) to move to the closest purple box. I have made all the events to make it move of course, but I can't figure out how to make the player actually pick the closest purple box. Any bright ideas guys?

• Yes make for those purple boxes a private variable "Distance" which will be the Distance from Orange Box, and then you have to make an event with checking the minimum value and then picking the object.

I tried simply setting "distance" to player.x-enemy.x

and then "pick with lowest variable".

This causes one problem though, if one of the enemy instances are to the left of the player, the value "distance" will be a negative value (like -200), and will therefore always be picked before a positive value (like 100).

see here for example cap: http://www.mediafire.com/?byy26uv0ybn

Is there a way to do this that I have missed? Or perhaps a way to change negative values into positive values?

• There's a maths thing you can use to get rid of negatives, i'm sure construct supports it.

I'm not good at maths so i don't know what it is, but it exists.

I think it might be 'absolute' Abs(value).

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I think it might be 'absolute' Abs(value).

This does the trick!

abs(player.x-enemy.x) works perfectly!

Thanks guys!

• i wouldnt reccomend using solely x for distance. i would get used to using

(x2 - x1)^2 + (y2 - y1)^2 = distance^2

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)

even if you didnt know about abs(), you coulda done some simple math to figure it out.

if you take a number and raise it to the power of 2, if its negative it becomes positive (negative * negative = positive). then all you have to do is take the radical (square root) of that and you end up with your original number, but positive.

• (x2 - x1)^2 + (y2 - y1)^2 = distance^2

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)

My brain hurts after reading that , my math skills are a bit rusty so I had to think about it for a little while, but I do get what you mean. Fortunately my game is so simple it only needs to calculate distance on the x axis, but I'll keep this in mind if I need it later on.

I have another problem though, I want to add more orange boxes, and have each orange box choose the purple box closest to them.

<img src="http://farm3.static.flickr.com/2411/2427819224_0a235ec3a0.jpg?v=0">

How do I make each orange box choose their own closest purple box? Needless to say, the solution with a distance variabel with the value abs(player.x-enemy.x) only seems to work with one player. I tried experimenting with the for each condition but it got me nowhere.

Any help is appreciated!

• Could you not use "for each" condition?

If you need to pick a specific object out of duplicates then you could use fast loops.

Give each duplicate a unique number, then use this number in conjunction with a fastloop to pick the object, eg.

Always > spread value 0 in private variables

Always > start loop "my loop"(get number of objects to loop for)

onloop "my loop"

+private variable = (which ever object you want to select) > do stuff

or to control them all with fastloops,

onloop "my loop"

+private variable = loop index "my loop" > do stuff

• Worth mentioning the 'Distance' expression - you can get the real distance using:

distance(Orange.X, Orange.Y, Purple.X, Purple.Y)

and that's always a positive value as well. (It does the sqrt(dx^2+dy^2) formula behind the scenes)

It's worth noting the spread value/fastloop pattern is redundant in Construct - always use a For Each instead. The Turret behavior can track objects in range, but if an object enters range it will track it until it leaves range (ignoring any objects which may move closer in that time) - perhaps a 'target closest' option would be handy for Turret...

I think the difficulty with finding the closest object with multiple orange and purple boxes is you have to first identify the closest one, then pick it with a separate event. You'd also have to track the targetted object being destroyed, other objects coming closer, etc. etc. I'll give you the events if you want, but I'd recommend the Turret behavior for now.

• You'd also have to track the targetted object being destroyed, other objects coming closer, etc. etc. I'll give you the events if you want, but I'd recommend the Turret behavior for now.

That's alright I don't need anything that advanced, and the turret behavior doesn't quite do what I want, but I've managed to solve the problem using a for each condition and storing the target location in private variables. (turns out I used the for each condition wrong the first time I tried )

It's worth noting the spread value/fastloop pattern is redundant in Construct - always use a For Each instead.

Good, cause I have no idea what a spread value is I've seen people talk about in some other posts on this forum when talking about mmf, but I had never heard of mmf before I read that post so I don't know much about it.

• Never heard of mmf?? Wow.. i thought everyone here was X-mmf-users.

• BTW since it's been a request a few times, the next build has an updated Turret movement which can target objects with a nearest-always method (which means if something else comes closer, it'll target that instead) - so it will be able to be used for finding closest objects too.

• BTW since it's been a request a few times, the next build has an updated Turret movement which can target objects with a nearest-always method (which means if something else comes closer, it'll target that instead) - so it will be able to be used for finding closest objects too.

Yeah that could be really handy. And if you find time to do it please add destructible terrain

Never heard of mmf?? Wow.. i thought everyone here was X-mmf-users.

I knew a little python before I started looking for game creation software, so I googled something like "game making software python" and somehow ended up on scirra.com and I started using construct, so I guess lucky googling kept me from mmf. (good thing cause I really prefer open source)

Anyways, I guess this is going off topic, and the problem I had is solved, so I'm just gonna.....uh......where is the "mark thread as solved" button?

• 13 posts