First Project Tetris Hello World Rotation

Hello i’m just getting started with babylon.js and for my hello world project i decided to do “something like” tetris and i already got stuck

My general question, would be if my code is clean, that turns in a few specific questions

Here my code:
https://playground.babylonjs.com/#G9EZ12#2

  1. i create instance from an initially created box, following the youtube tutorial “fun with instances” from the official babylon.js youtube channel, is it possible to dispose or get rid of the box in another way like i tried in line 15. without losing all the instances?
  2. in line 26 and 35 i create redundant anchor points, which was the only way to setup a pivot anchor to get the pieces to turn correctly, is there a way to do it without failure prone reduncancy (especailly when the blocks should move in the future and the anchor should change depending on a main block), or do it maybe differently altogether?
  3. i got the pieces to rotate pretty well, which i am very happy i got that far (the docs and tutorials are great!) i wanna be able for now, to turn the blocks in 8 directions which works, however i do it based on absolute angles, is it possible to do it relative to the last position?
  4. groupings
    4.1. for every block instance i set the piece object to be able to reference it when clicking ( i want to be able to turn the piece, regardless of the block-instance i click on) is this a fine approach and the way to go, or rather a dirty workaround, for a solution i dont know of, yet? or is it maybe possible to make a grouping or something?
    4.2 i also set the piece as a parent to each instance again to cycle through whenever i got one selected, so is there a cleaner way to group the sub-blocks in a piece?
    and last
  5. is my approach fine?
    i am fresh to babylon and really dont know about patterns, or best practices, if you have knowledge about any deep mistakes with my code i’m glad if you can point me in a good direction.

things i know of that i could change:
i could make a class for piece and change line 51 for example to piece.rotate(angle); thats not really my worry, i know about OO and can change that in the future, but i’m more concerned about babylon, there’s also a bug on line 65/66 i need one for each piece, i’M rather concerned about the other 5 questions

i am deeply appreciative of any answer to my questions, even if just an answer one question, or any advice in general, even if no question gets answered i’ll continue my journey alone, but again, i’m thankful for help and wish everyone happy coding :slight_smile:

Welcome aboard!

You can hide the box with box.setEnabled(false).

I think you should design your pieces to be centered at the (0,0,0) point. That way, there’s no anchor point and you will simply need to subtract the piece position before rotating:

https://playground.babylonjs.com/#G9EZ12#4

You can do piece.blocks[i].instance.rotation.z -= Math.PI / 4; instead of piece.blocks[i].instance.rotation.z = angle.

You can group all the blocks of a piece in a single piece thanks to Mesh.MergeMeshes.

That seems fine to me :slight_smile:.

Using MergeMeshes will help simplify the code as you will only have to rotate a single mesh and you won’t need to use setPivotPoint. So, something along the line:

https://playground.babylonjs.com/#G9EZ12#5

3 Likes