picking closest object

For questions about using Classic.

Post » Sat Apr 19, 2008 10:21 pm

I'm having some trouble trying to figure out how to make my player object pick the closest instance of another object.


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?
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sat Apr 19, 2008 10:38 pm

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.
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Sun Apr 20, 2008 8:32 am

Thanks for the answer :D

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?
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sun Apr 20, 2008 8:46 am

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. :oops:

I think it might be 'absolute' Abs(value).
B
37
S
19
G
18
Posts: 613
Reputation: 10,231

Post » Sun Apr 20, 2008 8:58 am

[quote="Ethan":2xkal17s]I think it might be 'absolute' Abs(value).[/quote:2xkal17s]

This does the trick! :D

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

Thanks guys!
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sun Apr 20, 2008 9:11 am

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.
B
2
S
1
G
4
Posts: 64
Reputation: 1,328

Post » Sun Apr 20, 2008 11:53 am

[quote="cecil":1mn03t21](x2 - x1)^2 + (y2 - y1)^2 = distance^2
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)[/quote:1mn03t21]

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.

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! :D
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sun Apr 20, 2008 1:12 pm

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
B
37
S
19
G
18
Posts: 613
Reputation: 10,231

Post » Sun Apr 20, 2008 4:42 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,590

Post » Mon Apr 21, 2008 6:13 pm

[quote="Ashley":1o44mrdz]
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.[/quote:1o44mrdz]

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 :oops: )


[quote="Ashley":1o44mrdz]It's worth noting the spread value/fastloop pattern is redundant in Construct - always use a For Each instead.[/quote:1o44mrdz]

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.
B
3
G
4
Posts: 28
Reputation: 1,133

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: GameOverBeast and 1 guest