WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 8 votes, 5.00 average. Display Modes
  #1  
Old 05-25-2010, 02:46 AM
Saz Saz is offline
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


#Add a joystick 
joy = vizjoy.add() 

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

sky = viz.add(viz.ENVIRONMENT_MAP,'sky.jpg')

skybox = viz.add('skydome.dlc')
skybox.texture(sky)

#add the road texture
road = viz.add('roadld3.png')
#road2 = viz.add('roadsd.png')


#add the ground
ground = viz.add('tut_ground.wrl')
ground.setPosition(0,0,25)

#create a texture quad to add the road texture to
#and place it over ground
quad = viz.addTexQuad()
quad.setScale(1,2,1)
quad.setPosition(0,.1,0)
quad.setEuler(0,90,0)
quad.texture(road)

road_position = 0
ground_position = 50
#add road and ground if getting near the end of road
def addRoad():
	
	global road_position, ground_position
	
	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:
		
		#add 50 meters of ground
		global ground_position
		groundcopy = ground.copy()
		groundcopy.setPosition([0,0,ground_position])
		ground_position +=50
		
		#add 50 meters of road
		for i in range(1,50):
			quadcopy = quad.copy()
			quadcopy.setPosition([0,.1,road_position])
			quadcopy.setEuler(0,90,0)
			road_position +=1

addRoad()
#call a timer every frame to check if road needs to be added
vizact.ontimer(0, addRoad)

#change the road texture on keypress
#vizact.onkeydown('1', quad.texture,road2)
#vizact.onkeydown('2', quad.texture,road)
vizact.onmousedown(viz.MOUSEBUTTON_LEFT, quad.texture,road)
#vizact.onmousedown(viz.MOUSEBUTTON_LEFT, quad.texture,road2)

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 = viz.addTextbox(parent = viz.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):
	#filename = 'adapt_.txt' + str(subject)
	publishPath = viz.getOption('Y:\backup\Vizard30\resources\road\_.txt','')
	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!
Reply With Quote
  #2  
Old 05-27-2010, 05:44 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
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.
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
random speed and associated coding Saz Vizard 1 05-20-2010 04:03 AM
displaying random scenes etc cl113 Vizard 1 05-10-2009 05:14 PM
custom avatar random walk krimble Vizard 2 03-20-2007 03:23 AM
Random head movements VR_enthusiast Vizard 2 07-19-2006 11:59 AM
Random Seeds Vygreif Vizard 1 04-27-2006 09:21 PM


All times are GMT -7. The time now is 12:49 PM.


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