Jump to content

[SOLVED] Incorrect node rotation


photo

Recommended Posts

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

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

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

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:

Link to comment

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
×
×
  • Create New...