snake like turns?

I want a snake like object to be controlled by the player that can turn and follow it’s “head” kinking 90 degrees (like the old 2d game snake).

But I’m new to blender and having problems figuring out how to apply the
animations locally. I have attatched a simple blend file to show you what i mean.
I’ve tried shape keys and armatures.
Perhaps you cannot apply shape actions in this manner?

I thought somebody might have already made a 3d snake game here that I could study, but I didn’t find one.



example_cube.blend (469 KB)

You can parent empties around the snake’s head, and use a track to actuator (arrow_up pressed = move and track to empty_up).
You can seach vertex parenting on the forum.

or you could use slow parenting maybe:)

I may be too new to the game engine, I can’t see the relation between what Will wants to achieve and your suggestion. Or maybe this is just my english that is too bad so that I couldn’t understand the topic.

I tried slow parenting with a snake game once.
The only problem is that you need the snake to be constantly moving.

Tried the parenting thing.
You can make it follow itself, but it is a little awkward as the following cubes
must turn 180 degrees to make a 90 degree turn. I need it to be smooth like
the action animation from the first blend file I posted.

Ignore the cubes going crazy at the end. This is just a quick viz file to show you what I


parented_examples.blend (136 KB)

Here is an example of how parenting could work.


snake.blend (150 KB)

Closer, but not quite. The object will have to be stationary at times. and maintain a consistent scale.
(It can’t be elongating and shrinking). I tried using the distance constraint to prevent the cubes from going back on themselves in the hopes of keeping a consistent volume, but I then I couldn’t get it to make real crisp 90 degree turns. Theere are gaps or cut corners that destroy the illusion.

The vertex parenting- I can get a vertex to parent an empty, but not an empty to parent a vertex, so I’m not sure I understand how I would use that.

You should probably go with python, are you familiar with python?

A little, but the only way of I can think of using python would be more of a gimmick.
Maybe you could divide the mesh up into pieces (as we were doing when we tried the parented cube examples) get the current global position of each piece, and then have a trigger that would reset the pieces to relative positions in an order that resembled keyframe animation.

I would rather have the mesh as a whole, but perhaps that’s an impossibility. Maybe you have a differet python solution in mind?

Keep the snake as seperate cubes.
Create a list in game logic:

GameLogic.snake = []

The list should contain positions, so it may look like this:

GameLogic.snake = [ [3,3,0], [3,2,0], [3,1,0], [3,0,0], [2,0,0], [1,0,0], [0,0,0] ]

That would look like this on a grid: (0,0 is the bottom left corner)

Create another list for storing the cubes:

GameLogic.cubes = []

Also store a int value as:

GameLogic.dir = 0
0: up
1: right
2: down
3: left

As you can see the values refer to directions.

So, using a timer property you can make the snake move every .2 seconds or so. To make it move do something like this:

if own.snake_move > .2: # snake_move is a timer property
    len = len(GameLogic.snake)
    dir = GameLogic.dir
    if dir == 0:
        x = GameLogic.snake[len-1]
        y = GameLogic.snake[len-1]+1
        z = GameLogic.snake[len-1]
    elif dir == 1:
        x = GameLogic.snake[len-1]+1
        y = GameLogic.snake[len-1]
        z = GameLogic.snake[len-1]
    elif dir == 2:
        x = GameLogic.snake[len-1]
        y = GameLogic.snake[len-1]-1
        z = GameLogic.snake[len-1]
    elif dir == 3:
        x = GameLogic.snake[len-1]+1
        y = GameLogic.snake[len-1]
        z = GameLogic.snake[len-1]

That doesn’t really move, it just makes the list “move” to make the cubes move you will need to do something like this underneath the above code:

if own.snake_move > .2: # snake_move is a timer property
    len = len(GameLogic.snake)
    dir = GameLogic.dir
    for i in range(len): 
        # The amount of elements in the snake list and cube list should be the same

When ever an apple is eaten you do the same thing but don’t do the GameLogic.snake.pop(0), you might have to change that like to:

if apple == 0: GameLogic.snake.pop(0)

Where apple is a boolean of weather an apple has been eaten.
You will also have to use a add object actuator when the apple is eaten, so the code will look like this:

if apple == 0:
    cube = add_object.getLastCreatedObject()

You also need to make sure you change GameLogic.dir whenever an arrow key is pressed.

I hope this helps you, I have to admit, I did have to put some thought into it.

I’ll give that a try. Thanks a lot. The implementation isn’t for a snake game exactly, but this type of thing was what I was looking for. I’ll let you know how it comes out.

I need to change my name to “will-beg-in-humiliation-4help” because I
just can’t get the script to work like it should.
Can somebody throw a cheap blend file in here that makes the above code function
because I’m a fool and can’t get it done. As soon as I see it, I’ll probably beat my head on the monitor. Thanks

Don’t worry about it, its not that humiliating to ask for help.

I attached the file.


snake.blend (111 KB)

You’re the man. I can’t tell you how much this template is going to help me.
I’ll show you the the implementation when I get it set up.

Ok, if you need some more insight on how a part works just tell me.

Very nice script. Good job.