Creating gravity inside a cylinder. [SOLVED]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
haku2510
Posts: 12
Joined: Thu Apr 09, 2015 5:46 pm

Creating gravity inside a cylinder. [SOLVED]

Post by haku2510 »

Hi, been using the engine for a few months now and have been having a blast.
Recently I have been trying to add the weapons(laser guns) and gravity to the game.
In most instances gravity would be pretty straight forward, except I'm making my game in an O'Neill space colony (a spinning Cylinder) and it lets you go out into space.

The plan was that the characters would be pulled to the outer walls of the cylinder and would be pulled more the further from the center of the cylinder they were.

The problem I am having is that when the gravity takes the suit it starts going from side to side and a bit vertically down, not out to the walls.

Just as a note, Velocity->direction is a vector3df and is += to with the position of the characters to give the effect of inertia,
position is the position of the Colony + the offset to center it on the origin.
Here is the code I have for checking if a character is inside the Colony.

Code: Select all

 
void Colony::checkGravity(Mobile_suit* suit)
{
    vector3df pos = suit->getPosition();
    Velocity* vel = suit->getVelocity();
    f32 X = pos.X;
    f32 Y = pos.Y;
    f32 Z = pos.Z;
    f32 rad = (Z*Z) + (Y*Y);
 
    if((rad) < (radius*radius))
    {
        if(X < (Length)+position.X && X > -(Length)+position.X)
        {
            vector3df grav = vector3df(0,-10,0);
            pos.X = 0;
 
            vector3df vect = pos.getHorizontalAngle();
 
            matrix4 m;
            m.setRotationDegrees(vect);
            m.rotateVect(grav);
 
            vel->direction += grav.setLength(grav.getLength()*((rad/(radius*radius))*frameDeltaTime));
 
            grav = vector3df(0,0,0);
        }
    }
}
 
Thanks in advance!
Last edited by haku2510 on Fri Apr 10, 2015 6:41 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating gravity inside a cylinder.

Post by CuteAlien »

Which of your directions is up? With default camera that would be y - so having a negative y it would make sense it goes down.
And why are your characters pulled to the outer wall anyway? (just curious). Should only happen if they are already in rotation together with the outer ring (because any straight movement will lead outwards). And shouldn't speed up in distance to the center.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
haku2510
Posts: 12
Joined: Thu Apr 09, 2015 5:46 pm

Re: Creating gravity inside a cylinder.

Post by haku2510 »

CuteAlien wrote:Which of your directions is up? With default camera that would be y - so having a negative y it would make sense it goes down.
And why are your characters pulled to the outer wall anyway? (just curious). Should only happen if they are already in rotation together with the outer ring (because any straight movement will lead outwards). And shouldn't speed up in distance to the center.
The idea of an O'Neill space colony is that it spins to create gravity, because it is spinning it pulls everything inside it to the outside of it, like swinging a bucket of water with your arms, while the bucket is above your head the water doesn't fall out because it is being pulled to the outside of the arc (I think).
If you have seen Interstellar, it's that colony at the end, where the kids hit the baseball straight up and it went through a window on the house above them.

Because of that the up direction is at vector3df(current_X_position, 0,0) while inside the colony, outside the Colony there is no "real orientation" in the eyes of the player.

The idea of the gravity working while not on the ground was that the "air" in the colony is moving and is pulling you down. The reason it starts increasing the pull the further you get from the center is that the radius of the swing gets longer and by the time you are on the ground(on the walls) you would be experiencing "normal" gravity.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating gravity inside a cylinder.

Post by CuteAlien »

Yeah, I get the principle. It works because all things want to move a straight line. So things at the outside want to continue the current movement but can't go a straight line because there's a curve and so they press against that.

