Creating a Custom Character Controller

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Creating a Custom Character Controller

Postby lesPaul456 » Thu Jan 07, 2010 3:29 pm

Okay, after a few days of messing around with two separate graphics engines and getting Newton to work well with them, I've decided to start on a project. I want to create a third-person platformer style game. Pretty much everything is going smoothly. However I'm not sure about a few things, and I'd like to ask for some guidance.

I want to use a capsule to check for collisions. Something like this:

Image

I know I can create a capsule using code like this:

Code: Select all
NewtonCollision* collision = NewtonCreateCapsule(world, radius, height, NULL, NULL);


Whenever I do this, though, and then render it using the debug display, the capsule is laying on it's side, not standing straight up like in the image.

I'm also curious about a few other things. Once I get the capsule setup, what would be the best way to move the object? I assume it would be to use the apply force and torque callback. I'm not sure how I would go about this though...

Also, what about going up stairs and slopes? Will Newton automatically handle this, or will I have to?

Thanks!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: Creating a Custom Character Controller

Postby JernejL » Thu Jan 07, 2010 5:11 pm

Once youcreate the capsule, rotate it to proper orientation by setting it's matrix.

What you need here is, a "player controller" to perform movement of the player, newton has it in the demos if i remember correctly.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Creating a Custom Character Controller

Postby Julio Jerez » Thu Jan 07, 2010 5:12 pm

from the wiki tutorial for newton 2. here is a code script to make a player usin teh c iteface, you can adapeted to use teh c class as well

Code: Select all
   // Create a Body and attach a player controller joint
   {
      dFloat y0;
      Entity* player;
      NewtonBody* playerBody;
      NewtonCollision* shape;

      // find  the a floor to place the player
      y0 = FindFloor (world, 0.0f, 0.0f) + 1.0f;

      // load the player mesh
      player = sceneManager->CreateEntity();
      player->LoadMesh ("gymnast.dat");
      player->m_curPosition.m_y = y0;
      player->m_prevPosition = player->m_curPosition;

      // get the bounding Box of the player to get the collision shape dimensions
      dVector minBox;
      dVector maxBox;
      player->GetBBox (minBox, maxBox);

      // calculate player high and width
      dFloat padding = 1.0f / 64.0f;  // this si the default padding, for teh palye joint, we must subtract it from the shape
      dFloat playerHigh = (maxBox.m_y - minBox.m_y) - padding;
      dFloat playerRadius0 = (maxBox.m_z - minBox.m_z) * 0.5f;
      dFloat playerRadius1 = (maxBox.m_x - minBox.m_x) * 0.5f;
      dFloat playerRadius = (playerRadius0 > playerRadius1 ? playerRadius0 : playerRadius1) - padding;

      // No we make and make a upright capsule for the collision mesh
      dMatrix orientation;
      orientation.m_front = dVector (0.0f, 1.0f, 0.0f, 0.0f);         // this is the player up direction
      orientation.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);         // this is the player front direction
      orientation.m_right = orientation.m_front * orientation.m_up;   // this is the player sideway direction
      orientation.m_posit = dVector (0.0f, 0.0f, 0.0f, 1.0f);

      // add a body with a box shape
      //shape = CreateNewtonCapsule (world, player, playerHigh, playerRadius, m_wood, orientation);
      shape = CreateNewtonCylinder (world, player, playerHigh, playerRadius, m_wood, orientation);
      playerBody = CreateRigidBody (world, player, shape, 10.0f);
      NewtonReleaseCollision (world, shape);

      // make sure the player does not go to sleep
      NewtonBodySetAutoSleep (playerBody, 0);

      // now we will attach a player controller to the body
      NewtonUserJoint* playerController;
      // the player can take step up to 0.7 units;
      dFloat maxStairStepFactor = 0.7f / playerHigh;
      playerController = CreateCustomPlayerController (&orientation[0][0], playerBody, maxStairStepFactor, padding);

      // set the Max Slope the player can climb to PLAYER_MAX_SLOPE degree
      CustomPlayerControllerSetMaxSlope (playerController, PLAYER_MAX_SLOPE * 3.1416f / 180.0f);


      // now we will append some application data for the application to control the player
      PlayerController* userControl = (PlayerController*) malloc (sizeof (PlayerController));
      userControl->m_isThirdView = 1;
      userControl->m_point = dVector (0.0f, playerHigh, 0.0f,0.0f);

      CustomSetUserData (playerController, userControl);
      CustomSetDestructorCallback (playerController, PlayerController::Destroy);
      CustomSetSubmitContraintCallback (playerController, PlayerController::ApplyPlayerInput);

      // we also need to set override the transform call back so the we can set the Camera
      userControl->m_setTransformOriginal = NewtonBodyGetTransformCallback(playerBody);
      NewtonBodySetTransformCallback (playerBody, PlayerController::SetTransform);

      // we will need some ID to fin this joint in the transform Callback
      CustomSetJointID (playerController, PLAYER_JOINT_ID);
   }



lesPaul456 wrote:...
Also, what about going up stairs and slopes? Will Newton automatically handle this, or will I have to?
Thanks!

i does all of that for you.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Creating a Custom Character Controller

Postby lesPaul456 » Mon Jan 11, 2010 1:29 pm

Thanks for the help!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 51 guests