Jump to content

[SOLVED] TESR - rotations


Cadiboo

Recommended Posts

Does anyone know how I would rotate an object to look at a point (xyz entity location) in my TESR? I've gotten my 3 axis rotating perfectly when I only do one of them, but when I try to do all 3 of them, it doesn't work at all. (I'm using GlStateManager.rotate(angle, x, y, z) rather that Quaternions because I just cannot understand them). I'm relatively new to rendering, and haven't rendered anything more complex than ItemBlocks & Models in my TESRs before. Any help would be greatly appreciated.

Link to the file in my GitHub: https://github.com/Cadiboo/WIPTech/blob/master/src/main/java/cadiboo/wiptech/client/render/tileentity/TESRWire.java

Same code but with most comments removed

	@Override
	public void render(TileEntityWire tileEntity, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
		
      	if (tileEntity.electrocutionTime > 0) {
      		
			double dX; //entity xy&z - set later
			double dY;
			double dZ;
			final int NumberOfBranches = 1; //for rendering the electricity
			final int NumberOfPossibleSubBranches = 2;
		
			List<Entity> entities = tileEntity.getAllEntitiesWithinRangeAt(tileEntity.getPos().getX(), tileEntity.getPos().getY(), tileEntity.getPos().getZ(), 113);
			for (int i = 0; i < entities.size(); i++) {
              
            	GlStateManager.pushMatrix();
				GlStateManager.translate(x + 0.5, y + 0.5, z + 0.5);
              
              	dX = entities.get(i).posX - tileEntity.getPos().getX() - 0.5;
				dY = entities.get(i).posY - tileEntity.getPos().getY() - 0.5;
				dZ = entities.get(i).posZ - tileEntity.getPos().getZ() - 0.5;
              
              
              	//Y rotation
              	double aa = dX;
				double bb = dZ;
				double cc = Math.sqrt(aa * aa + bb * bb);

				double angle = 180F / Math.PI * Math.acos((bb * bb - (-cc * cc) - aa * aa) / (2 * bb * cc));
              
             	GlStateManager.rotate(90F + (float) angle * (aa < 0 ? -1 : 1), 0, 1, 0);
              
              	//X rotation
              	aa = dY;
				bb = dX;
				cc = Math.sqrt(aa * aa + bb * bb);

				angle = 180F / Math.PI * Math.acos((bb * bb - (-cc * cc) - aa * aa) / (2 * bb * cc));

				//GlStateManager.rotate(70F + (float) angle * (aa < 0 ? -1 : 1), 0, 0, 1);
              
              	//Z rotation
              
              	aa = dY;
				bb = dZ;
				cc = Math.sqrt(aa * aa + bb * bb);

				angle = 180F / Math.PI * Math.acos((bb * bb - (-cc * cc) - aa * aa) / (2 * bb * cc));

				GlStateManager.rotate(270F + (float) angle * (aa < 0 ? 1 : -1), 1, 0, 0);
              
              	//... render electricity
              
              GlStateManager.popMatrix();
              
            }
		}
      //... render wires
    }

 

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

I think your problem here is that rotations are fundamentally a 4-axis object and you're trying to piecemeal it with three. Yes, Euler Angles are a Thing, but they suffer from gimbal lock.

 

This is the closest thing I can find that has the right math:

https://stackoverflow.com/questions/14720264/create-a-rotation-matrix-from-a-position-and-a-look-at-position

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

17 hours ago, Draco18s said:

I think your problem here is that rotations are fundamentally a 4-axis object and you're trying to piecemeal it with three. Yes, Euler Angles are a Thing, but they suffer from gimbal lock.

 

This is the closest thing I can find that has the right math:

https://stackoverflow.com/questions/14720264/create-a-rotation-matrix-from-a-position-and-a-look-at-position

What is the 4th axis??? (time aha?)

Its not gimbal lock so much as that when I rotate on 1 axis the other 2 axis rotate with it. This is as everything should be, but for my code I NEED them not to rotate (or better maths). I also don't understand Matrixes (Matricies??) or Quaternions at all and I don't really understand vectors very well either. I've set myself to fail pretty badly

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

4 hours ago, Cadiboo said:

What is the 4th axis???

Feel free to read up: https://en.wikipedia.org/wiki/Quaternion

4 hours ago, Cadiboo said:

when I rotate on 1 axis the other 2 axis rotate with it.

This is expected for one of two reasons:

1) GL deals with rotation as a quaternion and when you make an adjustment in Euler, the end result is not neccessarily the same Euler (to avoid gimbal lock)

2) Rotation around a rotated axis changes all of the Euler values from a non-rotated reference frame.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

9 hours ago, Draco18s said:

Feel free to read up: https://en.wikipedia.org/wiki/Quaternion

