WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   creating a deceleration zone (https://forum.worldviz.com/showthread.php?t=2220)

ptjt255 08-20-2009 08:04 AM

creating a deceleration zone
 
Hi,

I'm hoping someone can help me, I have created a left hand quad that if entered should force the joystick to decelerate to 0 without going off the screen, rather than the avatar colliding with it which is currently how it is set up. Any help would be really welcome - thanks.

Code:

import viz
import vizjoy
import viztask
import random
import math
from numpy import *
viz.go()

# Enable physics for collisions
viz.phys.enable()

# Set window to full size
viz.window.setSize(-1)


# Add a joystick and disable the mouse
joystick = vizjoy.add()
viz.mouse(viz.OFF)
viz.mouse.setTrap(viz.ON)
viz.mouse.setVisible(viz.OFF)

# Create rotating ball, scale it, position it and make it a collidable object
ball = viz.add('soccerball.ive', scale = [1,1,1], pos = (-1.5,0.5,0))
ball.collideBox()
ball.disable(viz.DYNAMICS)
ball.addAction(vizact.spin(0,1,0,-100))
ball.visible(viz.OFF)

# Create an avatar based on the gender of participant
choice = ['male', 'female']
select = viz.choose('Gender of participant?', choice)

if select == 0:
        avatar = viz.add('vcc_male.cfg')
if select == 1:
        avatar = viz.add('vcc_female.cfg')
avatar.setScale(0.2,0.2,0.2)
avatar.visible(viz.OFF)
avatar.collideBox()
avatar.disable(viz.DYNAMICS)
avatar.enable(viz.COLLIDE_NOTIFY)

# Create a target ball and link it to right hand of avatar
rHandBall = viz.add('soccerball.ive', scale = [0.01,0.01,0.01])
rHandBall.visible(viz.OFF)
rHandBall.collideBox()
rHandBall.disable(viz.DYNAMICS)
rHandBall.enable(viz.COLLIDE_NOTIFY)
rHandLink = viz.link(avatar.getBone('Bip01 R Hand'), rHandBall)
rHandLink.preTrans([0.05,-0.05,0])

# Create 'walls' around the screen and make them collidable
Leftquad = viz.addTexQuad()
pic = viz.addTexture('sky_negy.jpg')
Leftquad.texture(pic)
Leftquad.setScale(1.0,6,1)
Leftquad.setPosition(-2.5,0,0)
Leftquad.collideBox()
Leftquad.disable(viz.DYNAMICS)
Leftquad.enable(viz.COLLIDE_NOTIFY)

Rightquad = viz.addTexQuad()
Rightquad.setScale(1.0,6,1)
Rightquad.setPosition(3.0,0,0)
Rightquad.collideBox()
Rightquad.disable(viz.DYNAMICS)
Rightquad.enable(viz.COLLIDE_NOTIFY)

Bottomquad = viz.addTexQuad()
Bottomquad.setScale(6,1,1)
Bottomquad.setPosition(0,-1.5,0)
Bottomquad.collideBox()
Bottomquad.disable(viz.DYNAMICS)
Bottomquad.enable(viz.COLLIDE_NOTIFY)

Topquad = viz.addTexQuad()
Topquad.setScale(6,1,1)
Topquad.setPosition(0,3.5,0)
Topquad.collideBox()
Topquad.disable(viz.DYNAMICS)
Topquad.enable(viz.COLLIDE_NOTIFY)

# Set up 6 vertical positions in an array and put the ball in one position at a time randomly
objects = []
objects.append(ball)
pos = [[-1.5,0.0,0],[-1.5,0.5,0],[-1.5,1.0,0],[-1.5,1.5,0],[-1.5,2.0,0],[-1.5,2.5,0]]

def sceneOn():
        global rand_pos, p,x_target,y_target
        # Get random sample of positions
        rand_pos = random.sample(pos,6)
        # Apply objects to positions
        for p,o in zip(rand_pos,objects):
                o.setPosition(p)
                y_target = p[1]
                print 'Y Target Position: ', y_target
                x_target = p[0]
                print x_target

# Link the avatar to the joystick, define joystick velocity and parameters, track position and record time
# Defines collision parameters and avatar actions on collision
import time

# Variables
result = 0
tinc = 1/100
# Sets velocity of joystick - from avatar to target the distance is 3.5m
joyscale_x = 1.40 # scale max x joystick velocity to 1.125 m/s maximum (roughly the time it takes to cross a single lane of a road) - 4 seconds to cross 4.5 meters which is 1.5 seconds per meter
joyscale_y = -1.40 # scale max y joystick velocity to 1.125 m/s maximum (roughly the time it takes to cross a single lane of a road)
start_position = [2.0, 1.0,0]

def position_loop():
        global result, x_prev, y_prev, x_velocity, y_velocity, ctime, ij, cframe, y_pos, x_pos, x, y, joy_position, i, select, p, ctime, pos_inc, start_position,elapsed_time,tinc
        x_velocity = 0.0
        y_velocity = 0.0
        x_prev = start_position[0]
        y_prev = start_position[1]
        Start_tick = time.time()
        cframe = 0

        def mytimer(tinc):
                global result, x_prev, y_prev, ctime, ij, cframe, y_pos, x_pos, x, y, joy_position, i, select, p, ctime, x_velocity, y_velocity, elapsed_time
                # Set up joystick as velocity control
                ctime = time.time() - Start_tick
                cframe = cframe+1
                x = x_prev + x_velocity/60; y = y_prev + y_velocity/60
                avatar.setPosition(x,y)
                joy_position = joystick.getPosition()
                x_prev = x; y_prev = y;
                x_velocity = joyscale_x*joy_position[0]; y_velocity = joyscale_y*joy_position[1]
                elapsed_time = time.time() - Start_tick

                # Set up screen actions based on result
                def onCollideBegin(e):
                        global result
                        if e.obj1 is rHandBall:
                                if e.obj2 is ball:
                                        print 'hit the target'
                                        viz.killtimer(1)
                                        result = 1
                                        ball.color(viz.GREEN)
                                        avatar.setScale(0.8,0.8,0.8)
                                        avatar.lookat(10, 0, -40)
                                        avatar.setPosition(0.0,0.0,0.0)
                                        avatar.state(4)
                                        return
                                        avatar.setPosition(2.0, 1.0,0)

                        if e.obj1 is avatar:
                                if e.obj2 is Leftquad:
                                        print 'hit the left wall'
                                        viz.killtimer(1)
                                        result = 2
                                        ball.color(viz.RED)
                                        avatar.setScale(0.8,0.8,0.8)
                                        avatar.setPosition(-2.0,1.0,0)
                                        avatar.lookat(10, 0, -40)
                                        avatar.state(8)

                        if e.obj1 is avatar:
                                if e.obj2 is Rightquad:
                                        print 'hit the right wall'
                                        viz.killtimer(1)
                                        result = 3
                                        ball.color(viz.RED)
                                        avatar.setScale(0.8,0.8,0.8)
                                        avatar.setPosition(1.5,0.5,0)
                                        avatar.lookat(-10, 0, -40)
                                        avatar.state(8)

                        if e.obj1 is avatar:
                                if e.obj2 is Bottomquad:
                                        print 'hit the bottom wall'
                                        viz.killtimer(1)
                                        result = 4
                                        ball.color(viz.RED)
                                        avatar.setScale(0.8,0.8,0.8)
                                        avatar.setPosition(0.0,-0.5,0)
                                        avatar.lookat(10, 0, -40)
                                        avatar.state(8)

                        if e.obj1 is avatar:
                                if e.obj2 is Topquad:
                                        print 'hit the top  wall'
                                        viz.killtimer(1)
                                        result = 5
                                        ball.color(viz.RED)
                                        avatar.setScale(0.8,0.8,0.8)
                                        avatar.setPosition(0,2.0,0)
                                        avatar.lookat(10, 0, -40)
                                        avatar.state(8)

                        if elapsed_time > 10.0:
                                print 'missed'
                                viz.killtimer(1)
                                result = 6
                                ball.color(viz.RED)
                                avatar.setScale(0.8,0.8,0.8)
                                avatar.lookat(10, 0, -40)
                                avatar.state(1)

                viz.callback(viz.COLLIDE_BEGIN_EVENT,onCollideBegin)


                #print 'trial:', i, 'time:', ctime, 'x Velocity:',x_velocity, 'y_velocity', y_velocity, 'x', x, 'y', y, 'Result:', result

        viz.callback(viz.TIMER_EVENT, mytimer)
        viz.starttimer(1,tinc,viz.FOREVER)

# Runs 10 trials
def run_trials():
        global pos_inc, start_position, x_pos, y,elapsed_time, i, position, ball, result
        total_reps = 12
        for i in range(1,total_reps-1):
                global pos_inc, start_position, x_pos, y,elapsed_time, position, ball, result
                avatar.visible(viz.OFF)
                rHandBall.visible(viz.OFF)
                Ready = viz.ask('Are You Ready?')
                avatar.setPosition(2.0, 1.0, 0)
                avatar.setScale(0.2,0.2,0.2)
                avatar.visible(viz.ON)
                avatar.state(1)
                avatar.lookat(-10, 0, -10)
                yield viztask.waitTime(3.0)
                sceneOn()
                ball.color(viz.GRAY)
                rHandBall.visible(viz.ON)
                ball.visible(viz.ON)
                avatar.state(2)
                position_loop()
                yield viztask.waitTime(5.0)
                avatar.visible(viz.OFF); ball.visible(viz.OFF); rHandBall.visible(viz.OFF)


# Run the whole thing
viztask.schedule(run_trials())



All times are GMT -7. The time now is 03:42 PM.

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