- Home /
How to Recreate the Old Classic Snake with Hinged Joints
Remember Snake? Well I'm currently recreating it, but I've gotten stuck on the basis of my idea. I was thinking I could attach a hinged joint to my main snake object (the head) and make all the Connected bodies snake segments (little segments that increase in number as you eat the apples). I wanted the snake to be strung together with these joints, but I've ran into a few problems.
Currently my SnakeMotor.cs script continually applies force to the snake's forward vector (keeping the snake always moving like the original) and my SnakeController.cs rotates the snake on its up vector (left or right) depending on user input. When I attempt to use the hinged joints with these controllers in mind, I get very unfamiliar results as to what I imagined it would work. The segments seem to pull on the snake head affecting its rotation and I can't tweak the settings for the life of me to find a proper effect.
Also, Keep in mind it's all 3d instead of the Classic Snake, so no tiles are involved; pure Unity physics.
So... Does Anyone have any ideas on how to approach this? I was hoping to make some type of joint (hinged, configurable, etc) work because I think it would apply a nice chain snake effect but I'm open for ideas.
Thanks, Let me know if I need to include any more information. -Mason Cook-
Answer by dissidently · Mar 07, 2011 at 03:12 PM
You're biggest problem here is likely the MASS of the connected parts as you stitch them to the head. This is known as Forward and Inverse Kinematics if you want to look it up online.
In short, each new object you attach as "tail" has Mass, and is acting like an anchor to the head part of your Snake.
The easiest way to overcome this would be making dummy objects with zero size and zero mass as the joint holders for your new tail pieces. These need to be rigid bodies to work with the physics, and they'll have TINY impacts, but probably nothing that you'll see once their size is reduced to nothing and mass to zero. (even tho the manual recommends not setting mass to zero, I've tested in on some of the stuff I'm doing, and it works the way I want)
Then you'll need to attach "tail section" objects to these dummy objects, and ensure that these visible tail sections do not have rigidbodies on them.
Hope that works for you.
This fixes the problems I had with the rigid bodies fighting eachother. Unfortunately, I can't make a good snake effect with hinged joints I suppose, it just doesn't follow the snake game logic properly >.< Thank you for your help though..
Did you managed to solve the problem, i am also in the same situation as you (I am new to this tool, need to write the java script). If the "rigid bodies fighting each other" try to check the constraints of all the axis, hope it will work......
Answer by Bunny83 · Mar 07, 2011 at 06:40 PM
You try to use physics on a movement that's not based on physics. If the snake turns, the body(-parts) follow the exact path of the head. But in the "real" world if you form a rectangle with a rope and you pull on one end the rope will not stay on the path you've set up, at least not without some sheaves at the corners to keep the rope on track.
The old snake game doesn't even move the body parts. The head moves on and a new bodypart is created at the last head's position. The last part of the snakes tail is removed to simulate the movement.
What you want is a fix movement along fix waypoints (that are set at direction changes). I don't know what you want to use as snake body because the bodyparts have to bend around the corners.(skinned mesh?).
The real challange when creating a snake-clone in 3D is to create this "movement engine" to handle the movement correctly (not too sharp curves or it will look strange).
As I said: to use physics is not the right choice here.
It would be possible to use physics but you would have to create invisible colliders (cylinders) that act as sheaves at the corners. But that would restrict the movement and if the chain is really long the spring behaviour of the joints will cause problems.
Okay, I see... I should have thought of that before I made the entire rest of the game ha... So How would I go about doing the way points? I've never used waypoints before, but I'm familiar with idea..