mouse movement

Hi there,
i am a total noob in using the game engine (and of course english:o)
i am using blender since 2 years
but today i have decided in going deaper in the secrets of blenders game engine!!!

so i am able to do basic physic simulation and control objects via keyboard or ipo curves.

but i also want to control objects with mouse movement :frowning:
and i dont know the exact commands for doing that (help!!!)

thanks

Hi,

Here is some ways to use the mouse.

First off you might want to make the mouse visible, you can do this like so:
i

mport Rasterizer
Rasterizer.showMouse(1)

If you want to use the mouseā€™s movement to do something then make a mouse sensor set to movement.

Now in a python code do this:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

mouse = cont.getSensor("mouse")

mouse_x = mouse.getXPosition()
mouse_y = mouse.getYPosition()

mouse_x and mouse_y are variables that have the x and y values of the mouseā€™s position in them.

If you want to move an object using the mouse you use a mouse sensor set to mouse over and a left button mouse sensor.

The mouse over has a nice little function called .getHitPosition(), this returns the x,y and z values of where the mouse is over the object (in global co-ordinates).

So, if we want to move a cube around in a scene, we add the cube and add 3 sensors, an always, a mouse over and a mouse left click.

Then by using the .getHitPosition() values and setting the cubes position to those we can make a mouse movement script, but this makes a little problem as the cube will always move towards the camera because it will hit the side of the cube which is +1 of the centre of the cube so when its position is set to the hit position it will always be moving towards the camera by +1.

Hope I didnā€™t lose you there, if I did donā€™t fret it, its not too important.

So if we make a script to do this it could look like this:

import Rasterizer
Rasterizer.showMouse(1)

cont = GameLogic.getCurrentController()
own = cont.getOwner()

over = cont.getSensor("over")
lClick = cont.getSensor("lClick")

if over.isPositive() and lClick.isPositive():
    pos = over.getHitPosition()
    own.setPosition([pos[0],0,pos[2]])

This pretty much checks to see if the mouse if over the cube and if the left mouse button has been clicked. If both those conditions are true then then it assings the hit position to a variable called ā€˜posā€™, then we move the cube by using 'own.setPosition([x,y,z]).

As you can (or perhaps cannot, Iā€™m not too sure about your strengths in python) the x position that we set the cube to is pos[0], this is the x value of the hit position thingo. But the y value is 0, this is our work around to the cube flying towards the camera and then finally the z value is the z value of the hit position.

Lastly add a script controller and connect it to that script by putting ā€œTextā€ in the script field (or whatever you may have called it) and connect all 3 sensors to that and hit P and have fun dragging the cube.

Just for you I have made an example of this (attached), if you have any questions on bits you didnā€™t understand just say so and Iā€™ll go through it more detailed.

And also, if this doesnā€™t help tell me why and what your try to achieve with the mosue.

Attachments

drag example.blend (123 KB)

thank you
this works great:D
maybe the point why it didnt work before, is, that i have used mouse.x and mouse.y :smiley:

by the way the example file is great to play with
you are a pro!!!

okā€¦ i failed again

i wanted to attach an object to the mouse position (everytime without clicking on it like a second curser)
my first try was like this


cont = GameLogic.getCurrentController()
own = cont.getOwner()

mouse = cont.getSensor("mouse")

mouse_x = mouse.getXPosition()
mouse_y = mouse.getYPosition()


own.setPosition([ mouse_x,0,mouse_y])

of course i am not capable with the blender functions ( where can i find them???)
and the phyton language (i am usually programming with c :o)

the basic idea is to make a arcade shooter like ā€œthe house of the deadā€ or ā€œtime crysisā€
with ipos that controle camera and entenie movement the mouse simply should controle the crosshair after killoing all the enemys, id like to switch to the next cam ipo and the next enemys ipos

Ah, ok.

Your problem is using the mouse positions, it will be in values of 0 ~ 800 (depending on the screen size) because it returns the value of where the mouse is on the screen and not in global space.

To do what you wanted to do open up my last example add a camera and make it point to the cube in front view (num1> ctrl+alt+num0) then go into the camera view (num0) add a plane, make it face the camera and scale it up so its big enough to go out of the view of the camera. Now parent the the plane to the camera and go to texture draw-type (alt+z) If you can see the plane then rotate it 180 degrees on its z axis so you can no longer see it. Finally click on the cube and change the mouse over sensor to mouse over any, this sensor is like mouse over but like the name suggests it works for every object in the scene. So because we have the plane covering the whole view there is always going to be a value for the .getHitPosition(). I attached another example of this for you.

As for finding blenderā€™s functions you can use the method I use (I donā€™t even know them off by heart). What I do is use a python command called dir(). Lets start from the very top, type ā€œprint dir(GameLogic)ā€ into a script and connect it to an always sensor on any random object. Check blenders console and all the functions for GameLogic are there. For example thereā€™s setGravity, so GameLogic.setGravity([x,y,z]) will change the GEā€™s gravity direction.

Now use this code:

cont = GameLogic.getCurrentController() #This gets the controller that the script is attached to
own = cont.getOwner() # (Same as GameLogic.getCurrentController().getOwner()) This gets the owner of the object,
#in other words the object that the controller belongs to, in my example it is the cube.

print dir(own) #This will print out all the functions that you can use to manipulate own (the cube)

And lastly, for not knowing python too well I give you syntax refernce page that I used when I was learning: http://codesyntax.netfirms.com/lang-python.htm

Once again, if you donā€™t understand somethind just ask me and Iā€™ll help some more.

Good luck on your game, sounds like it will be fun to play

P.S. The crosshair can be done with what alot of the GE users refer to as a custom cursor script, here is an example of one. For hitting enemies you canhave the left click and mouse over sensors on them and when both are positive minus a certain number off their health and when their health = 0, dead.

Attachments

drag example2.blend (126 KB)

hey how would i get this to work x y axis? thanks