Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BABYLON.Mesh.prototype.{s,g}etLocalTranslation #116

Merged
merged 3 commits into from Jan 7, 2014

Conversation

gwenael-hagenmuller
Copy link
Contributor

Fixed BABYLON.Mesh.prototype.{s,g}etLocalTranslation to take into account the parent.
Renamed BABYLON.Mesh.prototype.{s,g}etLocalTranslation respectively by BABYLON.Mesh.prototype.setPositionWithLocalVector and BABYLON.Mesh.prototype.getPositionExpressedInLocalSpace
Added BABYLON.Mesh.prototype.locallyTranslate

BABYLON.Mesh.prototype.locallyTranslate adds the passed vector3 (expressed in local space; the origin of vector3 is the origin of the local space) after conversion to the parent space to the position (expressed in parent space)
whereas BABYLON.Mesh.prototype.setPositionWithLocalVector sets the position (expressed in parent space) to the passed vector3 (expressed in local space but which the origin is the origin of the parent space) once it has been converted.

The following code for BABYLON.Mesh.prototype.setPositionWithLocalVector could also be used. It explains itself the relation between setPositionWithLocalVector and locallyTranslate.

BABYLON.Mesh.prototype.setPositionWithLocalVector = function(vector3) {
    this.position = BABYLON.Vector3.Zero();
    this.locallyTranslate(vector3);
};

And here is a jsfiddle to illustrate it: http://jsfiddle.net/gwenaelhagenmuller/35uFf/12/

Fixed BABYLON.Mesh.prototype.{s,g}etLocalTranslation to take into account the parent.
Renamed BABYLON.Mesh.prototype.{s,g}etLocalTranslation respectively by BABYLON.Mesh.prototype.setPositionWithLocalVector and BABYLON.Mesh.prototype.getPositionExpressedInLocalSpace
Added BABYLON.Mesh.prototype.locallyTranslate

BABYLON.Mesh.prototype.locallyTranslate adds the passed vector3 (expressed in local space; the origin of vector3 is the origin of the local space) after conversion to the parent space to the position (expressed in parent space)
whereas BABYLON.Mesh.prototype.setPositionWithLocalVector sets the position (expressed in parent space) to the passed vector3 (expressed in local space but which the origin is the origin of the parent space) once it has been converted.

The following code for BABYLON.Mesh.prototype.setPositionWithLocalVector could also be used. It explains itself the relation between setPositionWithLocalVector and locallyTranslate.

BABYLON.Mesh.prototype.setPositionWithLocalVector = function(vector3) {
    this.position = BABYLON.Vector3.Zero();
    this.locallyTranslate(vector3);
};
@gwenael-hagenmuller
Copy link
Contributor Author

I updated https://github.com/BabylonJS/Babylon.js/wiki/How-to-handle-rotations-and-translations. I thought it would do a pull-request for it. Sorry. You probably want to change the wiki git repository settings to avoid this kind of mistake.

@deltakosh
Copy link
Contributor

I think there is an issue with :
this._worldMatrix = this._localWorld;

because if we do that, we cannot do the following (because in this case worldMatrix and localWorld are the same):
this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);

(There is no problem with the wiki update^^)

@gwenael-hagenmuller
Copy link
Contributor Author

so I could use this.worldMatrix = this.localWorld.clone() instead

Although this.localWorld.multiplyToRef(this.parent.getWorldMatrix(), this.worldMatrix) should never happen when this.worldMatrix = this.localWorld since it happens only when the mesh has a parent and the second one when it has no parent, you're right it could happen somewhere else.

@deltakosh
Copy link
Contributor

Yes but the cost of the clone is high when it comes to performance. I rather suggest duplicating this line:
this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);

(I agree the bug should never happen, but you know bugs... :))

@gwenael-hagenmuller
Copy link
Contributor Author

Works for me and thanks to you I learn new things :)

Left old functions with a comment (and a console.warn) on them to tag them as deprecated.
deltakosh added a commit that referenced this pull request Jan 7, 2014
BABYLON.Mesh.prototype.{s,g}etLocalTranslation
@deltakosh deltakosh merged commit 446b5e1 into BabylonJS:master Jan 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants