Character Controller

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Character Controller

Postby skarab24 » Sat Aug 17, 2013 5:23 pm

Hello!

I'm stuck into implementing a character controller using Newton Core only, cause I can't include all others dependencies.

Has somebody already made one please ?

I'm trying the way of applying the required force, using the velocity conversion (velocity-GetNode().GetRigidBody().GetVelocity() blabla),
it doesn't seem working good enough for now, and I wonder how to handle gravity to not have free-fly behavior.
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Sat Aug 17, 2013 7:37 pm

the joint library does no have any dependency, why don't you link that that library and use that controller?

In my opinion that will be less hassle than making the controller yourself. There is a lot of Black art programming tricks to make a player controller.

The controller in the joint library is nice enought that you can use as starting point and make the modification you want on top.
.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Sat Aug 17, 2013 9:20 pm

Ok, I added dCustomJoints and dMath to the build, it's working without other things.
Will try to play with the CustomPlayerController, thanks!
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Sun Aug 18, 2013 11:11 am

Oh yes, I am sorry about that, dJoint depends of the dMath lib.
But the dMath is pretty much the smallest math library that you can find and while offering everything. I do not consider a bad thing.
In fact in order to program anything you need to have some sort of math util. I hope that's alright.

also sync to the latest SVN version I just check in one pending thing, and fixed a bug in the controller, it should be very decent now
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Sun Aug 18, 2013 2:27 pm

Yes its alright, it builds nicely on all VS & mingw (i'm using jamplus, not cmake, thats why i need to integrate manually all other libs).
Will integrate your controller tomorrow :)

edit: ok, I'm synching to the svn repo
edit2: ok, build fine and works, going to test controller
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby skarab24 » Sun Aug 18, 2013 8:14 pm

Ok, so I've create a component::CharacterController, basically it's just this :

Code: Select all
    void CharacterController::_CreateCharacter()
    {
        physics::Scene* scene = GetNode().GetRoot().FindComponent<physics::Scene>();
       
        _PlayerController = COFFEE_New(CustomPlayerController);
        _PlayerController->m_body = GetNode().GetRigidBody()._Body;
        _PlayerController->m_world = scene->GetWorld();
        _PlayerController->Init(PLAYER_MASS, 1.0f, 1.0f, 2.0f, 0.7f, GetIdentityMatrix());
        _PlayerController->SetRestrainingDistance(0.1f);

        GetNode().GetTransform().SetTranslation(basic::Vector3(0.0f, 10.0f, 0.0f));
    }

    #define PLAYER_MASS                  80.0f
    #define PLAYER_WALK_SPEED            4.0f

    void CharacterController::OnFixedUpdate(const basic::Time& time_step)
    {
        if (GetNode().HasRigidBody() && GetNode().GetRigidBody().IsRunning())
        {
            if (_PlayerController==NULL)
            {
                _CreateCharacter();
            }
            else
            {
               
                dFloat forwarSpeed = (int (input::Keyboard::Get().IsButtonPressed(input::KEY_Up))
                    - int (input::Keyboard::Get().IsButtonPressed(input::KEY_Down))) * PLAYER_WALK_SPEED;
              dFloat strafeSpeed = (int (input::Keyboard::Get().IsButtonPressed(input::KEY_Right))
                    - int (input::Keyboard::Get().IsButtonPressed(input::KEY_Left))) * PLAYER_WALK_SPEED;

              // normalize player speed
              dFloat mag2 = forwarSpeed * forwarSpeed + strafeSpeed * strafeSpeed;
              if (mag2 > 0.0f) {
                 dFloat invMag = PLAYER_WALK_SPEED / dSqrt (mag2);
                 forwarSpeed *= invMag;
                 strafeSpeed *= invMag;
              }

                dVector gravity(0.0f, -9.81f, 0.0f, 0.0f);

                _PlayerController->SetPlayerVelocity(
                    forwarSpeed,
                    strafeSpeed,
                    0.0f,
                    0.0f,
                    gravity,
                    time_step.GetSecondCount());

                _PlayerController->PostUpdate(time_step.GetSecondCount(), 0);
            }
        }
    }



Creation goes well, it tries to update, but then there is a bug, here is kind of callstack, but the reason is obvious:
Code: Select all
CustomPlayerController::UpdateGroundPlane
NewtonWorldConvexRayCast
dgBroadPhase::ConvexRayCast
dgBody::ConvexRayCast
dgCollisionInstance::ConvexRayCast
dgAssert (0);


m_scaleIsUnit is false, because I use a non-uniform scaled box as ground, since I like it you know ;)

...Else I'm happy to get here tonight, just some points through :
- there is a CustomPlayerControllerManager, for now it's only used to get the NewtonWorld and an empty callback about contacts, I think you have ideas about extending it later on, but do I really require this or I may just use the PlayerController alone ? In my sample I just added a m_World member to override controller, for now.
- preupdate() and postupdate() methods, why not only update() ? (not a big deal, just want to be sure / not confused)
- I've already a FreeController with free move & collisions support, rotation / lookat is handled at full fps in the update() method, while translation is handled by physics in the fixed_update() / fixedtimestep method, I plan to do the same thing with your controller, do you use/need the correct rotation somewhere or I may just don't care ?

Thanks, I'm on the right track now !

