Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Demo: IK Demo #211

Closed
TwistedTwigleg opened this issue Feb 1, 2018 · 13 comments · Fixed by #226
Closed

New Demo: IK Demo #211

TwistedTwigleg opened this issue Feb 1, 2018 · 13 comments · Fixed by #226
Labels

Comments

@TwistedTwigleg
Copy link
Contributor

I have been working on a relatively simple IK (inverse kinematics) system for Godot 3, intended to be used in the FPS tutorial I'm making. Now that the system is fairly stable, I thinking about maybe sharing the system here on the demo repository.

The IK system works both in the editor and at runtime, the performance is pretty good (though it could use some touching up in a few places), and the demo would add IK implementation for others to use and build on. The code could also help as a reference on how to make a EditorPlugin that reacts when exported variables are set and can show how to add nodes to the scene tree (at runtime and in the editor).

You can see the system in action here, here, and here.

I can add a few examples to show how to use the system (like dynamically placing feet on terrain. Another potential use case that I'd like to look into is making ragdolls)

I can also write a tutorial in the docs explaining how the system works. It's fairly simple and should be relatively easy for intermediate Godot users to understand.

What do you guys think?

@pwab
Copy link

pwab commented Feb 2, 2018

This would be totally awesome! I already saw your nice work and hoped you would share 😄

I saw that your system works in 3D space. Is it based on the skeleton node? How about using constraints? Would it be possible to get it working in 2D space too?

I am asking because I was working on an IK system too but only for 2D space. It is based on the FABRIK algorithm by Andreas Aristidou and although this approach is intended to be used in 3D I was only interested in using it for some 2D models. Constraints still do not work as wanted because of deadlock situations but I'm sure I will get this working somehow.

If your system would work with 2D models too I am interested how it compares in complexity and performance. And if not I could add my 2D implementation as another demo.

@TwistedTwigleg
Copy link
Contributor Author

Thanks pwab! 😃

My system uses a look-at-IK solver to calculate the rotations in 3D space. I use the skeleton node to get the bone transforms, do the some calculations, and then send the transform back. Currently the system is built around the skeleton node, but thinking about it the system should (in theory) be able to work with any node that has a look_at function with some slight modifications.

Currently I do not have a constraint system in place. I have a relatively good on idea on what I need to do, but of course that generally means it's going to be harder than I think.

That's cool that you are working on an IK system too! I couldn't wrap my head around translating the algorithms I found into code, so I just used the look-at-IK solver because I understood how it worked. That's really neat that you're using a tried and true algorithm! I'd love to see it in action!

My system only works in 3D space currently, though it shouldn't be too hard to translate into 2D. That said, I wasn't planning on adding a 2D implementation, mostly because I primarily work in 3D and rarely use 2D 😆. If you want, I can see about making it work in 2D, or I can just leave it and we can use your (likely better) system for a 2D example.

Regardless I'd be happy to compare our implementations! I found working on the IK system to be really fun, and I've learned (and still am learning) a lot of interesting math as I've gone along.

@pwab
Copy link

pwab commented Feb 3, 2018

Well okay then I think we could try to add two demos for now: one with your implementation concerning 3D space and one with a 2D implementation. After that we can try to unite both - if feasible and reasonable.

I have to say that my implementation is just a plain script file (not a plugin) but it could be possible to work on that too. I will not have much time the next days/weeks but if I find the time I will prepare a PR. Here are just some first impressions:

Unconstrained
unconstrained

Constrained
(You can clearly see the deadlock situation where the IK chain can't get constructed even if the target is in reach.)
constrained

@TwistedTwigleg
Copy link
Contributor Author

Sounds good! Having two different demos adds more variety too!

I have to say: WOW! Your system looks really good! (Totally blows mine out of the water 😆). I'm also having some trouble working around a couple deadlock situations in my version too (with multi bone IK chains), so you're not alone there.

I wouldn't worry about making it a plugin if you don't want to. I've just made my a plugin in an effort to reuse it for the FPS tutorial. Also, no worries on the wait! Take your time, I don't mind waiting (and my apologizes if I made you feel pressured)

@pwab
Copy link

pwab commented Feb 5, 2018

[...] and my apologizes if I made you feel pressured

No worries. This encourages me even more! :)

Thanks to a recent twitter post by reduz I got some assets to test my system with sprites (cutout) instead of polygons. Works great too but I will have to place the sprites pivot points by the script itself.

Godette Test
godette

(Assets and License can be found in the godot-design repository)

@TwistedTwigleg
Copy link
Contributor Author

Nice! It looks like it works well with Godette (that's her name right?). I wish you luck! placing pivots in GDScript sounds like it could get interesting.


Unfortunately a couple days ago I found I had a huge bug: My IK system didn't work with anything but a skeleton at origin with no rotation applied. Thankfully I've managed to fix the look-at-IK side of things (mainly it's just converting from skeleton space to global space) but I still have an annoying global rotation bug with my IK chain.

After that, I'll have to see if differing scales brakes things or not... 😦 (At least it shouldn't be too hard to fix, in theory).

I don't want to release something that only works in one very specific scenario, so I guess it's going to take a little longer than I hoped to release a demo. If all goes well, I'll have the bug(s) fixed this week, and then can clean up the code and get a demo made shortly after.

@TwistedTwigleg
Copy link
Contributor Author

TwistedTwigleg commented Feb 25, 2018

Well, after several weeks of trying and working with 3 or so implementations, my two bone IK chain is entirely broken and I have no idea on how to fix it, nor how to make a new (working) version.

I may try to add 2 bone IK chains in later, but for now I'm going to move forward without them! Hopefully I can start working on the IK demo soon (though it won't be nearly as impressive/useful... 😢 )

@pwab
Copy link

pwab commented Mar 4, 2018

Hopefully I can start working on the IK demo soon [...]

It seems you found some time to create some impressive stuff here. Congrats on successfully implementing FABRIK for your IK-Chains 👍

At the moment I can't spare any time for working on the 2D version as well. I also want to wait a little bit how the new Skeleton2D stuff is implemented. Maybe that will supersede my implementation of constructing virtual bones.

@TwistedTwigleg
Copy link
Contributor Author

Congrats on successfully implementing FABRIK for your IK-Chains

Thanks! Everything seems to be working, and it even works with skeletons moved and/or rotated from origin. I tried add constraints, but I couldn't get it working, but for my uses I don't really need it. I'm probably going to skip constraints for now and maybe add it later.

I decided to try using FABRIK since your IK implementation uses it. After reading FABRIK from several sources, this Roblox one was probably the best of the ones I found, and my implementation is heavily based on it.

Knowing that it was at least possible for 2D gave me the inspiration to try and use it for 3D. So, thanks! 👍 I likely wouldn't have a working bone chain without your post!

At the moment I can't spare any time for working on the 2D version as well. I also want to wait a little bit how the new Skeleton2D stuff is implemented. Maybe that will supersede my implementation of constructing virtual bones.

Sounds good, and take all the time you need!

Hopefully the Skeleton2D stuff will work similarly to Skeleton, but without some of the quirks. (For example: get_bone_global_pose returns a transform for the bone in the skeleton's global space, not world space.). I took a look through it and it looks like it is similar, so who knows?

@TwistedTwigleg
Copy link
Contributor Author

Alright, I've finished the plugin, made some examples, and now it's ready to be made into a PR!

Where should I put it?
The demo has a plugin adding two nodes (IK_LookAt and IK_FABRIK), with a examples folder containing a few examples showing how the plugin works. I'm leaning towards putting it in 3D, since it's a neat feature to have, but technically the demo is more of a plugin...

Thoughts?

@pwab
Copy link

pwab commented Mar 16, 2018

Putting the demo into 3D seems fine. You could also provide an extra repository for the plugin itself.

@TwistedTwigleg
Copy link
Contributor Author

Okay. Thanks!

@hsandt
Copy link

hsandt commented Aug 4, 2023

i know this is old, but this is the only reference to 2D IK demo I could find on the repository. I opened the old 3D IK demo in Godot 4.1 but it has not been ported yet so it doesn't even run; and by just opening scenes, I can see there is no 2D example.

@pwab 's examples look great, since the character is side view, I thought that maybe they managed to make flipping work right, as I got this issue: godotengine/godot#80252

But since it was Godot 3, many things may have changed since...

Since this issue is old, I'll be glad to continue discussion elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants