Results 1 to 16 of 16
  1. #1
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default pushing pulling crates Re: danimals

    Coulnt mesage this because its too long.

    Sure! This code is about to be heavily modified though. but you may be able to pick somethign from it.
    - The main issue with this code 1. moving RigidBodies is not easy as asetting a location 2. if teh crate flips.. it will bug out when the player uses it again. (beacuse of my clunky side detection code)

    This is the crate class. it is abstract so we overload it for particular types of crates
    Code:
    class Crate extends KActor
    abstract;
    
    // Set to true to make the object pushable.
    var(Crate) bool bPushable;
    
    // Set to true to make the object pullable.
    var(Crate) bool bPullable;
    
    var(Crate) CrateHandle CrateHandle0;
    
    // Handle offset.
    var(Crate) Vector HandleOffSet;
    
    
    // 2D Constraint.
    var RB_ConstraintActorSpawnable TwoDWorldConstraint;
    
    simulated event PostBeginPlay()
    {
        super.PostBeginPlay();
    
        SetupWorldConstraint();
    }
    
    function InitConstraint(UTPawn p)
    {
    // Do nothing here. Each subclass will create it's own constraint.
    }
    
    function SetupWorldConstraint()
    {
        //add a contraint to only allow the crate to move in a flat movement.. and not side to side.
    
        TwoDWorldConstraint = Spawn(class'RB_ConstraintActorSpawnable', , '', Location, rot(0,0,0));
        TwoDWorldConstraint.ConstraintSetup.LinearYSetup.bLimited = 0;
        TwoDWorldConstraint.ConstraintSetup.LinearYSetup.LimitSize = 0;
        TwoDWorldConstraint.ConstraintSetup.LinearZSetup.bLimited = 0;
        TwoDWorldConstraint.ConstraintSetup.LinearZSetup.LimitSize = 0;
        TwoDWorldConstraint.ConstraintSetup.LinearXSetup.bLimited = 0;
        TwoDWorldConstraint.ConstraintSetup.LinearXSetup.LimitSize = 0;
        TwoDWorldConstraint.ConstraintSetup.bSwingLimited = true;
        TwoDWorldConstraint.ConstraintSetup.Swing1LimitAngle = 0;
        TwoDWorldConstraint.ConstraintSetup.Swing2LimitAngle = 0;
        TwoDWorldConstraint.ConstraintSetup.bTwistLimited = true;
        TwoDWorldConstraint.ConstraintSetup.TwistLimitAngle = 0;
        TwoDWorldConstraint.InitConstraint( self, None, '','',10000.0);
        
        `log("constraint initialized: "@TwoDWorldConstraint);
    }
    DefaultProperties
    {
    BlockRigidBody=true
    bBlockActors=true
    CollisionType=COLLIDE_BlockAll
    bCollideWorld=True
    bProjTarget=True
    bWakeOnLevelStart=true
    bCanStepUpOn=false
    
    bPushable=true
    bPullable=true
    HandleOffSet=(X=0,Y=64,Z=0)
    
    
    
    Begin Object Class=CrateHandle Name=CrateHandle0
        LinearDamping=1.0
        LinearStiffness=1000000.0
    
        AngularDamping=1.0
        AngularStiffness=1000000.0
    
        LinearStiffnessScale3D=(X=1.0,Y=1.0,Z=1000.0)
        LinearDampingScale3D=(X=1.0,Y=1.0,Z=1000.0)
        End Object
        Components.Add(CrateHandle0)
    }

    and here is the crate handle code
    Code:
    /* CrateHandle.uc written by Emil Levy 2011
       This class provides a method for temporarily attatching a specific rigid body actor
       to the player in such a way that the player seems to be pushing and/or pulling the
       actor.
    
       ver 1    - for prototyping purposes
    */
    
    class CrateHandle extends RB_Handle;
    
    // global vars
    var bool bDebug;
    var Crate CurrentCrate;
    var float height;
    var Rotator rote;
    var vector offset;
    
    // Grab the crate...
    // Specifically attatch a RB_Handle like used with the physics gun.
    // Work out which side of the crate the player has attatched the handle to
    // and lock movement in that local axis.
    function Grab(Crate object, TraceHitInfo hitInfo, vector hitLocation, MyPawn p)
    {
        local Box playerBox, crateBox;
        local float playerHalfDim, crateHalfDim;
        local vector temp, newPawnLoc, hitNormal;
        local float xDir, yDir;
        local float extraOffset;
        local Rotator rotat;
    
      if (p.bIsBlocking)
      {
        return;
      }
        //safety check for a null object
        if (object == none)
        {
        `log("Tried to attach a null object to CrateHandle.");
        return;
        }
    
        //make sure the object is set in line with the world axis.
        // this is only for prototyping purposes.
        //  -- FIX ME
        height = object.Location.z;
        object.SetCollision(false,false,false);
        object.StaticMeshComponent.SetRotation(rot(0,0,0));
        object.SetCollision(true,true,true);
    
        rote = Object.StaticMeshComponent.Rotation;
    
        //find player halfdim
        p.GetComponentsBoundingBox(playerBox);
        object.GetComponentsBoundingBox(crateBox);
    
        playerHalfDim = (playerBox.Max - playerBox.Min).x / 2;
    
        crateHalfDim = (crateBox.Max - crateBox.Min).x / 2;
    
        //work out the player/handle offset
        if(p.IsA('MyPawnSmall'))
        {
           extraOffset = 6;
        }
        else
        {
           extraOffset = 30;
        }
    
        hitNormal = hitLocation - object.Location;
    
        xDir = hitNormal.x;
        yDir = hitNormal.y;
    
        //set the offset of the handle
        offset = vect(0.0, 0.0, 0.0);
        newPawnLoc = p.Location;
        offset.Z = object.Location.Z - p.Location.Z;
        //right
        if(xDir > yDir && xDir > 0 && xDir > (yDir * -1))
        {
        //away
            `log("Right");
            offset.x = extraOffset;
            newPawnLoc.x = object.Location.x + crateHalfDim + playerHalfDim + offset.x;
            newPawnLoc.y = object.Location.y;
        }
        //bottom
        else if(yDir < xDir && yDir < 0 && yDir < (xDir * -1))
        {
        //in
            `log("Bottom");
            offset.y = extraOffset;
            newPawnLoc.y = object.Location.y - crateHalfDim - playerHalfDim - offset.y;
            newPawnLoc.x = object.Location.x;
        }
        //top
        else if(yDir > xDir && yDir > 0 && yDir > (xDir * -1))
        {
        //away
            `log("Top");
            offset.y = extraOffset;
            newPawnLoc.y = object.Location.y + crateHalfDim + playerHalfDim + offset.y;
            newPawnLoc.x = object.Location.x;
        }
        //left
        else if(xDir < yDir && xDir < 0 && xDir < (yDir * -1))
        {
        //in
            `log("left");
            offset.x = extraOffset;
            newPawnLoc.x = object.Location.x - crateHalfDim - playerHalfDim - offset.x;
            newPawnLoc.y = object.Location.y;
        }
        else
        {
            return;
        }
    
        //move the player to the correct position.
    
        // disable collision
        object.SetCollision(false,false,false);
        p.SetLocation(newPawnLoc);
        temp = object.Location;
        temp.z = temp.z+1;
    
        rotat = rotator(object.location - p.location);
        rotat.Pitch = 1;
        p.SetRotation(rotat);
    
        //set the object in the right direction
    
        object.SetRotation(rot(0,0,0));
        object.SetCollision(true,true,true);
        `log("Handle pos: "@self.GetHandleOffset(p));
    
    
        // Init a new handle
        super.GrabComponent(hitInfo.HitComponent, hitInfo.BoneName, self.GetHandleOffset(p), false);
    
        //check that the handle was initiated correctly
        if (GrabbedComponent == none)
        {
        `log("CrateHandle wasn't able to grab Crate:" @ object);
        return;
        }
    
        CurrentCrate = object;
    
        // initiate the physics constraint and lock the crate to its axis.
        //CurrentCrate.InitConstraint(p);
        if (bDebug)
            `log("crate pos: "@CurrentCrate.Location);
    
        // change the players control state
        p.GoToState('pushing');
    
        if (bDebug)
            `log("Handle_Location:" @ Location @ "HitActor_Location:" @ object.Location);
    
        // Set pawn variables
        p.bIsMovingAnObject = true;
    }
    
    //return true if the player has grabbed a crate
    function bool HasObject()
    {
        return CurrentCrate != none && GrabbedComponent != none;
    }
    
    // return the offset between the player and the cratehandle
    function Vector GetHandleOffset(MyPawn p)
    {
        local vector temp;
    
        temp = p.Location + offset;
        temp.Z = height +10;
        return temp;
    }
    
    //This method is to be called from a tick function to constantly update the position of the handle
    // and thereby the crate.  This MUST be in a tick function to produce smooth results.
    function UpdateHandlePosition(MyPawn p)
    {
        if (HasObject())
        {
            //rotate the object so it stays straight.
            CurrentCrate.StaticMeshComponent.SetRBRotation(rote);
    
            // Update handle position
            //GrabbedComponent.WakeRigidBody(GrabbedBoneName);
            super.SetLocation(self.GetHandleOffset(p));
            if (bDebug)
                `log("Handle pos: "@self.GetHandleOffset(p));
            if (bDebug)
                `log("crate pos updated: "@CurrentCrate.Location);
        }
    }
    
    //release the handle on the crate.
    function Release(MyPawn p)
    {
    
        // Release the object
        super.ReleaseComponent();
    
        //terminate the object's physics constraints.
        CurrentCrate.TwoDWorldConstraint.TermConstraint();
        CurrentCrate.SetLocation(CurrentCrate.Location);
        CurrentCrate = none;
    
        // Set pawn variables
        p.GoToState('walking');
        p.bIsMovingAnObject = false;
    }
    
    DefaultProperties
    {
        TickGroup=TG_PreAsyncWork
        bDebug=true
    
        LinearDamping=1.0
        LinearStiffness=1000.0
    
        AngularDamping=1.0
        AngularStiffness=1000.0
    
        LinearStiffnessScale3D=(X=1.0,Y=1.0,Z=1.0)
        LinearDampingScale3D=(X=1.0,Y=1.0,Z=1.0)
    }
    and in the player controller all you have to do is this

    Code:
    function GrabHandle()
    {
        local vector loc, norm, end;
        local TraceHitInfo HitInfo;
        local Actor traceHit;
        local vector startPos;
        local Crate tempCrate;
    
    
    
    
        startPos = ThePlayer.GetPawnViewLocation();
        end = ThePlayer.Location + normal(vector(ThePlayer.Rotation))*100;
        traceHit = worldinfo.trace(loc, norm, end, startPos, false,, HitInfo);
    
        //`log("rotation rate: "@ThePlayer.RotationRate);
        //ClientMessage("trace was fired from "$Location);
    
        if (traceHit == none)
        {
            //ClientMessage("Trace failed to register anything.");
            return;
        }
        //ClientMessage(traceHit);
    
        if(traceHit.IsA('Crate'))
        {
            tempCrate = Crate(traceHit);
            GrabbedCrate.Grab(tempCrate, HitInfo, loc, ThePlayer);
        }
        else
        {
            //ClientMessage("Not a crate");
            return;
        }
    
    
    }
    
    function ReleaseHandle()
    {
        //call the release functionality in the grabbed object.
        GrabbedCrate.Release(ThePlayer);
    }
    
    function PlayerTick(float DeltaTime)
    {
        super.PlayerTick(DeltaTime);
    
        if(ThePlayer.bIsMovingAnObject)
           GrabbedCrate.UpdateHandlePosition(ThePlayer);
    
    ...
    }

    Hope this helps you out a bit

  2. #2
    MSgt. Shooter Person
    Join Date
    Jul 2011
    Posts
    282

    Default

    so this is how to do the push pull create?

  3. #3
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    Thanks for this, I've implemented a push pull system, but was having issues with constraining the object. This will help alot.

  4. #4
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default

    Well use at your own risk as the code states this is concept proof level code (that never got updated).

    I will ofcourse post soem more final code for this when I get around to fixing and finalising this

    -Mega

  5. #5
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    Hey, you use this variable "GrabbedCrate" however it is not populated at any point that I can see.

    How is this variable used? Where is it declared?

  6. #6
    Skaarj
    Join Date
    Jan 2010
    Location
    Indiana
    Posts
    14

    Default

    This is awesome! So, assuming what I've read, this'll allow us to create a push / pull for the character? I do note that you mentioned it wasn't finalized, which is fine. Thanks a lot for the upload, and the information. Great job!

  7. #7
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default

    In the default porpoerties of my player controller

    Code:
     Begin Object Class=CrateHandle Name=newHandle
    
        End Object
        Components.Add(newHandle)
        GrabbedCrate=newHandle
    Quote Originally Posted by myertveho View Post
    Hey, you use this variable "GrabbedCrate" however it is not populated at any point that I can see.

    How is this variable used? Where is it declared?

  8. #8
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    Ah, thanks, that makes sense.

  9. #9
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    I have another issue.

    You are attempting to goto a pushing or walking state in the pawn class, however these states are not previously defined as far as i can tell. Do they need to be for the pawn to be able to to move the object?

    Currently I'm attaching to the object fine, but am unable to move it.

  10. #10
    MSgt. Shooter Person
    Join Date
    Jul 2011
    Posts
    71

    Default

    this is sick meganaut. I'll update this thread with my updates to the code as well.

  11. #11
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    ok, got it working. I can see the issue you are talking about now. I think what would be needed to is to offset everything by the objects rotation, as that is what is definitely causing the issue. Trying to pickup an object that has been rotated in the editor seems to cause significant issue, and i imagine its the same for an object thats upside dow.

    unless of course i misread what the real issue is of course.
    Last edited by myertveho; 10-26-2011 at 12:31 AM.

  12. #12
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default

    Quote Originally Posted by myertveho View Post
    I have another issue.

    You are attempting to goto a pushing or walking state in the pawn class, however these states are not previously defined as far as i can tell. Do they need to be for the pawn to be able to to move the object?

    Currently I'm attaching to the object fine, but am unable to move it.
    Hey man. I had some custom states in my pawn to basically change the movement of the pawn (make him slower) and also originally to make him unable to rotate or strafe. I olet strafing back in for teh game because it was a bit too frustrating without it.

    I think i also handled my animation blending in that part too which allowed me to play pushing and pulling animations on the pawn.. check in the game to see what i mean
    -Mega

  13. #13
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default

    And yes you are right. I think modifying the offset by the rotation of the cube might be the solution too.. i hadnt thought of that tbh
    Thanks for the clue

  14. #14
    MSgt. Shooter Person
    Join Date
    Feb 2011
    Location
    Sydney, Australia
    Posts
    72

    Default

    No problemo,

    I'll be looking into how to do it myself so I'll see what I can do, but i'm a bit busy chasing my tail on other areas of my project. If i crack it i'll let you know.

  15. #15
    MSgt. Shooter Person
    Join Date
    Jul 2011
    Posts
    71

    Default

    hmm, I keep getting errors thrown from MyPlayerController while rebuilding. As of now I'm only getting them in lines referencing ThePlayer. Any ideas?

    Bad or missing expression in '=' for this line "startPos = ThePlayer.GetPawnViewLocation();"
    'GrabbedCrate' : Bad command or expression for this line "GrabbedCrate.Release(ThePlayer);"
    Bad or missing expression in 'If' for this line "if(ThePlayer.bIsMovingAnObject)"

  16. #16
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Brisbane Australia
    Posts
    283

    Default

    I dont know how you set up "ThePlayer" but im pretty sure my code references my custom controller and pawn directly with casting etc.. might be worth checking through the code for instances of MyPawn and MyPlayerController and swapping them out with what you have.

    -mega


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.