How To Detect Looking At Sun?

Started by MamaLuigi on

Topic category: Help with modding (Java Edition)

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How To Detect Looking At Sun?

I am making a mod where if you are looking at the sun, and you are holding a specific item, (in my case being a Sun Medallion), then something will happen.

But how would I detect if you are looking at the sun at all?

 

Is this even possible?

If so, how would I do it, and if not, what could I do instead?

 

Any help would be greatly appreciated!

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It is possible, but would…
Fri, 02/09/2024 - 12:20

It is possible, but would likely require math well outside my area of expertise. (But it's still worth trying! Sounds like a cool idea.)

The basic idea is that you would need to get the player's look angle vectors, and use them to determine where they're looking. (The look angle vector measures the angle of a line from the player's eyes in each of the three axes- so the y look angle vector is 1 when the player is looking straight up, 0 when they're looking straight ahead, and -1 when they're looking straight down.)

You need to somehow write an equation that determines if the player is oriented towards the sun, (their X/Z vectors imply they're either facing east, west, or directly up in which case it doesn't matter), and whether their y look angle vector suggests they're looking at the sun. This is the tricky part, given that the sun moves across the sky- at noon the player's y look angle vector needs to equal 1, at sunrise or sunset it needs to be closer to 0, etc. You'd probably want a decent margin of error so that it's not super finnicky. And of course you'd need a way to check if the player can actually see the sun. (That is, they're not looking at a solid block. This shouldn't be too hard, since you can get the block at the player's look angle position pretty easily.)

But yeah, not impossible. I'm just not personally sure what the math would look like.

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Well for starters, how would…
Fri, 02/09/2024 - 13:32

Well for starters, how would i determine if the player is looking east or west?

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I believe you'd use the x…
Fri, 02/09/2024 - 13:34

I believe you'd use the x look angle vector, which is 1 when the player is looking east, and -1 when they're looking west. If the absolute value of the x look angle vector is greater than 0.75 or so, they're probably looking east or west.

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
As something extra, I want…
Fri, 02/09/2024 - 13:46

As something extra, I want to make the procedure so it only runs during the day.

 

Could i do this with pre-esisting blocks, or is there an event trigger that will do it for me?

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Just setting it to only…
Fri, 02/09/2024 - 13:48

Just setting it to only accept positive values for the y vector should work, since the vector would be below zero when the sun goes below the horizon. (But if you're worried about it, there's a world data function that returns true only when it's daytime in the provided world.)

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
At noon, the player's y look…
Fri, 02/09/2024 - 13:51

At noon, the player's y look angle vector should be 1 if they are looking at the sun/up, right? So is there an equation or formula that could maybe compare the time of day and the player's y look angle vector or something like that?

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You'd probably have to…
Fri, 02/09/2024 - 14:05

You'd probably have to convert them into percentages, and use a MOD, (a remainder) function on the time of day. The issue is that you can only get world time, (the current amount of time the world has been running, not the current time of day), and the world time will range from 0-6000-12000 depending on the position of the sun, whereas the vector ranges from 0-1-0. 

So you'd need to first use a MOD function on the world time out of 24000. (This will return the remainder of the current game time divided by 24000, essentially getting rid of any full days, and returning the time in the current day.) So you'll now have a number that ranges from 0-24000, which you only need to consider if it's below sunset. (Around 12000 ticks), but you want it to be more like 0-6000-0, so that you can cleanly convert between this and the look angle vector.

...So if the time is greater than 6000, you need to subtract 6000 from it and then subtract the result from 6000. (6001 becomes 5999, 7000 becomes 5000, 11999 becomes 1, etc.) Now you'll be getting a number that ranges from 0 to 6000 to 0, depending on the time of day.

From there, you should be able to cleanly convert to decimals by dividing out of 6000. (6000 becomes 1, 3000 becomes 0.5, etc.) If the player's y look angle vector is within a margin of error of this value, then they're probably looking at the sun. Again, I haven't personally tested any of this, but I'm pretty sure the principle's sound.

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Could you maybe send an…
Fri, 02/09/2024 - 15:09

Could you maybe send an image of the math/procedure?

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Like I said, I haven't…
Fri, 02/09/2024 - 15:11

Like I said, I haven't actually tried assembling this. I'll give it a try. (If it works though, I may or may not wind up using it in one of my projects, just 'cause it sounds like such a cool idea!)

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks!
Fri, 02/09/2024 - 15:13

Thanks!

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Any luck?
Fri, 02/09/2024 - 15:22

Any luck?

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This is what I've got so far…
Fri, 02/09/2024 - 18:22

This is what I've got so far. It works in principle, but the ratio is a bit off, (earlier or later in the day you have to look a little bit below the sun.) It's also not 100% accurate on East/West, (you can look a bit to either side and still trigger it successfully), and would require more vector shennanigans to make it more accurate horizontally. There should be a way to compensate for the difference, probably by changing the ratio. But this seems to be mostly functional.

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks! I'll try this out!
Fri, 02/09/2024 - 21:48

Thanks! I'll try this out!

Last seen on 18:33, 15. Jun 2024
Joined Sep 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I got it to work, but how…
Fri, 02/09/2024 - 23:34

I got it to work, but how could i change the procedure to fix the up/down bit?