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

Implement player:get_point_dir() and player:get_point_screen_pos() #15567

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

grorp
Copy link
Member

@grorp grorp commented Dec 16, 2024

This PR implements player:get_point_dir() as described in #15562

More design decision explanation in addition to the issue:

  • I chose yaw/pitch for the internal representation / for the network protocol because this means we only need two floats, a normal vector would need three and a quaternion four

  • After a discussion with Warr1024, I've also added player:get_point_screen_pos() so that games can place HUD elements taking the pointer position into account.

    • The value could be derived from point direction (or the other way around) on the server if you reconstructed the client's projection matrix, but this would be hacky and brittle, so I'm instead sending both values from client to server.

To do

This PR is a Ready for Review.

I'm no expert in this rotation business, but the rotation stuff works and makes sense as far as I can tell. EDIT: The more complicated rotation stuff I was doing in the first version of this PR doesn't exist anymore.

How to test

Some quick example code for testing player:get_point_screen_pos()
local hud_ids = {}

core.register_globalstep(function()
	for _, player in ipairs(core.get_connected_players()) do
		player:hud_set_flags({ crosshair = false })
		if not hud_ids[player] then
			hud_ids[player] = player:hud_add({
				type = "image",
				position = player:get_point_screen_pos(),
				alignment = {x = 0, y = 0},
				text = "crosshair.png",
				scale = {x = 1, y = 1},
			})
		else
			player:hud_change(hud_ids[player], "position", player:get_point_screen_pos())
		end
	end
end)

core.register_on_leaveplayer(function(player)
	hud_ids[player] = nil
end)

Install GGraffiti with a patch to use player:get_point_dir(): grorp/ggraffiti@1eed15c

  1. The mod must work as usual on desktop
  2. The mod must work as usual on Android with touch_use_crosshair = true
  3. The mod must now work properly on Android with touch_use_crosshair = false: You will now paint where you put your finger
    • arguably the camera shouldn't move while painting, but that's a different UX improvement

Also verify that things still work correctly with non-zero eye offset (player:set_eye_offset)

@grorp grorp added @ Script API Android Feature ✨ PRs that add or enhance a feature labels Dec 16, 2024
@grorp grorp marked this pull request as draft December 16, 2024 21:49
@grorp grorp marked this pull request as ready for review December 17, 2024 14:01
@grorp grorp changed the title Implement player:get_point_dir() Implement player:get_point_screen_pos() and player:get_point_dir() Dec 20, 2024
@grorp grorp changed the title Implement player:get_point_screen_pos() and player:get_point_dir() Implement player:get_point_dir() and player:get_point_screen_pos() Dec 20, 2024
@nauta-turbidus
Copy link
Contributor

I'm only now realizing how useful this would be for mobile compatibility.

Think hitting enemies with a weapon that is supposed to depend on distance in some way. You have no reliable way to detect the distance currently, because simple position difference length doesn't account for collision boxes for example.

This would allow to at least do a raycast along the actual strike direction (because doing it along look direction would break mobile compatibility), find intersection position, and get distance as position difference length from that to player "eyes".

@Zughy
Copy link
Contributor

Zughy commented Jan 9, 2025

I've also added player:get_point_screen_pos() so that games can place HUD elements taking the pointer position into account.

Practical example?

@nauta-turbidus
Copy link
Contributor

I've also added player:get_point_screen_pos() so that games can place HUD elements taking the pointer position into account.

Practical example?

Some movable crosshair?

@grorp
Copy link
Member Author

grorp commented Jan 9, 2025

I've also added player:get_point_screen_pos() so that games can place HUD elements taking the pointer position into account.

Practical example?

A crosshair-like HUD or a "what am I looking at" HUD text like the one in NodeCore (currently that one is placed next to the crosshair afaik, once it takes the actual pointing direction into account, it should also be placed next to the actual pointer position).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Feature ✨ PRs that add or enhance a feature @ Script API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants