April 21, 2013

[Code] Custom Gravity on Props (Kactor)

Some people ask me often how I released this effect :

I use a inherited class from kActors to achieve this. Then inside a custom tick you simply push a props with a gravity effect. That’s why in the video when I grab a barrel with the gravity gun, the barrel is shifted. This code is old, something like 2 years old, and I never updated it after I posted the video above. Still, it might interest some guys out there. πŸ™‚

The gravity direction is a vector that was saved inside the PlayerController class. It’s not the best idea. If I had to redo this I would create a custom gravity actor used to define a direction (something like an arrow in the editor), then every props with custom gravity would check to get the direction of the nearest gravity direction actor. You could then create dynamic path. It’s just an idea, I never tried it.

Also, the current code is not very optimized. You have to give a strong gravity effect to counteract the default gravity of the UDK. It’s might be best to put the KActor in PHYS_Flying physics mode and calculate manually a custom gravity direction. Maybe… As above I never tried this solution.

class KgravityActor extends KActor

var bool bDoGravity;
var vector GravityDirection, NormGravity;
var float Mass;
var float tickUpdate;

var PlayerController PC;

simulated event PostBeginPlay()
    Mass = StaticMeshComponent.BodyInstance.GetBodyMass();
    NormGravity = vect(0,0,0);
    GravityDirection = vect(0,-1000,0);
    NormGravity.z = GetNormGravityZ();


    bDoGravity = true;

function float GetNormGravityZ()
    local float GNGZ;

    GNGZ = Mass * 512;

    return GNGZ;

simulated event Tick(float DT)

    local EXILAcrobaticPlayerController exaPC;
    local EXILPlayerController exPC;

    if (bDoGravity)
        if(tickUpdate <= 0) 
            ForEach LocalPlayerControllers(class'PlayerController', PC)
                exPC = EXILPlayerController(PC);
                exaPC = EXILAcrobaticPlayerController(exPC);

                GravityDirection = exaPC.Kgravity;

            tickUpdate = default.tickUpdate;
            //`Log("Gravity update = " @ exaPC.gravity);
            tickUpdate -= DT;

        DoGravity(DT); //update gravity


function DoGravity(float DT)
    StaticMeshComponent.AddImpulse((NormGravity+GravityDirection) * DT);

    bDoGravity = true;
    Mass = 10;

    tickUpdate = 2.0f //in seconds

    //KActor properties