Planet atmosphere glow

Blitz3D Forums/Blitz3D Programming/Planet atmosphere glow

Krischan(Posted 2008) [#1]
I am experimenting with a simple planet atmosphere FX without textures. I have a sphere and a ring of triangles surrounding the sphere, the ring always points to the cam. It looks nice when you're far away from the planet but doesn't work when you get closer. I am looking for some kind of formula which resizes/repositions the ring according to the viewer's perspective along the sphere. I tried some experiments with SQR,EXP and so on but wasn't successful. And even if I got close to an acceptable result it didn't worked anymore when I resized the sphere from 1.0 to 2.0 or other values.

Here is a demo code to visualize the problem:



Any suggestions?


Pongo(Posted 2008) [#2]
I think what you have is pretty good to start with.

The thing that is screwing you up is the perspective of the sphere when you get close. If you change your camerazoom to a higher value (I used 4) things work a lot better.


John Blackledge(Posted 2008) [#3]
That's not bad - simple and practical.


big10p(Posted 2008) [#4]
I think this is doable. You basically want to position the ring on the horizon of the sphere/planet. Google 'calculating distance to horizon' to get started.


_PJ_(Posted 2008) [#5]

I think this is doable. You basically want to position the ring on the horizon of the sphere/planet. Google 'calculating distance to horizon' to get started


Wouldn't that get complicated and involve a re-scaling of the ring then?
As the horizon shrinks at higher 'latitudes' ('latitude' here is assuming an invariance where any point on the sphere's surface can be considered a pole)

Once you get close enough for the 'distortioon' to set in, might be niece to just switch to a sprite overlay instead. ?

I think it's acvtually a lot more complicated than it first appears...


big10p(Posted 2008) [#6]
Yes, it will require re-scaling the ring, but I dont see that as a problem.


_PJ_(Posted 2008) [#7]
Hehe well I had problems trying to work it..

I am in agreement wit the cameraoom comment though, because the fisheye distortion plays havoc with objects up close which I don't think is easily reslveable.


big10p(Posted 2008) [#8]
I am in agreement wit the cameraoom comment though, because the fisheye distortion plays havoc with objects up close which I don't think is easily reslveable.
You could always re-size the ring slightly smaller than the calculations say, to get over the perspective problem, I think.


Pongo(Posted 2008) [#9]
It's not only the perspective that is causing the problem.

The glow is using a look at constraint to the camera. When the camera gets really close and is not looking at the center of the glow, then the glow will actually tip away from the camera view. (I don't know if I explained this very good)

Anyways, the larger camerazoom also helps this because the camera stays farther away from the glow, so you do not get this effect as bad.


Krischan(Posted 2008) [#10]
I started a quick'n'dirty test to check out if it is really possible, and it IS - but with my solution not in realtime. Here is my testbed (with a improved Createring function, there was an error in the UV coordinates and we don't need to turn it 90° all the time anymore):

1. the dummy pivot always points to cam
2. do a linepick from cam to spherecenter
3. check if it hits the planet or the dummy
4. if it hits the planet move the dummy a little bit to the cam
5. if it hits the dummy move the dummy a little bit from the cam away
6. the ring scale is (the distance between dummy and sphere) / 2
7. scale the ring and point it to the cam

The process is due accuracy very slow so be patient if you get too far away from the planet (this is a testbed for a close orbit). You can speed this up by changing the 0.001 value to 0.01 or 0.1, which will result in precision lost.

big10p: I don't get the "calculate distance to horizon" function to work. To be precise: I can calc the distance between the cam and the horizon, but how shall I calculate the actual scale of the ring out of it? Can you post a small BB-Example?



Edit: it could look like this image if it is working:



big10p(Posted 2008) [#11]
big10p: I don't get the "calculate distance to horizon" function to work. To be precise: I can calc the distance between the cam and the horizon, but how shall I calculate the actual scale of the ring out of it? Can you post a small BB-Example?
Erm, I'd have to have a think about that, TBH. I just figured the 'distance to horizon' calc was along the right lines.

Actually, looking at that screenie, I'm thinking using a separate sphere for the atmosphere may be a better way to go. That pic doesn't look quite right - kinda looks like theres a light behind the planet.


Krischan(Posted 2008) [#12]
> That pic doesn't look quite right - kinda looks like theres a light behind the planet.

I know. But that is priority 5 at the moment. Its only the testbed with textures and stars.


Warner(Posted 2008) [#13]
I think that you should loop through each vertex of the planet, and project it. Then, determine min/max x/y/z values. If the ring was a 2D object, that would be enough. However, because the ring is 3D, you'll need to find a way to project the 2D coords back into the 3D world. CameraPick on a plane should usually work, but I had trouble getting it to work with this.
Anyway, here is code that projects the 2D size of the planet onto the screen:



Krischan(Posted 2008) [#14]
Woo-hoo! Finally I found the exact mathematical solution (with some help from this homepage - it is in german but you don't need to understand german to understand the solution :-)

We only need two right-angled triangles and some trigonometry knowledge to solve it. The ring scale is identical with the c2 hypotenuse we need to calc. I named the variables like they would appear in the two triangles. And we only need to know the scale (Radius of the planet) and the distance between the camera and the planet, it is very simple if you understand how ;-)

Here is a simple sketch of the two triangles, the red dot at the top is the cam, the red line at the left the distance, the black line the radius and the violet line at the bottom is the c2 hypotenuse we are looking for:


radius#=scale
distance#=EntityDistance(cam,planet)

; First triangle
c1#=distance
a1#=radius
q1#=a1^2/c1
p1#=c1-q1
h1#=Sqr(p1*q1)
gamma1#=90
alpha1#=ATan(h1/p1)
beta1#=gamma1-alpha1

; Second Triangle
alpha2# = 90-(90-beta1)
b2# = a1/Tan(alpha2)
c2# = (Sqr(a1^2 + b2^2))/radius

ScaleEntity ring,c2,c2,c2





Warner(Posted 2008) [#15]
Perfect! Nice work.


_PJ_(Posted 2008) [#16]

The glow is using a look at constraint to the camera. When the camera gets really close and is not looking at the center of the glow, then the glow will actually tip away from the camera view. (I don't know if I explained this very good)



Yeah I did erealise this tioo, just didn't mention it. The same problem occurs when you get within CameraNear Range etc. and objects seem to 'flip' over or around.

Still nice to see a solution works there. I gave up way too early because trig gives me a headache (why do I punish myself tryiong to work in 3D >? :D :D :D)



I'm thinking using a separate sphere for the atmosphere may be a better way to go

That pic doesn't look quite right - kinda looks like theres a light behind the planet.


I'm sure just alpha-ing it down a little (if that makes sense) will be fine.
Using a whole sphere is gonna be a lot more intensive than a smaller bunch of tri's at least.


Krischan(Posted 2008) [#17]
I am not sure If I understood this correct, but after my post I had the problem that when I got closer to the planet the glow "shrinked" instead of expanding like an atmosphere would do. I solved the problem by using some kind of "flipped" ring and bending the outer vertices towards and away from the cam that a "glow cone" exists. Now I can even "land" on the planet and see the stars shining through my atmosphere without changing anything, just rescaling the ring!

Sorry, I have no time for a small demo (perhaps later) but here is the function call and the main function parts (scale is here 1.0, 10.0... must match the planet size):



As you can see, the ring starts now at the size of the planet and moves inwards, I don't know how to explain. Just stop the glow rings pointing to the cam, set FX 16 and move around the planet and you will see what happens. And here three eye-candy shots using this technique (still room for improvement, e.g. fading the stars and matching the Cameraclscolor while inside the atmosphere):

Nice view from a geostationary orbit - lets land at the far left side


Entering the atmosphere


We reached the cruising altitude


Oh and yes I know - planets don't glow at the dark side. If anybody has a nice solution how to setup the ring normals according to the light angle - POST IT!


stayne(Posted 2008) [#18]
Nice work Krischan and cheers for the math lesson (with diagram even!). I have enjoyed this thread :).


John Blackledge(Posted 2008) [#19]
When can we see a demo of this?


Krischan(Posted 2008) [#20]
Well here is a little tech demo, I added some basic light functionality, not finished yet - but this is a good point to start from. Just copy'n'paste - it doesn't need any media but uses the same technique which I used to make the screenshots above. Additional, I tried to measure the angles between the cam, the planet and the sun. The variable alpha# holds a value from 0.0 to 1.0 where 1.0 is in a direct line between sun and the planet and 0.0 is behind the planet to check if the ring glow has full or some alpha.

To enjoy the effect just try to land at the far right side of the planet (the sunny side) and when you're close to the surface press the right arrow key to start in a 90° angle from the surface into space.




_PJ_(Posted 2008) [#21]
Beautiful. Great work, Krischan! Very impressive.

Of course what you have shown is purely a demo, and maybe you have considered this already, but I was wondering if it's worth using HideEntity when the 'atmosphere' is not visible? I guess this really depends on how many planets and what other stuff is going on at the time, either way, I really like what you have done here!


Krischan(Posted 2008) [#22]
It is only a tech demo. Do whatever you want with it, improve it and let us share if you think it is usable for others. I was only disappointed that I didn't found a nice and elegant solution for my atmosphere problem - so i coded my own. My solution is not the best or even physically correct, but my planet looks much nicer now and if I ever consider to implement some kind of "landing on the planet" feature this is the way I can go now.

I really enjoyed the game Starflight years ago where you could land on a planet and drive around to collect things from the surface. The GFX is a big laugher today but the gameplay and the imagination balanced everything. Later - Frontier was THE procedural game (until today?) but it was boring and too exact. Infinity looks very promising and i hope that it won't be a blown up Frontier without a challenging gameplay (if we'll ever see it).

It has to make fun, fun and fun again. Who cares about lit nightsides? We had a lot of fun with 64K/16 color games only a few years ago, and often even more fun than in some games today.

And if you never played Starflight go and get an Amiga Emulator, the ADF and play it. You really missed something -> Screenshots


_PJ_(Posted 2008) [#23]
I'm old enough (and ugly enough) to fondly remember Elite. Yeah, Frontier was a nice change moving from the speccy to the Amiga and getting all those colours, but just... too much overkill on the physics ;)

I don't recall Starflight, though, but I'm sure it was grand!

I was working on something similar to Elite with beng able to land on planets and including various land-based 'factories' etc. but as my coding improved I have scrapped a lot of previous attempts, and am now concentrating more on planning other aspects before I hit it again. However, I'm sure stuff like this will be a great addition. It's an open-source plan, so hopefully I'll have some useful routines in return especially seeing as the two projects appear to have some similarities.


Filax(Posted 2011) [#24]
Hi

The same with a type class



Last edited 2011