Jump to content

Move object relative to the camera view


ShimShamSam
 Share

Recommended Posts

Unity has a built-in function on transforms called "TransformDirection" which you pass in a Vector3 and it makes the direction relative to the transform. If you call this on the camera, you have an easy way to handle movement relative to the view. Does Babylon have something similar?

 

I could probably replicate it if I had access to the camera's view normal.

Link to comment
Share on other sites

I think I have figured it out. For future reference, here is the code I used:

// This is just some random movement vector.// Your code will probably use inputs to determine the movementsvar movement_vector = new BABYLON.Vector3(0.8, 12, 4);// This is the important partvar direction = BABYLON.Vector3.TransformNormal(movement_vector, camera.getWorldMatrix());

Let me know if there is a better way to do it.

Link to comment
Share on other sites

  • 3 years later...
On 11/21/2013 at 4:38 AM, Deltakosh said:

This is a good solution :) Babylon.js also provides you a setLocalTranslation function:

https://github.com/BabylonJS/Babylon.js/wiki/How-to-handle-rotations-and-translations

Yo @Deltakosh I can't seem to find setLocalTranslation...

How would I handle a TransformDirection unity like function in BabylonJS ?

 

Link to comment
Share on other sites

23 hours ago, Deltakosh said:

You can use mesh.translate(BABYLON.Axis.X, 1.0, BABYLON.Space.LOCAL) for instance

Yo @Deltakosh ... If I can bug you for a bit... I checked out the mesh.translate

But this returns a Mesh... not the 'Transformed Direction'

What I am trying to do is provide a unity like api for come of the common feature of unity. Like getUserInput And moveWithPhysics

Can you PLEASE - PLEASE (When you get a chance) fill in these three functions so that I can add to the scene manager api:

 

// ************************************ //
// *  Public Transform Tools Support  * //
// ************************************ //
        
/** Transforms position from local space to world space. */
public transformPoint(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 {
    return position.clone(); // TODO: Transform position from local to world space
}

/** Transforms vector from local space to world space */
public transformVector(owner: BABYLON.AbstractMesh | BABYLON.Camera, vector:BABYLON.Vector3):BABYLON.Vector3 {
   return vector.clone(); // TODO: Transform vector from local to world space
}

/** Transforms direction from local space to world space. */
public transformDirection(owner: BABYLON.AbstractMesh | BABYLON.Camera, direction:BABYLON.Vector3):BABYLON.Vector3 {
    return direction.clone(); // TODO: Transform direction from local to world space
}

 

Thank You Very Mesh :):):)

 

Link to comment
Share on other sites

/** Transforms position from local space to world space. */
public transformPoint(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformCoordinates(owner.getWorldMatrix(), position);
}

/** Transforms vector from local space to world space */
public transformVector(owner: BABYLON.AbstractMesh | BABYLON.Camera, vector:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformNormal(owner.getWorldMatrix(), position);
}

I did not test it and did it from memory but should be good.

Few warnings:

- transformDirection and transformVector are the same (unless I did not get what is the difference)

- This will generate a new Vector3 on each call. You may want to add a xxxToRef as well

Link to comment
Share on other sites

7 hours ago, Deltakosh said:

/** Transforms position from local space to world space. */
public transformPoint(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformCoordinates(owner.getWorldMatrix(), position);
}

/** Transforms vector from local space to world space */
public transformVector(owner: BABYLON.AbstractMesh | BABYLON.Camera, vector:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformNormal(owner.getWorldMatrix(), position);
}

I did not test it and did it from memory but should be good.

Few warnings:

- transformDirection and transformVector are the same (unless I did not get what is the difference)

- This will generate a new Vector3 on each call. You may want to add a xxxToRef as well

Very Kool....

 

This is what I have now for get Forward, right and up vector and transform position and direction:

 

// ************************************ //
// *  Public Transform Tools Support  * //
// ************************************ //

/** Transforms position from local space to world space. */
public transformPosition(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformCoordinates(position, owner.getWorldMatrix());
}
/** Transforms direction from local space to world space. */
public transformDirection(owner: BABYLON.AbstractMesh | BABYLON.Camera, direction:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformNormal(direction, owner.getWorldMatrix());
}

// ************************************ //
// *  Scene Direction Helper Support  * //
// ************************************ //

/** Gets the blue axis of the owner in world space. */
public getForwardVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 {
    return owner.getDirection(BABYLON.Vector3.Forward())
}
/** Gets the red axis of the owner in world space. */
public getRightVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 {
    return owner.getDirection(BABYLON.Vector3.Right());
}
/** Gets the green axis of the owner in world space. */
public getUpVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 {
    return owner.getDirection(BABYLON.Vector3.Up());
}

 

Thanks @Deltakosh ... Yet Again :)

 

Link to comment
Share on other sites

9 hours ago, Deltakosh said:

/** Transforms position from local space to world space. */
public transformPoint(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformCoordinates(owner.getWorldMatrix(), position);
}

/** Transforms vector from local space to world space */
public transformVector(owner: BABYLON.AbstractMesh | BABYLON.Camera, vector:BABYLON.Vector3):BABYLON.Vector3 {
    return BABYLON.Vector3.TransformNormal(owner.getWorldMatrix(), position);
}

I did not test it and did it from memory but should be good.

Few warnings:

- transformDirection and transformVector are the same (unless I did not get what is the difference)

- This will generate a new Vector3 on each call. You may want to add a xxxToRef as well

xxxToRef.... that like a "In-Place" type deal where I don't create and return a new vector but instead pass in existing vector and just change the x, y and z ???

... hardly use the xxxRef unless I saw some example code using it... I assume it was some 'In-Place' update type deal :)

 

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...