rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Pi3D animation/loop problem

Sat Nov 29, 2014 4:58 pm

Hello again.

I hope someone can help me with this Pi3D animation/loop problem. It is most likely just a rock bottom logic error.
Here's what I'm trying to do.

I have two buttons, left and right. Plus a basic scene consisting of a 3d-Object in front of a background plane.
If the left button was pushed the 3d-Object should move left, to a certain x value by a given acceleration value. The same with the right button, only in the right direction.

For my understanding it should work the way I implemented it (see code below). What happens though is that the Object just jumps to the left or right x value when the according button was pushed.

What's the problem here?

Code: Select all

…
knotX = 0	#initial x-position
accel = 0.07
left = False
right = False

while DISPLAY.loop_running():
	
	gesture_value = gesture(ser, 1000)

	#draw elements
	endlessKnot.draw()
	yellowBgnd.draw()

	#if left button pushed activate left trigger boolean
 	if gesture_value == 1:
 		left = True

	#if right button pushed activate right trigger boolean		
 	elif gesture_value == 2:
 		right = True

	#if left boolean triggered move knot left until x = -5
	while left == True:
		while knotX >= -5:
			knotX -= accel
		left = False

	#if right boolean triggered move knot right until x = 5
	while right == True:
		while knotX <= 5:
			knotX += accel
		right = False

	#animate
	endlessKnot.positionX(knotX)
	endlessKnot.rotateIncY( 1 )


	if mykeys.read() == 27:
		mykeys.close()
		DISPLAY.destroy()
		break
If I get the animation working, my next step will be to implement some animation tweening so that there's not only linear movement available. To do that, I thought this package might be useful: https://pypi.python.org/pypi/PyTweening/1.0.0.
Has anyone implemented tweening in a Pi3D project yet and could share some experience?

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D animation/loop problem

Sun Nov 30, 2014 12:01 am

Basically because you've got a while inside your while (actually inside another while!!). That way the inside while will loop until the test fails so

Code: Select all

while DISPLAY.loop_running():
   # knotX is 5 say
   while left == True:
      while knotX >= -5:
         knotX -= accel
      left = False
    # same frame but knotX is now -5
it would make more sense as something like

Code: Select all

while DISPLAY.loop_running():
   if left: #no point testing if a boolean variable is equal to a boolean variable!
     if knotX >= -5:
         knotX -= accel
     else:
         left = False
but actually you can do quite nice tweening with simple exponential smoothing

Code: Select all

target = [0.0, -5.0, 5.0]
while DISPLAY.loop_running():
   
   gesture_value = gesture(ser, 1000)
   knotX = knotX * 0.01 + target[gesture] * 0.99
   #animate
   endlessKnot.positionX(knotX)
   endlessKnot.rotateIncY( 1 )

also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”