Actor Constraints in UDK

First, the code:

simulated event PostBeginPlay()
 local RB_ConstraintActor TwoDConstraint;
 TwoDConstraint = Spawn(class'RB_ConstraintActorSpawnable', self, '', Location, rot(0,0,0));
 TwoDConstraint.ConstraintSetup.LinearYSetup.bLimited = 0;
 TwoDConstraint.ConstraintSetup.LinearXSetup.bLimited = 0;
 TwoDConstraint.InitConstraint(self, None);

This would go in any class that is either a rigid body (such as a KActor) or implements rigid body physics (such as a Pawn with PHYS_RigidBody enabled). What it does is initialize a rigid body constraint which — you guess it — constrains the movement of the actor.

By default movement in all directions is constrained. We change that by setting Linear[Dimension]Setup.bLimited to zero. For example, to allow movement in the Z-direction (up/down), you’d do TwoDConstraint.ConstraintSetup.LinearZSetup.bLimited = 0; There are many other things you can do with the variables in the ConstraintSetup class, but I’ll leave it to you to explore them.

An interesting consequence of this is that we can effectively “shut off” gravity by constraining movement on the Z-axis. The gravity is still there, pulling the actor down, but because the actor is not allowed to move from its initial Z-position it is not affected by gravity’s pull. You can then use the AddForce() and AddTorque() methods to simulate your own custom gravity.

Finally, I should note that this code is taken pretty much verbatim from the Whizzle Design Document. Anyone who wants to be a UDK programmer should be required to read this; I learned pretty much all the basics of UnrealScript from its pages. If you’ve never read it before, shame on you. Go do it now.


14 thoughts on “Actor Constraints in UDK

  1. Pingback: Learning UnrealScript Part 5 — Understanding the World | WillyG Productions

  2. WillyG,

    I’ve tried your little trick about constraining the actor on just the Z-axis to “bypass” gravity, but it’s not working for me. I leave the LinearZSetup.bLimited at its default value of 1 and add an impulse to the mesh straight up in the z direction, but he still falls back down to the ground. The actor is a UDKPawn with the physics set to PHYS_RigidBody and I wondered if I missed something. Are there any other tricks you had to play to get it to work?

    • I haven’t tested it out on player pawns, mainly because I was too lazy 😛 But I’d assume there’s a lot more to it than just doing “Physics=PHYS_RigidBody” in the default properties. Try doing this same thing on a vehicle (the Scorpion in the example map will do). Vehicles are pawns, but they also implement rigid body physics by default, and that’s why I know this will work for rigid body pawns. Problem is there’s probably a lot more underlying code to it.

  3. Sorry to bother you again but i really doubt that this is even possible to work like that.

    I have made a class extending from KActorSpawnable, and i tried your code but no matter what i try the constraint is also stopping the addforce, addimpulse etc. meaning that when you constraint in the Z axis it will not move in that axis no matter what you do later on.

    So i’m really confused here, how can you constraint in the Z axis but not constraint? lol

    • Yea, when you make a constraint that means the KActor won’t move in that direction no matter what. My question is, will your game still be 3D or will it be a 2/2.5D sidescroller, etc.?

      Like in Whizzle, the level is actually sideways, they constrain to shut off gravity and then simulate gravity in the horizontal direction, since the camera is top-down it looks like a 2D “floating up” kind of game.

      • Duh! i didn’t even thought that this was for 2d so i assumed it would be working in 3d xD. Anyways im basically trying to make a buoyant KActor at the moment and for that first i would need an effective way to completely “shut off” the default gravity and make a custom gravity. But not much luck so far.
        Oh well, thanks anyway.

    • Haha a buoyant KActor doesn’t need constraints, it needs special buoyancy physics! I know there are some tutorials out there and a few threads on the forums for this thing exactly (Rob Hale, the guy who made Waves, did it…), just search around. But it should work just fine with the default UDK gravity.

  4. Pasting this code into a subclass we have of KActorSpawnable isn’t changing the object’s behavior at all. I’ve been messing with RB_ConstraintInstance as well, and having no luck getting that to alter the object’s behavior either.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s