Search Unity

Centrifugal force physics - Gravity

Discussion in 'Scripting' started by Zer0Naught, Feb 5, 2013.

  1. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Hello guys,

    Me and a friend are having the issue of trying to recreate centrifugal force in a space scene. For those who don't know, it's a way to create artificial gravity in space, by spinning a certain section of the ship it creates an outward force. This force is artificial gravity and will keep you on the floor of the ship. Check it out.
    The section of the model would have to spin also, as the rest of the shop will be still and zero-g.

    So to recap: Need to simulate centrifugal force somehow with the craft spinning.

    My friend will no doubt join the discussion if need be.
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I doubt it'll happen automatically in the forced physics we have. Instead, you should create a zone along the 'floor' that orients things inside it toward the 'floor' and applies a force that pushes them towards it. It's easiest if you have a series of flat planes like that, but it could also be done with a giant circle... Just harder. (Well, not *that* hard. You just use the center of the circle as 'up'.)
     
  3. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    Hello I am the coder. The OP also wants the thing to be spinning so I have to be attached to the floor and still be able to walk in every direction.
     
  4. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Hmmm, I think the planes idea is a pretty good one. So do you think there is a way to script centrifugal force, or just mimic it?
     
  5. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    I have made it attract objects to the floor and just in general. It works great for everything BUT the player.
    Any ideas why it won't work?
     
  6. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    Are you using rigidbody for everything except for the player which is using a CharacterController?
     
  7. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    Nope I am using Ridgidbody for everything I want to be attracted and also have tried it without it.
     
  8. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    The problem here is that centrifugal force doesn't really exist at all, it just appears to when your frame of reference is rotating. Therefore trying to apply additional forces inside a static reference frame to simulate centrifugal like effects is just wrong. If you spin your craft within a static reference frame then objects within the craft should appear to be effected by centrifugal effects naturaly without the application of additional forces. This might not be exactly the case within Unity however since the physics simulation isn't perfect.

    One way you could deal with this is to move your reference frame to a rotational one which matches the spinning of the craft when you are inside the craft. Therefore the craft itself and objects within the rotational frame would actualy be static, and you would rotate objects which exist in the static frame in the opposite direction (for example the spine/axis of the craft which the outer walls spin around). In this reference frame it is now correct to apply additional forces to your objects to simulate cetrifugal effects. Therefore you would apply a centrifugal force to each object in the rotational frame in a direction which points directly away from the central axis of the rotational frame (ei, the spine of the craft). Note however that in a rotational frame gravity would no longer act in a constant direction, but since your game is set in space this wont be a problem since you would just set gravity to zero anyway.
     
    Last edited: Feb 6, 2013
  9. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    The issue is that we can't get our 'character' (rigid body) to work with these forces, though normal rigid bodies work.

    Leon (gartleon) downloaded the package from this forum, which is what we're using: http://forum.unity3d.com/threads/8873-Faux-Gravity-making-my-brain-spin...-Help!
    He's recently managed to get it working, but everything fell through the floor, because of the forces. We're very confused!

    Edit:

    Cheers nullstar, I did have the suspicion of doing it that way and it may have to be just that. I suppose the only issue is, the ship will be spinning (static part) and the character won't be, or is this easy to rectify?

    Also, our problem of the camera not acting like a rigid body, is still present.
     
    Last edited: Feb 6, 2013
  10. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    That's not really a problem. If the charatcer can transition from the spine of the ship to the outer walls of the ship in one continuous scene, then you would need to switch your games reference frame in realtime to match which reference frame of the surface the character is currently standing on belongs to. It wouldn't be the easiest thing to do but its not impossible.
     
    Last edited: Feb 6, 2013
  11. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    All but the rotating part of the ship would have zero-g. So the character wouldn't be standing on anything when it comes to the static part of the ship (which would in fact be rotating).

    It would be great to get this all working at the same time, but I'm beginning to think that a simple loading screen might solve this. A door/transition would take you to and from zero-g. It's seems simple enough.
     
  12. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    You could do a continuous transition in a single rotational reference frame with a few fudges. For example:

    - When the level starts the player is in the spine of the ship. The player and camera are given an initial opposite rotation around the ships axis so it appears that the outer walls are spinning and the ships spine is stationary. Additionaly the player will keep track of a parameter which scales the magnitude of centripetal force applied to the player. Initially this force parameter is set to zero so that the player freely floats around in space.

    - When the player touches the outer walls of the ship, the player's rotation around the ships axis is cancelled out and the players force parameter is set to one. Now it will appear that the player sticks to the outer wall and that the ships central axis is spinning instead.

    - If the player gets high enough off the surface of the outer walls then you can gradualy decrease the players force parameter and slowly adjust the players rotation around the central axis to match that of the ships spine in order to make the transition back to the center zero-g state.
     
  13. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    So that method seems do-able, but that means the character will have a constant spin on it and no doubt we'll run into a lot of glitches.
    How about what we originally wanted?

    Is there a way to walk along the ring (outer walls, as you called it) with seemingly normal gravity, and have it spin? Ignoring the fact that centrifugal force won't work, as we cannot simulate that. We can mimic it however, by setting up these 'zones' that keep us, and the objects, on the floor; but it would all have to rotate. Is that possible?

    If so, we have no idea how to do it.

    Edit: Fun fact, we are also from Liverpool. How odd.
     
    Last edited: Feb 6, 2013
  14. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Yes the character would have a constant rotation around the central ship axis when in the zero-g floating state. This could set up easily by parenting the character to another gameobject located at the central axis which you would spin. When the character is in contact with the outer walls then you simply stop rotating the parent gameobject. I dont see why this would cause any glitches, it would certainly be less of a headache than trying to get Unity physics to behave itself on the surface of a giant rotating ring. I think because of the way unity physics works that this would require your character to be a character controller rather than a physics rigidbody, but that's the best way to control characters anyway.

    As for walking along the outer wall with seemingly normal gravity, that's exactly what the centrifugal force does for you. You'd still have to adjust your characters up vector to be aligned with the surface normal however.
     
    Last edited: Feb 6, 2013
  15. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    A loading screen and separate areas would be the easiest way, yes.

    As for switching areas directly, remember that until the person grabs onto the spinning part of the ship, they'll still be zero-g, with the ship spinning around them. Centrifugal force only takes effect once they're on the ground and attached to it. I suppose the natural flow of air might be enough to get them started in that direction, but it would be very slow to happen that way. Slamming into a bulkhead would also help anchor them.
     
  16. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Thanks for all the help so far guys. I think it's safe to say that a loading screen would be best for this. Plus making the "static" parts of the ship spin instead.

    That said, the problem of our character, is still an issue. We cannot get the character to stay on the ring walkway. We've tried using the Unity Mario Galaxy scripts and just put a negative value on it instead, but it doesn't work. It effects objects, but not us. (Yes the character is also a rigid body)

    Would you be able to explain how to do it, maybe show a little code?
    Or an example of the 'zones' that were mentioned?
     
    Last edited: Feb 7, 2013
  17. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    If your character is a rigidbody, and it works for other rigidbodies but just not your character, then by elimination the problem has to be due to a script you have attached to your character and not your other objects. Post up your character scripts and your centrifugal force / fake gravity script and I can take a look to see if there's anything obviously wrong. Failing that you'll just have to crack out your debugger and some elbow grease.
     
  18. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
  19. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    I've only had a quick sweep over the scripts, but if these scripts work for your objects and just not for your character, then like I said earlier the problem is most likely due to a script you have attached to your character which isn't also attached to your objects. Try removing all scripts from your character excpet for the faux gravity script and see if it sticks. If so then re-add your other character scripts one by one until it stops working. Then you'll know where your problem lies.

    Edit: Also ensure that the rigidbody attached to your character is set to not use gravity and isn't set to kinematic. These things would likely prevent the faux gravity script from working properly.
     
    Last edited: Feb 7, 2013
  20. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    I have it working now with the scripts attached but now I can't move. D:
     
  21. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Leon, did you do what nullstar had suggested? It's hard to tell with your last message.
     
    Last edited: Feb 8, 2013
  22. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    I turned off use gravity. Now I can't move it keeps me in the center :O
     
  23. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Did you apply the scripts one by to see where the problem might lay?

    I'm sure it's just one little thing that we need to change and BAM.
     
  24. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    Yep I did. D:
     
  25. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186


    Gartleon, if you want to be the programmer then at some point you're gonna have to learn how to analyse code and debug scripts for yourself in order to identify the cause of issues. Crack out your debugger and step through the code line by line whilst the game is running, observe the values of variables and how they are effected with each line of code, and ultimately use this information to identify where the results generated by the code differ from the results you expect to see. Once you've identified the source of the problems, and if a solution to the problems don't occur to you by yourself, then that is the appropriate time to ask for help from others with a well worded question describing the nature of the problem. Until you begin to learn how to do these things then there's very little anyone here can do to help you short of writing the whole game for you.

    However, just this one last time, I have a hunch what your problem might be (although obviously I cant be sure since I'm only acting upon the very limited information you've given). Look at how the faux gravity is applied:

    I suspect that the force being calculated is zero (you could easily verify this by debugging the code ;) ). If this is the case then either your rigidbody mass is zero (which could be the case if its also kinematic), or you are spawning both your ring and character at the origin (position 0,0,0) or at least at the same position, thus the gravityUp vector is zero -> from the unity docs - "If this vector is too small to be normalized it will be set to zero. ".

    I also anticipate in the future that the standard character controller script that comes supplied with unity isn't going to suit your needs if that's what you're using, and that you're going to have to progam your own custom controller script. That script wasn't intended for characters which can walk up walls or around giant rings.

    Anyway this is the last time I'll help you in this way. If you put in the effort yourself to debug and analyse and end up with more specific questions to ask than "help, it doesn't work" then that's a different matter.
     
    Last edited: Feb 8, 2013
  26. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    In Leon's defence, he's dyslexic and has a little trouble explaining things, which was mainly the reason why I posted this up. He's a pretty good coder and USUALLY debugs and such; though he's working on something else right now, so no doubt his focus isn't fully on this. But who knows! Maybe he is just being a noob :p Haha.

    Chop chop Leon!
     
  27. gartleon

    gartleon

    Joined:
    Dec 18, 2012
    Posts:
    8
    I have tried debugging and the gravity does work but it works so well that I won't let me move once it has dragged me in. I am not using Unity controller I am using a custom coded script as you said which has its own gravity which I have deactivated for this and messed around with but no outcome to allow me to move.

    EDIT: The thing is the gravity works so well I will shoot to it then I won't be able to move due to it being so strong I lessened and re positioned it but when I within the parameters it won't let me move.
     
    Last edited: Feb 8, 2013
  28. Zer0Naught

    Zer0Naught

    Joined:
    Feb 5, 2013
    Posts:
    15
    Here's a quick one: According to Leon the RayCast has to be within camera sight. So you have to look at it to be able to cast to it... Or something. So in that sense, would you have to look at the floor at least once for the normals to project your direction of gravity? Or does it work differently.