Announcement

Collapse
No announcement yet.

Dynamically Change the Mass of a KActor In Game

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

    Dynamically Change the Mass of a KActor In Game

    Dear Everyone,

    I am happy to report that I've learned how to dynamically change the physical mass property of a KActor while in game.

    I am serious folks, I've actually had my KActor Jump, and while in mid-air, pressed a button, changed its mass higher, watched it fall, then found it much harder to move using my WASD setup.

    Then, I pressed another key, and the KActor was back to its normal self again!

    I've repeated this several times in same gaming session, it works fabulously!

    ~~~

    Dynamically Change Mass of KActor In Game

    Here's the function you can add to your custom KActor class

    Code:
    function setMass(float newMass) {
    	
    	local RB_BodySetup rbs;
            
            //invalid values
            if(newMass <= 0) return;
    	
    	rbs = new Class'RB_BodySetup';
    	
    	rbs.MassScale = newMass;
    	
    	( Self.StaticMeshComponent.GetRootBodyInstance() ).UpdateMassProperties(rbs);	
    	
    }
    Things to note:

    1. RB_BodySetup is not a struct, its a class, you have to create an instance using new

    2. There are many properties of RB_BodySetup, MANY. and we are only changing a single one, MassScale

    3. Kactors have this thing called StaticMeshComponent, which is different than pawns and other actors, and applies only to things like DynamicSMActors and KActors that run with physics engine so directly.

    We are first retrieving the RootBodyInstance of the KActor's StaticMeshComponent,

    and then calling update function on this retrieved info.

    I piled the code together to avoid un-needed local variables;


    4. Default MassScale is 1, its relative to the original properties and phys-Material of the KActor and its Mesh that you are working with.

    5. A massScale of 10 is 10 times heavier as far as I can tell (thus the phrasing Scale in MassScale), so you will certainly notice a big shift if you try 10 as a first sample

    6. more realistic change would be more like 1.7 or 2, for 2 times as heavy.

    7. If you want to modify a KActor from elsewhere in your game engine, just change Self to the name of your KActor class instance variable (myHappyKActor.StaticMeshComponent.....)

    8. It Works!!! Yay! Change the Mass of a KActor any time you want!


    I had to sort through a lot of unreal documentation to figure out a working way to do this.

    I hope my research helps you all out!



    I posted this as a thread by itself in case you want to share your own way of doing what I am writing about here, if you think you have a more effective way.


    Joy to you!

    Rama

    #2
    If anyone tries out my code let me know if it works for you!

    I was quite excited to to be able to alter mass at will during the game running time!

    Rama

    Comment


      #3
      Ill definately be using this, thanks!

      Comment


        #4
        Thank you for all of these tutorials, please use the Tutorial tag instead of writing one yourself.

        The tutorial tag helps people find tutorials only when searching the forums.

        Comment


          #5
          This is awesome! I will definitely be using this as well.

          Comment


            #6
            Great idea TheAgent, I updated the tag, will do so for my other posts



            Rama

            Comment


              #7
              Nice - this works for me, but does anyone know how expensive the "new Class'RB_BodySetup'" is?

              I have a situation where I'm updating the mass constantly based on input from the player, and it somehow feels dodgy creating a new instance every update? Should I also be destroying any instances after I've applied the new mass this way, to avoid memory leaks?

              Comment


                #8
                You shouldn't need to create a new body setup each time. However, (been awhile since I worked with this so try it yourself) you probably want to create an initial body setup per actor - you could modify the one bound to the mesh, but that would affect all actors using that mesh at least when they update (the recalculated mass is held in the body instance).

                One word of warning, don't create it in default properties. It's not an actor component so it won't get instanced and will exhibit the same shared behaviour. Create it in PostBeginPlay instead.

                Comment


                  #9
                  Thanks, will give that a try.

                  Comment


                    #10
                    Out of curiosity, why would you want to change an object's mass at run time?

                    Comment


                      #11
                      Honestly, you don't want to know.

                      Comment


                        #12
                        Cool ! will this make that my SpawnablekActors don't fall through floor, terrain ? ?

                        Comment

                        Working...
                        X