Game Development Community

Snake Physics

by Leo Gura · in Torque Game Builder · 10/23/2006 (5:18 pm) · 11 replies

Hi All,

I'm trying to replicate snake physics by using a series of mounted object. What I'm doing is creating an object for the snake's "head" to which I mount and "body" object. Then to this body object I mount another body object, and so on until a long chain is formed. The problem I'm running into is that to a get the snake effect I'm forced to use a non-zero mounting force, and this means that as the snake accelorates the gaps between the body objects increase in size (imagine a snake made of rubber). What I'm after is a snake effect which doesn't stretch. I've tried to achieve this effect using a mounting force of zero, but then the snake turns into a stick. How can I acheive an inelastic snake effect (it should behave like a bicycle chain when bent)? Essentially the distances between the objects must stay constant, but there needs to be some leeway in the angle between each body object. What functions would allow me to acheive this effect?

Thanks.

#1
10/23/2006 (5:25 pm)
Random thoughts begin here:

What happens if you use a really high mount force instead of 0? Or, you could increase the mount force depending on the acceleration of the head.

Are the pieces colliding with themselves? Perhaps a smaller, non-overlapping collision area would work better. (I don't even remember if mounted objects can collide with their parents.)

You could probably write your own system pretty quickly too, making it specific to this goal.
Or you could write your own algorithm that manages the angles between consecutive pieces down the chain.

Hope one of those helps a bit ;)
#2
10/23/2006 (6:27 pm)
I've tried varying the mount force from 0 to 10 and can't get the desired effect. I don't have collisions set between the body pieces, so this isn't really an issue.

As I understand the problem now, I need to use a mount force of zero but need a way of adding some leeway to the joints so that turning the head doesn't automatically turn all the body objects as if the whole snake is perfectly rigid. I'm now sure how to actually code this though, any suggestions? Which options can I tinker with besides mount force? Or perhaps my whole approach is wrong?
#3
10/23/2006 (6:34 pm)
Mount force should have no effect on the angles of your objects. If switching to a non-zero mount force seemed to help, its only because the positions were lagging. Your problem is with updating the angles, not the positions.

One way that you could achieve your desired effect is with an IK chain where the head is the base and the tail is the end effector. There are lots of resources online with different IK chain algorithms that would probably help a lot.
#4
10/23/2006 (6:34 pm)
Oh... I see. So the real problem right now is just that the rotation is being propagated down the chain?

If that's the case, then I'd turn off trackRotation (or whatever it's called) for each mounted object and then write my own function that manages the rotations. For instance, you could have the head call its child when it moves and the child would change its rotation depending if the difference between it and its parent is more than X degrees. Then, do the same thing down the line.

In any case, see if turning trackRotation off helps (if you have it on ;) and let us know if that fixes the whole-snake-rotating issue.

Edit: Darn - beat by Thomas!
#5
10/23/2006 (7:25 pm)
Muahaha... Ninja poster!

Also, it shouldn't matter if track rotation is on or off as long as you know whether to manipulate your rotations based on relative or absolute angles. You might consider not even mounting the pieces at all and just updating their positions manually.
#6
10/23/2006 (7:29 pm)
Actually, I remember when I was working on my IK chain thingy in TGB that I ran into some wierdness of mounted objects not having their positions updated until the next frame, which caused some very strange issues that were only obvious when dealing with very long chains (50+ links). Anyway, I wound up switching to manually updating postion.
#7
10/24/2006 (12:44 am)
While I appreciate the attempt Thomas, I'm affaird I don't understand how IK chains work, nor do I know how to go about implementing one in Torque. I need some suggestions using mounting and the like. Is there an option for mount angle tracking that be analogous to mount force? Something like angle mount force? So that when an object tracks the angle of another it does so with a bit of a delay rather than rigidly? I believe I read somewhere that this was possible, no?
#8
10/27/2006 (12:00 pm)
I made a snake like sprite once and ran into the same problem. If I remeber correctly, I ended up mounting them without tracking rotation. Then I set schedules and arrays to make the second section mimick the head's rotation a few milliseconds later (depending on the speed). Finally I checked to see if the head object had rotated more than, say 70 degrees away from the second section and adjusted that section so that it was only 70 degrees. And if the velocity of the head stops then all the rotation stops too, to keep the snake from straighting out every time it stops. Basicly you have to time the lag between the head's rotations and the next section's rotation based on the speed of the head.

If you have no idea what I'm talking about then its enough to know, that's its not easy to make a snake like thing. My advice is try to come up with ANYTHING else that might work for your game before trying to make a snake. But if you have to make a snake, then be prepared to do a lot of coding and testing.

-Peter
#9
10/27/2006 (12:38 pm)
@Peter - That's an ok way to fake it, but it will still look wrong when the head stops moving - all the body segments will slowly straighten out for no reason, whereas snakes generally don't come to rest in a straight line. Really, an IK solver is the best option for realistic movement, and a simple spine IK is fairly simple and fast enough to run in script in TGB. You can find all sorts of resources online if you just search for Inverse Kinematics.
#10
10/27/2006 (1:03 pm)
From Peter's post @ Thomas:
Quote:And if the velocity of the head stops then all the rotation stops too, to keep the snake from straighting out every time it stops.

;)

But I agree, IK is sweet, and refreshing.
#11
10/28/2006 (3:36 am)
Doh! I should really read more carefully. >_<

After a while on the forums I tend to kinda skim... sorry. I guess if the rotation intervals of the body segments are scaled by the linear velocity of the head this method could work fairly well. I still like to overcomplicate things for some reason... i.e. - "woohoo IK solver!"

Seriously though, an IK solver would be friken sweet. You could do stuff like have the snake coil back and strike just by animating a single point in space.