PDA

View Full Version : Progressive Motion Of The Viewpoint


lucalatini
02-10-2004, 06:24 AM
hello,

I'm trying to implement navigation with keypresses. I would like to make the viewpoint translate (o rotate) in a smooth and gradual way along a fixed distance (or angle). Thus, I use viz.move and viz.rotate within a cycle. But I have found some problems:

1. Sometimes the cycle does not work correctly. Here is a very simple example (running in Vizard 2.14 also if it is pure Python) to explain the problem.

#----- Example program--------
import math

a = 7.0
for x in range(10):
a = a + 0.1
print(a<7.5)
print a

a = 70
for x in range(10):
a = a + 1
print(a<75)
print a

a = 0.70
for x in range(10):
a = a + 0.01
print(a<0.75)
print a
#-----------------------------
It is interesting to note that the comparison a<value is true when a==7.5 and not when a==75 or 0.75. That is, in the former case the < operator behaves as a <= operator. Maybe I'm missing something obvious, but I don't understand the reason for this difference.

2. In endless_maze demo it is possible to set the velocity of the translation by means of time.clock() used within a timer cycle. The difference between two time recordings is divided by 0.016: why this value? and is there an alternative way to set the velocity of the movement?

Best,
Luca

farshizzo
02-10-2004, 11:14 AM
Hi Luca,

Here is some sample code that uses the keyboard for navigation, let me know if this is not what you are looking for:
import viz

viz.go()

def mytimer(num):
if viz.iskeydown(viz.KEY_UP):
viz.move(0,0,0.1)
elif viz.iskeydown(viz.KEY_DOWN):
viz.move(0,0,-0.1)

if viz.iskeydown(viz.KEY_RIGHT):
viz.rotate(viz.BODY_ORI,1,0,0)
elif viz.iskeydown(viz.KEY_LEFT):
viz.rotate(viz.BODY_ORI,-1,0,0)

viz.callback(viz.TIMER_EVENT,mytimer)
viz.starttimer(0,0.01,viz.FOREVER)
In regards to the < operator, you are hitting a rounding error. To see what I'm talking about have python print out more decimal places:a = 7.0
for x in range(10):
a = a + 0.1
print(a<7.5)
print '%.20f'%a
To fix this you could round off a to the nearest tenth of a decimal:
a = 7.0
for x in range(10):
a = a + 0.1
print(round(a,1)<7.5)
print a
Hope that explains it.
I'm honestly not sure why that 0.016 is in there, but you can control the velocity by adjusting the variable FORWARD_SPEED which is declared near the top of the script. Good luck!

FlyingWren
02-10-2004, 11:46 AM
I'm honestly not sure why that 0.016 is in there, [...]

Just a guess, but given 60 Hz is a common frame rate for VR stuff, note that

1/60 = 0.16666

The person who wrote that script probably wanted the object to update its movement about once each frame, which is why he/she set the timer to the .016 value.

lucalatini
02-11-2004, 07:23 AM
Hi,

1. Regarding the sample code, this is not exactly the kind of navigation I was looking for. My aim is to minimize the number of button pushes (as in endless_maze demo where to navigate you must press a button only when you are at the crossroads): I press once a button and then the viewpoint travels slowly ten meters (like when we push a button in an lift).
I think that the best solution is to create a keyboard function which calls a timer function which moves the viewpoint of a fixed distance and that is active until the viewpoint reaches the maximal distance. But which is the best way to set the speed?


2. Regarding the < operator, I have fixed the error in my program. Does that means that I will have to use the round function always to be sure that the float numeric value used is correct (i.e. to ensure that viz.move shifts the viewpoint of 7.3 meters and not of 7.29999999... meters) ? Is this a bug of Python ?

3. Regarding 0.016, I think that FlyingWren is right!

Thank you for your help.
Luca

farshizzo
02-11-2004, 10:11 AM
Hi Luca,

1) I understand now. Try the following code. When you press the 'g' key the viewpoint will be given a forward velocity of 1 meter/second. Then I start a timer that will expire in 5 seconds. When that timer expires it will set the velocity back to 0, thus the viewpoint will have moved 5 meters forward. Hope that makes sense :)
import viz
viz.go()

STOP_MOVING = 0

def mytimer(num):

if num == STOP_MOVING:
viz.velocity(0,0,0)


def mykeyboard(key):

if key == 'g':
viz.velocity(0,0,1)
viz.starttimer(STOP_MOVING,5)

viz.callback(viz.KEYBOARD_EVENT,mykeyboard)
viz.callback(viz.TIMER_EVENT,mytimer)

2) It's not really a bug of python. Floating point numbers are just inherently imprecise. This same type of error will occur in the C language, which python is based around. If you always need that sort of accuracy then you should probably round it.

3) I think so too, thanks FlyingWren!

lucalatini
02-12-2004, 08:22 AM
Hi,

1. This is what I was looking for. Is there a viz. velocity command also for rotations?

2. Ok.

3. Thanks to you and FlyingWren

Luca

farshizzo
02-12-2004, 09:54 AM
Hi Luca,

There is no velocity command for rotations, you'll need to perform it manually. For future reference, we will be adding built-in animation engines to viewpoints, similar to node3d objects.