Character Controller

General discussion around Urho3D.

Re: Character Controller

PostPosted by Lumak » 20 Dec 2016, 06:22

Single-shot step up to step heights looked ugly, but what if we can do incremental height stepping.

https://youtu.be/w6PtBgm4uaQ
Lumak
Have many posts
Have many posts
 
Posts: 425
Joined: 08 Jun 2015, 15:38

Re: Character Controller

PostPosted by rasteron » 20 Dec 2016, 08:34

Looking good! :)
User avatar
rasteron
Have many posts
Have many posts
 
Posts: 437
Joined: 07 Mar 2014, 07:46
Location: web

Re: Character Controller

PostPosted by Lumak » 20 Dec 2016, 16:08

Code to do this is rather simple.

In Character::HandleNodeCollision() func, in the while() loop, I have
Code: Select all
        // relative contact height
        float yOffset = contactPosition.y_ - node_->GetPosition().y_;

        // not the ground
        if ( yOffset > M_EPSILON && yOffset < MAX_STEP_HEIGHT)
        {
            // moving into an object in the direction that we're moving (approximately)
            if (contactImpulse > 1.0f && curMoveDir_.DotProduct(contactNormal) < -0.8f)
            {
                if (yOffset > stepHeightVec_.y_ )
                {
                    stepHeightVec_.y_ = yOffset;
                }
            }
        }


MAX_STEP_HEIGHT = 0.4f

then in Character::FixedUpdate() func., I evaluate if the stepHeightVec_.y_ > 0 and if (stepHeightVec_.y_ > INCREMENTAL_HEIGHT) set stepHeightVec_.y_ = INCREMENTAL_HEIGHT (0.06f)
and calculate linear velocity based on set stepHeightVec_.y_

Code: Select all
    // swat move speed:
    // walk - 1.842 m/s
    // run - 4.78 m/s
    // sprint - 7.164 m/s
    const float velWalk = 1.842f;
    const float velRun = 4.78f;
    moveForce_ = !runKey ? velWalk : velRun;

    if (moveDir.LengthSquared() > 0.0f)
    {
        moveDir.Normalize();
        moveDir = rot * moveDir;
    }

    curMoveDir_ = moveDir;

    // If in air, allow control, but slower than when on ground
    if (softGrounded)
    {
        if (stepHeightVec_.y_ > M_EPSILON)
        {
            if (stepHeightVec_.y_ > INCREMENTAL_STEP_HEIGHT)
            {
                stepHeightVec_.y_ = INCREMENTAL_STEP_HEIGHT;
            }

            // convert to vertical step velocity
            stepHeightVec_ = stepHeightVec_ / timeStep;

            body->SetLinearVelocity(stepHeightVec_ + moveDir * moveForce_);
        }
        else
        {
            body->SetLinearVelocity(moveDir * moveForce_);
        }
    }
    else
    {
        body->ApplyImpulse(moveDir * INAIR_MOVE_FORCE);
    }

   . . .
   stepHeightVec_ = Vector3::ZERO;


INCREMENTAL_STEP_HEIGHT = 0.06f

Edit: added code changes in my Character::FixedUpdate() func.
Lumak
Have many posts
Have many posts
 
Posts: 425
Joined: 08 Jun 2015, 15:38

Re: Character Controller

PostPosted by Lumak » 21 Dec 2016, 17:52

Remainder of changes in the Character::FixedUpdate() func (just changed Swat_JumpUp to Swat_JumpDown animation - not shown in the videos)

Code: Select all
    if (!onGround_)
    {
        float rayDistance = 5.0f;
        PhysicsRaycastResult result;
        GetScene()->GetComponent<PhysicsWorld>()->RaycastSingle(result, Ray(node_->GetPosition(), Vector3::DOWN), rayDistance, 2);

        if (result.body_  == NULL || result.distance_ > MAX_STEPDOWN_HEIGHT)
        {
            animCtrl->Play("Models/Swat/Swat_JumpDown.ani", 0, false, 0.2f);
        }
    }
    else
. . .



MAX_STEPDOWN_HEIGHT = 0.5f

That's all the changes that I made for the step height movement. Other changes were adding an over-the-shoulderCamera and having the main cameraNode_ interpolate the position/rotation with it.
Lumak
Have many posts
Have many posts
 
Posts: 425
Joined: 08 Jun 2015, 15:38

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests