Jump to content

Recommended Posts

Posted

Working on a CCD/Fabrik IK for skeleton, the main fonction used is to point in direction of a target (as position), already tryed joint that don't work with bones

As i did see on https://www.leadwerks.com/community/topic/16360-quaternion-lookat/#comment-107368 ,AlignToVector i tryed to make it work but with weird things going on without any conclusive result 

"

#include "Leadwerks.h"
using namespace Leadwerks;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();
    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);
    auto world = CreateWorld();
    auto camera = CreateCamera(world);
    camera->SetPosition(0, 2, -10);
    auto model = LoadModel(world, "https://github.com/Leadwerks/Documentation/raw/master/Assets/Models/Characters/Fox.glb");
    model->SetScale(0.05);
    model->SetRotation(0, -90, 0);
    model->SetPosition(0, 0, 0);
    auto bone = model->skeleton->FindBone("b_Neck_04");
    auto target = CreateBox(world);
    target->SetPosition(3, 0, 0);
    while (window->Closed() == false)
    {
        if (window->KeyHit(KEY_W))
        {
            target->Move(0, 1, 0);
        }
        if (window->KeyHit(KEY_S))
        {
            target->Move(0, -1, 0);
        }
        if (window->KeyHit(KEY_A))
        {
            target->Move(-1, 0, 0);
        }
        if (window->KeyHit(KEY_D))
        {
            target->Move(1, 0, 0);
        }
        world->Update();

        Vec3 bonePos = bone->GetPosition();
        Vec3 targetPos = target->GetPosition();
        Vec3 direction = (targetPos - bonePos).Normalize();
        bone->AlignToVector(direction, 2, 10.0f, 0.0f);
        world->Render(framebuffer);
    }
    return 0;
}

"

 

In a book i got, they use a function called Quat lookRotation that is mainly used for IK

Its implemented this way (for they own editor)

"

Quat lookRotation(const vec3& direction, const vec3& up) {

        // Find orthonormal basis vectors

       vec3 f  = normalized(direction); //Object Forward

       vec3 u = normalized(up); //Desired Up

       vec3 r = cross(u, f); //Object Right

      u = cross(f, r); // Object Up

 

      //  From world forward to object forward

     quat worldToObject = fromTo(vec3(0, 0, 1), f);

 

     //what direction is the new object up

     vec3 objectUp = worldToObject * vec3(0, 1, 0);

     // From object up to desired up

     quat u2u = fromTo(objectUp, u);

 

     // Rotate to forward direction first then twist to correct up

     quat result = worldToObject * u2u;

"

if that help

 

or anybody has a int on how i could do it differently ?

 

  • Like 1
  • WildThought changed the title to Quat lookRotation needed (for Bone point to get last to Skeleton IK)
Posted

Maybe this helps you

 

Vec3 Utils::GetForward(const Mat4& m)
{
	return Vec3(m.k.x, m.k.y, m.k.z).Normalize();
}

Vec3 Utils::TransformForward(const Mat4& m, const Vec3& v, const float distance)
{
	Vec3 forward = GetForward(m);
	Vec3 target = v + forward * distance;
	return target;
}

 

  • Like 1

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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

×
×
  • Create New...