Jump to content
Sign in to follow this  
Robalo

How do I detect sundown/sunrise ?

Recommended Posts

Does anyone know how to read night or day from the game ? Looking for something that would work for any time of year.

I tried setting up a dummy unit and giving it NVG thinking I'd just check to see when it switches to NVG and back, but currentVisionMode only seems to work on player, on AI always returns 0.

Share this post


Link to post
Share on other sites
Does anyone know how to read night or day from the game ? Looking for something that would work for any time of year.

I tried setting up a dummy unit and giving it NVG thinking I'd just check to see when it switches to NVG and back, but currentVisionMode only seems to work on player, on AI always returns 0.

Hi,

You just need the date command.

_neo_

Edited by neokika

Share this post


Link to post
Share on other sites

That is dependent on the month Neo ;)

@Robalo: It might be possible to pull the relevant data from the world's config, let me check.

EDIT: Darn, doesn't seem to be possible...

Edited by Deadfast

Share this post


Link to post
Share on other sites

_daytime = 0.5 - abs(daytime - 12) / 12;

// 0 = night, 1 = day, 0.5 = sunset/sunrise"

from Bis Environment Effects' module

Share this post


Link to post
Share on other sites
_daytime = 0.5 - abs(daytime - 12) / 12;

// 0 = night, 1 = day, 0.5 = sunset/sunrise"

from Bis Environment Effects' module

That will only work when you are close to 2 specific days of the year, not all year around.

Robalo if you want a simple solution which work more or less in arma then use the sunrise/sunset equation:

http://en.wikipedia.org/wiki/Sunrise_equation. (You have to find the latitude of your map first though).

Edited by Muzzleflash

Share this post


Link to post
Share on other sites

Can you elaborate on what you are trying to accomplish? DMarkwick had a similar question a year or so ago, and could find no 'real' way to tell. I think we came up with a workaround, but I'm not sure what it was. I'll do some research.

Share this post


Link to post
Share on other sites

I use the function below. It also works on other islands at different latitudes. It returns the suns elevation, it's up to you do define sunrise, but I typically go with 0° (nobody is that picky) :) It doesn't match 100% BIS own code, but it's close enough to be useful. Don't verify it against AIs NVG usage, because they depend on scene brightness (not available to us) and individual randomness.

X_fnc_SunElev = {
/*
	Author: CarlGustaffa

	Description:
	Returns the suns altitude for current day and hour of the year on any island (whos latitude may differ).

	Parameters:
	None needed.

	Returns:
	Suns altitude in degrees, positive values after sunrise, negative values before sunrise.
*/
private ["_lat", "_day", "_hour", "_angle", "_isday"];
_lat = -1 * getNumber(configFile >> "CfgWorlds" >> worldName >> "latitude");
_day = 360 * (dateToNumber date);
_hour = (daytime / 24) * 360;
_angle = ((12 * cos(_day) - 78) * cos(_lat) * cos(_hour)) - (24 * sin(_lat) * cos(_day));
_angle
};

  • Like 1

Share this post


Link to post
Share on other sites

Excellent Carl, thank you very much !

I was looking for a way do detect low light conditions and to alter some of the AI skills based on that and some other factors. I know the engine already handles that but not really satisfied with it.

Don't verify it against AIs NVG usage, because they depend on scene brightness (not available to us) and individual randomness.

Detecting scene brightness would actually be good for what I need.

VBS2 has a command isWearingNVG, would be nice to have that in Arma2.

Thanks for all replies.

Edited by Robalo_AS

Share this post


Link to post
Share on other sites
Detecting scene brightness would actually be good for what I need.

Go vote for it then :p

The original thread. Since that discussion (or maybe others, but I would like to think it was my "fault" :)), we got the new (ok, old by now) command dateToNumber in Arma2.

Share this post


Link to post
Share on other sites

I apologize for this necroing, but since this is such a recurrent question and there are a lot of newcomers in regards to scripting, but specially because to CarlGustaffa's solution in the earlier page one could add the following one:

