Some basic script questions

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 2 months

Some basic script questions

Post #1by Malenfant » 23.10.2005, 18:53

I've been playing around with some of the scripts from the Celestia wikibook ( http://en.wikibooks.org/wiki/Simple_CELX_Scripting ) and have come up with this to get the distance between Cassini and the observer:

Code: Select all

camera = celestia:getobserver()
cassini = celestia:find("Cassini") -- finds object named Cassini
cameraPosition = camera:getposition() -- gets position of observer
cassiniPosition = cassini:getposition() -- gets position of Cassini
distance = cassiniPosition:distanceto(cameraPosition) -- gets distance from  position of Cassini to that of observer
celestia:flash("Current distance to Cassini is "..distance.. " km")


To test it out, I selected Cassini and went to it (actually I set a distance of 1 km so I could verify the distance easily), so it's centred on the screen. When I run the code however, Cassini disappears - it seems that the direction the camera is facing suddenly changes for no apparent reason when I run the script (the distance stays the same so I know the camera isn't actually moving in space), and it doesn't seem to be looking at the same point each time. In fact this viewpoint shift happens whenever I run the script, regardless of what I'm looking at at the time.

Anyone know what's going on? How do I keep the view centred on whatever I'm looking at?

Also, I thought the default measurement for Celestia was the somewhat arcane "microlightyear"? If so, why does this script give results in kilometres? (not that I'm complaining, I prefer km by far. Just wondering what's going on).

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 2 months

Post #2by Malenfant » 23.10.2005, 22:49

Code: Select all

camera = celestia:getobserver()
cassini = celestia:find("Cassini") -- finds object named Cassini
camera:center(cassini,1)
cameraPosition = camera:getposition() -- gets position of observer
cassiniPosition = cassini:getposition() -- gets position of Cassini
distance = cassiniPosition:distanceto(cameraPosition) -- gets distance from  position of Cassini to that of observer
celestia:flash("Current distance to Cassini is "..distance.. " km")
wait(2)


OK, so this centres the view apparently. But I notice that everytime I run the script, the view centres - even when it's already centred on Cassini. It's like the viewpoint shifts to offcentre at the start, before centring itself. How can I stop this happening?

BrainDead
Posts: 238
Joined: 27.08.2005
With us: 19 years 2 months
Location: Germantown, OH

Post #3by BrainDead » 24.10.2005, 01:38

Malenfant wrote:OK, so this centres the view apparently. But I notice that everytime I run the script, the view centres - even when it's already centred on Cassini. It's like the viewpoint shifts to offcentre at the start, before centring itself. How can I stop this happening?

Don't know if it's related or not, but I had the same kind of opening view
problems when I ran my first test script. Seems that the offsets were
caused by the way in which I executed the scripts. (Double-clicking on the
file name in the open dialog.)

By the way, thanks for this example code. I've played with it a bit and
added my version of it to the discussion page on the Wikibook site.

Thanks.
Brain-Dead Bob

Windows XP-SP2, 256Meg 1024x768 Resolution
Intel Celeron 1400 MHz CPU
Intel 82815 Graphics Controller
OpenGL Version: 1.1.2 - Build 4.13.01.3196
Celestia 1.4.1

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 2 months

Post #4by Malenfant » 24.10.2005, 03:22

BrainDead wrote:Don't know if it's related or not, but I had the same kind of opening view problems when I ran my first test script. Seems that the offsets were caused by the way in which I executed the scripts. (Double-clicking on the file name in the open dialog.)


How else would you open a script from within Celestia? I thought that was the only way you could run a script in Celestia - is there another way? (and how did you get round this problem that you had?).

EDIT: I notice in the wikibook discussion you say "Mystery explained... Open Scripts dialog and I double-clicked on the celx script in order to open it. If I simply use the dialogs provided by Celestia to open the script, the movement does not occur. Thanks."

What "dialogs provided by Celestia" are you talking about? All that's obvious to me is the Open Scripts dialog.

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Post #5by cpotting » 24.10.2005, 04:40

Malenfant wrote:What "dialogs provided by Celestia" are you talking about? All that's obvious to me is the Open Scripts dialog.


