WorldViz User Forum random coding
 Register FAQ Search Today's Posts Mark Forums Read

#1
05-25-2010, 02:46 AM
 Saz Member Join Date: Nov 2008 Posts: 36
random coding

Hi,

I want to have some code in my driving simulation that randomly changes the gain of the accelerator, rather like when you're out driving and you go up and down hills and inclines in the road. There is also a speedometer in the corner of the screen so this will also need to change accordingly to reflect the change in the gain of accelerator and therefore the associated drop or increase in speed. This is the code I have so far:
Code:
```import viz
#Import vizjoy module
import vizjoy
import time
import math
import vizact
import vizinfo
import random

viz.displaymode(1024, 768)
viz.go(viz.FULLSCREEN)
viz.mouse(viz.OFF)
vizact.ontimer(40,viz.quit) #runs file for 40 secs

subject = viz.input('What is the participant number?')

MOVE_SPEED = 0.25 #initial speed scalar
#define random speed scalar constant for changing gain on accelerator

viz.clearcolor(0.5,0.5,1)
viz.MainView.setPosition(0,.5,0)

skybox.texture(sky)

ground.setPosition(0,0,25)

#and place it over ground

ground_position = 50

viewpoint_pos = viz.MainView.getPosition()
#check to see how close the viewpoint is to the end of the road
if road_position - viewpoint_pos[2] < 50:

global ground_position
groundcopy = ground.copy()
groundcopy.setPosition([0,0,ground_position])
ground_position +=50

for i in range(1,50):

#call a timer every frame to check if road needs to be added

#change the road texture on keypress

def UpdateJoystick():
#Get the joystick position
x,y,z = joy.getPosition()
#Get the twist of the joystick
#twist = joy.getTwist()
#Move the viewpoint forward/backward based on y-axis value
if abs(y) > 0.0001: #Make sure value is above a certain threshold
viz.MainView.move(0,0,-y*MOVE_SPEED,viz.BODY_ORI)
#Move the viewpoint left/right based on x-axis value
if abs(x) > 0.001: #Make sure value is above a certain threshold
viz.MainView.move(x*0.1,0,0,viz.BODY_ORI)
#Turn the viewpoint left/right based on twist value
#if abs(twist) > 0.001: #Make sure value is above a certain threshold
#    viz.MainView.rotate(0,1,0,twist,viz.BODY_ORI,viz.RELATIVE_WORLD)

#UpdateJoystick every frame
vizact.ontimer(0,UpdateJoystick)

#define random speed scalar constant for changing gain on accelerator
def MOVE_SPEEDSC():
global MOVE_SPEED, speedm, speedcon
MOVE_SPEED = random.choice([0.15,0.2,0.25,0.3])
vizact.ontimer(5, MOVE_SPEEDSC)

#add textbox and set position on screen
text_speed.setPosition(.15, .9 )
text_speed.fontSize (30)

old_posZ = 0
def showSpeed():

global old_posZ, speed, speedm, speedcon

current_pos = viz.MainView.getPosition()
current_posZ = current_pos[2]
distance = abs(current_posZ - old_posZ)

#get speed in meters/sec
speed = (distance/viz.elapsed())

#get speed in mph

#speedm = 4*(speed*3600)/1609.3

#chnage multiplier to reflect gain change
if MOVE_SPEED == 0.15:
speedm = 4*(speed*3600)/1609.3
speedm = 6.666666667*(speed*3600)/1609.3
if MOVE_SPEED == 0.2:
speedm = 5*(speed*3600)/1609.3
if MOVE_SPEED == 0.25:
speedm = 4*(speed*3600)/1609.3
if MOVE_SPEED == 0.3:
speedm = 3.33333333*(speed*3600)/1609.3

old_posZ = current_posZ

#enter speed in textbox rounded to 1 digit after decimal
text_speed.message(str(round(speedm,1))+ 'MPH')

vizact.ontimer(0.25, showSpeed)

#Open file in path

filename = 'gainchangeprac_.txt' + str(subject)
def getPublishedPath(filename):
if publishPath:
return '/'.join([publishPath,filename])
return filename

file = open(viz.res.getPublishedPath(filename), 'w')

def mytimer (num):

#joy_pos = str(joy.getPosition())
out = (str(round(speedm,1)) + '\n')
file.write(out)
file.flush()
print out
viz.callback( viz.TIMER_EVENT, mytimer )
viz.starttimer( 0, 0.25, viz.FOREVER ) #capturing 4 times a sec```
However whilst the random change in accelerator gain works fine, the associated change in the speed coding is not changing/updating as it should. Do you know where I'm going wrong?

Cheers!
#2
05-27-2010, 05:44 PM
 Jeff WorldViz Team Member Join Date: Aug 2008 Posts: 2,467
I don't quite understand why you are multiplying the calculated speed by another factor before dispaying it. Isn't the speed value correct if you just divide the distance covered by the elapsed time and convert that to MPH?

In the future if you post code, please try to create an example that is as concise as possible that anyone could copy and run.

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements Vizard Precision Position Tracker (PPT) Plug-in development

 Similar Threads Thread Thread Starter Forum Replies Last Post Saz Vizard 1 05-20-2010 04:03 AM cl113 Vizard 1 05-10-2009 05:14 PM krimble Vizard 2 03-20-2007 03:23 AM VR_enthusiast Vizard 2 07-19-2006 11:59 AM Vygreif Vizard 1 04-27-2006 09:21 PM

All times are GMT -7. The time now is 09:36 PM.