Announcement

Collapse
No announcement yet.

Tegleg KCar (alternative to SVehicle)

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

    Tegleg KCar (alternative to SVehicle)

    hello

    So....
    the cars are broken for ios, thanks for that Epic, so i needed a quick and dirty alternative to test some things in my game.
    also it didnt need to be too awesome as its only a toy rc car and not really part of the main game.

    it might be something that other people could benefit from until the real cars are fixed (at some point maybe, fingers crossed)
    or you could go all out and develop it into something amazing if you want.

    i used ITouchable interface to take control of it, if you dont need that take out "implements(ITouchable)" from the
    a vid

    *updated script (slightly different from the download)
    Code:
    /////////////////////////////
    // simple rigid body car   //
    // tegleg                  //
    // http://tegleg.co.uk     //
    /////////////////////////////
    class Tegleg_KCar extends KActorSpawnable implements(ITouchable)
    ClassGroup(Teg)
    placeable;
    
    var() StaticMeshComponent MeshComponent; //collision mesh, hidden in game
    var() SkeletalMeshComponent MyMesh; //car skeletal mesh, just used for visuals
    var float MyMass;    //rigid body mass
    
    //animation variables, visual only
    var() float WheelSpeed; //rotation speed of wheels
    var() float SteerSpeed; //how fast the front wheels steer to full lock
    var() float FullSteering; //steering full lock
    
    //skel control nodes - anims/wheel rotation
    var UDKSkelControl_SpinControl FWheelSpin, BWheelSpin;
    var UDKSkelControl_Rotate WheelTurn;
    
    //force variables, change to suit the physics mass of your car
    var() float Power; //engine power
    var() float TurnPower; //steering rotation force
    var() float Grip; //a reverse force so stop sideways motion while turning (temp and not ace)
    
    
    //call this from your player controller or whatever
    // InFwd - throttle
    // InTurn - steering
    // InFly -  input for flying, unused
    //DeltaTime - DeltaTime
    //same order as in PC.PlayerMove()
    function ToyMove(float InFwd, float InTurn, float InFly, float DeltaTime)
    {
        //movement logic
        local vector RotationForce;
        local vector Target;
        local float TurnForce;
    
        MyMass = MeshComponent.BodyInstance.GetBodyMass();
    
        //animate wheels
        RotateWheels(InFwd, InTurn);
    
        if (InFwd<0){InTurn = -InTurn;} //backwards
    
        //steering force
        TurnForce = InTurn * DeltaTime * 100;
        RotationForce = TurnForce * vect(0,0,1);
        RotationForce *= (MeshComponent.BodyInstance.GetBodyMass()*TurnPowe  r);
        MeshComponent.AddTorque(RotationForce);
    
        //engine force
        //get direction
        Target = Normal(vector(Rotation));
        //add forward force
        Target *= (MyMass*InFwd*Power* DeltaTime); //added * DeltaTime here
        //add external forces, drag slip ect...
        //Target += GetForces();
    
        MeshComponent.AddForce(Target);
    
    }
    
    //reference named skel controls in AnimTree
    simulated event PostInitAnimTree(SkeletalMeshComponent SkelComp)
    {
    	FWheelSpin = UDKSkelControl_SpinControl(SkelComp.FindSkelContro  l('FWheelRotation'));
    	BWheelSpin = UDKSkelControl_SpinControl(SkelComp.FindSkelContro  l('BWheelRotation'));
    
    	WheelTurn = UDKSkelControl_Rotate(SkelComp.FindSkelControl('St  eering'));
    
    	`Log("post init anims "$WheelTurn$" "$FWheelSpin$" "$BWheelSpin);
    }
    
    function RotateWheels(float Throttle, float Steering)
    {
       local vector Speed; //added this, rotate wheels depending on car velocity
    
    //steering
      if (WheelTurn != None)
      {
          WheelTurn.DesiredBoneRotation.Yaw = FullSteering*Steering;
          WheelTurn.DesiredBoneRotationRate.Yaw = SteerSpeed;
      }
      //rotation, //front and back wheels seperated for future messing, realistic rotation speeds from velocity, throttle, handbrake, wheelspin ect
      if (FWheelSpin  != None && BWheelSpin != None)
      {
         //add velocity into the mix
         Speed = Velocity * Normal(vector(Rotation)).X ;
         //WheelSpeed = Speed.X;
         FWheelSpin.DegreesPerSecond = WheelSpeed * Speed.X;
         BWheelSpin.DegreesPerSecond = WheelSpeed * Speed.X;
      }
    }
    
    //work out any extra sideways forces ect
    function vector GetForces()
    {
        local vector  AntiForce;
        //temp and not ace example
       AntiForce = -Velocity*Grip;
    
        return AntiForce;
    }
    
    
    function OnTouch(ETouchType Type, float X, float Y)
    {
      WorldInfo.Game.Broadcast(self, "Play!");
        //take over control of this toy on touch
        //in my PlayerController
        //var Tegleg_KCar Toy;
        //state PlayerWalking - PlayerMove()
        /*
           if (bToyActive && Toy != None)
    		{
    		  //toy control...
    		  Toy.ToyMove(PlayerInput.aForward, PlayerInput.aTurn, PlayerInput.aUp, DeltaTime);
              return; //exit this function before pawn gets moved
            }
        */
    
        //cast to my player controller and give it a ref to this actor and set bToyActive to take control
       // FootGardeningPlayerController(GetALocalPlayerContr  oller()).Toy = self;
       // FootGardeningPlayerController(GetALocalPlayerContr  oller()).bToyActive = true;
    }
    
    simulated event PostBeginPlay()
    {
        Super.PostBeginPlay();
        //set and wake up physics
        SetPhysics(PHYS_RigidBody);
        MeshComponent.InitRBPhys();
        MeshComponent.WakeRigidBody();
    
        //init animtree
        PostInitAnimTree(Mymesh);
    }
    
    
    
    DefaultProperties
    {
        //physics/collision mesh, hidden in game
        Begin Object Name=StaticMeshComponent0
            StaticMesh=StaticMesh'KCar_Pack.basic_cube'
            CollideActors = true
            BlockActors = true
            BlockRigidBody = true
            RBDominanceGroup = 15
            bNotifyRigidBodyCollision = true
            ScriptRigidBodyCollisionThreshold = 0.01;
            HiddenGame=True
        End Object
        Components.Add(StaticMeshComponent0)
        MeshComponent = StaticMeshComponent0
        CollisionComponent = StaticMeshComponent0
    
        //car skeletal mesh, just for visuals
        Begin Object class=SkeletalMeshComponent name=DisplayMesh
         SkeletalMesh=SkeletalMesh'KCar_Pack.faricarpsk'
         AnimTreeTemplate=AnimTree'KCar_Pack.KCar_AnimTree'
        End Object
        MyMesh=DisplayMesh
        Components.Add(DisplayMesh)
    
        bNoEncroachCheck=False
        BlockRigidBody = true
    
        //engine and steering forces
        Power=0.03
        TurnPower=0.15
        Grip=0.1
    
        //animation speeds
        WheelSpeed=2000
        SteerSpeed=50000.000000
        FullSteering=16
    }
    (the forum added random spaces in the code, watch out for that if you copy/paste from here)

    the animtree


    download the script and content here (Aug 2014 udk or later)

    Have Fun!

    #2
    You amaze me with what you are able to come up with tegleg.

    Steven

    Comment

    Working...
    X