Leaving the Mouse Where It Is

Edit: Okay, I simply made a function that Mouse_Control calls every time the right mouse button is pressed. The function stores the mouse’s location and then sets it there after the button is released. I looked over my code and found that I made a strange loophole that was causing it to not work. What I was originally trying to do would not work because the mouse’s location would constantly move (making the camera spin uncontrollably when the button was pressed).

Saving the mouses Pos and then resetting it works much better (well, it works, at least).

Thank you for pointing this out, ynjh_jo.

I have a lot to learn. :wink:

Note: Below is the original post, not what I stated above.

def Mouse_Control(self):
		try:
				
			if self.keys["mouse_control"]:
				
				if not self.m_c:
					
					x = base.win.getPointer(0).getX()
					y = base.win.getPointer(0).getY()
				
				if base.win.movePointer(0, 100, 100):
					
					self.heading = self.heading - (x - 100)*0.2
					self.pitch = self.pitch - (y - 100)*0.2
									
				if (self.pitch < -45): self.pitch = -45
				
				if (self.pitch >  20): self.pitch =  20
				
				self.c_node.setHpr(self.heading,self.pitch,0)
				
				self.c_node.setH(heading +180)
				
		except: pass

In this code (I didn’t write this myself, it’s just a slightly edited version of someone elses code) I want to be able to do this function but leave the mouse where it is. This code causes it to jump to a corner of the screen.

I’m still not very good at the whole camera/mouse thing (most of my questions are based around this) so I’m trying to learn from others code.

I know that

 if base.win.movePointer(0, 100, 100):
					
					self.heading = self.heading - (x - 100)*0.2
					self.pitch = self.pitch - (y - 100)*0.2

But when I try to change it, the mouse no longer controls the camera. Thank you in advance.

That code moves the pointer to (100,100). But at app start, the pointer mostly rests somewhere else, or (plus) the user may moves the mouse freely during loading. Is that what you meant ?
If you don’t want the offset at start, call “base.win.movePointer(0, 100, 100)” after loading all stuff, right before starting the main loop where Mouse_Control is called for the 1st time.

No, that’s not really what I was looking for. I’m sorry if my first post was misleading. I actually want the player to be able to move the mouse around, and since that player has this control, I want the mouse to remain at the location where the player has left the mouse when they right click (the if self.keys[“mouse_control”]: is checking for the right mouse button) but I can’t get the code to run properly when i try to change it.

Basically, right clicking the mouse gives you control over the mouse, and then when you’re done moving the mouse, you would expect the mouse to be where you left it right? I can’t get this code to work without moving it to another location (I tried setting the x, y to base.win.getPointer(0).getX(), base.win.getPointer(0).getY() but that just makes it stop working).

I really need help with the mouse and camera (I can do the basic stuff that the tutorial teaches and w/e easily) to do more “advanced” camera control schemes, and I feel bad coming here every second asking for help. Any place where I could get some help or maybe read up on theories/ideas on the best ways to do different camera schemes? I’ve never dealt with 3D (much) before using Panda so I only have knowledge on 2d concepts, and there is no camera in 2d. :laughing:

Doesn’t matter, same thing applies, move it to its rest position (100,100) upon switch.

Hmm, so I guess it is the rest of my code. Thanks a lot for your help, I guess I’m overlooking something else. I will definitely come back and post what the problem was if it seems necessary.

Btw, is that Spawn in your avatar? Looks like it but not sure. :laughing:

Edit: Okay, I simply made a function that Mouse_Control calls every time the right mouse button is pressed. The function stores the mouse’s location and then sets it there after the button is released. I looked over my code and found that I made a strange loophole that was causing it to not work. What I was originally trying to do would not work because the mouse’s location would constantly move (making the camera spin uncontrollably when the button was pressed).

Saving the mouses Pos and then resetting it works much better (well, it works, at least).

Fixed first post.

I would go this way instead of wasting unnecessary time in Python every frame while mouse control is enabled.
I would redirect the RMB down event to this method, thus solves it just by a single line of code :

def enableMouseControl(self):
    self.keys["mouse_control"]=1
    base.win.movePointer(0, 100, 100)

You’re right, my old way caused problems (camera spinning strangely if the mouse was already moving in a certain direction). Until you helped me out, I hadn’t really thought of doing it a different way. Guess I should be more open minded about my code.

Also, thanks for taking the time to help out a beginner.