Inverse Kinematics

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Inverse Kinematics

Post by Xero » Mon Nov 01, 2021 3:04 pm

Hi Brainstrust,
I am working on a little pet project that will be using inverse kinematics to simulate a spine's movement.
Think of what I am doing like a stack of coins. If I pick up the bottom coin, all of the coins above it move as well. If you pick up the second to bottom coin, the coins above it move, but the bottom coin stays where it is. The end model would have about 24 segments (one for each of the spinal vertebrae), so there's a bit of stuff that needs to be juggled.
I had an idea about how to get it done, but it seems unfeasible in Livecode. It would have nested groups (group A containing all vertebrae, Group B containing all vertebrae except the bottom one, Group C containing all but the bottom 2, etc.). The groups would be rotated at whatever level you wanted, so rotating group B would rotate all but one vertrbrae, but that vertebrae would still be part of Group A which now would be changed because Group B (part of Group A) would have been rotated.
Problem is you can't rotate groups!
My next idea was to do it with polygons, but I would need to rotate them around a point that was not at their centre which is problematic.
I did a test where I put line around the axis point, one for each polygon vertebrae and rotated the line, rotated the polygon then moved the polygon to the end of the line. Problem is, each vertebrae needs to be rotated and moved with a line for each axis point, so 24 vertebrae, 24 lines each time... so about 575 lines to manage. Gah!
Is anyone aware of some Inverse Kinematics in Livecode (a simple search hasn't yielded anything for me) or does anyone know a good way of being able to nominate a rotation point for an object (polygon, image, etc). If I can easily rotate around a nominated point, I can work around a lot of stuff in the code...
Thanks in advance.
XdM

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Inverse Kinematics

Post by richmond62 » Mon Nov 01, 2021 3:31 pm

SShot 2021-11-01 at 16.30.05.png
-
Well, they do say "God likes trier", but, obviously not today. :)

Mind you, nutty as a fruitcake, I have "been this way before" (and that pun was intentional):

viewtopic.php?t=30405
Last edited by richmond62 on Tue Nov 02, 2021 7:49 am, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Inverse Kinematics

Post by richmond62 » Mon Nov 01, 2021 3:54 pm

SShot 2021-11-01 at 16.53.01.png
-
This probably says more about how primitive my knowledge of vertebrae is than anything else.

"Bash" any of the 3 red triangles (too much of a lazy slob to provide 24, one for each vertebra)
and observe.

NO groups . . .

Just a simple repeat loop. 8)
Attachments
Spine.livecode.zip
Here's a stack
(2.27 KiB) Downloaded 97 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Inverse Kinematics

Post by dunbarx » Mon Nov 01, 2021 6:50 pm

seems unfeasible in Livecode
Not at all sure what that means. Stole about 10 minutes from what I was doing (with LC) at work...
Spinal Crackin.livecode.zip
(1.8 KiB) Downloaded 101 times
Craig

EDIT.

Thinking just a little, this need to be done with a "mouseMove" handler.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Inverse Kinematics

Post by richmond62 » Mon Nov 01, 2021 7:18 pm

Clever stuff, Craig, and all in the cardScript.

Me bow and take darshan at lotus feet of Master! 8)
-
craig.jpg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Inverse Kinematics

Post by dunbarx » Mon Nov 01, 2021 8:53 pm

Aw, thanks, Richmond.

The stack has flaws, though. I now notice that you cannot raise only the topmost vertebra. The move itself is jerky, which is why using "mouseMove" would probably be nicer. And at first I thought about creating a group on the fly, based on the vertebra of choice and all the ones above it. That group could be disbanded when the move was finished.

Anyway, it was the comment that LC was inadequate that made me drop what I was doing...

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Inverse Kinematics

Post by dunbarx » Mon Nov 01, 2021 9:00 pm

Xero.

Rereading your initial post, I am confused about the later rotation stuff. What beyond the ability to lift, in a stack, a particular vertebra and its overlying ones are we talking about? My gadget only lifts, and needs work anyway.

Craig

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Inverse Kinematics

Post by Xero » Tue Nov 02, 2021 2:41 am

