Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411511 Posts in 69375 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 12:07:10 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Cube sphere coordinate
Pages: 1 [2]
Print
Author Topic: Cube sphere coordinate  (Read 3193 times)
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #20 on: November 29, 2017, 05:14:31 AM »

What I want is to have a spatial hash for planetary terrain system:
- I'll compute the position of the ship projected at the surface level (just one point)
- find the terrain's cell he is hovering (using the reverse projection to cube)
- build terrain and LOD around that position. (which is projected to the sphere trivially)

Since it's a cube sphere I need to find corresponding position on cube
- first I find the face
- then I find the cell hash of the position

It allow me to treat the data as a flat plane and do logic based on that, then project back to the sphere all the data, which is trivial. One problem is that I didn't knew how to do boundary checking on a cube sphere. On a flat plane it's trivial, it's just axis aligned interval. So it has to be regular and proportional to the sphere size (basically finding the original points before projection on the sphere).

That leave a lot of logic for stitching corner and edges.
Logged

bateleur
Level 10
*****



View Profile
« Reply #21 on: November 29, 2017, 05:32:55 AM »

Sure, but the point is that the much simpler mapping I give above still acts as a spatial hash, the question is whether you mind the distortions it creates in which the transformation is not area-preserving.
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #22 on: November 29, 2017, 06:48:47 AM »

I'll give a try since I have nothing to do until I understand what's wrong lol

But I think it wouldn't work, because I need the correct data to project back the generated data to the sphere. I mean if my terrain and city generation is deformed and my coordinate are wrong my game is dead because I can't establish any gameplay metrics.
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #23 on: November 30, 2017, 06:06:53 PM »

I just want to say in the final product the goal won't be to project a sphere to a cube, but to find a mapping equivalence from one point on the sphere surface.

In the final product it will be "analytic" planet, ie they are represented by only 2 variables, position and radius. The player's position (as a spaceship) is just the vector to the planet truncated by the radius to find the surface position.

But because the underlying gamplay mapping model is a cubesphere I need to find the equivalent position translated to the cube space where it's easier to hash the position and find the correct mapping on the sphere.

Planet can be as big as earth, a terrain (close chunk +lod together) is visually a square as big as 36km² (6km per side). Which mean you need 400 terrains to make a earth side planet, with each terrain being 1/960000th of the cubesphere.

So while the test implementation is showing a full sphere as a mesh, it won't exist in the final product, and we can't rely on its vertices. Trying to find the proper cell division of the projected position of the spaceship has proven to be quite a challenge in sphere space, hence the retro projection used here and design by one of the thread contributor. It's not about the square per see, but still dividing the sphere in proper cell regarding the cubemapping, but analytically. We never compute the entire sphere at any moment.

I'll refer to this previous post as to way I used this math and not just a normalized sphere:
https://forums.tigsource.com/index.php?topic=58921.msg1301873#msg1301873

And this post proposed the solution I tried to used, which go through the process of reversing:
https://forums.tigsource.com/index.php?topic=58921.msg1302578#msg1302578
« Last Edit: November 30, 2017, 06:12:55 PM by gimymblert » Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #24 on: January 20, 2018, 06:09:09 PM »

New algo:
https://imgur.com/a/1PCBW
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #25 on: January 20, 2018, 10:46:54 PM »

Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Did not work out as expected:
Didn't worked out as expected, seems like a 0 problem ...

Someone know where the problem is?




Code:
public static Vector3 cubify( Vector3 s)
        {
            float xx2 = s.x * s.x * 2.0f;
            float yy2 = s.y * s.y * 2.0f;
 
            Vector2 v = new Vector2(xx2 - yy2, yy2 - xx2);
 
            float ii = v.y - 3.0f;
            ii *= ii;
 
            float isqrt = -Mathf.Sqrt(ii - 12.0f * xx2) + 3.0f;
 
            v = new Vector2( Mathf.Sqrt( v.x + isqrt),  Mathf.Sqrt( v.y + isqrt) );
            v *= isqrt2;
 
            return  new Vector3(
                Mathf.Sign(s.x) * v.x,
                Mathf.Sign(s.y) * v.y,
                Mathf.Sign(s.z) * 1.0f);
        }
 
    public static Vector3 sphere2cube( Vector3 sphere)
    {
        Vector3 f = new Vector3 (
            Mathf.Abs(sphere.x),
            Mathf.Abs(sphere.y),
            Mathf.Abs(sphere.z));
 
        bool a = f.y >= f.x && f.y >= f.z;
        bool b = f.x >= f.z;
 
       
        Vector3 result;
     
        if (a)
        {
            result = cubify( new Vector3(sphere.x,sphere.z,sphere.y));
            result = new Vector3(result.x,result.z,result.y);
        }
        else if (b)
        {
            result = cubify( new Vector3(sphere.y,sphere.z,sphere.x));
            result = new Vector3(result.z,result.x,result.y);
        }
        else
        {
            result = cubify(sphere);
        }
     
        return result;
    }
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #26 on: January 21, 2018, 09:28:38 PM »

Solved!

Code:
public static Vector3 cubify( Vector3 s)
    {
        float xx2 = s.x * s.x * 2.0f;
        float yy2 = s.y * s.y * 2.0f;

        Vector2 v = new Vector2(xx2 - yy2, yy2 - xx2);

        float ii = v.y - 3.0f;
        ii *= ii;

        float isqrt = -Mathf.Sqrt(ii - 12.0f * xx2) + 3.0f;

        v = new Vector2(
            Mathf.Sqrt(Mathf.Abs( v.x + isqrt) ),
            Mathf.Sqrt(Mathf.Abs( v.y + isqrt) ));//v corruption            v *= isqrt2;

        return  new Vector3(
            Mathf.Sign(s.x) * v.x,
            Mathf.Sign(s.y) * v.y,
            Mathf.Sign(s.z) * 1.0f);
    }

public static Vector3 sphere2cube( Vector3 sphere)
{
    Vector3 f = new Vector3 (
        Mathf.Abs(sphere.x),
        Mathf.Abs(sphere.y),
        Mathf.Abs(sphere.z));

    bool a = f.y >= f.x && f.y >= f.z;
    bool b = f.x >= f.z;


    Vector3 result;

    if (a)
    {
        result = cubify( new Vector3(sphere.x,sphere.z,sphere.y));
        result = new Vector3(result.x,result.z,result.y);
    }
    else if (b)
    {
        result = cubify( new Vector3(sphere.y,sphere.z,sphere.x));
        result = new Vector3(result.z,result.x,result.y);
    }
    else
    {
        result = cubify(sphere);
    }

    return result;
}

Logged

Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic