Topper Posted February 17, 2018 Share Posted February 17, 2018 Hi folks, I´ve been going through a lot of PGs and the BJS docu as well to figure out the best (most easy way) to place meshes in a scene. I still don´t understand why it´s necessary to distinguish between world space an local space, and I´ve seen a lot of posts of people struggling with the same topic. What is the easiest (and most elegant) way to position and rotate a created mesh based on one coord system (probably world) always based on a mesh´s CoG? I just want to spend a Vector3(x, y, z) and perform some rotations after, always based on a mesh´s CoG. I studied this PG, but I don´t understand why I have to do all the calcs (transformations) by myself: http://www.babylonjs-playground.com/#M0UJB Is there some standard functionality i didn´t come across yet? BR, Markus Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 17, 2018 Share Posted February 17, 2018 Probably worth reading the documentation on rotations and pivots. Start here http://doc.babylonjs.com/babylon101/position and follow the further reading links. 35 minutes ago, Topper said: I still don´t understand why it´s necessary to distinguish between world space an local space In a way you answer your own question. 36 minutes ago, Topper said: What is the easiest (and most elegant) way to position and rotate a created mesh based on one coord system (probably world) always based on a mesh´s CoG? I just want to spend a Vector3(x, y, z) and perform some rotations after, always based on a mesh´s CoG If you want to position a mesh based on its CoG it position will always be (0, 0, 0) since the CoG moves with the mesh and the positions of everything else in the world would have to be recalculated, so you have changed the coord system. So for positioning it is best to maintain one world coord system and allow the CoG to move in the world. On the other hand since rotations generally take place about a CoG is is best to form axes local to the CoG, those in local space to calculate and perform rotations. So you need two coordinate systems for the calculations. Of course Babylon.js does all this internal work for you. Consider the two boxes in the following PG https://www.babylonjs-playground.com/#PBNUN7 In World Space the CoG for box1 is (0, 0, 0) is its position and the CoG for box2 is (4, 2, 3) also its position. To rotate box2 based on box2's CoG means to rotate box2 about the point (4, 2, 3). Take a rotation about box2's CoG of PI/4 about an axis parallel to the world axis. How do you calculate the new positions of the box corners in the world space? Well it is more straight forward to do rotations about the origin rather than an arbitrary point. In practice the only way to do rotations about an arbitrary point is to treat that point as an origin and use a different set of axes, ie those in local space, through this point as origin. Do the rotation in the local space and then calculate the world space for the corners. In Babylon.js rotations are about the local axes in local space and the engine does the corner calculations for you. Hence box2.rotation.y = Math.PI/4 is a rotation about the CoG in local space. When you need to rotate a box about some other point and not its position you need to set and use a pivot. Quote Link to comment Share on other sites More sharing options...
claydev Posted February 17, 2018 Share Posted February 17, 2018 Just a reference, you may want to read 3D Math Primer for Graphics and Game Development by Fletcher Dunn, Chapter 3. The book should explain these coordinate spaces and the reason why. JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 17, 2018 Author Share Posted February 17, 2018 Hi JohnK, thanks for your reply. I went throug your PG and I´ve a question on it: When I spawn a mesh like a box or a sphere it will always be in (0, 0, 0). But when i create a more complex mesh based on an extruded 2D-shape (bunch of vector2() in an array) the mesh´s reference point is still the 2D´s reference point and not the new mesh´s (3D) CoG. Since my workflow is like this: - make 2D shape - extrude 2D shape to 3D shape - perform first rotation (PI / 2 in z (world axes) - perform second rotation (calculated angle in y (world axis)) - move mesh in z (world axes) - move mesh in y (world axes) I kept sticking with the idea that it may be easier to calculate arbitrary points as "rotation axes" then using anything else. Here you can find the example I´ve been working on: https://www.babylonjs-playground.com/#FY8S2X#1 My challenge is to position the diagonal braces in between the two blue uprights. I already calculated the "rotation axes" for every singe diagonal brace (green spheres), but I keep failing in using these points as "rotation axes" My plan was to do this "pivot point thing", since a mesh's CoG is not in (0, 0, 0) when I extrude a 2D shape. Would it help to clone my mesh to get one with its CoG in (0, 0, 0)? Thanks in advance guys, Markus Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 17, 2018 Share Posted February 17, 2018 Order matters with rotations/translations. You may be able to accomplish what you're after by moving and then rotating, because then the position will be in world space. Quote Link to comment Share on other sites More sharing options...
Topper Posted February 17, 2018 Author Share Posted February 17, 2018 @ brianzinn Thanks, but I´m not getting it. I know that order matters, but when i move (position) my braces, their "reference points" are still (0, 0, 0). Do I my mean something else by "moving a mesh"? BR, Markus Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 17, 2018 Share Posted February 17, 2018 I think you are calculating diagonals wrong, as your beams are not long enough for the angle you have. not to send you in the wrong direction, but here I calculated diagonals with purely Pythagorean (a^2 + b^2 = c^2). I don't want to throw you off target and hope this is not too unrelated, but it's just moving and rotating like you have done already. https://www.babylonjs-playground.com/#R555G1#0 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 17, 2018 Author Share Posted February 17, 2018 Hi, the angels of the diagonal braces are correct and so are the braces lengths (they are still not in the middle though). Believe me. Math is not my pain. They are supposed to meet the inner side holes of the uprights and future screws. Here´s my pain: https://www.babylonjs-playground.com/#FY8S2X#2 I position seven diagonal braces along Y. I make BJS return the centers (Line 298) of each: var meshLocalCenter = braceD.getBoundingInfo().boundingBox.center; var meshWorldCenter = braceD.getBoundingInfo().boundingBox.centerWorld; For every single diagonal brace I get the same values although their positions (at least in Y) are definitely different: center: {X: 0 Y:-6.448263332091828 Z:-0.15} centerWorld: {X: 0 Y:-6.448263332091828 Z:-0.15} How´s that? Check it yourself. That´s what I´m not getting. BR, Markus Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 17, 2018 Share Posted February 17, 2018 That's because the world matrix isn't updated, you can force by calling mesh.computeWorldMatrix(true); Your meshes position is not centered. I've set the circle to be the position and added mesh.showBoundingBox = true, so you can see: https://www.babylonjs-playground.com/#FY8S2X#3 I think you just need to adjust the positions accordingly. Quote Link to comment Share on other sites More sharing options...
Topper Posted February 17, 2018 Author Share Posted February 17, 2018 brianzinn, you´re my man. That simple line of code did the trick. Can you explain to me what this exactly does? Is it like "freezing prior transformations"? I came across "setAbsolutePosition / getAbsolutePosition ". What´s the sense of this? Is the "absolute position" always in relation to the world axes? Thanks a lot, man. BR, Markus brianzinn 1 Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 18, 2018 Share Posted February 18, 2018 alright - glad you got it!! it's an optimization to evaluate world matrix once per frame. You can read more about it here on freezing your matrices: https://doc.babylonjs.com/how_to/optimizing_your_scene -> Reducing World Matrices Computation absolute position is always in relation to world co-ordinates, yes. If a mesh is not parented then it's position is already in world co-ordinates, otherwise in relation to parent mesh. It's a bit of a learning process, so glad you got that one also, there's a sticky thread for marking solved. cheers. JohnK and JackFalcon 2 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 19, 2018 Author Share Posted February 19, 2018 @ brianzinn OK, I´m still struggling with rotations. I made a simple PG with one mesh extruded from a plane: https://www.babylonjs-playground.com/#12YQ6V#1 What do I have to do, to make the mesh rotate around its CoG and not (0, 0, 0)? Thanks, Markus Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 19, 2018 Share Posted February 19, 2018 On 2/17/2018 at 4:29 PM, JohnK said: When you need to rotate a box about some other point and not its position you need to set and use a pivot I used the term CoG before as that is what you were using. However a mesh will always rotate about its 'local origin' given by its position. You want it to rotate about some other point. When you work through the documentation as I suggested before you will come across pivots. http://doc.babylonjs.com/how_to/pivots3.2 Set the pivot as suggested to the CoG and it will then rotate about this point. EDIT As you will also realise there is a difference between the 'centre' of the object as given by the bounding box and the actual CoG which because of the shape off the cross section may not be the same as its bounding box centre. There is no way in BJS to calculate the CoG you would have to do this by hand. Quote Link to comment Share on other sites More sharing options...
Wingnut Posted February 19, 2018 Share Posted February 19, 2018 Can I try it, JK? https://www.babylonjs-playground.com/#12YQ6V#3 Hey, that's close. Look at line 80... the positioning before the bake. divide-by 50's? Holy cow. I think I am pretty far lost. A guy needs to be a nuclear scientist to get THIS mesh baked into submission (into being squared-up with its world). heh. Unreal. I have no idea why it is acting that way, but it sure is fascinating. (PS: I changed camera to z-facing) JohnK 1 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 19, 2018 Author Share Posted February 19, 2018 Thanks. Seems like i should have learned sth more useful. Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 19, 2018 Share Posted February 19, 2018 There 1 hour ago, Wingnut said: Can I try it, JK? Good PG. Alternative https://www.babylonjs-playground.com/#12YQ6V#2 Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 19, 2018 Author Share Posted February 19, 2018 @ JK That´s elegant. Thanks Quote Link to comment Share on other sites More sharing options...
Wingnut Posted February 19, 2018 Share Posted February 19, 2018 See, that's why @JohnK makes the BIG money. Thx JK! JohnK 1 Quote Link to comment Share on other sites More sharing options...
Topper Posted February 20, 2018 Author Share Posted February 20, 2018 Thanks again, guys. With the help of this awesome place and its people i was able to finish work on my current project: https://www.babylonjs-playground.com/#FY8S2X#24 BR, Markus GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted February 20, 2018 Share Posted February 20, 2018 Hey, congrats, Topper! I did a little indent-adjusting on #25... so I could more-easily see your work. Your scene looks great. Nice work. I bookmarked its URL. Quote Link to comment Share on other sites More sharing options...
Topper Posted February 21, 2018 Author Share Posted February 21, 2018 Thanks, I really appreciate it, Wingnut. Did you get along with my code? Just saw that there are still some comments in my first tongue. BTW, not long I lived in MI, too (Shelby Township, greater Detroit area). Liked it lot. BR, Markus Wingnut 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.