WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-26-2010, 01:04 PM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
Code for Avatar control 3rd Person Adventure Game Style

Example code for controlling a user avatar. Console adventure game style.

Left mouse button rotates view, mouse wheel zooms.

WASD keys move avatar. Spacebar triggers jump. 'V' key triggers attack.

Code:
import viz
import viztask
import vizmat
viz.go()

#avatarRoot = viz.addGroup()
avatarRoot = viz.add('marker.wrl')
cameraLookAtNode = viz.addGroup(parent=avatarRoot, pos=[0, 1.5, 0])
avatar = viz.add('vcc_male.cfg', parent=avatarRoot)
viz.add('tut_ground.wrl')

def moveAvatar():
	while True:
		#get user input
		newPos = vizmat.Vector()
		newPos.set([0,0,0])
		if viz.iskeydown('w'):
			#walk forward
			newPos = newPos + [0, 0, 1]
		if viz.iskeydown('s'):
			newPos = newPos + [0, 0, -1]
		if viz.iskeydown('a'):
			newPos = newPos + [-1, 0, 0]
		if viz.iskeydown('d'):
			newPos = newPos + [1, 0, 0]		
		isJumping = False
		isAttacking = False
		if viz.iskeydown(' '):
			#jump
			isJumping = True
		elif viz.iskeydown('v'):
			isAttacking = True
		
		if isAttacking:			
			avatar.state(6)
			yield viztask.waitTime( 1.5 )
		if newPos.length() > 0 or isJumping:
			if newPos.length() == 0:
				#jumping but no direction key, use current heading
				t = vizmat.Transform()
				t.setTrans([0, 0, 1.8])
				t.postEuler(avatar.getEuler()[0],0,0)
				newPos = vizmat.Vector( t.getTrans() )
			newYaw = vizmat.AngleToPoint(0, 0, newPos[0], newPos[2])
			avatar.setEuler(newYaw, 0, 0)
			if not isJumping:
				#just walking				
				cameraEuler = viz.MainView.getEuler()
				avatarRoot.setEuler(cameraEuler[0], 0, 0)
				newPos = newPos * viz.getFrameElapsed()
				avatarRoot.setPosition(newPos, viz.REL_LOCAL)
				avatar.state(2)
			else:
				#jumping
				avatar.state(7)
				yield viztask.waitTime( avatar.getDuration(7)+.01 )
				#compute jump direction
				newJumpPos = vizmat.Transform()
				newJumpPos.setTrans([0, 0, 1.8])
				newJumpPos.postEuler(avatar.getEuler(viz.ABS_GLOBAL)[0], 0, 0)
				avatarRoot.setPosition(newJumpPos.getTrans(), viz.REL_GLOBAL)
		
		
		
		if not (viz.iskeydown(' ') or viz.iskeydown('w') or viz.iskeydown('s') or viz.iskeydown('a') or viz.iskeydown('d') ):
			avatar.state(1)
			
		yield None
		
viztask.schedule(moveAvatar())

import vizcam
camera = vizcam.PivotNavigate()
camera.setDistance(5)

def moveCamera():
	while True:
		lookAtPos = avatar.getBone('Bip01 Pelvis').getPosition(viz.ABS_GLOBAL)
		lookAtPos[1] = lookAtPos[1] + .5
		camera.setCenter(lookAtPos)
		camera.updateCenter()
		yield None	

viztask.schedule(moveCamera())
Attached Files
File Type: zip avatar3rdPerson.zip (993 Bytes, 2146 views)
__________________
Paul Elliott
WorldViz LLC
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
3rd person camera 'game' Nevyle Vizard 4 07-11-2010 04:11 AM


All times are GMT -7. The time now is 11:40 PM.


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