Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

Homing Help: Designate closest object?


Recommended Posts

I need help again :3;

I have my homing events set up like this;

Cause:

<b>Pick one of 'group: nuetral'</b>

'Group: Nuetral' is overlapping 'Homing Mask'

'Homing = 0'

Event:

Set 'Enemy X' to 'X of Group:Nuetral'

Set 'Enemy Y' to 'Y of Group:Nuetral'

So you see, I have it pick a random 'Group: Nuetral', which is any object that can be homed in on. This means my character might decide to home in on an enemy to the right of the screen when theres one to the left right next to her.

Anyway I can pick the object closest?

Thank you very very much!

Link to comment
Share on other sites

Hmmmm....... It should be possible, but wont be easy.

EDIT: Scroll down for tutorial

You'll need an array object 2d (with the height of the current number of objects and the width of 2)

Then, you'll have to spread the value 0 into all the objects, so they are numbered(that weird action does this). Then you'll have to calculate the distance between the character and each of them using a loop and the pythagorean theorem: sqrt((Xa - Xb)² + (Ya - Yb)²) and store them on the first column of the array, storing on the second column the spreaded value of that object.

Then you'll have to sort the objects on the array, according to their distance (least distance first). I'm not sure if there's an array object with sorting capabilities, buf if theresn't, you'll need to do it by your own.

What we'll do is called bubble sorting. It basicly consists of calling a loop inside other loop, until we sorted an array.

You'll need a flag initially set to false. You'll have to have a loop that itinerates(passes) from the position 0 to the position Lenght - 2 of it, comparing the distances (stored on the first column) of this position to the position after it. If it's greater, you swap the values (you'll need a counter to store the temporary value) and set the flag to true. If it's not, you do nothing.

Then you'll need to have one "infinite loop" that will be deactivaded when the flag is 0, and that will call the other loop, while not deactivated.

Then you get the first position of the sorted array(at the second column) and that's the spreaded value of the closest object. You can use it to catch the object.

Doing this in MMF is a complete pain. But doing it on a programming language would be much simpler.

Link to comment
Share on other sites

For each target:

Distance = (Your_X - Target_Y)^2 + (Your_Y - Target_Y)^2

Calculate the distance to the first target and remember it. Calculate the distance to the second target and compare it to the first.

If the distance to target #2 is less than that of target #1, forget the first distance and remember the new one. If it's more, then discard the second calculation and continue.

Once you are done checking all the targets, you'll have the closest one. (No sorting involved)

(Note: Technically the true distance is the square-root of the above equation, however you are only interested in the magnitude of the distance relative to the others, so you can safely forget about the square-root part.)

=Smidge=

Link to comment
Share on other sites

Here it is.

Btw, Smidge is right, you don't need to use the bubble sort :o.

But using it, you can have more control. For example, if the nearest object is on the left of Sonic(and he's facing right), maybe it's better for him to homming attack one object that's a little further (but is on the side he's facing)

  • Like 2
Link to comment
Share on other sites

But using it, you can have more control. For example, if the nearest object is on the left of Sonic(and he's facing right), maybe it's better for him to homming attack one object that's a little further (but is on the side he's facing)

This is a benefit, yes. This also makes it easier to do multiple homing attacks, since you can save the sorted list, adding/removing targets as they come and go from range. More work, but if that's an effect you want I'd say go for it.

Edt: Also, bubble sort is horrid for anything but small groups of things, like 20 or less. Probably not an issue here, but I can explain how to make a "modified shell sort" that will easily handle a hundred or so without being too complicated. :P

=Smidge=

Link to comment
Share on other sites

Wow. This..looks reallie difficult. Thank you so much for putting it in a .cca O_O I'll try it out and see if I can get it. Thank you!!

Below is a double post that has been automagically merged into the original.

Gah..you know what. I have no idea what I'm doing.

EDIT: EDIT: Wooo. Everythings fixed. It works perfectly. Thank you all so much!! n_n

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...