Get position in front of a mesh including its angles

I want to have a child mesh follow a parent mesh but its always in front of it. Like a player model, I want it to always be in front of the players body which is subject to be changed depending on where the player is.

Its for spawning an item, I want the item to always drop in front of the player with angles accounted for.

Hi Abstract,

Will ordinary parenting be sufficient? If you make the child mesh child of the parent in the node hierarchy, it should follow the parent around like you’re describing.

Slightly over-elaborate example of parenting: https://www.babylonjs-playground.com/#3BKQ7B#1

Does that work, or have I misunderstood your use case? :slight_smile:

No, I don’t think parenting would work, think of a rpg and its rocket. the rocket needs to project from the position you’re facing with your character. The origin has to be infront of the player. I tried using a RayCast but it doesn’t seem to return vector of the last point of the end unless it hits something which isn’t always the case.

Hmm, sounds like I might not be understanding your use case. Can you show what you have in a Playground?

Taking your example of a rocket launcher, the rocket launcher always believes itself to be sending rockets forward — in the direction of (0, 0, 1) — in its local space. It doesn’t matter where the rocket launcher is or whether it’s really facing up, left, or wherever; it still thinks of the direction it’s facing as forward. Thus, if you stuck a projectile “in front of” the rocket launcher — perhaps at position (0, 0, 2) — then parented it to the rocket launcher, it would stay “in front” no matter how you moved the rocket launcher. If, for example, you moved the rocket launcher to (6, 3, 8) and rotated it so that it was “pointing” straight down, the projectile would be automatically moved to (6, 1, 8) because that position is “in front of” the rocket launcher — at local position (0, 0, 2) — even though it’s actually below the rocket launcher in world space.

Is any of that helpful? Or have I completely missed the boat again. :upside_down_face: If so, a Playground should help me better understand what you’re describing.

Here are three pictures, all pictures have a red box. I am trying to find the solution to how to perfectly get the position of the red box which accounts for the players angle.

Here is a simple (VERY VERY simple) RPG remember to click on viewport before using keyboard

Arrows up down forward and backward, arrows left and right rotate

Even if this is not what you are looking for it gives something for people to work with. A playground with a simplified focus on the part of the project you want help with always helps.

s to shoot rocket (aka a sphere) forward

h to hide red map, m to show red map, map uses person as parent

Even if this is not exactly what you want it gives people something to work on. A playground showing a much simplified version of a project focusing on the problem area will more likely get a good response.

I don’t see the link haha

ooops!!!:confused: Now edited in :slightly_smiling_face:

Very nice, however it seems to get the position in front I have to attach a mesh to the player to then get its position calculation. Is there a way to get the position of the red mesh without using a mesh? that would be the ideal situation here. I could like temporarily spawn one then delete it but i don’t feel that would be a good solution.

Sorry not following what you want!

If you just want the forward direction then lines 42, 43 do that, this has nothing to do with the red mesh.

Red mesh shows use of parenting, bullet shows use of direction.

That’s funny.

Last week I added code to my GeekTrains.com project to actually fire bullets in line with the weapon aiming and shooting from the gun front:

So in the picture above the (water)bullets exit the (water)cannon in the direction the cannon is aimed. The bullet spawn location is marked by the red cross which is attached to the local Z (forward) axis of the cannon. As the cannon is placed on a moving wagon, the solution is dynamic so relative to any place and rotation the cannon currently is in the scene.

The recipe (of many ways to do it probably):

  • child an aimer mesh to the parent thing firing (“a crosshair at (0,0,1) of the parent gun”)
  • spawn the bullet at the absolute position of the child mesh (“local (0,0,0) to world (x,y,z)…”)
  • use lookAt function to let the bullet rotation align with the absolute position of the crosshair

Is that something you were looking for?

In this PG when person is close enough to red mesh he picks it up and it stays with him https://www.babylonjs-playground.com/#NNT3VZ#2

So basically due to all these mixed answers, I provided a video that shows what I am trying to do.

I am trying to have my player carry a tree in front of them and stays in front of them while the player moves/changes angle. I kinda have it working with your proposed solution, but the “forward” direction seems to change as you can see in the video, the tree follows behind, then follows on the side, this should be consistently always in front of my player. When I mouse click again, the tree stops where it is currently at (uncarrying). I did a mesh.parent = null; mesh.position = absolutePositionBeforeParentNull and it seems to flicker when letting go.

Have you checked where your code differs from my PG? When and where you detach the camera makes a difference. The type of camera could make a difference as could using none arrow keys for movement (camera detachment not needed). Does the forward vector match the direction your person is looking at at the start of the game, I just set the initial direction as +Z arbitrarily.

Change the code in the PG to match your code, reproduce the errors and ask again.

By the way weekends tend to be quiet on the forum.

We saw this by Messaging. A solution has been found by redefining the pivot point that seems to have solved its problem.

2 Likes