簡體   English   中英

如何將對象的Y軸與從其父對象的中心點開始的矢量對齊

[英]How to align the Y-Axis of an object with a vector eminating from the centre point of their parent

因此,我有一個對象(球體),我想將球體的y軸與用於繪制其中點的向量對齊。

現在,所有創建的對象都具有如下所示的“軸”設置,因此,在世界空間中創建的所有對象都具有一個軸方向,如下圖所示。 但是我希望局部坐標中的球體的軸的y軸朝向其父對象(它們所圍繞的球體)的方向,但是我不確定我可以對每個對象進行什么整體變換以使變換為發生。

在此處輸入圖片說明

下圖是對象的所需方向。

物體軸的期望方向

我需要這種方向,因此子球體(而不是像下面的兩張圖片一樣都指向上方)會根據該方向向外伸出。 因此,如果我要在底部生成一個球體,而不是在其x軸的頂部生成其子球體,然后將其與大的中心球體半合並,則它們會指向遠離大的中心球體

下面是我生成球體的代碼(我在此代碼中生成的球體比圖片中生成的球體多,即9個,但為清晰起見,減少了圖片中存在的球體數量,以免使圖像混亂)

bool static CreateSphereLevels(Sphere *parent, float childRadiusRatio, int levels,
            Material** materials)

{
    if (levels == 1) {
        for (int i = 0; i < 6; i++) {
            Sphere * s = new Sphere(childRadiusRatio, materials[i % 6]);
            parent->AddChild(s);
            float rm = parent->GetRadius() + s->GetRadius();
            if (i == 0)
                s->SetPosition(vec3(0.0f, rm, 0.0f));
            if (i >= 1 && i < 6)
                s->SetPosition(vec3(RotationY(60 * i) * vec4(0.0f, rm, 0.0f, 0.0f)));
            if (i == 1 || i == 3 || i == 5) {
                Sphere * sn = new Sphere(childRadiusRatio, materials[i]);
                parent->AddChild(sn);
                sn->SetPosition(vec3(RotationY(60 * i) * RotationZ(60) * vec4(0.0f, rm, 0.0f, 0.0f)));
            }
        }
        return true;
    }
    else {
        for (int i = 0; i < 6; i++) {
            Sphere * s = new Sphere(childRadiusRatio, materials[i % 6]);
            parent->AddChild(s);
            int newLevels = levels - 1;
            CreateSphereLevels(s, (childRadiusRatio / 3), newLevels, materials);
            float rm = parent->GetRadius() + s->GetRadius();
            if (i == 0)
                s->SetPosition(vec3(0.0f, rm, 0.0f));
            if (i >= 1 && i < 6)
                s->SetPosition(vec3(RotationY(60 * i) * vec4(0.0f, rm, 0.0f, 0.0f)));
            if (i == 1 || i == 3 || i == 5) {
                Sphere * sn = new Sphere(childRadiusRatio, materials[i]);
                parent->AddChild(sn);
                CreateSphereLevels(sn, (childRadiusRatio / 3), newLevels, materials);
                sn->SetPosition(vec3(RotationY(60 * i) * RotationZ(60) * vec4(0.0f, rm, 0.0f, 0.0f)));
            }
        }
    }
    return true;
}

現在,下面是可以對我的對象執行的可能轉換

#ifndef RAYTRACER_SCENES_SCENEOBJECT_H
#define RAYTRACER_SCENES_SCENEOBJECT_H

#include <Raytracer/Scenes/SceneObjectType.h>

#include <vector>

namespace Raytracer
{
    namespace Scenes
    {
        class SceneObject
        {
        private:

            /**
             * The transformation matrix to transform a point from world coordinates to local
             * coordinates.
             */
            glm::mat4x4 globalTransformation;

            glm::mat4x4 transformation;

            glm::mat4x4 globalToLocal;

            /**
             * Updates the global transformation based on the current transformation. This
             * includes child objects.
             */
            void UpdateTransformations();

            std::vector<SceneObject *> children;
            SceneObject * parent;

        public:

            /**
             * Constructs a new SceneObject.
             */
            SceneObject();

            /**
             * Destructs a SceneObject and deletes all child objects.
             */
            virtual ~SceneObject();

            /**
             * Adds a new child to this object.
             *
             * @param child The new child object. This object becomes child's parent object. This
             *   object takes ownership of child.
             * @return true if the child was added successfully, false otherwise.
             */
            bool AddChild(SceneObject *child);

            /**
             * Retrieves a list of all children of this object.
             *
             * @return A list of all children of this object
             */
            const std::vector<SceneObject *> &GetChildren() const;

            /**
            * Retrieves the position of this object in world space, i.e. the translation component
            * of the global transformation matrix.
            *
            * @return The global position of this object
            */
            const glm::vec3 GetGlobalPosition() const;

            /**
            * Retrieves a matrix that can be used to transform coordinates from world space to
            * object space. This is the inverse of the global transformation matrix.
            *
            * @return The inverse of the global transformation matrix
            */
            const glm::mat4x4 &GetGlobalToLocal() const;

            /**
             * Retrieves the global transformation matrix. The global transformation matrix is used
             * to transform coordinates from object space to world space.
             *
             * @return The global transformation matrix
             */
            const glm::mat4x4 &GetGlobalTransformation() const;

            /**
             * Retrieves the parent object of this object.
             *
             * @param The parent object of this object or NULL if this object has no parent
             */
            SceneObject *GetParent() const;

            /**
             * Retrieves the position of this object, i.e. the translation component of the
             * transformation matrix.
             *
             * @return The position of the object
             */
            const glm::vec3 GetPosition() const;

            /**
             * Retrieves the transformation matrix. The transformation matrix is used to transform
             * coordinates from object space to the object space of the parent object (or world
             * space if this object has no parent).
             *
             * @return The transformation matrix
             */
            const glm::mat4x4 &GetTransformation() const;

            /**
             * Checks whether this instance is of the given type.
             *
             * @param type The type to check against
             * @return true if this object is of type type, false otherwise
             */
            virtual bool IsInstanceOf(SceneObjectType type) const = 0;

            /**
            * Sets the global transformation matrix. The global transformation matrix is used
            * to transform coordinates from object space to world space.
            *
            * @param transformation The new global transformation matrix
            */
            void SetGlobalTransformation(const glm::mat4x4 &transformation);

            /**
             * Sets the position of this object, i.e. the translation component of the
             * transformation matrix.
             *
             * @param position The new position
             */
            void SetPosition(const glm::vec3 &position);

            /**
             * Sets the transformation matrix. The transformation matrix is used to transform
             * coordinates from object space to the object space of the parent object (or world
             * space if this object has no parent).
             *
             * @param transformation The new transformation matrix
             */
            void SetTransformation(const glm::mat4x4 &transformation);

            void ChildRemove(SceneObject * child);
        };
    }
}

#endif // RAYTRACER_SCENES_SCENEOBJECT_H

換句話說,您想要一種將子球指向父球的方法。

通過從父母的位置減去孩子的位置(以獲得指向父母中心的Y軸)並對其進行歸一化,為孩子計算新的Y軸方向。 通過將新的Y軸向量與現有的Z軸向量相交(在左​​手系統中)來計算新的X軸,請小心處理它們指向相同方向或彼此完全相反的情況(可能通過歸一化刪除)錯誤)。 然后將此新的X軸向量與新的Y軸向量交叉,以獲得新的Z軸向量。 將其存儲在孩子的方向矩陣中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM