Jump to content

Recommended Posts

Posted

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.

Posted (edited)

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.

Posted

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.

Posted

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.

Posted

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.

Posted
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.

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

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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