Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with get_global_mouse_position in multiple Viewports #31551

Closed
Nophlock opened this issue Aug 21, 2019 · 5 comments
Closed

Problem with get_global_mouse_position in multiple Viewports #31551

Nophlock opened this issue Aug 21, 2019 · 5 comments
Labels

Comments

@Nophlock
Copy link

Godot version:
3.1.1 - Stable

OS/device including version:
Windows 10-64 Bit

Issue description:
Hello,

I'm currently trying to implement split-screen in my game and after watching some tutorials I decided to gave this a go. At first everything seems to work fine, but then I stumbled upon the problem, that my topdown-character doesn't want to "look" at the mouse-position anymore. After some more investigation I found out, that the problem seems related to the Stretch-Mode that I have defined(currently "2D" and "keep" for the aspect) because the "lookat" function seemed to kinda work when I maximize the window but not, as soon as I start walking away from the origin with my player.

The code/function I use to let the player look at the mouse is basically just this:

func getLookatDirection() -> float:
	var mpos = self.attached_to.get_global_mouse_position()
	var apos = self.attached_to.global_position 
	var diff = mpos - apos

	return diff.angle()

With that everything worked fine, as long as I've got only one player (actually the other player is controlled with an PS4-Controller and only one player is using the PC-Inputs), but with two or more it's basically impossible to aim at anything. Since I have no clue now how to fix this, I decided to create this Issue even though this might not be a bug at all and I just used the wrong function(maybe the get_global_mouse_position isn't just the right function to use in this case even though I expected to take this kind of stuff into account and always give me the correct global position). Regardless of this, it would be pretty nice if someone could help me out with this (basically all I want in relation to the minimal reproduction project is, that the red and blue sprites are aware of the split-screen and depending in which viewport the mouse currently is, the red or blue sprite should perfectly look at the mouse position).

I hope it was understandable what I mean and I'm looking forward for any help ^^

Steps to reproduce:
I think the easiest way is to just look at the example project I've provided

Minimal reproduction project:
Multiple_Viewports_Bug.zip

@KoBeWi
Copy link
Member

KoBeWi commented Aug 22, 2019

Sounds like #31551 or #30950

@bojidar-bg
Copy link
Contributor

Definitely sounds like #31551 -- it is the same issue after all..

@KoBeWi
Copy link
Member

KoBeWi commented Aug 22, 2019

Omg ;_;
I meant #30215

@Nophlock
Copy link
Author

Nophlock commented Aug 22, 2019

Oh wow, I totally overlooked these Issues and yeah I'm pretty sure they are related to mine ... sorry. Anyway if anyone stumbles upon the same problem as I(and to make this issue here at least a bit useful), I found a workaround that's works with window scaling etc.

I simply calculate the mouse position by myself. Here is the function that I have created for this:

const DESIRED_RESOLUTION = Vector2(1920,1080)

func getWindowBarSize() -> Vector2:
	var viewport_scale		= OS.window_size / DESIRED_RESOLUTION# 
	var viewport_aspect		= viewport_scale.x / viewport_scale.y
	var result			= Vector2()
	
	if(viewport_aspect < 1.0):
		result.y = floor(OS.window_size.y * (1.0 - viewport_aspect))
	else:
		result.x = floor(OS.window_size.x * (1.0 - viewport_scale.y / viewport_scale.x))
	
	return result

func getMousePosition() -> Vector2:
	var global_mouse_pos 		= self.get_viewport().get_mouse_position()
	var window_bar_size		= self.getWindowBarSize()
	
	var viewport_bar_scale		= (OS.window_size - window_bar_size ) / (DESIRED_RESOLUTION) 
	var viewport_position		= self.camera.custom_viewport.get_parent().rect_position
	var mouse_viewport_scale	= (global_mouse_pos - window_bar_size * 0.5) / viewport_bar_scale
	
	var real_view_size		= self.camera.custom_viewport.get_parent().rect_size
	var viewport_mouse_pos		= mouse_viewport_scale - viewport_position - real_view_size * 0.5
	viewport_mouse_pos		= viewport_mouse_pos * self.camera.zoom + self.camera.get_camera_screen_center()

	return viewport_mouse_pos

If you want to give this a go, you can simple place this function inside the "LookAtMouse.gd" that I've provided in the "Minimal reproduction project" and use this one instead of the "get_global_mouse_position". After that, it shouldn't be to hard to adapt these codelines to your own project.

Hope this helps someone

Edit: Actually I found out that the way I calculate the mouse position was indeed incorrect(but only slightly so I didn't noticed) and also doesn't work with camera zoom and borders. So after a lot of trying, I've finally found a solution that should work every time (no guarantee off course ^^). Anyway I've update the code above

@KoBeWi
Copy link
Member

KoBeWi commented Aug 24, 2019

So yeah, this really is about viewports and stretch mode, so closing it as a duplicate of #30950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants