[Intro] Hello Fellow Scratchers! Trying
out new ideas for Scratch projects is a whole not of fun, and right now I’m playing with simulating
a multi legged creature using inverse kinematics – Yes its Spidey cat! Ain't they the cutest!
And since I’m right here at the start of the process, I thought I’d share the fun with you all,
and make this into a devlog. I'm not even sure what kind of game this will be! And
nothing you see thus far is set in stone; so as you watch, and get ideas, drop me comments
to help influence the game before it finally gets released on Scratch! Exciting stuff!
[Summary] So, let's rewind
just a tad, and see just how this project began. With a fresh project, and not having the heart to
delete Scratchy completely, I kept their head and gave them a new body! The legs need to move
around, so I’ll leave them out for now. Next for the level, I just need it to be
filled with interesting platforms for our spider legs to interact with.
How we actual move the spider I’ll also work out later,
for now they can just follow the mouse pointer. [The first Limb – Find a surface within reach]
So with the scene set, we can look at adding our first spider limb… Just one. I want it to
reach out from the spider body, and attach to any nearby solid wall it can find.
Easier said than done, perhaps I can sweep the area around the player with
a fake leg looking for collisions. So I drew the fake leg, scripted the
loop. And when touching the level… I recorded the direction in a list of possible contacts.
Then we’re free to pick any one of those that seems best. Hey, you might like this. Did you
know that to pick a random item out of a Scratch list, you can copy and paste
the word “random” into the item of list block, and that actually works! How cool is that!?!
Turning off the “run without screen refresh”, and the leg now point at any wall within its reach.
[Finding the point of contact] And Yikes – That’s quite jiggly, lot’s of possible
contacts then obviously… All except down here. Now to make things more stable, we would
expect the leg to pick a contact point on a wall, and then stay with it. But for that,
we need to improve out detection, and find the actual point on the fake leg
where the wall contact actually occurs. For this, I’ll create a smaller second
costume, which I can shoot down the leg, until it comes into contact with the level. Recording the
point as “target x & y”. [Keep holding on]
Then, once we’ve found a target… we position the leg there, and point back towards the player’s
body. This we can repeat until the player becomes too far away to keep hold, and then we’ll just
need to find another place to hold on to. And there we go, this is far more stable. I
love how the leg stays attached and now rotates in place.
I’m less keen on how the leg extends through Spidey cat’s body.
To fix that we’ll need to give the spide leg a knee joint!
This allows the body to move right up close to the foot, and the leg bends to accommodate. This is
cool, and it’s called inverse kinematics! [Inverse Kinematics using Trigonometry]
It sounds dead hard, but in reality it’s just a little math. So here’s Spidey cat,
and here is their foot. We know the leg is made of
two parts… If they are the same length then that will make the math a little easier.
So the first half of the leg can be swung freely around Spidey’s body forming a circle.
And the other half we want to be touching the contact point, so again it can swing around there
making a second circle. And this is interesting! Do you see where the two circles intersect
(that is, where they cross). Well that is the point where we need to draw our leg’s knee!
Brilliant, so is there some math we can use to calculate this? Well of course,
it’s just some simple trigonometry for this right angled triangle.
We know the length of half a leg is 40 pixels. And we can get the distance of
the contact point to the player using the distance to block and divide it by 2.
Trigonometry tells us that: “angle = acos(adj / hyp)”.
Perfect, we can plug that in to our code, but first I’m going to switch to using pen
so that I can easily draw both halfs of a leg without having to use two sprites or clones.
Here it is with a simple line in pen without a knee.
Looks cool, but you wait until we add IK (inverse kinematics).
To prove it works, I’ll pop a circle around the player and the contact point. Where they cross is
where I expect the knee to flex. We need the distance from the
contact to the player. And then we’ll calculate
the angle to the knee using acos, or asin as I did here… whoops, now I’ll have to
do a subtract from 90. Next we move forward towards the knee, and let’s give that a test?
Oh yes! Guys look at this. That’s some beautiful articulation. And do you see how it is matching
the expected circle cross over perfectly. Yeah, it’s looking quite spider like over here.
[8 Legs are better than one] I think we are ready to up
our game and add in the other 7 legs! For that, I’ll swap to using clones. So the erase
block has to go in the main player sprite. And then I’ll create 8 clones.
Oh wow – This is looking exciting. There’s a bit of flickering as the
legs go out of range… I think we can fix that easy enough, I probably shouldn’t be doing
trig on numbers that are not valid. Yeah, when out of range, don’t even
try to draw the leg. That’s better. Smooth!
Hey isn’t it interesting how all the legs kind of form a spiral around the cat? Remember how
when we drew the circles to calculate knee joins? Well, theirs is always two possible intersections.
But we are only calculating this one, so our knee always bends the same way. Clockwise.
We could mess around with that, for example, when the foot is to the left of the spider,
make the knee bend in the opposite direction. How does this look?
Ha, cool – feels like a cross between a crab and a plasma ball! – It looks dead cool in certain areas
of this level like this vertical shaft. [Next Time]
I think what I’m going to try and work on next is having the legs transition from one position
to another rather than instantly teleport… And perhaps assign each leg to a body position to
keep the limbs spread around the body and stop them crunching up into one place.
Also, now that the legs follow the body, we’ll have to start thinking how the body should follow
the legs if you know what I mean? After all, the body should be being supported by the legs.
Hey and I’ve have some great news – You guys must know CrystalKeeper7 right? Well he’s amazing at
game art and it looks like we might even get a collab up and running here! Now that would
be so awesome! Go and take a look at his youtube channel and scratch account and you’ll see why
I’m excited! Can you imagine what this game could become? In fact, don’t just think about
it… post your comments under the video and take a look at what other people are saying and vote
them up. I think there’s a heap of potential and I’d love to try some of the best ideas!
[Outro] If you wouldn’t mind smashing the
like button on this video to get things rolling then I’d appreciate that, and do subscribe to
the channel and make sure to click the bell icon so you get notified when the next video drops!
For those who want to support the channel further we have a channel membership with
perks like channel emoji & early access to videos. Special thanks to my channel Patrons Emperoroz55,
Minecwaft, Bernardo C, Thor scratcher & Scratch Logan – You guys rock.
Well thank you for watching,
that’s all I’ve got for you today. Have a great week ahead, and Scratch on guys!