# Following a specific path

**URL:** https://forum.kirupa.com/t/following-a-specific-path/33498
**Category:** Uncategorized
**Created:** [October 20, 2003, 12:38pm UTC](https://forum.kirupa.com/t/following-a-specific-path/33498 "2003-10-20T12:38:46Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![assassin](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@assassin](https://forum.kirupa.com/u/assassin)
#### Post date: [October 20, 2003, 12:38pm UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/1 "2003-10-20T12:38:46Z")

</div>

How can i make an object following my cursor ( but not directly) through a specific path ( something like a city plan) and every time my cursor change location the object would change path so it will stop where my cursor is?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 20, 2003, 2:15pm UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/2 "2003-10-20T14:15:39Z")

</div>

try using hitTest or something like that.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 20, 2003, 2:30pm UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/3 "2003-10-20T14:30:00Z")

</div>

Are there multiple routes? Does it have to choose the fastest, like a mouse chasing cheese in a maze? If yes, search for “path finding algorithm”. If no - like if there is just one crazy path and the thing has to go either left or right (or forward/backward) along it, then you could just continually test the position of the mouse in relation to the object, and change the direction of the objects movement MC when the mouse is in a different spot.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 20, 2003, 2:34pm UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/4 "2003-10-20T14:34:43Z")

</div>

If I understand this correctly, then you want your object to be in the position on the path, that’s the shortest way from the cursor?

I don’t know how your path is made. Is it from straight lines, circles or curves?

Finding the point on a circle that’s nearest to another point is pretty easy. On a line it’s also doable. I wouldn’t want to code it for a curve… 🙂

If your path is made from several segements, then you have to test each segment and pick the one with the shortest distance.

There’s some code in ‘Graphics Gems’ by Andrew Glassner that can help you with circles and lines.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 21, 2003, 9:05am UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/5 "2003-10-21T09:05:00Z")

</div>

It will be only lines but as jingman says it may have to find the shortest distance! The example he used ( with the mouse) is what i’m thinking of!

By the way, i can’t understand how an object can find the shorter distance on a circle

thank all of you for your help!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 21, 2003, 9:31am UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/6 "2003-10-21T09:31:39Z")

</div>

Not that you need it, but just to show you how a movieclip can be set to find the point on a circle where it’s nearest to the mouse:

[AS]  
onEnterFrame = function() {  
circle\_radius = 100;

```
circlecenter_x = 150;
circlecenter_y = 150;

// Vector from circle center to mouse
x = _root._xmouse - circlecenter_x;
y = _root._ymouse - circlecenter_y;

// Normalize the vector
l = Math.sqrt(x * x + y * y);
x = x / l;
y = y / l;

// Calculate point on circle
mc_x = circlecenter_x + circle_radius * x;
mc_y = circlecenter_y + circle_radius * y;

// Move the movieclip
&lt;movieclip&gt;._x = mc_x; // &lt;- change to your clip name
&lt;movieclip&gt;._y = mc_y; // &lt;- change to your clip name

```

}  
[/AS]

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 22, 2003, 8:31am UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/7 "2003-10-22T08:31:31Z")

</div>

Thank you very much Hans Kilian for sending me the code.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [October 22, 2003, 10:18am UTC](https://forum.kirupa.com/t/following-a-specific-path/33498/8 "2003-10-22T10:18:26Z")

</div>

No problem. Glad I could help.
