WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 7 votes, 5.00 average. Display Modes
  #1  
Old 10-07-2008, 03:10 PM
TrashcanPatrol TrashcanPatrol is offline
Member
 
Join Date: Aug 2008
Posts: 43
Using hotspots to enable opening doors

Hello, I have a question about hotspots. How is it possible to use a hotspot to enable someone to open a door?
What I'm looking for is for the player to walk up to a door, being right next to the door will enable him/her to actually use the command to open the door.

My code is as follows:
Code:
def onHotspot(id,x,y,z):
	if id == door:
             def mykeyboard(key):
  # If the user presses the e key do something
  if key == 'e':
    # Open the door
    door.runAction( vizact.spinto(0,1,0, -90, 30.0) )
		

viz.callback(viz.HOTSPOT_EVENT,onHotspot)
Now, I know that won't work as it is. How can I change it so that the player can only open the door when they're in the hotspot?
Thanks in advance
Reply With Quote
  #2  
Old 10-07-2008, 03:28 PM
TrashcanPatrol TrashcanPatrol is offline
Member
 
Join Date: Aug 2008
Posts: 43
Also, just curious, is there a way to view the hotspots so as to make it easier to move them around?
Reply With Quote
  #3  
Old 10-08-2008, 04:22 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
you could set a variable that is true when one is inside or false when outside of the hotspot. Then if the door key is pressed and the value is true you open the door.

here is a link to some code that keeps track of whether you have entered or left a hotspot. it also draws a circle where one of the hotspots is. Look at the code at the end of the post

http://www.worldviz.com/forum/showth...hlight=hotspot

then you could add something like

Code:
def onKeyDown(key):
	if key == 'e' and inHotspot == True:
		print "open the door"

viz.callback(viz.KEYDOWN_EVENT,onKeyDown)
Reply With Quote
  #4  
Old 10-14-2008, 02:34 PM
TrashcanPatrol TrashcanPatrol is offline
Member
 
Join Date: Aug 2008
Posts: 43
Hi thank you for your response, I've tried what you said but it doesn't work still. Here is what I tried:

Code:
import viz
viz.go()

SAFE = 1
UNSAFE = 2
TARGET = 3



CIRCLE_X = -3.5
CIRCLE_Z = 3.4
CIRCLE_RADIUS = 0.5

def handlemyhotspots(id,x,y,z):

	if id == SAFE:
		print 'inside safe zone'
		def mykeyboard(key):
			if key == 'e':
			door.runAction( vizact.spinto(0,1,0, -90, 30.0) )
		viz.clearcolor(1,1,1)
		viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)

	elif id == UNSAFE:
		print 'outside safe zone'
		viz.clearcolor(0,0,0)
		viz.starthotspot(SAFE,viz.RECTANGLE_HOTSPOT_IN,0,3,2.4,6)

	elif id == TARGET:
		print 'at target'

viz.callback(viz.HOTSPOT_EVENT, handlemyhotspots)
viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)
viz.starthotspot(TARGET,viz.CIRCLE_HOTSPOT_IN,CIRCLE_X,CIRCLE_Z,CIRCLE_RADIUS) 

import math
viz.startlayer(viz.LINES)
viz.linewidth(3)
viz.vertexcolor(viz.RED)
viz.vertex(CIRCLE_X,0,CIRCLE_Z)
viz.vertex(CIRCLE_X,2,CIRCLE_Z)
viz.startlayer(viz.LINE_LOOP)
for a in range(0,360,10):
	x = math.sin(viz.radians(a))*CIRCLE_RADIUS
	z = math.cos(viz.radians(a))*CIRCLE_RADIUS
	viz.vertex(x+CIRCLE_X,0.1,z+CIRCLE_Z)
viz.endlayer()


room=viz.add('room.wrl')
viz.collision(viz.ON)
door = room.getchild('door-FACES')
import vizcam
vizcam.FlyNavigate()

def mykeyboard(key):
  if key == 'e' and id == SAFE:
    door.runAction( vizact.spinto(0,1,0, -90, 30.0) )
--but it doesn't let me open the door like that though. The boldened part I know won't work [and it's not in my code that I was just using] but that's what I'm trying to do with it, it's hard to describe using two definitions with words though.
What do you think I should do for that?
Reply With Quote
  #5  
Old 10-14-2008, 03:27 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
There are two problems with your code. First your mykeyboard function should not be inside of you handlemyhotspots function. You also need a viz.callback line. Try the following code.
Once you enter the safe zone the variable openDoor will be set to true. When you leave the safe zone openDoor is set to false. When you press a key the onKeyDown function will be called. If openDoor is true and the keypressed is 'e' you will be able to open the door.


Code:
import viz
viz.go()

viz.MainView.setPosition(0,1.8,-5)

SAFE = 1
UNSAFE = 2
TARGET = 3

viz.add('tut_ground.wrl')

CIRCLE_X = 0
CIRCLE_Z = 3
CIRCLE_RADIUS = 0.5

openDoor = False

def handlemyhotspots(id,x,y,z):
	global openDoor

	if id == SAFE:
		print 'inside safe zone'
		openDoor = True
		viz.clearcolor(1,1,1)
		viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)

	elif id == UNSAFE:
		print 'outside safe zone'
		openDoor = False
		viz.clearcolor(0,0,0)
		viz.starthotspot(SAFE,viz.RECTANGLE_HOTSPOT_IN,0,3,2.4,6)

	elif id == TARGET:
		print 'at target'

viz.callback(viz.HOTSPOT_EVENT, handlemyhotspots)
viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)
viz.starthotspot(TARGET,viz.CIRCLE_HOTSPOT_IN,CIRCLE_X,CIRCLE_Z,CIRCLE_RADIUS) 

import math
viz.startlayer(viz.LINES)
viz.linewidth(3)
viz.vertexcolor(viz.RED)
viz.vertex(CIRCLE_X,0,CIRCLE_Z)
viz.vertex(CIRCLE_X,2,CIRCLE_Z)
viz.startlayer(viz.LINE_LOOP)
for a in range(0,360,10):
	x = math.sin(viz.radians(a))*CIRCLE_RADIUS
	z = math.cos(viz.radians(a))*CIRCLE_RADIUS
	viz.vertex(x+CIRCLE_X,0.1,z+CIRCLE_Z)
viz.endlayer()


def onKeyDown(key):
	if key == 'e' and openDoor == True:
		print "open the door"

viz.callback(viz.KEYDOWN_EVENT,onKeyDown)
Reply With Quote
  #6  
Old 10-16-2008, 02:19 PM
TrashcanPatrol TrashcanPatrol is offline
Member
 
Join Date: Aug 2008
Posts: 43
It worked like a charm, thanks!

...Do you know of a way that one can bind an avatar to the viewpoint, and use the door in a way where, the user presses 'e' and sees the avatar's hand opening the door? If not, that's okay, this will do perfectly.
Reply With Quote
  #7  
Old 10-20-2008, 04:58 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
You can link the viewpoint to the avatar and manipulate the arm bone so it moves to the doorknob. Here's an example using a link with an offset so the viewpoint is behind the avatar. When the avatar stops moving he raises his arm.

Code:
import viz

viz.go()

viz.add('court.ive')

avatar = viz.add('vcc_male.cfg')

link = viz.link(avatar, viz.MainView)
link.preTrans([0,0,-2])

walk = vizact.walkTo([0,0,5])
armup = vizact.boneSpinTo('Bip01 R UpperArm', euler = [0,0,-75], speed = 45)

avatar.addAction(walk)
avatar.addAction(armup)
Reply With Quote
  #8  
Old 10-21-2008, 02:42 PM
TrashcanPatrol TrashcanPatrol is offline
Member
 
Join Date: Aug 2008
Posts: 43
Hmm... I'm having some difficulty combining this into an environment where there's a door to open. Here is my code so far:

Code:
import viz
viz.go()

SAFE = 1
UNSAFE = 0


RECTANGLE_X = -4
RECTANGLE_Z = 3.4
RECTANGLE_WIDTH = 0.5

openDoor = False
dooropen = False
walk = vizact.walkTo([-3.5,0,3.5])

def handlemyhotspots(id,x,y,z):
	global openDoor
	if id == SAFE:
		openDoor = True
		print 'Door can be opened'
		viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,RECTANGLE_X,RECTANGLE_Z,RECTANGLE_WIDTH,1)
	if id == UNSAFE:
		openDoor = False
		print 'Door can not be opened'
		viz.starthotspot(SAFE,viz.RECTANGLE_HOTSPOT_IN,RECTANGLE_X,RECTANGLE_Z,RECTANGLE_WIDTH,1)

viz.callback(viz.HOTSPOT_EVENT, handlemyhotspots)
viz.starthotspot(SAFE,viz.RECTANGLE_HOTSPOT_IN,RECTANGLE_X,RECTANGLE_Z,RECTANGLE_WIDTH,1)
#viz.starthotspot(TARGET,viz.CIRCLE_HOTSPOT_IN,CIRCLE_X,CIRCLE_Z,CIRCLE_RADIUS) 

room=viz.add('room.wrl')
viz.collision(viz.ON)
door = room.getchild('door-FACES')
import vizcam
vizcam.FlyNavigate()

avatar = viz.add('vcc_male.cfg')

link = viz.link(avatar, viz.MainView)
link.preTrans([0,0,.1])

armup = vizact.boneSpinTo('Bip01 R UpperArm', euler = [0,0,-75], speed = 45)

avatar.addAction(walk)
avatar.addAction(armup)



def mykeyboard(key):
	global dooropen
	if key == 'e' and openDoor == True:
		door.runAction( vizact.spinto(0,1,0, -90, 30.0) )
		dooropen = True
		avatar.action(armup)
		
#Further help: http://www.worldviz.com/vizhelp/commands/viz/starthotspot.htm
		
#	if dooropen == True:
#		print 'The door is open'

#	if key == 'e' and openDoor == True and dooropen == True:
#		door.runAction(vizact.spinto(0,1,0,0,30.0) )

#Need to be able to close door as well, for concept.
#after door is opened, dooropen = True
#if key == 'e' and dooropen == True:
	#door.runAction( vizact.spinto(0,1,0,0,30.0) )
	
viz.callback(viz.KEYBOARD_EVENT,mykeyboard)

avatar.disable(viz.INTERSECTION)
I'm having some trouble controlling the viewpoint. It's linked to the avatar I know, but is there any way to control the avatar to go forward/back with Up, Down and strafe with Left, Right? As well as use the mouse to look around, so the player sees the hand open the door.. is this possible?
Thanks again for sticking around and helping me a bit through this.
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


All times are GMT -7. The time now is 11:12 AM.


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