Thanks guys, I just got back and will have a look at them.
If the "rotate about x,y" thing works, I should be able to get it all functional.
As for the action... it's a rotation that I want. Basically, the vertebrae rotate around a central point that is in the middle of the cartilage discs between the vertebrae. A better analogy may be coin, silicon coin, coin, silicon coin, where the solid coin rotates around the central point of the silicon coin below it.
It's the rotation of the elements that's giving me issues. I am sure you can move a group, image or whatever really easily.
Sorry about the "inadequate" comment. Probably a bad choice of words!
Leave it with me and when I can get on to it tonight, I will see what has come of it all.
Thanks for your responses!
XdM

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Inverse Kinematics

Post by FourthWorld » Tue Nov 02, 2021 3:20 am

Ken Ray's Stykz has inverse kinematics, made with LiveCode:

https://www.stykz.net/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Inverse Kinematics

Post by richmond62 » Tue Nov 02, 2021 7:49 am

I spent about 30 minutes "pissing about" yesterday re rotation about a point
and got nowhere.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Inverse Kinematics

Post by bn » Tue Nov 02, 2021 8:57 am

Here is a stack based on code from Jim James Hurley that rotates an image around arbitrary points of the image.
If I understand Xero correctly then this should be adaptable to his problem.

Kind regards
Bernd
Attachments
turnImageCenterOfMassII.livecode 2.zip
(76.77 KiB) Downloaded 100 times

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Inverse Kinematics

Post by Xero » Tue Nov 02, 2021 3:08 pm

OK... previous ideas didn't come to fruition, but Bernd has hit the nail on the head.
That's the maths that I needed. I now just need to adapt to suit. I have done a trial stack with rectangular images, and managed to get the pieces to rotate around a selected point so I can move upper without lower vertebrae. Great Success!
I am just now working through getting the images of each vertebrae together and will need to do some working out on pivot points as they will be different for each inter-vertebral disc. Then some maths to work out that one it's been rotated, but that's within my scope.
Thanks team. I will keep you posted on progress. I am sure people will need this maths if doing a clock, or any animation of linked segments.
Regards,
XdM

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Inverse Kinematics

Post by Xero » Sat Nov 13, 2021 1:53 pm

Alright...
I have adapted the code to work for the spinal model.
I have managed to get the images to all rotate as necessary. I can set a rotation point and use it for multiple vertebrae, all works fine.
I am using line graphics to keep track of the rotation points though. So each vertebra has a line drawn under it that starts at the centre point of the image and ends at the rotation point of the vertebra. When I rotate the model around a point, the image and line rotate where they are, then move to a calculated new centre point. This way, the rotation point for a higher vertebral level moves into place and I don't have to calculate where the new point should be- it's just at the end of the line...
Here's the kicker... the code (see below) is doing something strange. When I reset the line (poly) manually, then hit the button, I get a weird rotation of the poly for the first click. Every subsequent click, with nothing in between, gets the code to work as expected. Only the first click goes awry.
I separated the code and put it into a "clock" stack (see attached). This stack should do exactly what I am doing in my spine model. The button should rotate the hand 6 degrees. The first click= whacky, the rest, all good, but the hand is now on an angle. Reset the hand manually to a certain direction and try again, same happens.
Why am I getting an odd rotation on the first click only?
Xero
(Livecode Community 9.0.4, Windows)
Spinal Model.jpg
Attachments
rotatepoly.zip
(1.02 KiB) Downloaded 90 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Inverse Kinematics

Post by bn » Sat Nov 13, 2021 3:29 pm

Hi Xero,

I think that is a bug in revRotatePoly.

if you change your code to

Code: Select all

on mousedown
   -- set rotation degrees
   put "6" into tRotDegrees
   
   set the realpoints of grc "line" to the points of grc "line" -- < add this line
   
then it works.

As far as I understand the "realpoints" are used to minimize rounding errors in revRotatePolygon because LC uses integers for location.

In my testing "realpoints" need only to be resetted once before doing revRotatePoly repeatedly. But if in your use case you do not accumulate rounding errors in setting the realPoints to the points each time then I would just go this way.

Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Inverse Kinematics

Post by richmond62 » Sat Nov 13, 2021 5:25 pm

Totally OT, and totally elsewhere, I have been pissing around, for some extremely daft reasons, with "Rudolf the alcoholic reindeer",
and got movement to focus on his nose rather than the centre of the reindeer image:

viewtopic.php?f=25&t=36434&start=45 On "page" 4.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”