10
$\begingroup$

In my game I have a Nurbs curve in the XY plane. I have a rigged car, that has a follow path constraint, so that it follows this curve.

I would like to make the Nurbs curve behave like a snake in the snake game. The snake should move by extending its head and cutting off its tail. When the player presses the 'up' arrow, the head should extend forward and the tail should be cut off. When the player presses the 'right' arrow, than the head should make a turn to the right, and idem for the 'left' arrow.

Is it possible to do that? In the pictures, the dots are the vertices of the Nurbs curve. I can give more details if necessary. Thanks :)

Examples of the curve turning to the left, the right, and moving straight:

curve turning to the left curve turning to the right moving straight

$\endgroup$
2
  • 2
    $\begingroup$ You said ..[the head will extend forward and the tail will be cut off]. Some readers may not understand. How is this different than regular movement? Can you include a picture with some frames or a small animation file.gif? $\endgroup$ Commented Jun 8, 2015 at 14:28
  • $\begingroup$ So you want the previous nurb curve vertex to be placed at the position where the next vertex was during previous frame if the position isn't where the next vertex is right now? I don't know how to do it, I'll be honest. $\endgroup$ Commented Mar 4, 2016 at 19:39

2 Answers 2

1
$\begingroup$

The following code moves each point in the curve ahead one spot. So the second point moves to where the first point is, the third to the second, and so on.

All you need to do is decide where the first point needs to be moved next.

import bpy

# Assumes your curve is the only one in the scene.
curve = bpy.data.curves[0]

items = curve.splines[0].points.items()
point_count = len(items)

for index in range(point_count - 1, 0, -1):
    current = items[index][1].co
    previous = items[index - 1][1].co
    current.x = previous.x
    current.y = previous.y
    current.z = previous.z

first = items[0][1].co
first.x -= 1

The final part moves the first point to the new position. So you could do first.x -= 1 for left, first.x += 1 for right, and first.y -= 1 for forward. This is making assumptions about the orientation of the scene, so you should adjust x, y, and z to suit your scene.

This code is very simple, but hopefully it is enough to get started.

$\endgroup$
0
$\begingroup$
  • Consider using shape keys. You have the shapes above. Also move the entire curve object in the direction that suits you.
$\endgroup$
1
  • $\begingroup$ Thanks, but I would like to keep extending its head and cutting off its tail, while the 'up arrow' key is pressed $\endgroup$ Commented Jun 8, 2015 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .