PDA

View Full Version : Viewpoint Manipulation


ChrisB
09-16-2004, 12:17 PM
Hi,

I'm working on an experiment in vizard where I want the user to be stationary in the environment, and only be able to rotate left and right to survey the room. I want to use the left and right arrow keys to control the rotation, but I find that when using a callback function to adjust the viewpoint angle on a keypress, the program runs too choppy . What I have is something similar to this:

def GetUserInput(input):
global myView
global angle
if input == viz.KEY_LEFT:
angle = angle - 1
myView.rotate(0,1,0,angle)
elif input == viz.KEY_RIGHT:
angle = angle + 1
myView.rotate(0, 1, 0, angle)

Increasing the angle of rotation makes it faster but choppier. I went through some of the help files, and it recommended the use of the viz.iskeydown() for when a user would be holding down a key. I was hoping to use something like this:

while viz.iskeydown(viz.KEY_LEFT) == 1:
angle = angle - 2
myView.rotate(0,1,0,angle/delayfactor)

(or alternatively a spinto option)

but when this happens there seems to be some blocking occuring, it waits until you have released the direction key, before rotating the viewpoint afterwards, with the amount of rotation depending on how long you held the key down for.

Maybe I am totally going about this the wrong way though, is there an easier method, or am I just missing something? I tried putting the while loop in a director function as well thinking that would not be blocking but it had the same effect, the screen only rotates after you release the key, not while you're holding it down.

farshizzo
09-16-2004, 12:25 PM
Hi,

The trick is to use a timer to check if the key is down. Here's some sample code:view = viz.get(viz.MAIN_VIEWPOINT)
ROTATION_SPEED = 20.0 #Degrees/second

def ontimer(num):
if viz.iskeydown(viz.KEY_RIGHT):
view.rotate(0,1,0,ROTATION_SPEED*viz.elapsed(),viz .HEAD_ORI,viz.RELATIVE_LOCAL)
elif viz.iskeydown(viz.KEY_LEFT):
view.rotate(0,1,0,-ROTATION_SPEED*viz.elapsed(),viz.HEAD_ORI,viz.RELA TIVE_LOCAL)

viz.callback(viz.TIMER_EVENT,ontimer)
viz.starttimer(0,0.01,viz.FOREVER)