WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-10-2004, 06:24 AM
lucalatini lucalatini is offline
Member
 
Join Date: Feb 2004
Posts: 5
Progressive Motion Of The Viewpoint

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
Reply With Quote
  #2  
Old 02-10-2004, 11:14 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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:
Code:
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:
Code:
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:
Code:
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!
Reply With Quote
  #3  
Old 02-10-2004, 11:46 AM
FlyingWren FlyingWren is offline
Member
 
Join Date: Mar 2003
Location: Fargo, North Dakota, USA
Posts: 48
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.
Reply With Quote
  #4  
Old 02-11-2004, 07:23 AM
lucalatini lucalatini is offline
Member
 
Join Date: Feb 2004
Posts: 5
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
Reply With Quote
  #5  
Old 02-11-2004, 10:11 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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
Code:
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!
Reply With Quote
  #6  
Old 02-12-2004, 08:22 AM
lucalatini lucalatini is offline
Member
 
Join Date: Feb 2004
Posts: 5
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
Reply With Quote
  #7  
Old 02-12-2004, 09:54 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:51 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC