View Single Post
  #1  
Old 06-07-2010, 11:03 AM
Enlil Enlil is offline
Member
 
Join Date: May 2008
Posts: 61
UserAvatar questions

Hello, I am using the ideas from
http://vizworkshop.com/articles/walkthrough_app
with the intent of making a moving collide capsule to move the camera with.

The code looks like this:
Code:
import viz

class UserSkeleton(viz.EventClass):
    def __init__(self, eye_height, HeadObjectName, move_speed, turn_speed):
        viz.EventClass.__init__(self)
        self.TurnSpeed = turn_speed
        self.MoveSpeed = move_speed
        print "Move speed: " + str(move_speed)
        self.CollideCapsule = viz.addGroup()
        self.CollideCapsule.setPosition(0,1,0)
        self.Feet = viz.addGroup(self.CollideCapsule)
        self.ChestAndCalibration = viz.addGroup(self.Feet)
        self.ChestAndCalibration.setPosition([0,0,0])
        self.HeadNode = viz.addGroup(self.ChestAndCalibration)
        self.HeadNode.setPosition([0, eye_height-1.2, 0])
        self.EyeNode = viz.addGroup(self.HeadNode)
        self.EyeNode.setPosition([0,.08,.1])
        
        self.ball = viz.add('ball.wrl')
        self.ball.setScale([.1,.1,.1])
        print "HeadNode position: " + str(self.HeadNode.getPosition(viz.ABS_GLOBAL))
        self.HeadObject = viz.add('biohead_talk.vzf')
        self.HeadObject.alpha(.5)
        #self.physicShape = self.CollideCapsule.collideCapsule(euler=[0,90,0], radius=.36, length=1, friction=.00000001, hardness=.0000001, bounce=0)
        self.physicShape = self.CollideCapsule.collideCapsule(euler=[0,90,0], radius=.36, length=1, friction=.1, hardness=.0000001, bounce=0)
        #self.physicShape = self.CollideCapsule.collideBox([.25, 1, .25])
        self.velMotor = self.CollideCapsule.addVelocity(velocity=[0,0,0], maxForce=100, gravity=False)

        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])
        ## rotation Control for feet
        yawChange = self.mouseDx * self.TurnSpeed
        # move distance
        throttleChange = self.mouseDy * self.MoveSpeed
        #print "move speed: " + str(throttleChange) + " Move speed: " + str(self.MoveSpeed) + " Dy: " + str(self.mouseDy)
        #discard old mouse movement and start collecting new mouse input
        self.mouseDx = 0
        self.mouseDy = 0
        
        feetEuler = self.Feet.getEuler(viz.ABS_GLOBAL)
        feetEuler[0] = feetEuler[0] + yawChange
        feetEuler[1] = 0 #no pitch
        feetEuler[2] = 0 #no roll
        self.Feet.setEuler(feetEuler, viz.ABS_GLOBAL)
            
        ## Translate Control
        throttle.set([0,0,throttleChange])
        
        #throttle.normalize() #<--direction
            
        if throttle.length(): #if throttle not zero
            #move forward in direction of head
            newVel = vizmat.Transform()
            newVel.setTrans(throttle)            
            newVel.postEuler(self.Feet.getEuler(viz.ABS_GLOBAL))
            self.velMotor.setVelocity(newVel.getTrans())
        else:
            self.velMotor.setVelocity([0, 0, 0])


    def updatePostPhysics(self):
        self.CollideCapsule.setEuler([0, 0, 0])
        self.HeadObject.setPosition(self.HeadNode.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        self.HeadObject.setEuler(self.HeadNode.getEuler(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        viz.MainView.setPosition(self.EyeNode.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        viz.MainView.setEuler(self.EyeNode.getEuler(viz.ABS_GLOBAL))
        #self.ball.setPosition(self.EyeNode.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        #self.ball.setEuler(self.EyeNode.getEuler(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        #print "Head position: " + str(self.HeadObject.getPosition(viz.ABS_GLOBAL))
        #print "Capsule position: " + str(self.CollideCapsule.getPosition(viz.ABS_GLOBAL))
        #print "Feet position: " + str(self.Feet.getPosition(viz.ABS_GLOBAL))
        #print "HeadNode euler: " + str(self.HeadNode.getEuler(viz.ABS_GLOBAL))
        #print "EyeNode euler: " + str(self.EyeNode.getEuler(viz.ABS_GLOBAL))
        #print "MainView euler: " + str(viz.MainView.getEuler())
        #viz.MainView.lookat(self.HeadNode.getPosition(viz.ABS_GLOBAL))
        
    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 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()
    viz.stepsize(1)
    #viz.phys.setGravity([0,1,0])
    #t = viz.add('tankmaze.wrl')
    t = viz.add( 'playground.wrl' )
    t.setPosition([-10,0,10]) 
    #t = viz.add('C:/AXE/Resource/Rooms/apartment-small3.IVE')
    #t.setScale([.01, .01, .01])
    t.collideMesh(bounce=0, hardness=1, friction=0)
    user = UserSkeleton(1.82, 'biohead_talk.vzf', .4, .1)
    def mykeyboard(key):
        if key == ' ':
            print "Adding thruster: " 
            user.CollideCapsule.addThruster(mode=viz.ABSOLUTE_LOCAL,force=[0,-20,0])
        if key == '1':
            print "Position: " + str(user.HeadNode.getPosition(viz.ABS_GLOBAL))
            print "Adding thruster: " 



    
    viz.callback(viz.KEYBOARD_EVENT,mykeyboard)
Moving the mouse forward moves the view forward, moving from side to side rotates the camera.

It does almost exactly what I want, save for one thing - there are couches in the environment, and the capsule seems to tend to 'climb' up them. Is there any good solution to this using the above method? Or is there some nicer, cleaner method?

Thanks,
Christian
Reply With Quote