Announcement

Collapse
No announcement yet.

How to Remove Gravity for a KActor (like a rolling physics ball)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to Remove Gravity for a KActor (like a rolling physics ball)

    Here's how you can turn gravity on or off for a KActor or any RB Physics based object

    I use this code so I can simply press a button to toggle gravity for my rolling WASD controlled KACTOR ball.

    You can do this as many times as you want during gameplay!

    put this code in your KActor class

    Code:
    function gravityOn(){
      ( Self.StaticMeshComponent.GetRootBodyInstance() ).CustomGravityFactor = 1;
    }
    
    function gravityOff(){
      ( Self.StaticMeshComponent.GetRootBodyInstance() ).CustomGravityFactor = 0;
    }

    use this code to turn gravity off for your KActor class at time of game-start:

    Code:
    Simulated Event PostBeginPlay() {
    	
    	super.postbeginplay();
            gravityOff();
    }
    enjoooy!

    (You don't have to use Self but I am using it for reading Clarity)



    Rama

    #2
    Thanks this is very helpful!

    Comment


      #3
      Yay!

      I'm glad you found it useful!



      Rama

      Comment


        #4
        Rama, thanks so mch i have been looking for something like this! also, how could you make a KActor's gravity 0 but have it slow down over time?

        Blake

        Comment


          #5
          you could use a timer, and gradually reduce to 0.



          Code:
          //in the class of object you want to slow gravity for
          
          //0.1 is in seconds
          //true means timer will run every 0.1 seconds, forever....
          //so you must call cleartimer somewheres
          function startGravityReduction(){
             SetTimer(0.1,true, 'slowGravityToZero');
          }
          
          function slowGravityToZero(){
             //end timer
             if(( Self.StaticMeshComponent.GetRootBodyInstance() ).CustomGravityFactor <= 0) {
          
               clearTimer('slowGravityToZero');
             
               //dont let gravity factor become negative
               (Self.StaticMeshComponent.GetRootBodyInstance() ).CustomGravityFactor = 0;
               return;
             }
          
             //reduce gravity
             (Self.StaticMeshComponent.GetRootBodyInstance() ).CustomGravityFactor -= 0.1;
          }


          Rama

          Comment


            #6
            Sorry Rama for being unclear. My mistake I meant how can you slow the objects velocity down to a stop. Almost like it's in water.

            Thanks for your help!

            Blake

            Comment


              #7
              well if you are using a Kactor, you can constantly divide the kactor's velocity by some amount

              This code below only works for actors with PHYS_RigidBody active



              Current exact numbers in code below reduces velocity of Rigid Body / Kactor by 10% every 0.1 seconds until it's linear velocity is below 3 (pretty small, but adjust as you need to)

              Code:
              //in the class of object you want to slow velocity for
              
              //global class var
              var float velocityReductionFactor;
              
              //0.1 is in seconds
              //true means timer will run every 0.1 seconds, forever....
              //so you must call cleartimer somewheres
              function startVelocityReduction(){
                 SetTimer(0.1,true, 'slowVelocityToZero');
              }
              
              //if object leaves the slowing zone
              function cancelSlowVelocity(){
                  clearTimer('slowVelocityToZero');
              }
              
              function slowVelocityToZero(){
                 
                 //end timer
                 //using a threshold like 3 because with current system of dividing by an amount greater than 1
                 //this will never reach 0 = never-ending loop. Adjust threshold to your needs
                 if( StaticMeshComponent.GetRootBodyInstance().GetUnrealWorldVelocity() ) <= 3) {
              
                   clearTimer('slowVelocityToZero');
                 
                   //remove all velocity
                   StaticMeshComponent.SetRBLinearVelocity(vect(0,0,0));
                   StaticMeshComponent.SetRBAngularVelocity(vect(0,0,0));
                   return;
                 }
              
                 //Velocity Reduction
                 StaticMeshComponent.SetRBLinearVelocity(
              	StaticMeshComponent.GetRootBodyInstance().GetUnrealWorldVelocity() /
              	velocityReductionFactor 
                 );
              	
                 //Angular Velocity Reduction
                 StaticMeshComponent.SetRBAngularVelocity(
              	StaticMeshComponent.GetRootBodyInstance().GetUnrealWorldAngularVelocity() /
              	velocityReductionFactor 
                 );
              }
              
              defaultproperties
              {
              velocityReductionFactor = 1.1   //~10% if used as divisor
              }


              Rama

              Comment


                #8
                Originally posted by Blakeanator View Post
                Almost like it's in water.
                Set the damping properties of the physical material.

                Comment


                  #9
                  Spoof's solution is very elegant, as then the physics engine handles all the velocity reduction for you

                  Here's the specific phys material value to edit I believe

                  LinearDampening

                  if you're using a Kactor you can use

                  GetKactorPhysicalMaterial




                  Rama

                  PS: you could set a basic physical material via the UDK or you can use
                  Code:
                  StaticMeshComponent.SetPhysMaterialOverride(physmat)
                  and create your own phys material via unrealscript

                  Code:
                  //class var, this class's physical material
                  //note each instance of the class gets its own
                  //physical material
                  var PhysicalMaterial physmat;
                  
                  Simulated Event PostBeginPlay() {
                    super.postbeginplay();
                    physmat = new class'PhysicalMaterial';
                  }
                  the advantage of making your own physmaterial is that then you don't need to call getkactorphysmaterial

                  and can just access physmat directly



                  Rama

                  Comment

                  Working...
                  X