Jump to content

[1.16.5] Rotation angles/axis not consistent when rotated


That_Martin_Guy

Recommended Posts

I am rendering a model as part of a tile entity renderer. The model is supposed to be rotated based on the block's facing direction. The kicker is that it's also supposed to be rotated based on a "setting" variable, which can be changed when right clicked.

 

My solution to the problem is rotating around the Y-axis depending on the direction, and rotate either the X or Z axis (depending on the block's direction) by a certain amount per setting. Implementation looks like the following

	public void turnToSetting(Direction direction, TheClockworksTileEntity.Setting setting)
	{
		switch(direction)
		{
			case NORTH:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = setting.rotation;
				break;
			case SOUTH:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = -setting.rotation;
				break;
			case WEST:
				group2.xRot = setting.rotation;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = 0;
				break;
			case EAST:
				group2.xRot = -setting.rotation;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = 0;
				break;
		}
	}

	private float rotationFromDirection(Direction direction)
	{
		double rotation = 0;
		switch(direction)
		{
			case SOUTH:
				rotation = Math.PI;
				break;
			case EAST:
				rotation = Math.PI / 2;
				break;
			case WEST:
				rotation = Math.PI * 3 / 2;
				break;
		}

		return (float) rotation;
	}

In practice this works well when the block is facing north or south, but not when the block is facing east or west. Demonstration here. When observing the way it rotates, it seems like the model's coordinate system has been changed when it was rotated around the Y axis. Therefore I feel like the solution should be to rotate around the Z-axis on the west and east directions. However, this results in the behavior demonstrated here. Code demonstrating that below:

	public void turnToSetting(Direction direction, TheClockworksTileEntity.Setting setting)
	{
		switch(direction)
		{
			case NORTH:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = setting.rotation;
				break;
			case SOUTH:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = -setting.rotation;
				break;
			case WEST:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = setting.rotation;
				break;
			case EAST:
				group2.xRot = 0;
				group2.yRot = rotationFromDirection(direction);
				group2.zRot = -setting.rotation;
				break;
		}
	}

To me this behavior is very similar to rotating on the X-axis. I am confused as to how this is the case.

Link to comment
Share on other sites

What code is processing these angles? To me, it looks like the rotations are processed in the order X, then Y, then Z. This would make it impossible to get the desired skew result in the East/West directions. Here's what I think is happening:

Your model starts with the face pointing to Z+, and the tip to Y+. To get it into a skew for the North/South position, you rotate around Y to account for the flip, then around Z for the skew. All good. To get it into a skew for the East/West, you would have to rotate around Y ± π/2, then around X. Problematic, since the X rotation happens first. If you try to rotate around X first, you just tilt the piece forward or backward, as you've seen. If you try and rotate around Z after, the piece is already pointing face to X±, tip to Y+, and at that point the Z rotation will just tilt it forward/back, as you've seen.

If this is the case, you need to process the rotations in a more useful order, namely with Y first. If you need a more flexible system: I don't think matrix transformations have a specific order*, which would prevent this problem from occurring.

*Not true. They have a specific order. They take effect in the order opposite that of the original multiplications.

Edited by SerpentDagger
Correction

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Link to comment
Share on other sites

The code handling the rotation itself is part of vanilla, the same code that handles rotation of limbs and such. I am not entirely sure this works under the hood, but I'd prefer to not have to create my own rotation system if possible. In that case I'd probably just create a new model that is rotated east/west instead of north/south.

Link to comment
Share on other sites

It looks like the code in ModelRenderer applies rotations X→Y→Z, then translation (Matrix transformations definitely have an order. Sorry, I wasn't thinking). That's why your problem is presenting itself. On the bright side, I think* ModelRenderers can have children which inherit the rotations and translations of the parent. This means that you should be able to have a parent ModelRenderer with the Y rotation, and then the child can have the Z rotation, which is then independent of the block's direction.

*Never used them before, sorry.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Link to comment
Share on other sites

Just looked at the code for ModelRenderer, sorry for not doing that earlier. It actually rotates around the z-axis first, if I understand it correctly.

 

   public void translateAndRotate(MatrixStack p_228307_1_) {
      p_228307_1_.translate((double)(this.x / 16.0F), (double)(this.y / 16.0F), (double)(this.z / 16.0F));
      if (this.zRot != 0.0F) {
         p_228307_1_.mulPose(Vector3f.ZP.rotation(this.zRot));
      }

      if (this.yRot != 0.0F) {
         p_228307_1_.mulPose(Vector3f.YP.rotation(this.yRot));
      }

      if (this.xRot != 0.0F) {
         p_228307_1_.mulPose(Vector3f.XP.rotation(this.xRot));
      }

   }

 

I'm not sure how I would go about setting up this parent-child relationship when doing the rotations separately with this knowledge. Not the greatest at this sort of math.

Link to comment
Share on other sites

48 minutes ago, That_Martin_Guy said:

It actually rotates around the z-axis first, if I understand it correctly.

Matrix transformations actually take effect in the opposite order from that of multiplication, which means that it is indeed how I described.

 

53 minutes ago, That_Martin_Guy said:

I'm not sure how I would go about setting up this parent-child relationship when doing the rotations separately with this knowledge. Not the greatest at this sort of math.

I think the way ModelRenderer works means that you could have a parent instance whose yRot is set based on block rotation, and then add your current ModelRenderer with addChild, but only set its zRot. The child should be rendered when the parent's render method is called.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • KLIK DISINI UNTUK DAFTAR       WsoSlot88 Situs Bandar Togel Terpercaya dengan Hadiah 4D Tertinggi yaitu 10 juta WsoSlot88 adalah BO Agen Togel Terpercaya yang sudah berdiri sejak tahun 2016 yang terkenal dengan proses transaksi deposit dan withdraw yang super cepat serta jaminan pembayaran untuk berapa pun kemenangan yang didapat oleh para member nya. Jadi anda para pecinta game Togel Online dan Slot Gacor tidak perlu takut atau ragu bermain di BO WsoSlot88, karena jika anda Jackpot Paus berapapun, pasti akan dibayar lunas oleh WsoSlot88.   67 Pasaran Togel yang paling dicari oleh para pecinta Togel Online yaitu : - Pasaran Togel Hongkong (HK) - Pasaran Togel Sydney (SDY) - Pasaran Togel Singapore (SGP) - Pasaran Togel Cambodia (CM) - Pasaran Togel Taiwan (TW) - Pasaran Togel Jepang (JPN) - Pasaran Togel China (CN) - Pasaran Togel Chicago - Pasaran Togel Malaysia - Pasaran Togel Vegas - Pasaran Togel Miami - Pasaran Togel Vietnam - Pasaran Togel Carolina - Pasaran Togel Bullseye - Dan masih banyak pasaran togel terkenal lainnya
    • I can play my server & others can connect to it but it cannot load certain chunks & times out because it's bottlenecked by the default 4GB. I know this to be the case because when copy+pastaing the exact server world into curseforge singleplayer with 8G+, all the heavy chunks load in fine.  I have JavaSDK 17.0.9 x64-bit & reinstalled twice already as well as trying with Java 8 also installed & without.  I tried using: java @user_jvm_args.txt @libraries/net/minecraftforge/forge/1.20.1-47.2.0/win_args.txt %* pause but all that gives me is: Error: Could not find or load main class java Caused by: java.lang.ClassNotFoundException: java  Any ideas or help would be appreciate. 
    • KLIK DISINI UNTUK DAFTAR     Situs Slot Thailand Terpercaya adalah situs yang menyediakan permainan slot online dengan tema dan layanan yang sesuai dengan kebutuhan dan preferensi pemain asal Thailand. Situs-situs ini biasanya menawarkan permainan slot dengan server khusus yang berasal dari Thailand, yang diklaim memiliki tingkat kemenangan yang tinggi dan mudah mendapatkan jackpot. Beberapa situs juga menyediakan permainan slot dengan server dari negara lain, seperti Rusia dan Singapura, yang juga populer di kalangan pemain Thailand.  
    • I was trying to use viaforge (latest version) on forge 1.20.2 and then it crashed showing exit code 1 heres the log : 04Dec2023 11:40:52.058] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, Goldenretriver, --version, 1.16.5-forge-36.2.34, --gameDir, C:\Users\rashr\AppData\Roaming\.minecraft, --assetsDir, C:\Users\rashr\AppData\Roaming\.minecraft\assets, --assetIndex, 1.16, --uuid, a688ba406e4f4635a428d7555a467474, --accessToken, ????????, --userType, msa, --versionType, release, --launchTarget, fmlclient, --fml.forgeVersion, 36.2.34, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20210115.111550] [04Dec2023 11:40:52.061] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.1.3+8.1.3+main-8.1.x.c94d18ec starting: java version 1.8.0_51 by Oracle Corporation [04Dec2023 11:40:52.071] [main/DEBUG] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Found launch services [minecraft,testharness,fmlclient,fmlserver] [04Dec2023 11:40:52.075] [main/WARN] [cpw.mods.modlauncher.SecureJarHandler/]: LEGACY JDK DETECTED, SECURED JAR HANDLING DISABLED [04Dec2023 11:40:52.082] [main/DEBUG] [cpw.mods.modlauncher.NameMappingServiceHandler/MODLAUNCHER]: Found naming services : [] [04Dec2023 11:40:52.118] [main/DEBUG] [cpw.mods.modlauncher.LaunchPluginHandler/MODLAUNCHER]: Found launch plugins: [mixin,eventbus,object_holder_definalize,runtime_enum_extender,accesstransformer,capability_inject_definalize,runtimedistcleaner] [04Dec2023 11:40:52.125] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Discovering transformation services [04Dec2023 11:40:52.132] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Found additional transformation services from discovery services: [C:\Users\rashr\AppData\Roaming\.minecraft\mods\viaforge-1.12.2-3.4.3.jar]
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.