Thursday, October 4, 2012

Cookbook: Moving actors in UDK using physics

Imagine you have your ball actor and you want to control it with input keys. You want it to roll in some direction if the key is pressed. Very easy way to achieve this on rigid bodies is by utilizing physics.

You only need to have your actor PrimitiveComponent in PHYS_RigidBody state:

   1:  //========================================================================
   2:  defaultProperties
   3:  {   
   4:      Begin Object Name=StaticMeshComponent0
   5:          StaticMesh=StaticMesh'BallPuzzlerGame.Actors.PlayerBall'
   6:          bNotifyRigidBodyCollision=false
   7:          CastShadow=true
   8:          BlockRigidBody=true
   9:          bCastDynamicShadow=true
  10:          HiddenGame=false
  11:          LightEnvironment=MyLightEnvironment
  12:      End Object
  13:      Components.Add(StaticMeshComponent0)
  15:      Physics=PHYS_RigidBody //set physics!
  16:  }

Then there are multiple possibilities how to move it using impulses/force, etc. (check PrimitiveComponent documentation for other possibilities):

   1:  native final function AddImpulse(vector Impulse, ...);
   3:  native final function AddForce(vector Force, ...);

And of course, teleporting (useful for resetting to some initial position):

   1:  native final function SetRBPosition(vector NewPos, ...);

Let's see it in some example. I'm moving my ball-like actor using AddImpulse, applying custom gravity using AddForce and can interact (pick up, move, put down, throw) using SetRBPosition (overriding original one, while being held) and AddImpulse when throwing:

   1:  // Use the player's input to move character
   2:  simulated function AddInputForce(float DeltaTime, vector MovementDir)
   3:  {
   4:      StaticMeshComponent.AddImpulse(MovementDir * DeltaTime * Properties.Speed);
   5:  }
   7:  //apply gravity in custom direction
   8:  simulated function GravityForce( float DeltaTime )
   9:  {
  10:       StaticMeshComponent.AddForce(GravityVector*Properties.Gravity);
  11:  }
  13:  //handle being locked
  14:  simulated event Tick(Float DT)
  15:  {
  16:      //we are locked, follow owner, override default behaviour
  17:      if (lockInstigator != none)
  18:      {
  19:          StaticMeshComponent.SetRBPosition(lockInstigator.Location + lockTranslation);
  20:      }
  21:      else
  22:      {
  23:          super.Tick(DT); //basic behavior
  24:      }
  25:  }

Here is a video showing all this in motion:


  1. Dude can you post more about that? Thank :)

  2. Thanks for the tutorial, it would be perfect if you can finnish the code tutorial,
    For noob developer it is not so clear, how would this code be used ?
    Where do the missing parameters are declared,
    MovementDir ?
    Properties.speed ?
    lockInstigator ?

    Thank you

    1. Hi, Moshe!

      This tutorial is not really meant as copy&paste and run thing. It's supposed to be a short introduction into how to do particular thing I've found interesting for others.
      Anyway it's always good to see some simple running game code (for example sidescroller like and try to change a few things to get to understand what is what...

      But to answer your questions:
      Properties.Speed is an artist editable property (
      MovementDir is a function parameter, I'm using vector between my ball-like actor and the object it's grabbing (thus throwing it away from actor)
      lockInstigator is a reference to ball-like actor set when object is grabbed by actor, to be able to override default movement handling (if grabbed, object follows it's instigator = ball)

      Hope that helps!

    2. Hi Boris,
      Thank you for replay :)

      I did try the sidescroller and meny more tutorials i could find,
      What i really try to learn for long time is How to control a kactor ball with keyboard,
      I did try extending utpawn, with animation of the ball roll movment.
      Also i try to follow the "Whizzle" code.

      Thank you anyway :)