[C++, DX9] Player controller?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[C++, DX9] Player controller?

Postby Ripiz » Sun Jul 18, 2010 7:26 am

I create body this way:
Code: Select all
      D3DXMATRIX matRotX, matRotY, matRotZ, matTrans, matWorld;
      D3DXMatrixRotationX( &matRotX, 0);
      D3DXMatrixRotationY( &matRotY, 0);
      D3DXMatrixRotationZ( &matRotZ, 0);
      D3DXMatrixTranslation(&matTrans, 0, 0, 14);
      matWorld=(matRotX*matRotY*matRotZ)*matTrans; //temporal matrix

      newtonData *data = new newtonData;
      data->id = eMyPlayer;
      data->data = 0;

      float mass = 70;
      Vector3 origin, inertia;
      extern NewtonWorld *world;
      //NewtonCollision *collision = NewtonCreateCylinder(world, 4.8f, 28, 0, matWorld);
      NewtonCollision *collision = NewtonCreateBox(world, 6, 3, 28, 0, matWorld);
      playerBody = NewtonCreateBody (world, collision);
      //NewtonConstraintCreateUpVector(world, Vector3(0, 0, 1), body);
      NewtonBodySetDestructorCallback (playerBody, DestroyBodyCallback);
      NewtonBodySetUserData (playerBody, data);
      NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
      D3DXMatrixIdentity(&matWorld);
      NewtonBodySetMatrix (playerBody, matWorld);
      NewtonBodySetMassMatrix (playerBody, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z);
      NewtonBodySetCentreOfMass (playerBody, &origin[0]);
      NewtonBodySetForceAndTorqueCallback (playerBody, ApplyForceAndTorqueCallback);
      NewtonBodySetTransformCallback (playerBody, SetTransformCallback);
      NewtonReleaseCollision (world, collision);


And this is the way I move player:
Code: Select all
         Vector3 move(0, 0, 0);
         if(chr_KeybState[DIK_W])
            move+=Vector3(sin(xrot - D3DX_PI), cos(xrot - D3DX_PI), 0);
         if(chr_KeybState[DIK_S])
            move+=Vector3(sin(xrot), cos(xrot), 0);
         if(chr_KeybState[DIK_D])
            move+=Vector3(sin(xrot + D3DX_PI/2), cos(xrot + D3DX_PI/2), 0);
         if(chr_KeybState[DIK_A])
            move+=Vector3(sin(xrot - D3DX_PI/2), cos(xrot - D3DX_PI/2), 0);
         D3DXVec3Normalize(&move, &move);


But how would I make player controller? I mean, I don't want player to go through buildings, or fly over terrain, neither fall when he starts climbing the hill.
Could anyone show how to achieve what I need?
Thanks in advance.
Ripiz
 
Posts: 47
Joined: Sat Oct 03, 2009 12:07 pm

Re: [C++, DX9] Player controller?

Postby JernejL » Sun Jul 18, 2010 8:28 am

What are you doing with #2 piece of code? do you use the forcetorque callback and add the vector to body's forces?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [C++, DX9] Player controller?

Postby Ripiz » Sun Jul 18, 2010 8:36 am

I don't. That's why I ask how to create player controller. I only knew I need NewtonBody with Collision, but no idea what next.
Ripiz
 
Posts: 47
Joined: Sat Oct 03, 2009 12:07 pm

Re: [C++, DX9] Player controller?

Postby Carli » Sun Jul 18, 2010 8:44 am

Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: [C++, DX9] Player controller?

Postby Ripiz » Tue Jul 20, 2010 6:41 am

Creating Body and Collision:
Code: Select all
      D3DXMATRIX matRotX, matRotY, matRotZ, matTrans, matWorld;
      D3DXMatrixRotationX( &matRotX, 0);
      D3DXMatrixRotationY( &matRotY, 0);
      D3DXMatrixRotationZ( &matRotZ, 0);
      D3DXMatrixTranslation(&matTrans, 0, 0, 14);
      matWorld=(matRotX*matRotY*matRotZ)*matTrans;

      newtonData *data = new newtonData;
      data->id = eMyPlayer;
      data->data = 0;

      float mass = 70;
      Vector3 origin, inertia;
      extern NewtonWorld *world;
      NewtonCollision *collision = NewtonCreateBox(world, 6, 3, 28, 0, matWorld);
      playerBody = NewtonCreateBody (world, collision);
      NewtonConstraintCreateUpVector(world, Vector3(0, 0, 1), playerBody);
      NewtonBodySetDestructorCallback (playerBody, DestroyBodyCallback);
      NewtonBodySetUserData (playerBody, data);
      NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
      D3DXMatrixIdentity(&matWorld);
      NewtonBodySetMatrix (playerBody, matWorld);
      NewtonBodySetMassMatrix (playerBody, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z);
      NewtonBodySetCentreOfMass (playerBody, &origin[0]);
      NewtonBodySetForceAndTorqueCallback (playerBody, ApplyForceAndTorqueCallback);
      NewtonBodySetTransformCallback (playerBody, SetTransformCallback);
      NewtonReleaseCollision (world, collision);


And my CallBack:
Code: Select all
void ApplyForceAndTorqueCallback (const NewtonBody* body, float timestep, int threadIndex){
   float Ixx;
   float Iyy;
   float Izz;
   float mass;
   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
   Vector3 gravityForce(0.0f, 0.0f, mass * -10.0f);
   newtonData *data = (newtonData*)NewtonBodyGetUserData(body);
   if(data->id == eMyPlayer){
      NewtonBodySetForce(body, gravityForce + movement * timestep * speed);
      NewtonBodySetTorque(playerBody, movement * timestep * speed);
   }else
      NewtonBodySetForce(body, gravityForce);
}


The collision doesn't move, any ideas why?
Ripiz
 
Posts: 47
Joined: Sat Oct 03, 2009 12:07 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron