# Draw an arrow between the surface of two meshes?

**URL:** <https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347>\
**Category:** Questions\
**Created:** [April 4, 2024, 3:03pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347 "2024-04-04T15:03:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 4, 2024, 3:03pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/1 "2024-04-04T15:03:24Z")

</div>

I’m trying to draw a arrow between the surface of two meshes.

Eventually the meshes will be moving and will be of arbitrary shape, so it’s necessary to dynamically calculate the starting and ending points of the line.

I’ve setup a simple demo using a Ray to calculate the points between the meshes:

> **[Babylon.js Playground](https://playground.babylonjs.com/#FR67LC%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Unfortunately, the Ray seems to pick an intercept point that is on the opposite face of the mesh from where I want it to start. (You can see the red line runs through the mesh rather than starting on the surface). I don’t understand why, since the Ray doesn’t even intercept that face of the mesh – the white line for the Ray starts in the middle of the mesh.

Any ideas of how to calculate the start and end of the line so that it’s on the surface of the meshes nearest to each other?

---

<div class="post-metadata">

**Author:** ![bvaisman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bvaisman/32/20835_2.png) [@bvaisman](https://forum.babylonjs.com/u/bvaisman)\
**Post date:** [April 4, 2024, 3:51pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/2 "2024-04-04T15:51:34Z")

</div>

May be something like that ?

> **[Babylon.js Playground](https://playground.babylonjs.com/#FR67LC%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 4, 2024, 4:26pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/3 "2024-04-04T16:26:43Z")

</div>

This unfortunately not universal. If you swap the positions of the meshes:

> **[Babylon.js Playground](https://playground.babylonjs.com/#FR67LC%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/2/5/25881b5396c2b1a6ec14244d24ad7d92dcf82e3f.png)

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 4, 2024, 5:16pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/5 "2024-04-04T17:16:53Z")

</div>

> [@apowers313](#):
>
> Eventually the meshes will be moving and will be of arbitrary shape,

This makes the whole thing damn time consuming to calculate.

If we just focus on faces you can use [Babylon.js docs](https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData) FaceData to get the faces with additional information. (we call them facets)

You need to calculate distance between the faces of mesh1 to mesh2. First you can filter the list of faces only to those pointing from mesh1 to mesh2 and vice versa to speed up finding the two closest faces. Use the [Babylon.js docs](https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#facet-normal) face normal to filter.

Then you can easily find the center of the faces (triangle):

```javascript
const facetVertices = [];
mesh.getFacetVertices(facetIndex, facetVertices); 
const center = BABYLON.Vector3.Zero();
for (let i = 0; i < facetVertices.length; i++) {
    center.addInPlace(mesh.getAbsolutePosition().add(facetVertices[i]));
}
center.scaleInPlace(1 / facetVertices.length);

```

Loop through the filtered facets to find the two with the lowest distance.

Quite interesting question, I’ll try to write a PG later.

🖖

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 4, 2024, 5:35pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/6 "2024-04-04T17:35:03Z")

</div>

> **[Babylon.js Playground](https://playground.babylonjs.com/#U3P6YP%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Highly unoptimized version!! This is not what I was writing about in my previous post.

Finds the closest two vertices and draws a line between them. Rotate the camera for a better view 🙈

🖖

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 4, 2024, 8:03pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/7 "2024-04-04T20:03:01Z")

</div>

Thanks for all the quick replies!

Not that beggars can be choosers, but using Facets or Vertices may lead to some strange results. Here’s your last example when using boxes instead of sphere-ish things:

> **[Babylon.js Playground](https://playground.babylonjs.com/#U3P6YP%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

I suspect that using Facts will lead to some strange endpoint alignment too, since a plane may be divided up into two large triangles and you’d end up pointing to the center of one of those two triangles.

Maybe there’s a solution if we flip the problem around the other way? Is it possible to translate a vector pointing from the center of a mesh to a point on the texture of the mesh?

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 4, 2024, 9:40pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/8 "2024-04-04T21:40:57Z")

</div>

> [@apowers313](#):
>
> I suspect that using Facts will lead to some strange endpoint alignment too, since a plane may be divided up into two large triangles and you’d end up pointing to the center of one of those two triangles.

You asked for arbitrary meshes and it’s difficult to get the closest two points on the surfaces of I emphasize here, arbitrary meshes. You can use the `Ray` approach only when you have a starting `Vector3` for the Ray and a direction `Vector3`. If it’s good enough for you to cast the ray from the center of one mesh towards the center of the second mesh it’s fairly easy to get the intersection points.

> [@apowers313](#):
>
> divided up into two large triangles

You can use `increaseVertices`:

> **[Babylon.js Playground](https://playground.babylonjs.com/#U3P6YP%235)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

There are multiple solutions here but I need to know how precise results you need. I’ll gladly help you to find the best one. 🙂 Could provide some more inputs on your requirements?

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 5, 2024, 12:36am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/9 "2024-04-05T00:36:57Z")

</div>

> [@roland](#):
>
> If it’s good enough for you to cast the ray from the center of one mesh towards the center of the second mesh it’s fairly easy to get the intersection points.

Yea, sorry my original post wasn’t very articulate. This is what I had in mind – draw a line between the center points of the two meshes and where that line intersects the surface of the two meshes would be the start and end points of the line. That way the movement of the line would be smooth as the objects move around. Lame attempt at a diagram below – I want to find the red points on the surface of the mesh along the dotted line between the blue centers of the meshes so that I can draw the green line. 🙂

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/c/9cfe2ec5817e388e0f821991665523d82a9363a9.png)

I like the Ray idea, but I can’t figure out how to get it to pick a point on the mesh that is along the line between the two centers. e.g. - the original PG I posted picks the wrong face of the box and your first PG demonstrates that swapping direction causes problems.

Hopefully that’s a better description?

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 5, 2024, 12:45am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/10 "2024-04-05T00:45:44Z")

</div>

> [@apowers313](#):
>
> Lame attempt at a diagram below

It’s cool! I like it 🙂

> [@apowers313](#):
>
> Hopefully that’s a better description?

Absolutely! Let me create a PG. (2:44 AM here, maybe I will fall asleep before finishing it. If so, I’ll get back to you tomorrow)

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 5, 2024, 1:07am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/11 "2024-04-05T01:07:08Z")

</div>

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Here you go! Hopefuly you’ll manage to add the arrow cap yourself 😉 I’m going to sleep…  
🖖

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 5, 2024, 1:12am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/12 "2024-04-05T01:12:05Z")

</div>

You’re amazing, thanks so much! 🖖

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 5, 2024, 1:12am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/13 "2024-04-05T01:12:38Z")

</div>

You are very welcome!

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 5, 2024, 1:15am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/14 "2024-04-05T01:15:32Z")

</div>

Please mark as a solution. Thanks!

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 5, 2024, 2:24am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/15 "2024-04-05T02:24:21Z")

</div>

I don’t get how this works. When I comment out the render events and just draw the Ray once it doesn’t connect to the surface of the mesh. Is there something magical that happens with Rays during rendering?

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 5, 2024, 5:19am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/16 "2024-04-05T05:19:37Z")

</div>

After you modify the `position` and/or `direction` of the `Ray` just call `scene.render()` to update everything on the scene and get the proper intersections.

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Or check for the intersections after the scene is ready.

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 8, 2024, 5:12am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/17 "2024-04-08T05:12:20Z")

</div>

> Hopefuly you’ll manage to add the arrow cap yourself

Man, I tried. I can’t figure out how to subtract the arrow cap length from the length of the line. Also, re-creating it on every render seems wrong. There must be a better way…

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%237)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 9, 2024, 5:50pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/18 "2024-04-09T17:50:14Z")

