Jump to content

Get a point/vector relative to nested object's location.


rainerpl
 Share

Recommended Posts

Hi,

Im looking for a way to place objects relatively to other nested objects. ( objects that have a chain of parents that have been moved, rotated ).
I searched the docs but couldnt find anything that could do what im looking for.

Ive tried position.add / addToRef etc but they dont take object's rotation/parent chain into account.

An example of what id like to do:
  A player turns 90 degrees in x direction. A weapon that is parented to the player, turns another "n" degrees.
  Now, how would i put a bullet at the tip of the weapon. ( without setting its parent = weapon ).
  After the bullet is created, it would move/follow objects in global space/coordinate system.
  
in pseudo code:
bullet = new Bullet();//extends Mesh.
bullet.position = player.weapon.position.clone().translate(BABYLON.Axis.Z, weapon_barrel_length, BABYLON.Space.LOCAL);

Note: position object doesn't have translate function, only mesh. 

My actual case is a bit different but this example explains it better.

 

Link to comment
Share on other sites

Great, ill give it a try soon and see how it works.

I was looking at the source code for:
locallyTranslate=function(t){this.computeWorldMatrix(!0),this.position=e.Vector3.TransformCoordinates(t,this._localWorld)},

Looks like it does something similar, but yeah, i didnt know what "e" is was in the code, and whats the thing with ( !0 ) in compute matrix call.

Link to comment
Share on other sites

4 hours ago, Deltakosh said:

Hello

For every mesh, you can get mesh.getWorldMatrix().

From this world matrix, just call matrix.getTranslation(0 and you're good to go :)

I tried the matrix.getTranslation, but it returns exactly the same object that i can get using 
mesh.getWorldMatrix().getTranslation() seems to be same as mesh.position

Im not sure how i can use it to get a point relative to the mesh.
All changes to mesh.position are done in global space, not local.

I could probably do something like this to get the point, but its a hack and id rather use a nicer solution.


mesh.translate(BABYLON.Axis.Z, 2, BABYLON.Space.LOCAL);
var relative_point = mesh.position.clone();
//move mesh back to undo the translation.
mesh.translate(BABYLON.Axis.Z, -2, BABYLON.Space.LOCAL);

 

/* Quick Update - I found a solution that works in my case. Below is what i used for future readers. */
relative_pos = player.calcMovePOV(0, 0, 10);
world_matrix = player.getWorldMatrix();
world_translation = world_matrix.getTranslation();

world_obj.position = world_translation.add(relative_pos);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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