Character Controller

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Character Controller

Postby stringa » Wed Jun 24, 2009 1:00 pm

Hello. I'm using Newton and OpenGL. I wrote a simple implementation for the character controller off the tutorial lesson. I'm wondering if my rotational matrix is wrong. For some reason, I'm getting weird results when testing my character controller ( I get lots of rotation ) like my force is being applied wrong. All I'm doing is moving forward and backward.
As you can see, I should be applying the force on the center of mass.

Thanks for help....
stringa

EDIT : So I'm not thinking it this code, but possibly the shapes I'm using. At first, I was just using a box as the collision shape, but now I"m using a capsule. It seems to work a little better, except at certain points on hills in my terrain. It spins the object like crazy

Code: Select all

void AddGlobalForce( const NewtonBody* newton_body, vec3& global_force, vec3& global_point )
{
  // --- Get the body position
  mat4 body_matrix;
  vec3 torque;

  NewtonBodyGetMatrix( newton_body, body_matrix.mat_array );

  vec3 R = global_point - vec3( body_matrix.mat_array[12], body_matrix.mat_array[13], body_matrix.mat_array[14] );
 
 
  cross( torque, R, global_force );
  std::cout << "Torque " << torque.x << " " << torque.y << " " << torque.z << std::endl;

  NewtonBodyAddForce( newton_body, global_force.vec_array );
  NewtonBodyAddTorque( newton_body, torque.vec_array );

}

void AddLocalForce( const NewtonBody* newton_body, vec3& local_force, vec3& local_point )
{
  // --- We need to rotate the force by the body rotation
  mat4 body_matrix;
  mat3 rotation_matrix;

  NewtonBodyGetMatrix( newton_body, body_matrix.mat_array );

  rotation_matrix = mat3( body_matrix.mat_array[0],
                          body_matrix.mat_array[1],
                          body_matrix.mat_array[2],
                          body_matrix.mat_array[4],
                          body_matrix.mat_array[5],
                          body_matrix.mat_array[6],
                          body_matrix.mat_array[8],
                          body_matrix.mat_array[9],
                          body_matrix.mat_array[10] );
 

  vec3 global_force = rotation_matrix * local_force;
  vec3 global_point = vec3( body_matrix * vec4( local_point.x, local_point.y, local_point.z, 1.0 ) );
 
  AddGlobalForce( newton_body, global_force, global_point );
}

void UpdateCharacter( GameObject* obj, const NewtonBody* newton_body )
{

  NewtonBodySetAutoFreeze( newton_body, 0 );

  std::queue< MOVEMENT_MODE >& applied_movements = obj->m_body->movement;

  vec3 force = vec3( 0.0f, 0.0f, -100.0f );
  vec3 p1 = vec3( 0.0f, 0.0f, 0.0f );
  vec3 p2 = vec3( 1.0f, 0.0f, 0.0f );

  while( !applied_movements.empty() )
  {
    MOVEMENT_MODE cur_movement = applied_movements.front();

    applied_movements.pop();

    switch( cur_movement )
    {
    case MM_WALK_FORWARD:
      {

      AddLocalForce( newton_body, force, p1 );
      //AddLocalForce( newton_body, force, p2 );
      }
      break;

    case MM_WALK_BACKWARD:

      AddLocalForce( newton_body, -force, p1 );
      //AddLocalForce( newton_body, -force, p2 );

      break;

    case MM_TURN_LEFT:

      AddLocalForce( newton_body, force, p1 );

      break;

    case MM_TURN_RIGHT:

      AddLocalForce( newton_body, force, p2 );

      break;

    }

   
  }

}



stringa
 
Posts: 5
Joined: Fri May 08, 2009 6:39 pm

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron