Guest User

Mouselook

a guest
Feb 3rd, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. #Blender Game Engine 2.55 Simple Camera Look
  2. #Created by Mike Pan: mikepan.com
  3.  
  4. # Use mouse to look around
  5. # W,A,S,D key to walk around
  6. # E and C key to ascend and decend
  7.  
  8. from bge import logic as G
  9. from bge import render as R
  10. from bge import events
  11. from mathutils import *
  12.  
  13. scene = G.getCurrentScene()
  14. yaw = scene.objects["Empty"]
  15.  
  16. speed = 0.08                # walk speed
  17. sensitivity = 0.002     # mouse sensitivity
  18. smooth = 0.7            # mouse smoothing (0.0 - 0.99)
  19.  
  20. cont = G.getCurrentController()
  21. owner = cont.owner
  22. Mouse = cont.sensors["Mouse"]
  23.  
  24. w = R.getWindowWidth()//2
  25. h = R.getWindowHeight()//2
  26. screen_center = (w, h)
  27.  
  28. # center mouse on first frame, create temp variables
  29. if "oldX" not in owner:
  30.     #R.setMousePosition(w , h )
  31.     R.setMousePosition(w + 1, h + 1)
  32.     owner["oldX"] = 0.0
  33.     owner["oldY"] = 0.0
  34. else:
  35.     scrc = Vector(screen_center)
  36.     mpos = Vector(Mouse.position)
  37.     x = scrc.x-mpos.x
  38.     y = scrc.y-mpos.y
  39.     # Smooth movement
  40.     owner['oldX'] = (owner['oldX']*smooth + x*(1.0-smooth))
  41.     owner['oldY'] = (owner['oldY']*smooth + y*(1.0-smooth))
  42.     x = owner['oldX']* sensitivity
  43.     y = owner['oldY']* sensitivity
  44.     rotation_y = yaw.localOrientation
  45.     rotation_y = rotation_y[0]
  46.     rotation_y = rotation_y[1]
  47.  
  48.     if rotation_y < -0.89:
  49.         if y > 0:
  50.             y = 0
  51.         else:
  52.             y = y
  53.     elif rotation_y > 0.86:
  54.         if y < 0:
  55.             y = 0
  56.         else:
  57.             y = y
  58.  
  59.     # set the values
  60.     owner.applyRotation([0, 0, x], False)
  61.    
  62.     yaw.applyRotation([y, 0, 0], True)
  63.    
  64.     # Center mouse in game window
  65.     R.setMousePosition(*screen_center)
  66.    
  67. keyboard = G.keyboard.events
  68.  
  69. # KEY BINDINGS
  70.  
  71. kbleft = events.AKEY # Replace these with others, if you wish
  72. kbright = events.DKEY # An example would be 'events.WKEY, DKEY, SKEY, and AKEY
  73. kbup = events.WKEY
  74. kbdown = events.SKEY #
  75.  
  76. ##################
  77.  
  78. if not 'moveinit' in owner:
  79.     owner['moveinit'] = 1
  80.     owner['mx'] = 0.0
  81.     owner['my'] = 0.0
  82.    
  83.     #
  84.     #
  85.     #
  86.     # ~~SETTINGS~~ #
  87.     #
  88.     #
  89.     #
  90.     # Tweak these settings to fit your needs
  91.    
  92.     owner['accel'] = 2.0      # Acceleration
  93.     owner['maxspd'] = 1.0    # Top speed
  94.     owner['friction'] = 0.75   # Friction percentage; set to 0.0 for immediate stop
  95.     owner['movelocal'] = 1    # Move on local axis?
  96.    
  97. if keyboard[kbleft]:
  98.     owner['mx'] += owner['accel']
  99. elif keyboard[kbright]:
  100.     owner['mx'] -= owner['accel']
  101. else:
  102.     owner['mx'] *= owner['friction']
  103.    
  104. if keyboard[kbup]:
  105.     owner['my'] += owner['accel']
  106. elif keyboard[kbdown]:
  107.     owner['my'] -= owner['accel']
  108. else:
  109.     owner['my'] *= owner['friction']
  110.    
  111. # Clamping
  112.    
  113. if owner['mx'] > owner['maxspd']:
  114.     owner['mx'] = owner['maxspd']
  115. elif owner['mx'] < -owner['maxspd']:
  116.     owner['mx'] = -owner['maxspd']
  117.    
  118. if owner['my'] > owner['maxspd']:
  119.     owner['my'] = owner['maxspd']
  120. elif owner['my'] < -owner['maxspd']:
  121.     owner['my'] = -owner['maxspd']
  122.    
  123. # Actual movement
  124.    
  125. owner.setLinearVelocity([owner['my'], owner['mx'], owner.getLinearVelocity()[2]], owner['movelocal'])
Advertisement
Add Comment
Please, Sign In to add comment