Ahhh, I duplicated the problem. It only happens when you do the following:
1) select File -> Open Script from the Celestia menu
2) navigate the windows dialogue to the desired directory
3) double-click on the file name.

The other ways to open a script that do not cause this
a1) select File -> Open Script from the Celestia menu
a2) navigate the windows dialogue to the desired directory
a3) single-click on the file name and either press ENTER or click on the Open button

b1) open Windows Explorer
b2) navigate to the desired directory
b3) double click on the script or
right-click and select open or
single click on the script and press ENTER
Clive Pottinger
Victoria, BC Canada

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 2 months

Post #6by Malenfant » 24.10.2005, 06:47

Huh. How bizarre - yeah, it works fine if I just open the file by pressing enter instead of doubleclicking it in the Open Script menu.

What could cause such odd behaviour?!

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #7by Vincent » 24.10.2005, 07:36

Hi,

I think I found the trick...

When you double-click on your celx file in the Celestia script browser, you change the camera view as if you had directly double-clicked on another object in the celestia main window. Try to double-click on a star : Celestia change the view to center the object you have selected...

You can also try to move the Celestia script browser to center of the screen, then double-click on your celx file. You will be able to see Cassini because you didn't change the camera view (too much).

@+
Vincent
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

BrainDead
Posts: 238
Joined: 27.08.2005
With us: 19 years 2 months
Location: Germantown, OH

Post #8by BrainDead » 24.10.2005, 10:22

I think that Clive and Vincent have hit the Nail on the Head! here.

Isn't it interesting that such a trivial matter causes sooo much confusion
though.

Thanks guys.
Brain-Dead Bob



Windows XP-SP2, 256Meg 1024x768 Resolution

Intel Celeron 1400 MHz CPU

Intel 82815 Graphics Controller

OpenGL Version: 1.1.2 - Build 4.13.01.3196

Celestia 1.4.1

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Re: Some basic script questions

Post #9by cpotting » 24.10.2005, 11:13

Malenfant wrote:I thought the default measurement for Celestia was the somewhat arcane "microlightyear"? If so, why does this script give results in kilometres? (not that I'm complaining, I prefer km by far. Just wondering what's going on).


You are right. For most of the methods, distances are given in microlightyears - a nice compromise unit for dealing with interplanetary and interstellar distances.

But it seems that, for some cases at any rate, the authors realised that a more appropriate unit would be the kilometre. So radius(), gotodistance(), distanceto() and some others return kilometres.

You just have to check the documentation to make sure you're getting the right units. It has caught me more than once.
Clive Pottinger
Victoria, BC Canada

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 2 months

Re: Some basic script questions

Post #10by Malenfant » 24.10.2005, 20:26

cpotting wrote:You are right. For most of the methods, distances are given in microlightyears - a nice compromise unit for dealing with interplanetary and interstellar distances.


What's wrong with just sticking to AU as the default measurement though (just calculating it now, a microlightyear is about 0.063 AU, or just under 10 million km? What possible use is that for anything? Seems like a comprimise that is beneficial for nobody)

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Re: Some basic script questions

Post #11by cpotting » 25.10.2005, 12:42

Malenfant wrote:What's wrong with just sticking to AU as the default measurement though (just calculating it now, a microlightyear is about 0.063 AU, or just under 10 million km? What possible use is that for anything? Seems like a comprimise that is beneficial for nobody)


Well, you would really have to talk to Chris about that. But remember 99.99% of what Celestia deals with are stars. Distances to stars are measured in light-years, so it would be a logical first-choice. However, trying to measure interplanetary distances in lightyears would be a major pain - so the uLy (mico-lightyear) seems a good compromise. It gives reasonable values for interplanetary distances and can be easily converted to/from light-years.

Choosing the au as a base unit would make interplanetary distances easy to work with, but it doesn't convert well to light-years (read it doesn't convert well to 99.99% of what Celestia works with).

I can't say for sure if that is why uLy was chosen, but it seems logical to me.
Clive Pottinger
Victoria, BC Canada


Return to “Scripting”