Jump to content
  • Advertisement
Sign in to follow this  
abadosa

Unity Planet and inhabitants

This topic is 3082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all!

I've joined this amazing community to ask a question. I'm not sure wether is it posted in the correct section or not, so sorry if it's wrong.

I have programmed my own personal OpenGL 3D engine in C++ and now I have some population (little balls, for example) walking freely in a 3D plane surface. What I need is to adapt this system into a "real" planet, which it means that the inhabitants (whatever they be) must move in the spherical environment.

I've no idea of what I should do to achieve this. Supposing that I know the radius of the planet, which doesn't rotate, and it also has an attached collision model, what could be the best way to mantain the inhabitants on the surface (like in Mario Galaxy)?

Any idea would be very apreciate! Thank you and excuse my english (I'm spanish).

Share this post


Link to post
Share on other sites
Advertisement
I suppose you're aiming for a newtonian model. With your average shooter, you have the same gravity vector for every object, probably (0, -9.81, 0), depending on which axis is your top-down axis. If you want your objects to "fall" towards the center of the sphere, then you need to calculate the gravity vector yourself, which depends on the position of both objects involved:

*edit*
I thought I'd explain those variables:
-G is the gravitational constant that you can tweak until you get visually pleasing results
-m1, m2 are the masses of both objects involved
-r² is the squared distance of the center of masses of both objects
-You can ignore e in your case

You can of course find a pleasing value for G * m1 * m2 alltogether, if you like.

Since you want your spheres to "move" along the surface as well, you simply need to move them along one (ie. any) vector that is right angled to the gravity vector you calculated above.
If you keep your increments/timesteps small enough, it will appear as if those spheres are moving along the surface.

Share this post


Link to post
Share on other sites
Good answer!

However, it seems too much complex to me and I'm probably leaving it. Can't figure out how to find this vectors and my entities in the project don't have masses. Introducing gravity will probably get me crazy.

Thank you anyway!

EDIT: Thanks for the "edit"... I'm looking at it now!

The difficult part for me is to decide which are the initial positions for the inhabitants on the planet... just to calculate the r value. Then I'd need to specify the limits for the balloons not to cross the surface, etc. Finally, I would have to operate with the Force(r) result within the whole process.

Share this post


Link to post
Share on other sites
Perhaps the first thing to decide is whether you want to use spherical coordinates for each "inhabitant," or Cartesian coordinates.

Spherical coords may be easier in some respects to position each inhabitant. Neither coord system will be a lot of fun as you'll probably have to convert back and forth for the purposes of moving them and drawing them.

E.g., each inhabitant could have 2 angles (equivalent to latitude and longitude) and a distance from the center of the sphere. The distance isn't necessary if they'll always be on the surface (not burrowing or jumping).

Movement would then be just a small change in one or both the angles. To draw them, however, you'll have to convert those angles (using the radius) to Cartesian coords. Collisions could be detected by comparing either angles or Cartesian coordinates for some small delta between them.

Is that the sort of thing you're asking about?

Share this post


Link to post
Share on other sites
You may use a spherical coordinate system to specify the position of your inhabitants on a sphere. A spherical coordinate consists of 2 angles and a radius: (r, a, b). You can think of 'a' being the latitude and 'b' being the longitude.
Assuming you use radians to use describe angels: -pi/2 <= a <= pi/2 and -pi <= b <= pi. You can easily use a random number generator (boost::random) to come up with values for both angles. The radius you want to use here consists of the radius of the planet itself and the bounding radius of your inhabitants: r = r_planet + r_bounding_inhabitant
Spherical coordinates alone are no use for you, since OpenGL uses a cartesian coordinate system. However it's quite easy to obtain the latter:

x = r * sin(a) * cos(b)
y = r * cos(a)
z = r * sin(a) * sin(b)

(Assuming y is your up-axis, but that should be the case)

About the masses, there's no need for them. You may use G * m1 * m2 = 1, just as an example, though I'm pretty sure you will need to experiment with this factor until the balls "fall" with an acceptable acceleration.

However since you're not looking for such a "detailed" model you may do something else. Simply make your inhabitants change their spherical coordinates over time by a constant factor. An inhabitant may increase both angles by a certain delta (a_delta, b_delta) each timestep. All you need to do is to calculate the cartesian coordinates each timestep as well, in order to render the inhabitants:

class inhabitant
{
spherical_coordinate coord;
spherical_coordinate speed;
vector3 pos;

inhabitant()
{
boost::mt19937 rng;
boost::uniform_float<> latitude(-pi/2,pi/2);
boost::uniform_float<> longitude(-pi,pi);
boost::variate_generator<boost::mt19937&, boost::uniform_float<> >
lat(rng, latitude);
boost::variate_generator<boost::mt19937&, boost::uniform_float<> > lon(rng, longitude);

coord.a = lat();
coord.b = lon();
coord.r = 10;

// I am kind of lazy right now, but must now do the same thing for
// the speed, although the distribution should be much smaller, otherwise
// your inhabitants move ultra fast
speed.a = speed_lat();
speed.b = speed_lon();
}

void update(float dt)
{
coord.a += speed.a * dt;
coord.b += speed.b * dt;

pos.x = coord.r * std::sin(coord.a) * std::cos(coord.b);
pos.y = coord.r * std::cos(coord.a);
pos.z = coord.r * std::sin(coord.a) * std::sin(coord.b);
}
}




*edit* Who dared to ninja me :P

Share this post


Link to post
Share on other sites
I tried the random initial positions part... and it works!!!

Now I go with the recalc and move. Thank you very much to everybody!

Share this post


Link to post
Share on other sites
Personally, I'd avoid the use of spherical coordinates for this; I'd recommend sticking with Cartesian.

Here is a recent thread on the same topic that may give you some ideas. (Be sure to check out the Gamasutra article.)

Share this post


Link to post
Share on other sites
The problem now is that I want to move the inhabitants on the surface of the planet but not in terms of spherical coordinates. To put it simply, I need to move and rotate these entities in a "local way" (like you would do in a plane) without thinking in angles, latitudes or longitudes.

The matter with the spherical coordinates is that I can't make my inhabitants trace curves (I can only move them in the four directions plus in diagonal) and it's impossible to keep them facing their front.

Maybe I'm not explaining well enough. This is a representation of what I want:

HERE

Share this post


Link to post
Share on other sites
Quote:
Original post by abadosa
The matter with the spherical coordinates is that I can't make my inhabitants trace curves (I can only move them in the four directions plus in diagonal) and it's impossible to keep them facing their front.


It's not impossible. In order to make them move in curve you simply need to change the velocity vector over time. The example I gave you above used a constant velocity but can simply use any formula you want to make the velocity change.
Just as an example, the following should make your inhabitants go in circles (unless I'm not thinking straight again, it's hot here):

void update(float dt)
{
spherical_coord speed;
speed.a = sin(dt);
speed.b = cos(dt);

pos += speed;
}


It's neither impossible to make them facing their front. Given their current position and their next position (ie. 'pos' and 'pos+ speed') you simply need to transform both into the cartesian coordinate system:

vector3 curr_pos = transform(pos);
vector3 next_pos = transform(pos + speed);

Then you can use those 2 vectors to describe the rotation of your inhabitants:

vector3 planet_center = ...;
vector3 facing = next_pos - curr_pos; // This is where they look
vector3 up = curr_pos - planet_center; // This is their local up-vector
vector3 side = cross(facing, up); // This is their side-vector
matrix3x3 rotation = matrix3x3(norm(facing), norm(up), norm(side)); // Those 3 vectors already describe their rotation, simply build a matrix from them

Share this post


Link to post
Share on other sites
Oops, I can't get it to work...

I think the method proposed by jik could be a good choice. Using spherical coords when I need to work with cartesian seems to me too much tricky.

I never used vectors like forward, yaw, etc. but I'm using matrix transformations instead. This may be the reason I can't understand your last indications.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!