I’ve been messing around in the blender game engine lately, tonight I decided to muck around with mouse input.
First off, here’s the code:
from bge import events from bge import render from mathutils import Vector as v def cursor(cont): obj = cont.owner sce = logic.getCurrentScene() cam = sce.active_camera movement = obj.sensors['Mouse'] hover = obj.sensors['hover'] data = logic.globalDict mouse = logic.mouse.events render.showMouse(True) #Sets up direction vector for movement and distance from the point if mouse[events.LEFTMOUSE] > 0 and hover.hitObject!=None: data['direction'] = obj.getVectTo(hover.hitPosition) data['destination'] = hover.hitPosition #Only runs if there's a direction to go toward if data['direction'] != [0,0,0]: obj.worldLinearVelocity = (data['direction']*10) #Checks if the object has reached the destination, resets applicable variables if obj.getDistanceTo(data['destination']) < 2: data['direction'] = [0,0,0] obj.applyMovement((0,0,0)) def init(): #Sets up the variables that are going to be used logic.globalDict['direction'] = [0,0,0] logic.globalDict['destination'] = [0,0,0]
Not Terribly complex, but I have some issues with it. For one, the object can still follow the cursor indefinitely if you hold down the left mouse button. Secondly, getting the mouse’s position on the screen is only possible when it’s ‘touching’ an object. Although there are multiple ways to get the mouse input, I could only find a way to get the x and y position on the screen, not in the physical game (unless I used the mouse actuator set to over any).
I tried messing with the projection matrices to map the x and y coordinates given to the 3d view, but I don’t know enough to put it to use!
I made an attempt at getting an object to follow the mouse and I need to find a better way to get mouse input. Thoughts/suggestions?
mousetest.blend (570 KB)