Tire rotation axis

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Tire rotation axis

Postby Leadwerks » Fri Apr 28, 2017 6:23 pm

My tires are being aligned to rotate around the Z axis. I want them to rotate around the X axis, since my vehicle models are always modeled along the Z axis. Right now my tires are 90 degrees sideways. :shock:

I remember this was an issue in old versions of Newton. Can you remind me how to fix this? Changing the matrix in the vehicle creation function does not appear to do anything.

It looks to me like the vehicle frame parameter is being ignored:
Code: Select all
void dCustomVehicleController::Init(NewtonBody* const body, const dMatrix& vehicleFrame___, NewtonApplyForceAndTorque forceAndTorque, void* const userData, dFloat gravityMag)
{
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Fri Apr 28, 2017 9:39 pm

the vehicle manager has callback named PostUpdate
when you make you own manager
class MyVehicleControllerManager: public dCustomVehicleControllerManager
you must overload that function more of less like this

Code: Select all
   virtual void PostUpdate (dFloat timestep)
   {
      // do the base class post update
      dCustomVehicleControllerManager::PostUpdate(timestep);
      
      // update the visual transformation matrices for all vehicle tires
      for (dListNode* node = GetFirst(); node; node = node->GetNext()) {
         dCustomVehicleController* const controller = &node->GetInfo();
         SuperCarEntity* const vehicleEntity = (SuperCarEntity*)NewtonBodyGetUserData (controller->GetBody());
         vehicleEntity->UpdateTireTransforms();
      }


there you can iterate over the tires and get the transform, in the demos you can see that the function UpdateTireTransform apply a local 90 rotation aligmentMatrix->m_matrix the body matrix to the tire matrix

Code: Select all
   void UpdateTireTransforms()
   {
      NewtonBody* const body = m_controller->GetBody();
      DemoEntityManager* const scene = (DemoEntityManager*) NewtonWorldGetUserData(NewtonBodyGetWorld(body));

      for (dList<dCustomVehicleController::dBodyPartTire>::dListNode* node = m_controller->GetFirstTire(); node; node = m_controller->GetNextTire(node)) {
         const dCustomVehicleController::dBodyPartTire* const part = &node->GetInfo();
         dCustomVehicleController::dBodyPart* const parent = part->GetParent();

         NewtonBody* const body = part->GetBody();
         NewtonBody* const parentBody = parent->GetBody();

         dMatrix matrix;
         dMatrix parentMatrix;
         NewtonBodyGetMatrix(body, &matrix[0][0]);
         NewtonBodyGetMatrix(parentBody, &parentMatrix[0][0]);

         DemoEntity* const tirePart = (DemoEntity*) part->GetUserData();
         TireAligmentTransform* const aligmentMatrix = (TireAligmentTransform*)tirePart->GetUserData();
         matrix = aligmentMatrix->m_matrix * matrix * parentMatrix.Inverse();

         dQuaternion rot (matrix);
         tirePart->SetMatrix(*scene, rot, matrix.m_posit);
      }
   }
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby Leadwerks » Fri Apr 28, 2017 10:13 pm

Will the old vehicle code work with the current build of Newton?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Fri Apr 28, 2017 10:23 pm

if you mean the 3.13, the answer is not because the new vehicle is all rigid body, so I had to make some changes.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 11:33 am

This is what I tried, and all it does is make the car fly around:
Code: Select all
   void NewtonDynamicsVehicle::UpdateTireTransforms()
   {
      for (int i = 0; i < tire.size(); ++i)
      {
         NewtonDynamicsBody* body = (NewtonDynamicsBody*)tire[i]->GetUserData();
         dFloat mat0[16];
         dFloat mat1[16];
         NewtonBodyGetMatrix(body->body, mat0);
         mat1[0] = mat0[8];
         mat1[1] = mat0[9];
         mat1[2] = mat0[10];
         mat1[3] = mat0[11];

         mat1[4] = mat0[4];
         mat1[5] = mat0[5];
         mat1[6] = mat0[6];
         mat1[7] = mat0[7];
         
         mat1[8] = -mat0[0];
         mat1[9] = -mat0[1];
         mat1[10] = -mat0[2];
         mat1[11] = -mat0[3];
         
         mat1[12] = mat0[12];
         mat1[13] = mat0[13];
         mat1[14] = mat0[14];
         mat1[15] = mat0[15];
         NewtonBodySetMatrix(body->body, mat1);
      }
   }


This is a complete mess. I might have to disable vehicles in order to release version 4.4 on time.

Either that or I am going to have to code my own vehicles from scratch.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Sat Jun 03, 2017 1:51 pm

You can code your own vehicle if you want, that's not under my control.

if you want to get the right callback, here is how the it should be written.
You get that matrix from the tire body and apply a local rotation by 90 degree before you apply to the visual mesh, you and applying back to the rigid body, and them you say is a mess.

assuming the rotation is around the z axis, tham roation aroun y will align z to x here is a pseudo code
Code: Select all
void NewtonDynamicsVehicle::UpdateTireTransforms()
{
      static dMatrix localRotation (dYawMatrix (3.1416f * 0.5));
      for (int i = 0; i < tire.size(); ++i)
      {
            dMatrix matrix;
            NewtonBodyGetMatrix(body->body, matrix);
            matrix = localRotation * matrix;
             // here apply that rotation to matrix the visual tire mesh)

        }
}
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 2:31 pm

Our vehicle is set up to work with tire rotation around the X axis. I can't change that behavior without breaking people's games. Simply changing the visual mesh does nothing.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Sat Jun 03, 2017 3:15 pm

the code a posted do not changes the visual is simple set the matrix of the visual.
is like a an animation changing a bone transform of an entity.
is the same method you use to update the matrix of any other entity that is controlled by a rigid body.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby Julio Jerez » Sat Jun 03, 2017 3:25 pm

even that demos that comes with sdk has the tire rotation along an axis that does not matches the axis of the vehicle tire so I have to do the same thing.
I added comments to the function that does the same tire rotation.
Code: Select all
void UpdateTireTransforms()
   {
      NewtonBody* const body = m_controller->GetBody();
      DemoEntityManager* const scene = (DemoEntityManager*) NewtonWorldGetUserData(NewtonBodyGetWorld(body));

      for (dList<dCustomVehicleController::dBodyPartTire>::dListNode* node = m_controller->GetFirstTire(); node; node = m_controller->GetNextTire(node)) {
         const dCustomVehicleController::dBodyPartTire* const part = &node->GetInfo();
         dCustomVehicleController::dBodyPart* const parent = part->GetParent();

         NewtonBody* const body = part->GetBody();
         NewtonBody* const parentBody = parent->GetBody();

         dMatrix matrix;
         dMatrix parentMatrix;
         NewtonBodyGetMatrix(body, &matrix[0][0]);

// Here it get that tire body matrix whi is in world space.
         NewtonBodyGetMatrix(parentBody, &parentMatrix[0][0]);

         DemoEntity* const tirePart = (DemoEntity*) part->GetUserData();

// Here is Take the 90 dregree rotaion to aligne the visual matrix to the tire body matrix
         TireAligmentTransform* const aligmentMatrix = (TireAligmentTransform*)tirePart->GetUserData();

// here the aligment matrix is applyied to the tire, al also the global matrix is conveted to local bone space
         matrix = aligmentMatrix->m_matrix * matrix * parentMatrix.Inverse();

// here that matrix is set to the visual tire bone.
         dQuaternion rot (matrix);
         tirePart->SetMatrix(*scene, rot, matrix.m_posit);
      }
   }
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 3:34 pm

I understand that I can make a hack to rotate the matrix for a mesh with a different orientation. This is not useful. In our engine, the entity 4x4 matrix always matches the physics body matrix. Every entity is (potentially) a physics body, and they share the same orientation at all times. We have tools for building physics shapes from meshes, for example, and we can't start arbitrarily rotating our vehicle body around without breaking a lot of things.

All of our existing code is based on the assumption that the front of a vehicle faces in the +Z direction, and the tires rotate around the X axis, as the vehicleFrame parameter allowed in the previous implementation.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Sat Jun 03, 2017 4:05 pm

is it not a hack, is totally correct, you are just setting a bone matrix on a mesh.
you think of the body matrix coming pout with a 90 rotation matrix that needs to be undone.

all is a offset rotation just try that function and it will work.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 7:21 pm

Except I'm not making a simple little demo that I can just hard-code. I'm making an engine with 700 documented commands that work in a certain manner, with hundreds of examples. Introducing a low-level change like this will break peoples' code all across the engine.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Tire rotation axis

Postby Julio Jerez » Sat Jun 03, 2017 8:02 pm

I do not know what that mean, that tire of the vehicle spin around one axis, for you to set the transform to the mesh that representing the shape if you mesh spin around a different axis you need to apply a rotation matrix. that does not required you to change anything in you code, it only requires to write that transform callback for the tires whish is called form the PostUpdate method of the vehicle manager. If you display the debug you will see the shape rotate the proper way, all it nee is that offset rotation to align the visual mesh, I do not get why this is so difficult
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 9:15 pm

I introduced an additional offsetmatrix into my body class, and I am attempting to insert it into any places where the engine converts matrices from Newton to my engine. I am not very happy about it, but it will probably work.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Tire rotation axis

Postby pHySiQuE » Sat Jun 03, 2017 9:43 pm

Okay, here is the behavior I have at this point.
https://www.leadwerks.com/files/NewtonCar.zip

The application can be debugged from Visual Studio.

Attachments
vehicle class.zip
(6.73 KiB) Downloaded 280 times
Last edited by pHySiQuE on Sat Jun 03, 2017 9:52 pm, edited 1 time in total.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests