Does anybody have a mouse look script that works in 2.53, I can’t find one. Thanks.
Never mind, I made my own. If anyone want it, here it is
import GameLogic as gl
import Rasterizer as r
cont = gl.getCurrentController()
obj = cont.owner
parent = obj.parent
mouse = cont.sensors['Mouse']
winh = r.getWindowHeight()
winw = r.getWindowWidth()
hwinh = winh//2
hwinw = winw//2
print(winh,winw,hwinh,hwinw)
sens = 0.001
x, y = mouse.position
x = (hwinw - x)*sens
y = (hwinh - y)*sens
obj.applyRotation([y,0.0,0.0], True)
parent.applyRotation([0.0,0.0,x], False)
r.setMousePosition(hwinw,hwinh)
just parent the camera above a cube, then on the camera have a mouse movement sensor connected to the python script actuator.
I’ve tested what you did, and I must say, it works brilliant! Congrats! It’s a short and very effective code!
Do you know of good references to help programming with Blender and Python new series?
Thanks, I’m glad you liked it.
A site I always go to when I’m coding is tutorialsforblender3d.com. It’s an amazing site, especially the python class index.
good and simple python script
I think a reset function may be good, because when the mouse is not the center at start the view is jumping
how can I limit it to only move turn left/right?
I made a script too, it’s commented and very simple also. It has a boolean prop to turn on/of the X or Y movement so you can rotate only in one axis. There is already a thread about that here and an example .blend here.
import bge
#bge.render.showMouse(True)
cont = bge.logic.getCurrentController()
obj = cont.owner
mouse = cont.sensors['Mouse']
def DeltaMouse(Xcenter,Ycenter,lockToCenter=False,Xpos=mouse.position[0],Ypos=mouse.position[1]):#this function calculates the ditance (in pixels) of the mouse from two given points
Dx = Xpos - Xcenter
Dy = Ypos - Ycenter
delta = [Dx,Dy]
if lockToCenter and Dx or Dy:
bge.render.setMousePosition(Xcenter,Ycenter)
return delta
delta = DeltaMouse(int(bge.render.getWindowWidth()/2),int(bge.render.getWindowHeight()/2),True)
if obj['firstTime']:
delta = [0,0]
obj['firstTime'] = False
Hrot = delta[0] * obj['sens'] / -1000
Vrot = delta[1] * obj['sens'] / -1000
if obj['x']: obj.applyRotation([0,0,Hrot])
if obj['y']: obj.applyRotation([Vrot,0,0],True)
It’s not commented here but it is in the .blend…
Nice!!! thanks…
No LogicBricks solution
small MouseLook class, tested with Moguris Blender patch!
import bge
class Ego(bge.types.KX_GameObject):
def __init__(self):
bge.types.KX_GameObject.__init__(self)
def main(self):
if (bge.events.MOUSEX, bge.logic.KX_INPUT_ACTIVE) in bge.logic.mouse.events:
x, y = bge.logic.mouse.position
# Cube (Player Body)
self.applyRotation([0.0,0.0,-x], True)
self.childrenRecursive[0].applyRotation([-y, 0.0,0.0], True) # Camera
bge.render.setMousePosition(0, 0)
runs without any logic brick! Just these eighteen lines of code.
For more informations and other oop GameEngine stuff look there:
http://blenderartists.org/forum/showthread.php?t=190856
greetings, moerdn