How to set Celestia's time in C++

Have a question about using Celestia? Check here first for FAQs and helpful advice.
Forum rules
Please help to make this forum more useful by checking the FAQs before posting! Keep it clean, keep it civil, keep it truthful, stay on topic, be responsible, share your knowledge.
Topic author
su27
Posts: 10
Joined: 26.08.2010
With us: 14 years 2 months

How to set Celestia's time in C++

Post #1by su27 » 20.09.2010, 13:51

I've modified Celestia to a network time synchonizing program

the function to set time is appCore->getSimulation()->setTime(curTime);
the time data(curTime) is sent from the network server,so it can share the same time with the server.

but sometimes the Celestia program crash,when it synchonize time with the network server,
though not very often, it troubles me.

I've look at the debugging information and it shows that when the time is set, the velocity and position is also calculated,so there's a mistake.

How to solve this problem?
Thanks. :roll:

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Re: How to set Celestia's time in C++

Post #2by selden » 20.09.2010, 14:33

SU27,

Your comments are ambiguous.

Do you mean that your C++ code is calculating a velocity? (it should not)

What 'velocity' do you mean? Please be specific about the variables involved.

My understanding is that Celestia should not be calculating any velocity values based on the current and previous time, although it can use externally supplied velocities to calculate the position of an object at a particular time.

Celestia is supposed to be able to calculate the positions of all objects on the screen at any simulated time, whether that time is before or after the time of the previous set of screen calculations. The code in Celestia, and the code in any Lua modules that you provide, must not assume that simulated time is monotonically increasing. Some Addons do assume that the simulated time is always increasing and will do strange things if it goes backward, but Celestia should not crash.
Selden

Topic author
su27
Posts: 10
Joined: 26.08.2010
With us: 14 years 2 months

Re: How to set Celestia's time in C++

Post #3by su27 » 20.09.2010, 14:59

selden wrote:SU27,

Your comments are ambiguous.

Do you mean that your C++ code is calculating a velocity? (it should not)

sorry, selden,my English is not so good.

I mean that I'm writing a time synchonizing function for Celestia,
which is listening to the time data sent from a server via UDP protocol.

what I want is to set the time according to the server's data,
so I wrote "appCore->getSimulation()->setTime" in this function,
but however, I find sometimes it crashes....


bool CTimeSync::datagramHandler( )
{
SSyncPkg pkgBuf;
int rl = m_pSock->getDatagram((char*)(&pkgBuf), sizeof( SSyncPkg)); //from UDP Socket

//Test the length of UDP data
if ( rl != sizeof( SSyncPkg) )
{
return false;
}
//Parse the UDP data
if( strcmp( pkgBuf.header, SYNC_PKG_HEADER))
{
return false;
}
//Parse the UDP data
if ( !strcmp(pkgBuf.cmd, SYNC_CMD_TIME) ) // Synchonize time
{
appCore->getSimulation()->setTime(pkgBuf.tdb);
}

return true;
}

Above is my code, I find simply call celestiacore and settime, sometimes leads to crash....
Maybe because Celestia is a multi-thread program,
Should I pause the celestiacore's simulation when I settime or?

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Re: How to set Celestia's time in C++

Post #4by selden » 20.09.2010, 16:03

Celestia's internal time should only be changed before Celestia starts the calculations necessary for a screen update. If the time is changed asynchronously in the middle of a screen update, I'm sure strange things will happen, including crashes.

I am not familiar with the details of Celestia's code or when your UDP function is called. If your routine is called asynchronously by the network code, you would need to store the new time in an intermediate global variable and use that variable to update Celestia's internal time when the current screen update has completed.
Selden

chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Re: How to set Celestia's time in C++

Post #5by chris » 20.09.2010, 16:29

Su27,

I know that other people have synchronized Celestia's time using data read from a network connection, so it's possible. Setting the time shouldn't cause a crash, even if it's done in a separate thread (though as Selden mentioned, you may see strange results if you update the time mid-frame.) Have you tried building a debug version of Celestia and running that? It would be very helpful if you could provide the call stack at the time of the crash.

--Chris

Topic author
su27
Posts: 10
Joined: 26.08.2010
With us: 14 years 2 months

Re: How to set Celestia's time in C++

Post #6by su27 » 20.09.2010, 16:52

chris wrote:Su27,

I know that other people have synchronized Celestia's time using data read from a network connection, so it's possible. Setting the time shouldn't cause a crash, even if it's done in a separate thread (though as Selden mentioned, you may see strange results if you update the time mid-frame.) Have you tried building a debug version of Celestia and running that? It would be very helpful if you could provide the call stack at the time of the crash.

--Chris

Thank you very much Selden and Chris
I've tried the method, add the time changing part into tick() function of celestiacore.cpp and it works good :roll: ,

Best wishes!


Return to “Help Central”