But it's not a gravity in the sense of things getting faster when they approach the outside. Thought not sure right now what would happen if you were holding yourself to a pole connected to the center which has no friction. You'd slide toward the outside - likely with force increasing linear to distance (not sure - just did a quick rough paper drawing, but velocity increases and tangent stays the same so I guess you speed up and velocity seems to increase linearly - but any physicists are free to correct me). But this only happens until you no longer hold yourself to the pole - then it's like letting lose your buckets of water. As there's no air-resistance your speed stays the same (until the outside is hit). Or maybe there even is air-resistance, then you slow slightly down (and if the station is large enough you might get stuck in air and never reach the outside.. uh oh...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
haku2510
Posts: 12
Joined: Thu Apr 09, 2015 5:46 pm

Re: Creating gravity inside a cylinder.

Post by haku2510 »

Okay, so what you're saying is that with the character not rotating with the cylinder around it's origin, it shouldn't feel the gravity and be pulled out?

To be honest this is something I want in the game regardless of how accurate it actually is, the game is going to be used with a mini motion simulator rig with an Arduino for my final year project (its only like 20cm cubed). I just want the gravity working vaguely to show how it might respond differently in 0 gravity compared to normal gravity. I also want the gravity done in a tricky way just to have more of a talking point in the 30 minute demonstration (Also this was the only 3D model I could get for a space colony!).
Sorry if I'm being a pain!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating gravity inside a cylinder.

Post by CuteAlien »

haku2510 wrote:Okay, so what you're saying is that with the character not rotating with the cylinder around it's origin, it shouldn't feel the gravity and be pulled out?
I'm saying there is no gravity (well, none that is noticable). The rotation of the space-station only gives the feeling of gravity - but no real one. Things still move like they always do in the direction they moved last until they hit something that changes their direction. It's only that at the outside there's a wall in the way so they are pressed against that (any straight line in a circle will hit the outside) - and that feels somwhat (but not exactly) like gravity.

I don't say your game has to use real physics! The kind of acceleration toward the outside could for example be done with magnet fields I guess (and people wearing stuff affected by it). If you care about reality - which you really don't have to for a game.

Just be sure what kind of physics you really want before doing it - makes it easier to think about it and discuss it.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
haku2510
Posts: 12
Joined: Thu Apr 09, 2015 5:46 pm

Re: Creating gravity inside a cylinder.

Post by haku2510 »

I suppose a better phrase would be "artificial gravity generated through inertia".

Technicalities aside, Any clue what may be causing the problem?
I added comments just to show what I think I'm doing, the code works as expected when I'm checking the values in debug, however when it's actually running in the game I get the side to side motion and the vertical down.
If you want some video showing whats happening, I can try and make something quick and stick it on youtube.

Code: Select all

 
void Colony::checkGravity(Mobile_suit* suit)
{
    vector3df pos = suit->getPosition();
    Velocity* vel = suit->getVelocity();
    f32 X = pos.X;
    f32 Y = pos.Y;
    f32 Z = pos.Z;
    f32 rad = (Z*Z) + (Y*Y);
 
    if((rad) < (radius*radius)) //checks if suit is inside the radius of the Colony
    {
        if(X < (Length)+position.X && X > -(Length)+position.X) // checks if the suit is inside the shaft if it's inside the radius
        {
            vector3df grav = vector3df(0,-10,0); //creates a downward force of 10
            pos.X = 0;
 
            vector3df vect = pos.getHorizontalAngle();
 
            // Rotates the downward force to the angle that would push it out from it's center on the X axis
            matrix4 m;
            m.setRotationDegrees(vect);
            m.rotateVect(grav);
 
            //adds the resultant outward force to the Velocity of the suit
            vel->direction += grav.setLength(grav.getLength()*((rad/(radius*radius))*frameDeltaTime));
 
            //included this so I could check the values during Debug, it would exit before showing me the Vel->direction.
            grav = vector3df(0,0,0);
        }
    }
}
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating gravity inside a cylinder.

Post by CuteAlien »

Not really getting what you try to make happen with your grav vector. If you want it just to point away from the center of your station you can do something like (suit->getPosition()-stationNode->getPosition).normalize() to get a vector of length 1 which points from station position to your suit. Then you can scale it by any value. That stuff with getHorizontalAngle expect Y to be up (which is the default with Irrlicht cameras). And is also depending on station sitting exactly at 0,0,0.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Creating gravity inside a cylinder.

Post by thanhle »

Can you draw a free body diagram of what you want to achieve.
Too much text to read : ).

Looks like you are dealing with some sort of Centrifugal or centripetal force. If a person standing on a circular rotating object he will likely be thown off at tangent to the circular path. But that depends on his inertia or force to resist the motion.

Is there any type of friction involve?
Regards
thanh
haku2510
Posts: 12
Joined: Thu Apr 09, 2015 5:46 pm

Re: Creating gravity inside a cylinder.

Post by haku2510 »

CuteAlien got it right on the ball, thank you!
I took out all the matrix stuff and just set the length of pos to 10 to create the desired effect!

Code: Select all

 
void Colony::checkGravity(Mobile_suit* suit)
{
    vector3df pos = suit->getPosition();
    Velocity* vel = suit->getVelocity();
    f32 X = pos.X;
    f32 Y = pos.Y;
    f32 Z = pos.Z;
    f32 rad = (Z*Z) + (Y*Y);
 
    if((rad) < (radius*radius)) //checks if suit is inside the radius of the Colony
    {
        if(X < (Length)+position.X && X > -(Length)+position.X) // checks if the suit is inside the shaft if it's inside the radius
        {
            pos.X = 0;
 
            // normalises the the vector in relation to the origin and sets the length to the gravity facing down.
            vector3df grav = pos.normalize().setLength(10);
 
            //adds the resultant outward force to the Velocity of the unit
            vel->direction += grav.setLength(grav.getLength()*((rad/(radius*radius))*frameDeltaTime));
 
            //included this so I could check the values during Debug, it would exit before showing me the Vel->direction.
            grav = vector3df(0,0,0);
        }
    }
}
Thanhle you pretty much have it, there is no other forces at work besides the force pushing the character to the edges. If I plan on making them match the rotation of the colony I'll be doing it in the character classes.
Post Reply