WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Randomising position of an object (https://forum.worldviz.com/showthread.php?t=1682)

ptjt255 10-17-2008 04:22 AM

Randomising position of an object
 
Hi

I wonder if someone can help me. I have created a 3x3 array of cubes using the copy function, I have set each cube to rotate. I now want each cube to randomly loom (i.e. come towards me) so that only 1 cube is looming at a time. Then I want to record reaction times to a key press, but if a key isn't pressed want to wait x seconds and then show the next random cube looming.

I suppose the easiest way of doing this would be to have one cube that looms and randomly shuffle it's location, but I'm sure how to do this.

Any help would be much appreciated.

Many thanks.

farshizzo 10-17-2008 03:18 PM

If you are storing all these cubes in a list, then you can use the random.choice() function to get a random cube from the list. Example code:
Code:

import random

# List containing all cube objects
cubeList = [...]

# Select a random cube from the list
randomCube = random.choice(cubeList)

Now you can have the cube loom at the user.

ptjt255 10-20-2008 12:39 AM

Thanks very much for your reply, unfortunately I get an error message:

randomCube = random.choice(cubeList)
AttributeError: 'module' object has no attribute 'choice'

I suspect this has something to do with the way I have set this up?

# Add a cube
cube1 = viz.add('box.wrl')

# Positions the first row of cubes
cube1.setPosition(0,0)
cube2 = cube1.copy()
cube2.setPosition(-3,0)
cube3 = cube1.copy()
cube3.setPosition(3,0)

# Position the second row of cubes
cube4 = cube1.copy()
cube4.setPosition(0,2)
cube5 = cube1.copy()
cube5.setPosition(-3,2)
cube6 = cube1.copy()
cube6.setPosition(3,2)

# Position the third row of cubes
cube7 = cube1.copy()
cube7.setPosition(0,4)
cube8 = cube1.copy()
cube8.setPosition(-3,4)
cube9 = cube1.copy()
cube9.setPosition(3,4)

# Define speed and rotation of cubes
def mytimer(rotate):
global angle
angle = angle + (speed * viz.elapsed())
cube1.rotate(0, 5, 20, angle)
cube2.rotate(0, 5, 20, angle)
cube3.rotate(0, 5, 20, angle)
cube4.rotate(0, 5, 20, angle)
cube5.rotate(0, 5, 20, angle)
cube6.rotate(0, 5, 20, angle)
cube7.rotate(0, 5, 20, angle)
cube8.rotate(0, 5, 20, angle)
cube9.rotate(0, 5, 20, angle)

# Opens file 'response.txt' in write mode
file = open('reaction_time', 'a')

# Sets up looming characterics for cube1
backAndFourth1 = vizact.sequence([vizact.goto(0, 0, -1, 1), vizact.goto(0, 0, 0, 1)])


# Define a function that records reaction times in response to a looming cube
def SaveData():
#Data for getting values from wait conditions
d = viz.Data()
while True:
#Wait random amount of time
yield viztask.waitTime(2)
# Cube 1 looming
cube1.addAction(backAndFourth1)
#Wait for next frame to be drawn to screen
yield viztask.waitDraw(d)
# Calculate reaction time
displayTime = d.time
#Wait for keyboard reaction
yield viztask.waitKeyDown(None,d)
reactionTime = d.time - displayTime
# Create the output string
out = str(reactionTime) + '\t''\n'
# Write the string to the output file
file.write(out)
print 'Reaction time:',reactionTime

farshizzo 10-20-2008 01:49 PM

Can you post the code for the entire script that is causing this error? Also, please use the [code][/code] tags when posting code samples on the forum. This will preserve the indentation, which is essential for running Python code.

ptjt255 10-21-2008 01:39 AM

Code:

testing that this is how you

ptjt255 10-21-2008 01:44 AM

Please find below the entire code which is giving me the error message: randomCube = random.choice(cubeList)
AttributeError: 'module' object has no attribute 'choice'

Code:

viz.go()
viz.mouse(0)
viz.cursor(viz.OFF)

# Sets viewpoint at z meters away
viz.move(0,0,-10)

# Asks for the participant number
subject = viz.input('Enter the participant number?')

# Add a cube
cube1 = viz.add('box.wrl')

# Sets the speed of the rotation for all 9 cubes
speed = 50

# Sets the speed of the timer for all 9 cubes
UPDATE_RATE = 0.001

# Sets a variable that will hold the angle
angle = 0

# Sets the rotation rate degrees/sec
rotate = 270

# Positions the first row of cubes
cube1.setPosition(0,0)
cube2 = cube1.copy()
cube2.setPosition(-3,0)
cube3 = cube1.copy()
cube3.setPosition(3,0)

# Position the second row of cubes
cube4 = cube1.copy()
cube4.setPosition(0,2)
cube5 = cube1.copy()
cube5.setPosition(-3,2)
cube6 = cube1.copy()
cube6.setPosition(3,2)

# Position the third row of cubes
cube7 = cube1.copy()
cube7.setPosition(0,4)
cube8 = cube1.copy()
cube8.setPosition(-3,4)
cube9 = cube1.copy()
cube9.setPosition(3,4)

# Define speed and rotation of cubes
def mytimer(rotate):
        global angle
        angle = angle + (speed * viz.elapsed())
        cube1.rotate(0, 5, 20, angle)
        cube2.rotate(0, 5, 20, angle)
        cube3.rotate(0, 5, 20, angle)
        cube4.rotate(0, 5, 20, angle)
        cube5.rotate(0, 5, 20, angle)
        cube6.rotate(0, 5, 20, angle)
        cube7.rotate(0, 5, 20, angle)
        cube8.rotate(0, 5, 20, angle)
        cube9.rotate(0, 5, 20, angle)

# Opens file 'response.txt' in write mode
file = open('reaction_time', 'a')

# Sets up looming characterics for each cube
backAndFourth = vizact.sequence([vizact.goto(0, 0, -1, 1), vizact.goto(0, 0, 0, 1)])

# List containing all cube objects
cubeList = [cube1, cube2, cube3, cube4, cube5, cube6, cube7, cube8, cube9]
randomCube = random.choice(cubeList)

# Define a function that records reaction times in response to a looming cube
def SaveData():
        #Data for getting values from wait conditions
        d = viz.Data()
        while True:
                #Wait random amount of time
                yield viztask.waitTime(2)
                # Cube 1 looming
                randomCube.addAction(backAndFourth)
                #Wait for next frame to be drawn to screen
                yield viztask.waitDraw(d)
                # Calculate reaction time
                displayTime = d.time
                #Wait for keyboard reaction
                yield viztask.waitKeyDown(None,d)
                reactionTime = d.time - displayTime
                # Create the output string
                out = str(reactionTime) + '\t''\n'
                # Write the string to the output file
                file.write(out)
                print 'Reaction time:',reactionTime

# Defines a callback for functions
viz.callback(viz.TIMER_EVENT, mytimer) 
viz.starttimer(1, UPDATE_RATE, viz.FOREVER)
viztask.schedule(SaveData())


ptjt255 10-21-2008 07:59 AM

Hi - I have been working on this all day and I'm still having difficulty in getting the objects to loom randomly, in the below script I have one object looming successfuly. Pressumably there also needs to be an if statement somewhere that says if key isn't pressed after x time move to next object.

Code:

viz.go()
viz.mouse(0)
viz.cursor(viz.OFF)

# Sets viewpoint at z meters away
viz.move(0,0,-10)

# Asks for the participant number
subject = viz.input('Enter the participant number?')

# Add a cube
cube1 = viz.add('box.wrl')

# Sets the speed of the rotation for all 9 cubes
speed = 50

# Sets the speed of the timer for all 9 cubes
UPDATE_RATE = 0.001

# Sets a variable that will hold the angle
angle = 0

# Sets the rotation rate degrees/sec
rotate = 270

# Positions the first row of cubes
cube1.setPosition(0,0)
cube2 = cube1.copy()
cube2.setPosition(-3,0)
cube3 = cube1.copy()
cube3.setPosition(3,0)

# Position the second row of cubes
cube4 = cube1.copy()
cube4.setPosition(0,2)
cube5 = cube1.copy()
cube5.setPosition(-3,2)
cube6 = cube1.copy()
cube6.setPosition(3,2)

# Position the third row of cubes
cube7 = cube1.copy()
cube7.setPosition(0,4)
cube8 = cube1.copy()
cube8.setPosition(-3,4)
cube9 = cube1.copy()
cube9.setPosition(3,4)

# Define speed and rotation of cubes
def mytimer(rotate):
        global angle
        angle = angle + (speed * viz.elapsed())
        cube1.rotate(0, 5, 20, angle)
        cube2.rotate(0, 5, 20, angle)
        cube3.rotate(0, 5, 20, angle)
        cube4.rotate(0, 5, 20, angle)
        cube5.rotate(0, 5, 20, angle)
        cube6.rotate(0, 5, 20, angle)
        cube7.rotate(0, 5, 20, angle)
        cube8.rotate(0, 5, 20, angle)
        cube9.rotate(0, 5, 20, angle)

# Opens file 'response.txt' in write mode
file = open('reaction_time', 'a')

backAndForth = vizact.sequence([vizact.goto(0, 0, -1, 1), vizact.goto(0, 0, 0, 1)])

# Define a function that records reaction times in response to a looming cube
def SaveData():
        #Data for getting values from wait conditions
        d = viz.Data()
        while True:
                #Wait random amount of time
                yield viztask.waitTime(2)
                # Cube looming
                cube1.addAction(backAndForth)
                #Wait for next frame to be drawn to screen
                yield viztask.waitDraw(d)
                # Calculate reaction time
                displayTime = d.time
                #Wait for keyboard reaction
                yield viztask.waitKeyDown(None,d)
                reactionTime = d.time - displayTime
                # Create the output string
                out = str(reactionTime) + '\t''\n'
                # Write the string to the output file
                file.write(out)
                print 'Reaction time:',reactionTime

# Defines a callback for functions
viz.callback(viz.TIMER_EVENT, mytimer) 
viz.starttimer(1, UPDATE_RATE, viz.FOREVER)
viztask.schedule(SaveData())


farshizzo 10-21-2008 09:38 AM

I needed to add the following two lines to the top of your script in order for it to run properly:
Code:

import random
import viztask

Also, make sure you don't have any other file in the same directory as your script that is named random.py. If you do, this might conflict with the built-in random module.

Also, can you explain in more detail what behavior you are expecting? Should the box disappear after it looms, or should it go back to its original position?

ptjt255 10-21-2008 10:06 AM

The script above works exactly as I want it to work, i.e. I had import random and import viztask at the top of my script. And as per the script above the cube should loom and then go back to its original position.

The only two things I am having difficulty with is:

1. getting the objects to loom randomly
2. adding an if statement that if there is no keyboard reaction to wafter x seconds to move onto the next object

Thanks

farshizzo 10-22-2008 05:51 PM

I modified your script to randomly loom a new cube every iteration, and to handle the case if no reaction was registered for a certain amount of time. I also place the cube back to the original location once the reaction is entered.

Code:

import viz
import vizact
import viztask
import random

# Time limit for keyboard reaction
REACTION_TIME_LIMIT = 5.0

# Sets the speed of the rotation for all 9 cubes
ROTATE_SPEED = 50

viz.go()
viz.mouse(0)
viz.cursor(viz.OFF)

# Sets viewpoint at z meters away
viz.move(0,0,-10)

# Asks for the participant number
subject = viz.input('Enter the participant number?')

# List of cubes
cubes = []
for row in [0,2,4]:
        for col in [0,-3,3]:
                pos = (col,row,0)
                cube = viz.add('box.wrl',cache=viz.CACHE_CLONE,pos=pos)
                cube.originalPos = pos
                cubes.append(cube)

# Function that will update cube rotation every frame
def RotateCubes():
        increment = ROTATE_SPEED * viz.elapsed()
        for cube in cubes:
                cube.setAxisAngle([0, 5, 20, increment],viz.REL_PARENT)
       
vizact.ontimer(0,RotateCubes)

# Opens file 'response.txt' in write mode
file = open('reaction_time', 'a')

# Define a function that records reaction times in response to a looming cube
def SaveData():
        #Data for getting values from wait conditions
        d = viz.Data()
        while True:
               
                #Wait 2 seconds
                yield viztask.waitTime(2)
               
                # Randomly loom a cube
                randomCube = random.choice(cubes)
                backAndFourth = vizact.sequence([vizact.goto(0, 0, -1, 1), vizact.goto(randomCube.originalPos, 1)])
                randomCube.addAction(backAndFourth)
               
                #Wait for next frame to be drawn to screen and save display time
                yield viztask.waitDraw(d)
                displayTime = d.time
               
                #Wait for keyboard reaction or timeout
                waitKey = viztask.waitKeyDown(None,d)
                waitTimeout = viztask.waitTime(REACTION_TIME_LIMIT)
               
                yield viztask.waitAny([waitKey,waitTimeout],d)
               
                if d.condition is waitKey:
                       
                        reactionTime = d.time - displayTime
                       
                        # Create the output string
                        out = str(reactionTime) + '\t''\n'
                       
                        # Write the string to the output file
                        file.write(out)
                        print 'Reaction time:',reactionTime
                       
                else:
                       
                        print 'Reaction timeout exceeded'

                # Restore cube to original location and stop looming action
                randomCube.setPosition(randomCube.originalPos)
                randomCube.clearActions()
               
# Defines a callback for functions
viztask.schedule(SaveData())


ptjt255 10-22-2008 11:13 PM

Thanks so much for your help Wizard.

The only problem is that the cubes aren't coming towards to the user they are moving to position (0, 0, -1, 1)

Any thoughts?

Many thanks.

ptjt255 10-23-2008 02:27 AM

Hi - just to say I've worked it out. Thanks for all of your help with this.
:D


All times are GMT -7. The time now is 01:53 AM.

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