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. |
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 |
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 |
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.
|
Code:
testing that this is how you |
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() |
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() |
I needed to add the following two lines to the top of your script in order for it to run properly:
Code:
import random 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? |
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 |
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 |
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. |
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 07:42 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC