Jump to content

Recommended Posts

Posted (edited)

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)

Posted

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.

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

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

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

Posted

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

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

Posted (edited)

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)

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

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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