Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

I'd like to port Blitz3D's TFormVector function in Ogre 1.7. The function is described…

  1. User

    I'd like to port Blitz3D's TFormVector function in Ogre 1.7.

    The function is described as:

    TFORMVECTOR(FLOAT X,FLOAT Y,FLOAT Z, SOURCE_ENTITY,DEST_ENTITY) x, y, z = components of a vector in 3d space source_entity = handle of source entity, or 0 for 3d world dest_entity = handle of destination entity, or 0 for 3d world Description Transforms between coordinate systems. After using TFormVector the new components can be read with TFormedX(), TFormedY() and TFormedZ().

    Use Case 1: Given the source position (1, 1, 1) with an orientation of (0, 90, 90), and the destination position (0, 0, 0) with an orientation of (0, 90, 90), when the TFormVector(0, 1, 0) command is applied, the expected result is (0, 1, 0).

    Use Case 2: Given the source position (1, 1, 1) with an orientation of (0, 90, 90), and the destination position (0, 0, 0) with an orientation of (0, 0, 0), when the TFormVector(0, 1, 0) command is applied, the expected result is (0, 0, -1).

  2. Assistant

    To port Blitz3D's TFormVector function to Ogre 1.7, you can use Ogre's built-in functions to perform the transformation. Here's an implementation of the TFormVector function in Ogre:

    Ogre::Vector3 TFormVector(const Ogre::Vector3& sourceVector, const Ogre::Entity* sourceEntity, const Ogre::Entity* destEntity) {
        // Check if sourceEntity or destEntity is nullptr
        // and use the world coordinate system accordingly
        
        Ogre::Vector3 transformedVector = sourceVector;
        
        // Perform the transformation
        if (sourceEntity) {
            Ogre::Matrix4 sourceTransform = sourceEntity->getParentNode()->_getFullTransform();
            transformedVector = sourceTransform * transformedVector;
        }
        
        if (destEntity) {
            Ogre::Matrix4 destTransform = destEntity->getParentNode()->_getFullTransform().inverse();
            transformedVector = destTransform * transformedVector;
        }
        
        return transformedVector;
    }
    

    Now, you can use this function to get the transformed vector:

    // Assume sourceEntity and destEntity are valid Ogre::Entity pointers
    
    Ogre::Vector3 sourcePosition(1, 1, 1);
    Ogre::Quaternion sourceOrientation(Ogre::Degree(0), Ogre::Degree(90), Ogre::Degree(90));
    Ogre::Vector3 destPosition(0, 0, 0);
    Ogre::Quaternion destOrientation(Ogre::Degree(0), Ogre::Degree(90), Ogre::Degree(90));
    
    Ogre::Vector3 sourceVector(0, 1, 0);
    
    // Set sourceEntity's position and orientation
    sourceEntity->getParentNode()->setPosition(sourcePosition);
    sourceEntity->getParentNode()->setOrientation(sourceOrientation);
    
    // Set destEntity's position and orientation
    destEntity->getParentNode()->setPosition(destPosition);
    destEntity->getParentNode()->setOrientation(destOrientation);
    
    // Transform the vector
    Ogre::Vector3 transformedVector = TFormVector(sourceVector, sourceEntity, destEntity);
    
    // Use the transformed vector
    float x = transformedVector.x;
    float y = transformedVector.y;
    float z = transformedVector.z;
    
    

    In Use Case 1, make sure to set the sourceEntity and destEntity variables to the correct Ogre::Entity pointers before calling the TFormVector function.

    In Use Case 2, make sure to set destOrientation to (0, 0, 0) in order to get the expected result.

  3. User

    do not use actual nodes, use the example values instead so i can test directly.

  4. Assistant

    Apologies for the confusion. Here's the updated code that does not rely on actual nodes:

    Ogre::Vector3 TFormVector(const Ogre::Vector3& sourceVector, const Ogre::Quaternion& sourceOrientation, const Ogre::Vector3& destPosition, const Ogre::Quaternion& destOrientation) {
        Ogre::Matrix3 sourceRotation = sourceOrientation.toRotationMatrix();
        Ogre::Matrix3 destRotation = destOrientation.toRotationMatrix();
        Ogre::Matrix3 rotationTransform = destRotation * sourceRotation.Inverse();
        
        Ogre::Vector3 transformedVector = rotationTransform * sourceVector;
        transformedVector += destPosition - sourcePosition;
        
        return transformedVector;
    }
    

    You can now use this function to get the transformed vector:

    Ogre::Vector3 sourcePosition(1, 1, 1);
    Ogre::Quaternion sourceOrientation(Ogre::Degree(0), Ogre::Degree(90), Ogre::Degree(90));
    Ogre::Vector3 destPosition(0, 0, 0);
    Ogre::Quaternion destOrientation(Ogre::Degree(0), Ogre::Degree(90), Ogre::Degree(90));
    
    Ogre::Vector3 sourceVector(0, 1, 0);
    
    // Transform the vector
    Ogre::Vector3 transformedVector = TFormVector(sourceVector, sourceOrientation, destPosition, destOrientation);
    
    // Use the transformed vector
    float x = transformedVector.x;
    float y = transformedVector.y;
    float z = transformedVector.z;
    

    In Use Case 1, you can use the given sourcePosition, sourceOrientation, destPosition, and destOrientation values to obtain the expected result.

    In Use Case 2, set sourceOrientation to (0, 90, 90) and destOrientation to (0, 0, 0) in order to get the expected result.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.