This is expected for one of two reasons:

1) GL deals with rotation as a quaternion and when you make an adjustment in Euler, the end result is not neccessarily the same Euler (to avoid gimbal lock)

2) Rotation around a rotated axis changes all of the Euler values from a non-rotated reference frame.

Thanks! The maths for quaternions is over my head for the moment (I’m trying to fix this). You specified that because I was using Euler angles the axis were also rotated, do you know (off the top of your head) if there is a way use quaternions to avoid this?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

*I* don't understand quaternions. I just know what they're good for.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2 minutes ago, Draco18s said:

*I* don't understand quaternions. I just know what they're good for.

All good thanks for the help!

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

After ≈ 6 days of solid work I have made it work, I hope this helps someone.

you find your y rotation through Math.atan2(originX - pointX, originZ - pointZ);

and your up down (z/x) rotation through Math.atan2(originY - pointY, Math.sqrt(originX * originX + originZ * originZ) - Math.sqrt(pointX * pointX + pointZ * pointZ);

 

then you convert both from radians to degrees.

then you rotate around your y axis by your y rotation

GlStateManager.rotate((float) yAngle + 90, 0, 1, 0);

 

then you rotate around your x or z angle (I chose z because a) your normal rotation order is Y, Z, X and b) I was already using x for rotating my object 180 degrees from straight down to straight up)

GlStateManager.rotate(90 - (float) _Angle, 0, 0, 1);

 

I really hope this helps someone.

Here is the full java code that I used

 

Entity Position code (removes render shake)

Spoiler

double flyingMultiplier = 1.825;

double yFlying = 1.02;

double yAdd = 0.0784000015258789;

 

if (entities.get(i) instanceof EntityPlayer) {

if (((EntityPlayer) entities.get(i)).capabilities.isFlying) {

flyingMultiplier = 1.1;

yFlying = 1.67;

yAdd = 0;

}

}

 

double yGround = entities.get(i).motionY + yAdd == 0 && entities.get(i).prevPosY > entities.get(i).posY ? entities.get(i).posY - entities.get(i).prevPosY : 0;

double xFall = 1;

if (flyingMultiplier == 1.825) {

if (entities.get(i).motionX != 0) {

if (entities.get(i).motionY + yAdd != 0) {

xFall = 0.6;

} else if (yGround != 0) {

xFall = 0.6;

}

} else {

xFall = 0.6;

}

}

 

double zFall = 1;

if (flyingMultiplier == 1.825) {

if (entities.get(i).motionZ != 0) {

if (entities.get(i).motionY + yAdd != 0) {

zFall = 0.6;

} else if (yGround != 0) {

zFall = 0.6;

}

} else {

zFall = 0.6;

}

}

 

double dX = entities.get(i).posX - (entities.get(i).prevPosX - entities.get(i).posX) * partialTicks - (entities.get(i).motionX * xFall) * flyingMultiplier - tileEntity.getPos().getX() - 0.5;

double dY = entities.get(i).posY - yGround - (entities.get(i).prevPosY - entities.get(i).posY) * partialTicks - (entities.get(i).motionY + yAdd) * yFlying - tileEntity.getPos().getY() - 0.5;

double dZ = entities.get(i).posZ - (entities.get(i).prevPosZ - entities.get(i).posZ) * partialTicks - (entities.get(i).motionZ * zFall) * flyingMultiplier - tileEntity.getPos().getZ() - 0.5;

dY += entities.get(i).getEyeHeight();

 

Rotation code

//get Y angle
double yAngle = Math.atan2(0 - dX, 0 - dZ);
//convert angle to degrees
yAngle = yAngle * (180 / Math.PI);
yAngle = yAngle < 0 ? 360 - (-yAngle) : yAngle;
//rotate by angle
GlStateManager.rotate((float) yAngle + 90, 0, 1, 0);
//rotate object (It starts facing down, I want it to start facing up)
GlStateManager.rotate(180, 1, 0, 0);

//get Up/Down (X/Z) angle
double _Angle = Math.atan2(dY, Math.sqrt(dX * dX + dZ * dZ));
//convert angle to degrees
_Angle = _Angle * (180 / Math.PI);
_Angle = _Angle < 0 ? 360 - (-_Angle) : _Angle;
//rotate by angle
GlStateManager.rotate(90 - (float) _Angle, 0, 0, 1);

//...draw object

 

GitHub perma-link (High-accuracy entity positioning and object rotation)

https://github.com/Cadiboo/WIPTech/blob/1bc15df55251b6631d31b6d101ae5a729ed2a4ca/src/main/java/cadiboo/wiptech/client/render/tileentity/TESRWire.java#L77-L126

Edited by Cadiboo
added GitHub perma-link

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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



×
×
  • Create New...

Important Information

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