Move camera with keyboard and mouse wheel

I can’t get to work camera: i have a task that updates the camera and moves it if the keyboard arrows are pressed, so if up is pressed camera Y += 10, if right is pressed camera X += 10

	if self.Xkeys[ 0 ]:
	     base.camera.setPos( base.camera.getX()+10, base.camera.getY(), base.camera.getZ() )
 
	elif self.Xkeys [ 1 ]:
	     base.camera.setPos( base.camera.getX()-10, base.camera.getY(), base.camera.getZ() )
 

        if self.Ykeys [ 0 ]:
	     base.camera.setPos( base.camera.getX(), base.camera.getY()+10, base.camera.getZ() )
 
        elif self.Ykeys [ 1 ]:
	     base.camera.setPos( base.camera.getX(), base.camera.getY()-10, base.camera.getZ() )

but if the camera rotates some degrees ( for example 100) pressing the left arrow won’t move the camera at the user left.

How to rotate the camera with the mouse wheel?

if self.mouse[ 2 ]:
base.camera.setHpr( ???? )

you can define positions relative to nodes doc in the middle of here:

   if self.Xkeys[ 0 ]:
        base.camera.setPos( base.camera.getX()+10, base.camera.getY(), base.camera.getZ() )

->

if self.Xkeys[ 0 ]:
        base.camera.setX(base.camera,10)

same for rotation:

if self.mouse[ 2 ]:
base.camera.setH(base.camera, 10)

(i hope to have understand your coder right)

fantastic!
Just a thing: if I press UP and DOWN keys now it zooms in and out instead of moving camera up and down. How to fix this?

So that can be a difference to what you (and I) might have learned.
There propably is a way to ‘correct’ that but id like to know that, too.

I used SetY, but instead of moving forward it moves down, because Y is relative and the camera is rotated 30° on Y axis. When pressing UP i need that only Y is changed, not Z

so you could replace

if self.Ykeys[ 0 ]:
        base.camera.setY(base.camera,10)

with:

yhelper=Vec3(0,12,1)
yhelper.normalize()

if self.Ykeys[ 0 ]:
        base.camera.setPos(base.camera,0,
                yhelper.getY()*10,yhelper.getZ()*10)

What should this code do? It doesn’t work.

I need to pan the camera forward pressing the UP key when the camera is watching down, like if you are walking forward while you are watching the floor. How to correct this?

# camera Y control keys
# 0 = up
# 1 = down
# 


if self.Ykeys[ 0 ]:
             base.camera.setY(base.camera,10)

 
        elif self.Ykeys [ 1 ]:
	     base.camera.setY(base.camera,-10)

Is that really the way it looks in your file? Remember that in Python, indentation level is very important. If you don’t indent it properly, it won’t run properly.

David

No, it isn’t. It’s just a copy/past error. The program starts with no indentation errors. So?

Well, I don’t see anything obviously wrong other than indentation; but it’s not a lot of context, and you don’t specify in what way it fails to work. So I don’t have a lot of information to go on, sorry.

But in principle, you seem to have the right idea.

David

hey, i’m working on this project too, and essentially the problem is this… we are making a RTS game, and need a camera that has the following funtions…

up, down, left, right keys make the camera pan only north, south, west, and east in relation to the current map… as in, the altitude doesnt change when you push the keys… only your position above the map…

when the scroll wheel is rolled, that changes altitude up and down…

and when the scroll wheel is pushed down, you can change the angle the camera is facing, and that chosen position locks when the mouse wheel is depressed… so that when you push the keys, you move north, west, south, east but with the same camera heading and angle…

essentially the problem is this, at first nothing worked right… but then we figured out each problem little by little, and we have fixed everything but this ONE thing…

the left and right buttons pan the camera east and west correctly, altitude doesnt change at all… but the up and down buttons… if your camera is pointed at the ground for example, when you push the up key, the camera starts moving forward… TOWARDS the ground… rather than maintaining altitude and moving north like it should…

we want the camera to maintain altitude when the up key and down key is pressed… we dont want it to just move towards whatever its looking at… it should be able to look around and be able to move north, while looking down or sideways, or up or wherever…

does that clarify the problem and the intended goal a little better?

So, you want to change the camera’s y position in global space, not in the camera’s own space.

Instead of:

camera.setY(camera, 10)

use:

camera.setY(camera.getY() + 10)

Or, separate rotation from translation completely: parent the camera to a dummy node that you create, and move the camera by moving the dummy node, and rotate the camera by rotating the camera.

David

thank you