Move Camera with Mouse

Hi, i try to create a interface for my game. Therefore i created the camer high in the sky. I made the windows mouse to show during gameplay.
Than i created a simple plane (right now its only exit game) to resemble a button. WOrks fine, if i click on it, it exits the game.

Now i try to move the camera. but not like 100 tutorials show with the help of buttons. I want to move the camera to the left when the cursor is on the left side. i want it to go to the right side when the cursor is on the right and so on.

Here the problem starts. i cant find any logic combinations to work.

I am this far:
i created an empty and made it parrent to the camera. i made a plane on the left side of the window and made the empty parrent of this plane as well. so now my “interface” can be moved with the empty. but i cant find a way to “Mouse over” the left plate and move the empty with it. i can move the plane if im mouse over it but not another object.

Any tips on how to move object B when “mouse over” object A?

Why not try python?

Create a new text document in the text viewer in blender, call it ‘camera_movement’ and add this to it:

#-----------------------------------------------------------------------------------------
# Import Modules
#-----------------------------------------------------------------------------------------
import Rasterizer
Rasterizer.showMouse(1)
#-----------------------------------------------------------------------------------------
# Define Variables
#-----------------------------------------------------------------------------------------
cont = GameLogic.getCurrentController()
own = cont.getOwner()
    
#-----------------------------------------------------------------------------------------
# Define Sensors
#-----------------------------------------------------------------------------------------
for i in cont.getSensors():
    exec(i.getName() + " = i")

#-----------------------------------------------------------------------------------------
# Define Actuators
#-----------------------------------------------------------------------------------------
for i in cont.getActuators():
    exec(i.getName() + " = i")

This is my code set up, I use this when ever I make a script (minus some things that are not necessary for this script).
Now click on the camera, add an always sensor, call ‘always’ then add a mouse movement sensor call it ‘mouse’. Add a script controller and call it whatever you wish but put ‘camera_movement’. Finally add a motion actuator set called ‘motion’

Now back to the text editor, I’m not to sure how much you know about python so I will explain what the code does so far.

import Rasterizer
Rasterizer.showMouse(1)

This imports a module called Rasterizer which handles the viewport basically, it then makes the mouse visible.

cont = GameLogic.getCurrentController()

Assigns the controller (remember the script controller we made on the camera? that controller) to variable cont.

own = cont.getOwner()

Assigns the owner of the controller (thats the camera because the controller is on the camera) to a variable called own.

for i in cont.getSensors():
    exec(i.getName() + " = i")

for i in cont.getActuators():
    exec(i.getName() + " = i")

This defines all our sensors and actuators for us automatically, if we didn’t do this we would have to type out:

mouse = con.getSensor("mouse")

Which isn’t much but for some scripts there can be a lot of sensors and this saves time (note that this takes the name of the sensor in the logic brick editor so naming is important there)

Now lets add some more code to it:

mouse_x = mouse.getXPosition()
window_width = Rasterizer.getWindowWidth()

mouse_x stores the x position of the mouse and window_width stores the width of the window.

Lets make the camera move:

if mouse_x < 10:
    motion.setDLoc(-0.2,0,0,1)
    GameLogic.addActiveActuator(motion, 1)
elif mouse_x > window_width-10:
    motion.setDLoc(0.2,0,0,1)
    GameLogic.addActiveActuator(motion, 1)
else:
    GameLogic.addActiveActuator(motion, 0)

This code sets the d location field of the motion actuator (thats the first row of input sliders) so that the camera will move -.2 on the x axis, thats to the left. But it only does this is the mouse is at the absolute left of the screen.

Its pretty much vise versa for the elif condition and the else condtion is just there to turn the motion actuator off when the mouse is in the middle of the screen.

Tell me if you need some more explination, like I said I don’t know how strong you are with python. Also I’ve attached an example blend.

Attachments

mouse_example.blend (148 KB)

Thx for the answer.

It seems i have to learn another programming language as it seems because the problem is solved in a much better way. Most of your code is understandable (i know already the basics of some programming languages (C++, PHP, Ruby))

I take it and do some changeing so y axis and a zoom will work too.

One more question: do you know some good python tutorial (if possible with blender in mind)? (not something like -> google python tutorial) I know how to find basic tutorials and i know what books are. Im more looking for a realy good tutorial (e.g. the quakenet php tutorial is one of the best i ever found)

Ok, well python is very similar to ruby so it should be easy to pick up.

For a good python blender tutorial read socials Begging BGE python tutorial and for python syntax use this site, its the one I used to refer to.

For reference on functions in the GE, tutorialsforblender3d.com python functions is gold.

Making the camera move up and down shouldn’t be hard.

Add two more variables:

mouse_y = mouse.getYPosition()
window_height = Rasterizer.getWindowHeight()

and add these under the elif condition:

elif mouse_y < 10:
    motion.setDLoc(0.0,0.0,.2,1)
    GameLogic.addActiveActuator(motion, 1)
elif mouse_y < window_height - 10:
    motion.setDLoc(0.0,0.0,-0.2,1)
    GameLogic.addActiveActuator(motion, 1)

Thx much!

yes i figured out how to move in other directions, but i rewrote the else ifs to only ifs :slight_smile:

You should at least have 2 elif’s in there because if the mouse is all the way over to the left then its not going to be at the right, it just saves cpu determining things that don’t need to be determined.

Very helpful info here andrew-101. Thanks again.

Here’s another basic Python link (similar to the Syntax link):

http://www.astro.ufl.edu/~warner/prog/python.html

OMG I have been looking ALL over the net for something like this and no one has ever been able to answer it. I’m on a search to make a 3rd person shooter game. You have just given me an explanation on how to move the camera with the mouse w/o it being FPS setup if you know what I mean there. You broken down the coding for me that I have been trying to figure out, cept the examples I was basing off of was at expert level. Now If I can figure out how to make an object move with the mouse.

Btw just to save me time from trial and error would you happen to know how to also Rotate to the right when the xPos of the Camera is to the Left and vice versa so that the camera is always pointing toward the middle w/o using Parent? Or perhaps move an object around with the mouse constantly?

Thanks very useful.

Thanks very useful.

Thanks that really helped but in my game i want the movement to rotate around the character. Do you know how i could achieve this???:confused:

sorry i m a noob and i want to know how to create the same thing but with rotation so i can make a shooter…

please help me

Does anyone have an example of this using a IPO curve for X and Y. I’m wondering about games that would be like Tempest or other such games where the movement is not always in a straight line. I tried to do it using the IPO playback feature and giving it the same start and end frames, but for some reason it would sense any movements made from right to left, smoothly, but if I went left to right, it would skip a lot. I have a copy of the blend file I was using on my site here, I cut down the script size thinking that might have something to do with it.

http://www.chann3lz.com/gameblender/gamecontroller.blend

Where would I put the code for the mouse’s y axis?

EDIT Never mind, I think I found it.