JohnK Posted October 2, 2017 Share Posted October 2, 2017 The problem Create a box fig 1 Use box.setPivotMatrix(BABYLON.Matrix.Translation(-0.5, 0.5, 0.5)); To set the pivot at the lower right front corner (shown by small grey sphere). fig 2 Position the box at (2, 0, 0) and note that it is the pivot that is positioned at (2, 0, 0). box.position = new BABYLON.Vector3(2, 0, 0); fig 3 Delete box.position and replace with box.translate(BABYLON.Axis.X, 2, BABYLON.Space.LOCAL); As you would expect since the local and world axes are in alignment you get the same result. fig 4 Now replace LOCAL with WORLD box.translate(BABYLON.Axis.X, 2, BABYLON.Space.WORLD); Since local and world axes are in alignment and translate is a vector displacement and the displacement is the same then you would expect the box to be at the same position. However it is not as is seen below. fig 5 Probable Issue Getting the position of the box for fig 2 it is at (0, 0, 0) ie the position of the pivot, the absolutePosition for the box in fig 2 is (-0.5, 0.5, 0.5) the position of the box's center in world space. So a translation in either local or world space would give the boxes position as (2, 0, 0) and its absolute position as (1.5, 0.5, 0.5) which it is for fig 3 using position and for fig 4 for translate in local space. However translation in world space gives a position of (1.5, 0.5, 0.5) and an absolutePosition of (1, 1, 1) = (1.5, 0.5, 0.5) + (-0.5, 0.5, 0.5) . It is out by the pivot vector. Tracking down translate in Mesh/babylon.abstractMesh.ts public translate(axis: Vector3, distance: number, space?: Space): AbstractMesh { var displacementVector = axis.scale(distance); if (!space || (space as any) === Space.LOCAL) { var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector); this.setPositionWithLocalVector(tempV3); } else { this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector)); } return this; } where for Space.WORLD there is a call to 'setAbsolutePosition' the code for which is public setAbsolutePosition(absolutePosition: Vector3): AbstractMesh { if (!absolutePosition) { return; } var absolutePositionX; var absolutePositionY; var absolutePositionZ; if (absolutePosition.x === undefined) { if (arguments.length < 3) { return; } absolutePositionX = arguments[0]; absolutePositionY = arguments[1]; absolutePositionZ = arguments[2]; } else { absolutePositionX = absolutePosition.x; absolutePositionY = absolutePosition.y; absolutePositionZ = absolutePosition.z; } if (this.parent) { var invertParentWorldMatrix = this.parent.getWorldMatrix().clone(); invertParentWorldMatrix.invert(); var worldPosition = new Vector3(absolutePositionX, absolutePositionY, absolutePositionZ); this.position = Vector3.TransformCoordinates(worldPosition, invertParentWorldMatrix); } else { this.position.x = absolutePositionX; this.position.y = absolutePositionY; this.position.z = absolutePositionZ; } return this; } which takes into account a parent but not a pivot. Also it sets the absolutePosition by setting only the position. However as is seen above when using a pivot, like for the parent case, position and absolute position are different by the pivot vector. Possible Solution Would be very nervous of suggesting changes to 'setAbsolutePosition' as I would not know what else it might affect. So suggest the following change to translate - 'add(this.getPivotPoint())' - but am not submitting a PR since I am not absolutely certain this is the correct way to go. Have tried it locally and it gives me what I expect but do not know whether it affects anything else and will wait until better BJS minds than mine check it out. public translate(axis: Vector3, distance: number, space?: Space): AbstractMesh { var displacementVector = axis.scale(distance); if (!space || (space as any) === Space.LOCAL) { var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector); this.setPositionWithLocalVector(tempV3); } else { this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector).add(this.getPivotPoint())); } return this; } Quote Link to comment Share on other sites More sharing options...
Sebavan Posted October 2, 2017 Share Posted October 2, 2017 Amazing details, thx. I completely agree it is an issue and I will try deeper your proposed solution today. The fix should be in tonight. JohnK 1 Quote Link to comment Share on other sites More sharing options...
Sebavan Posted October 3, 2017 Share Posted October 3, 2017 I was not able to test deeply enough today, I ll check more tomorrow. Quote Link to comment Share on other sites More sharing options...
Sebavan Posted October 3, 2017 Share Posted October 3, 2017 The issue looks deeper. We ll check with @Deltakosh next week as there is more issues and potential impacts than expected. I close it from here and you can follow the open issue on Github: https://github.com/BabylonJS/Babylon.js/issues/2896 JohnK 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 6, 2017 Share Posted October 6, 2017 Will work on it this monday JohnK 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 9, 2017 Share Posted October 9, 2017 It is fixed. I introduced a way to set a pivot matrix only (and not a premult only matrix): https://www.babylonjs-playground.com/#KCMKTC (check line #28) JohnK 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.