edit: Oh, and I planned to do the Ludum Dare next week-end, basically it'll be game based on non-uniform scaled cubes with some Newton basic physics and some audio... I hope to get that controller working, in bad case I'll use the other, but I've lot of other things to do & test you know :) It'll be used as a demo & test case for my project, so it'll be upgraded later on as well, I will share the resulting thing here.
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Sun Aug 18, 2013 9:44 pm

from what file did you get this code fragments
Code: Select all
CustomPlayerController::UpdateGroundPlane
NewtonWorldConvexRayCast
dgBroadPhase::ConvexRayCast
dgBody::ConvexRayCast
dgCollisionInstance::ConvexRayCast
dgAssert (0);


function CustomPlayerController::UpdateGroundPlane
does not have any assert, this is how it look like now

Code: Select all
void CustomPlayerController::UpdateGroundPlane (dMatrix& matrix, const dMatrix& castMatrix, const dVector& dst, int threadIndex)
{
   CustomPlayerControllerManager* const manager = (CustomPlayerControllerManager*) GetManager();
   NewtonWorld* const world = manager->GetWorld();

   CustomControllerConvexRayFilter filter(m_body);
   NewtonWorldConvexRayCast (world, m_castingShape, &castMatrix[0][0], &dst[0], CustomControllerConvexRayFilter::Filter, &filter, CustomControllerConvexRayFilter::Prefilter, threadIndex);

   m_groundPlane = dVector (0.0f, 0.0f, 0.0f, 0.0f);
   m_groundVelocity = dVector (0.0f, 0.0f, 0.0f, 0.0f);

   if (filter.m_hitBody) {
      m_isJumping = false;
      dVector supportPoint (castMatrix.m_posit + (dst - castMatrix.m_posit).Scale (filter.m_intersectParam));
      m_groundPlane = filter.m_hitNormal;
      m_groundPlane.m_w = - (supportPoint % filter.m_hitNormal);
      NewtonBodyGetPointVelocity (filter.m_hitBody, &supportPoint.m_x, &m_groundVelocity[0]);
      matrix.m_posit = supportPoint;
      matrix.m_posit.m_w = 1.0f;
   }
}


are you using the code from the download? is better if you you need to sync to the latest code from SVN
I will try to put a new update tomorrow.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Sun Aug 18, 2013 9:51 pm

Misundertanding, the callstack is inversed :

Code: Select all
    dgAssert (0);
    dgCollisionInstance::ConvexRayCast
    dgBody::ConvexRayCast
    dgBroadPhase::ConvexRayCast
    NewtonWorldConvexRayCast
    CustomPlayerController::UpdateGroundPlane


It seams non uniform is kind of handled with disabling the assert, but I've other problems after I think.

edit: Im synced to the trunk.
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Mon Aug 19, 2013 5:09 am

you still are not telling me where are you getting that code from?
I do not know if you are telling me of a bug of what? I am confused.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Mon Aug 19, 2013 9:17 am

Sorry, here it is (coming from google code):

Code: Select all

dgFloat32 dgCollisionInstance::ConvexRayCast (const dgCollisionInstance* const convexShape, const dgMatrix& convexShapeMatrix, const dgVector& localVeloc, dgFloat32 minT, dgContactPoint& contactOut, OnRayPrecastAction preFilter, const dgBody* const referenceBody, void* const userData, dgInt32 threadId) const
{
   if ((GetCollisionPrimityType() != m_nullCollision) && (!preFilter || preFilter(referenceBody, this, userData))) {
      if (m_scaleIsUnit) {
         dgFloat32 t = m_childShape->ConvexRayCast (convexShape, convexShapeMatrix, localVeloc, minT, contactOut, referenceBody, this, userData, threadId);
         if (t <= minT) {
            if (!(m_childShape->IsType(dgCollision::dgCollisionMesh_RTTI) || m_childShape->IsType(dgCollision::dgCollisionCompound_RTTI))) {
               contactOut.m_shapeId0 = GetUserDataID();
               contactOut.m_shapeId1 = GetUserDataID();
            }
            contactOut.m_collision0 = this;
            contactOut.m_collision1 = this;
         }
         return t;
      } else {
         dgAssert (0);
      }
   }

   return dgFloat32 (1.2f);
}


scaleIsUnit is false, so it goes into dgAssert (0); instead of doing raycast
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Mon Aug 19, 2013 9:25 am

Oh I see you are moving the player over a static scaled mesh, is that what going on?

if that the case can you test it on a non scale mesh until I complete that part of the code?
I place those assert on place that not too many people use but that I will eventually complete when the case arises.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Mon Aug 19, 2013 9:30 am

Yes, got a scene with a single cube scaled in X/Z directions only.
I'll test with non scaled things then.
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby skarab24 » Mon Aug 19, 2013 2:09 pm

Ok, got it working, at least with static geometry.

When I add a simple rigid body, pushing it seams a bit random, does anyone experience this as well with the latest svn code ?

edit: the rigid bodies sometime interpenetrates the floor, which cause "blocking", going to debug
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Re: Character Controller

Postby Julio Jerez » Mon Aug 19, 2013 2:14 pm

what do you mean random, can you make a youtube video. so that I can see.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Character Controller

Postby skarab24 » Mon Aug 19, 2013 2:32 pm

http://youtu.be/Izw9nvDL8dQ

Sometimes I can push it, sometimes not, loks like there is interpenetration, not sure if its some problem in my code or build settings / accuracy.
skarab24
 
Posts: 45
Joined: Sun Jul 28, 2013 8:33 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests