View Single Post
  #2  
Old 02-09-2010, 08:40 AM
ssaha ssaha is offline
Member
 
Join Date: Feb 2010
Posts: 4
Please disregard my previous post

I have been trying to use UserAvatar.py for navigation from link:
http://vizworkshop.com/articles/walkthrough_app

I did two following changes in UserAvatar file:

Change 1: In _init_ function, I used collideBox in place of collideCapsule.

Change 2: In updatePrePhysics, I changed headEuler following way:

headEuler = self.head.getEuler(viz.ABS_GLOBAL)
headEuler[0] = headEuler[0] + yawChange
headEuler[1] = headEuler[1]
headEuler[2] = headEuler[2]
self.head.setEuler(headEuler, viz.ABS_GLOBAL)


Problem: In code, there is no change of y-coordinate of self.feet. According to code, it should be
always zero (0). But when I run the code with above change, I get change in y-coordinate of self.feet. So in order to keep it zero, I add following code in updatePostPhysics:

temp_pos= self.feet.getPosition(viz.ABS_GLOBAL)
temp_pos[1]=0
self.feet.setPosition(temp_pos)


After doing this , y is zero . But movement is not smooth.


Could you give any solution? For your convenience, updated UserAvatar.py is attached below.

Code:
'''
Moves the view according to user input
'''	

import viz
import vizact
import vizmat
import viztask

class UserAvatar(viz.EventClass):
	THROTTLE_SPEED = 4
	HYPER_THROTTLE_SPEED = 30
	TURN_SPEED = .1
	def __init__(self):
		viz.EventClass.__init__(self)
		self.feet = viz.addGroup(pos=[0, 0, 0]) #position of user avatar
		
		#physics init
		self.physicShape = self.feet.collideBox([0.5,1,0.5],friction=.00000001, hardness=.0000001, bounce=0)
		self.velMotor = self.feet.addVelocity(velocity=[0,0,0], maxForce=100, gravity=False))		
		#orientation of user avatar
		self.head = viz.addGroup()
		#view should link to this
		self.avatar = viz.mergeLinkable(self.feet, self.head)
		
		self.mouseDx = 0
		self.mouseDy = 0
		self.callback(viz.MOUSE_MOVE_EVENT, self.onMouseMove)
		
		vizact.onupdate(viz.PRIORITY_PHYSICS-1, self.updatePrePhysics)
		vizact.onupdate(viz.PRIORITY_PHYSICS+1, self.updatePostPhysics)
		
	def updatePrePhysics(self):
		throttle = vizmat.Vector([0,0,0])
		## Steer Control
		yawChange = self.mouseDx * self.TURN_SPEED
		pitchChange = -self.mouseDy * self.TURN_SPEED			
		#discard old mouse movement and start collecting new mouse input
		self.mouseDx = 0
		self.mouseDy = 0
		
		headEuler = self.head.getEuler(viz.ABS_GLOBAL)
		headEuler[0] = headEuler[0] + yawChange
		headEuler[1] = headEuler[1]
		headEuler[2] = headEuler[2]
		self.head.setEuler(headEuler, viz.ABS_GLOBAL)
			
		## Translate Control
		throttle.set([0,0,0])
		#forward/back
		if viz.key.isDown('w') or viz.mouse.getState() & viz.MOUSEBUTTON_LEFT:
			throttle += [0,0,1]
		if viz.key.isDown('s') or viz.mouse.getState() & viz.MOUSEBUTTON_RIGHT:
			throttle += [0,0,-1]
		#left/right
		if viz.key.isDown('a'):
			throttle += [-1,0,0]
		if viz.key.isDown('d'):
			throttle += [1,0,0]
		throttle.normalize() #<--direction
		if viz.key.isDown(' ') or viz.mouse.getState() & viz.MOUSEBUTTON_MIDDLE:
			throttle *= self.HYPER_THROTTLE_SPEED
		else:
			throttle *= self.THROTTLE_SPEED
			
		if throttle.length(): #if throttle not zero
			#move forward in direction of head
			newVel = vizmat.Transform()
			newVel.setTrans(throttle)			
			newVel.postEuler(self.head.getEuler(viz.ABS_GLOBAL))
			self.velMotor.setVelocity(newVel.getTrans())
		else:
			self.velMotor.setVelocity([0, 0, 0])


	def updatePostPhysics(self):
		self.feet.setEuler([0, 0, 0])
		
	def onMouseMove(self, e):
		#fuction called twice, once for movement on x axis, once for y axis
		self.mouseDx = self.mouseDx + e.dx
		self.mouseDy = self.mouseDy + e.dy
	
	def linkView(self, view):
		return viz.link(self.avatar, view, srcFlag=viz.LINK_ABSOLUTE, dstFlag=viz.LINK_POS_RAW)

	def setPosition(self, xyz):
		self.feet.setPosition(xyz)
		
	def setEuler(self, euler):
		self.head.setEuler(euler)

if __name__ == '__main__':
	viz.go()
	viz.move([0, 0, -2])
	viz.phys.enable()
	t = viz.add('tankmaze.wrl')
	t.collideMesh(bounce=0, hardness=1, friction=0)
	user = UserAvatar()
	user.feet.setPosition([0, 0, 0])
	user.linkView(viz.MainView)
Reply With Quote