</div>

Kinda got arrow caps working. Not sure why I need the `fudgeFactor` variable to adjust the end of the line offset.

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%239)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 9, 2024, 6:29pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/19 "2024-04-09T18:29:18Z")

</div>

Oh sorry I totally forgot about your question.

By adding the arrow cap you make the line longer by the length of the cap so you have to draw the line shorter.

I’m glad you made it yourself. Maybe you could write a `drawArrow` helper function and share with the community 😉

---

<div class="post-metadata">

**Author:** ![apowers313](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/apowers313/32/38132_2.png) [@apowers313](https://forum.babylonjs.com/u/apowers313)\
**Post date:** [April 10, 2024, 10:33pm UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/20 "2024-04-10T22:33:13Z")

</div>

I figured if you ignored me too long and I was stuck I’d just open a new topic. 😉

I still don’t understand why I need the `fudgeFactor` in this PG. When the arrow cap is 0.1 units long, I have to make the line 0.3 units shorter so that everything lines up okay… any insights?

> **[Babylon.js Playground](https://playground.babylonjs.com/#8E7PN9%239)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Happy to write a `drawArrow` helper. Were you imagining that as a separate project or a pull request?

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [April 11, 2024, 4:33am UTC](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347/21 "2024-04-11T04:33:27Z")

</div>

> [@apowers313](#):
>
> ignored

Ignored is a too strong word 😃

> [@apowers313](#):
>
> I still don’t understand why I need the `fudgeFactor` in this PG. When the arrow cap is 0.1 units long, I have to make the line 0.3 units shorter so that everything lines up okay… any insights?

Let me see. Hang on…

> [@apowers313](#):
>
> Were you imagining that as a separate project or a pull request?

Maybe we could create helper functions for drawing various elemenrts. Circles, rectangles, lines with various configurable caps, arrows, stripes, SVG support (I have this one already partialy implemented, but is uses an external library to parrse the SVG), etc. The community could build a quite extensive library for various stuff. 💪

@sebavan @Deltakosh @Evgeni_Popov @RaananW  
What package would be the best place to put these helpers in? Or should it be a separate 3rd party npm package?

Thank you!

[Next page](https://forum.babylonjs.com/t/draw-an-arrow-between-the-surface-of-two-meshes/49347.md?page=2)