X_fnc_SunRise = { // it was (X_fnc_SunElev)
/*
	Author: CarlGustaffa (modified by gammadust)

	Description:
	Returns the sun's rise in hours for the current day of the year on any island (whos latitude may differ).

	Parameters:
	None needed.

	Returns:
	Sun's rise in hours, in the same format as engine's [daytime] command (16.5 == 16:30)
	To obtain sun's set, just subtract the result from 24.

	Reference:
	http://forums.bistudio.com/showthread.php?107476-How-do-I-detect-sundown-sunrise
*/
private ["_lat", "_day", "_hour"];
_lat = -1 * getNumber(configFile >> "CfgWorlds" >> worldName >> "latitude");
_day = 360 * (dateToNumber date);
_hour = acos ((24 * sin(_lat) * cos(_day)) / ((12 * cos(_day) - 78) * cos(_lat)));
_daytime = _hour / 360 * 24;
_daytime
};

This returns a more direct way of checking for the event of nighttime/daytime cycle with the very close CG's approximation to sun declination depending on the current time.

Depending on implementation one would need to check in a loop the current sun declination and act accordingly.

This modification allows to access the time of the event in advance (only needs to run once per in-game day) obtaining directly the time in hours of sunrise for further processing.

Share this post


Link to post
Share on other sites

There are two scripts in Rube's recent WIP function suite that might be useful in addition:

fn_sun.sqf

Author's description:

calculates sunrise/sunset on a given date,

based on the algorithm from:

Almanac for Computers, 1990

published by Nautical Almanac Office

United States Naval Observatory

Washington, DC 20392

(see williams.best.vwh.net/sunrise_sunset_algorithm.htm)

Latitude and longitude are read from the world config.

Local time is approximated by: floor (_longitude / 15)

and fn_daylightHours.sqf

Author's description:

Description:

Calculates the daylight hours at a given place (latitude) and

on a given date (day in year).

Algorithm by: Food and Agriculture Organization of the United

Nations, http://www.fao.org/docrep/X0490E/x0490e07.htm

The 27 March 2012 release is here:

http://non.sense.ch/shared/rube-wip/

BR

Orcinus

Share this post


Link to post
Share on other sites
There are two scripts in Rube's recent WIP function suite that might be useful in addition...

Yeah. Just keep in mind that these functions calculate stuff according to the real world, while the arma engine unfortunately does stuff in a slightly "different" way (due to a bug that has been there since forever and never been corrected, because "it might break old missions blabla...").

So while the precision of these functions would be absolutely fine, they do not really correspond to arma's model. And I haven't figured out how to correct for this error. Not that I've really tried though... IMHO BIS should simply provide functions (according to their specs including that bug...) for these things. And then let's all pray that this shit gets finally fixed for ArmaIII.

In short: the precision for the sunrise/sunset times is horrendous. And just forget about the moon phase, it's totally off.

Edited by ruebe

Share this post


Link to post
Share on other sites

I did my best interpreting ofp sun position formula and it looks like:

- Obliquity of the ecliptic is simplified to 23º (rounded to unit from 23.43...)

- I suspect the earth's orbit is assumed to be circular instead of keplerian (so there is no mean longitude or mean anomaly to compensate for)

- there is no Epoch (variables do not vary with julian centuries)

- i wonder what happens in leap years

- it does not account for longitude (every map is UTC - higher doubts on this one)

I am probably wrong on this interpretation though, specially since the reference is related to ofp and not arma, and above all my programming/math/astronomy skills are average at best.

That said, i think a simplified model of earth's orbit is all that is needed, if a more realistic model would be used so would processing (much trig functions) to achieve basically the same result.

We do have seasons, and having atmosphere refraction, for example, would be over the top.

Share this post


Link to post
Share on other sites

@ruebe: thanks. I hadn't looked at those 2 scripts other then a glance. Pity.

Thanks for making the package available anyway - one script will, I hope, make my first attempt at an addon somewhat easier.

BR

Orcinus

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×