santosh.a Posted September 17, 2014 Share Posted September 17, 2014 Hi all. While coding I came up with an issue involving incorrect node rotation which, for the life of me, I'm not able to figure out. If would be helpful if someone could point out where the issue is. Basically I'm trying to create and align a shell along the barrel of an artillery. The final line of world transformation is incorrectly rotating the shell. Kindly find pics attached. Code for spawning and aligning shell is like this : void spawnShell() { // Create shell node. m_cRefNode = class_remove( new NodeReference( "nodes/weapons/ammunition/155mm_shell.node" ) ); engine.editor.addNode( m_cRefNode ); assert( m_cRefNode != NULL ); m_cRefNode.setName( "he_shell" ); m_cRefNode.setProperty( "HEShell" ); // Find and assign necessary nodes. Node container = m_cRefNode.getNode(); m_cObject = node_cast( container ); int id = container.findChild( "nose" ); m_cNoseNode = node_cast( container.getChild( id ) ); id = container.findChild( "base" ); m_cBaseNode = node_cast( container.getChild( id ) ); assert( m_cObject != NULL ); assert( m_cBaseNode != NULL ); assert( m_cNoseNode != NULL ); // Place and align the shell w.r.t artillery barrel NodeDummy barrelEnd = m_cOwnerArtillery.getBarrelEnd(); m_cRefNode.setWorldPosition( barrelEnd.getWorldPosition() ); // Rot_Issue_2/2A.jpg m_cRefNode.setWorldRotation( m_cOwnerArtillery.getWorldRotation() ); // Rot_Issue_3/4.jpg vec3 v1 = m_cNoseNode.getWorldPosition() - m_cBaseNode.getWorldPosition(); vec3 v2 = m_cOwnerArtillery.getLaunchDirection(); if( v1 != v2 && length( v1 ) != 0.0f && length( v2 ) != 0.0f ) { v1 = normalize( v1 ); v2 = normalize( v2 ); vec3 v3 = cross( v1, v2 ); v3 = normalize( v3 ); float angle = acos( dot( v1, v2 ) ) * RAD2DEG; m_cRefNode.setWorldTransform( m_cRefNode.getWorldTransform() * rotate( v3, angle ) ); // Rot_Issue_5/5A.jpg } } rotation_issue.rar Link to comment
silent Posted September 17, 2014 Share Posted September 17, 2014 Hi, For quick test you can try to replace: vec3 v3 = cross( v1, v2 ); with vec3 v3 = cross( v2, v1 ); If this doesn't help, I'm afraid we will need to check the whole scene with actual content. Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
santosh.a Posted September 18, 2014 Author Share Posted September 18, 2014 Thanks for the quick reply, Silent. That is fixing the rotation partially but not fully. Also, cross( v2, v1 ) would mean that we want the node to be rotated from v2 direction to v1, which is an incorrect assumption. I will set a test case asap and get back, thanks again! Link to comment
santosh.a Posted September 18, 2014 Author Share Posted September 18, 2014 I've set up a test case, kindly find the zipped file attached. Create new project, place test_case folder inside data folder and do turn visualizer on too. Thanks. test_case.rar Link to comment
silent Posted September 18, 2014 Share Posted September 18, 2014 Great! I will report as soon as I will have a feedback from the developers. Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
silent Posted September 19, 2014 Share Posted September 19, 2014 Hi, The reason you get such rotation issue is in line 142 of shell.h script. If you remove this line shell will be aligned correnly in the world coordinates. Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
santosh.a Posted September 19, 2014 Author Share Posted September 19, 2014 I'm already aware that there would not be any problem if shell rotation is done in 1 step without line 142. But I had to add it for a reason :) I prefer to maintain the local Z axis of shell in upwards direction for further trajectory calculations, so if I remove line 142 the shell would be awkwardly rotated. That is the reason for rotating it horizontally first and then vertically. Anyway, the code snippet 144-162 should theoretically work correctly on its own especially since the vectors are all correct. That's where my concern is. Edit : Another reason that I cannot just remove line 142 and fix the issue is that the code of trajectory movement (that I haven't shared yet) also has this same issue. So I need to know the exact problem (or whether I'm doing some silly mistake). Link to comment
silent Posted September 19, 2014 Share Posted September 19, 2014 Hi, Please, try to change line 159 in shell.h script: m_cRefNode.setWorldTransform( translate(m_cRefNode.getWorldPosition()) * rotate( v3, angle ) * rotation(m_cRefNode.getWorldTransform()) ); At first we rotate shell in it's local coordinates, then we apply world transformations. Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
santosh.a Posted September 19, 2014 Author Share Posted September 19, 2014 That worked perfectly. Thanks Silent, that was big help. So what lesson can I learn here? I mean, whenever I need to rotate a node around an axis, I should always apply translate first and current world rotation last? Should I never directly apply getWorldTransform() value? Kindly let me know the reasoning behind it so that I can avoid this issue in the future. Thanks again! Link to comment
binstream Posted September 19, 2014 Share Posted September 19, 2014 It is the way 3D math works, the issue is not related to the engine. Link to comment
santosh.a Posted September 22, 2014 Author Share Posted September 22, 2014 Okay I will figure it out. Thanks. Link to comment
Recommended Posts