Skip to content

Commit

Permalink
more on enabling vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Jan 3, 2025
1 parent c61f88c commit eeca24c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ ndBodyKinematic* BuildFlatPlane(ndDemoEntityManager* const scene, bool optimized

ndShapeInstance plane(new ndShapeStatic_bvh(meshBuilder));
ndMatrix uvMatrix(ndGetIdentityMatrix());
uvMatrix[0][0] *= 1.0f / 2.0f;
uvMatrix[1][1] *= 1.0f / 2.0f;
uvMatrix[2][2] *= 1.0f / 2.0f;
uvMatrix[0][0] *= 1.0f / 50.0f;
uvMatrix[1][1] *= 1.0f / 50.0f;
uvMatrix[2][2] *= 1.0f / 50.0f;

ndSharedPtr<ndDemoMeshInterface>geometry (new ndDemoMesh("box", scene->GetShaderCache(), &plane, "marbleCheckBoard.png", "marbleCheckBoard.png", "marbleCheckBoard.png", 1.0f, uvMatrix));

Expand Down
17 changes: 17 additions & 0 deletions newton-4.00/applications/ndSandbox/toolbox/ndVehicleCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ void ndVehicleCommon::ApplyInputs(ndWorld* const world, ndFloat32)
ndFloat32 throttle = axis[m_gasPedal];
ndFloat32 steerAngle = axis[m_steeringWheel];
ndFloat32 handBrake = buttons[m_handBreakButton] ? 1.0f : 0.0f;

//for (int i = 0; i < buttons.GetCount(); ++i)
//{
// if (buttons[i])
// {
// ndTrace(("button %d\n", i));
// }
//}

if (m_parking.Update(buttons[m_parkingButton] ? true : false))
{
Expand Down Expand Up @@ -663,3 +671,12 @@ void ndVehicleCommonNotify::PostUpdate(ndWorld* const world, ndFloat32 timestep)
}
}

void ndVehicleCommonNotify::Debug(ndConstraintDebugCallback& context) const
{
ndModelNotify::Debug(context);
ndMultiBodyVehicle* const vehicle = (ndMultiBodyVehicle*)GetModel();
if (vehicle)
{
vehicle->Debug(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class ndVehicleCommonNotify : public ndModelNotify

void ApplyInputs(ndWorld* const world, ndFloat32 timestep);

void Debug(ndConstraintDebugCallback& context) const override;
void Update(ndWorld* const world, ndFloat32 timestep) override;
void PostUpdate(ndWorld* const world, ndFloat32 timestep) override;

Expand Down
217 changes: 107 additions & 110 deletions newton-4.00/sdk/dNewton/dModels/dVehicle/ndMultiBodyVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,116 +133,6 @@ ndMultiBodyVehicleTorsionBar* ndMultiBodyVehicle::AddTorsionBar(ndBodyKinematic*
//return *m_torsionBar;
}



//void ndMultiBodyVehicle::Debug(ndConstraintDebugCallback& context) const
void ndMultiBodyVehicle::Debug(ndConstraintDebugCallback&) const
{
ndAssert(0);
//// draw vehicle cordinade system;
//const ndBodyKinematic* const chassis = m_chassis;
//ndAssert(chassis);
//ndMatrix chassisMatrix(chassis->GetMatrix());
//chassisMatrix.m_posit = chassisMatrix.TransformVector(chassis->GetCentreOfMass());
////context.DrawFrame(chassisMatrix);
//
//ndFloat32 totalMass = chassis->GetMassMatrix().m_w;
//ndVector effectiveCom(chassisMatrix.m_posit.Scale(totalMass));
//
//// draw front direction for side slip angle reference
//
//// draw velocity vector
//ndVector veloc(chassis->GetVelocity());
//ndVector p0(chassisMatrix.m_posit + m_localFrame.m_up.Scale(1.0f));
//ndVector p1(p0 + chassisMatrix.RotateVector(m_localFrame.m_front).Scale(2.0f));
//ndVector p2(p0 + veloc.Scale (0.25f));
//
//context.DrawLine(p0, p2, ndVector(1.0f, 1.0f, 0.0f, 0.0f));
//context.DrawLine(p0, p1, ndVector(1.0f, 0.0f, 0.0f, 0.0f));
//
//// draw body acceleration
////ndVector accel(m_chassis->GetAccel());
////ndVector p3(p0 + accel.Scale(0.5f));
////context.DrawLine(p0, p3, ndVector(0.0f, 1.0f, 1.0f, 0.0f));
//
//ndFloat32 scale = ndFloat32 (3.0f);
//ndVector weight(chassis->GetForce().Scale (scale * chassis->GetInvMass() / m_downForce.m_gravity));
//
//// draw vehicle weight;
//ndVector forceColor(ndFloat32 (0.8f), ndFloat32(0.8f), ndFloat32(0.8f), ndFloat32(0.0f));
//ndVector lateralColor(ndFloat32(0.3f), ndFloat32(0.7f), ndFloat32(0.0f), ndFloat32(0.0f));
//ndVector longitudinalColor(ndFloat32(0.7f), ndFloat32(0.3f), ndFloat32(0.0f), ndFloat32(0.0f));
//context.DrawLine(chassisMatrix.m_posit, chassisMatrix.m_posit + weight, forceColor);
//
//for (ndReferencedObjects<ndMultiBodyVehicleTireJoint>::ndNode* node = m_tireList.GetFirst(); node; node = node->GetNext())
//{
// ndMultiBodyVehicleTireJoint* const tireJoint = *node->GetInfo();
// ndBodyKinematic* const tireBody = tireJoint->GetBody0()->GetAsBodyDynamic();
// ndMatrix tireFrame(tireBody->GetMatrix());
// totalMass += tireBody->GetMassMatrix().m_w;
// effectiveCom += tireFrame.m_posit.Scale(tireBody->GetMassMatrix().m_w);
//}
//
//for (ndReferencedObjects<ndMultiBodyVehicleTireJoint>::ndNode* node = m_tireList.GetFirst(); node; node = node->GetNext())
//{
// ndMultiBodyVehicleTireJoint* const tireJoint = *node->GetInfo();
// ndBodyKinematic* const tireBody = tireJoint->GetBody0()->GetAsBodyDynamic();
//
// // draw upper bumper
// ndMatrix upperBumberMatrix(tireJoint->CalculateUpperBumperMatrix());
// //context.DrawFrame(tireJoint->CalculateUpperBumperMatrix());
//
// ndMatrix tireBaseFrame(tireJoint->CalculateBaseFrame());
// //context.DrawFrame(tireBaseFrame);
//
// // show tire center of mass;
// ndMatrix tireFrame(tireBody->GetMatrix());
// //context.DrawFrame(tireFrame);
// upperBumberMatrix.m_posit = tireFrame.m_posit;
// //context.DrawFrame(upperBumberMatrix);
//
// // draw tire forces
// const ndBodyKinematic::ndContactMap& contactMap = tireBody->GetContactMap();
// ndFloat32 tireGravities = scale /(totalMass * m_downForce.m_gravity);
// ndBodyKinematic::ndContactMap::Iterator it(contactMap);
// for (it.Begin(); it; it++)
// {
// ndContact* const contact = *it;
// if (contact->IsActive())
// {
// const ndContactPointList& contactPoints = contact->GetContactPoints();
// for (ndContactPointList::ndNode* contactNode = contactPoints.GetFirst(); contactNode; contactNode = contactNode->GetNext())
// {
// const ndContactMaterial& contactPoint = contactNode->GetInfo();
// ndMatrix frame(contactPoint.m_normal, contactPoint.m_dir0, contactPoint.m_dir1, contactPoint.m_point);
//
// ndVector localPosit(m_localFrame.UntransformVector(chassisMatrix.UntransformVector(contactPoint.m_point)));
// ndFloat32 offset = (localPosit.m_z > ndFloat32(0.0f)) ? ndFloat32(0.2f) : ndFloat32(-0.2f);
// frame.m_posit += contactPoint.m_dir0.Scale(offset);
// frame.m_posit += contactPoint.m_normal.Scale(0.1f);
//
// // normal force
// ndFloat32 normalForce = -tireGravities * contactPoint.m_normal_Force.m_force;
// context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_normal.Scale (normalForce), forceColor);
//
// // lateral force
// ndFloat32 lateralForce = -tireGravities * contactPoint.m_dir0_Force.m_force;
// context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_dir0.Scale(lateralForce), lateralColor);
//
// // longitudinal force
// ndFloat32 longitudinalForce = tireGravities * contactPoint.m_dir1_Force.m_force;
// context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_dir1.Scale(longitudinalForce), longitudinalColor);
// }
// }
// }
//}
//
//effectiveCom = effectiveCom.Scale(ndFloat32(1.0f) / totalMass);
//chassisMatrix.m_posit = effectiveCom;
//chassisMatrix.m_posit.m_w = ndFloat32(1.0f);
//context.DrawFrame(chassisMatrix);
}

//void ndMultiBodyVehicle::ApplyVehicleDynamicControl(ndFloat32 timestep, ndTireContactPair* const tireContacts, ndInt32 contactCount)
void ndMultiBodyVehicle::ApplyVehicleDynamicControl(ndFloat32, ndTireContactPair* const, ndInt32)
{
Expand Down Expand Up @@ -1053,3 +943,110 @@ void ndMultiBodyVehicle::PostUpdate(ndWorld* const, ndFloat32)
{
ApplyAlignmentAndBalancing();
}


void ndMultiBodyVehicle::Debug(ndConstraintDebugCallback& context) const
{
// draw vehicle cordinade system;
const ndBodyKinematic* const chassis = m_chassis;
ndAssert(chassis);
ndMatrix chassisMatrix(chassis->GetMatrix());
chassisMatrix.m_posit = chassisMatrix.TransformVector(chassis->GetCentreOfMass());
context.DrawFrame(chassisMatrix);

ndFloat32 totalMass = chassis->GetMassMatrix().m_w;
ndVector effectiveCom(chassisMatrix.m_posit.Scale(totalMass));

// draw front direction for side slip angle reference

// draw velocity vector
ndVector veloc(chassis->GetVelocity());
ndVector p0(chassisMatrix.m_posit + m_localFrame.m_up.Scale(1.0f));
ndVector p1(p0 + chassisMatrix.RotateVector(m_localFrame.m_front).Scale(2.0f));
ndVector p2(p0 + veloc.Scale (0.25f));

context.DrawLine(p0, p2, ndVector(1.0f, 1.0f, 0.0f, 0.0f));
context.DrawLine(p0, p1, ndVector(1.0f, 0.0f, 0.0f, 0.0f));

//// draw body acceleration
////ndVector accel(m_chassis->GetAccel());
////ndVector p3(p0 + accel.Scale(0.5f));
////context.DrawLine(p0, p3, ndVector(0.0f, 1.0f, 1.0f, 0.0f));

ndFloat32 scale = ndFloat32 (3.0f);
ndVector weight(chassis->GetForce().Scale (scale * chassis->GetInvMass() / m_downForce.m_gravity));

// draw vehicle weight;
ndVector forceColor(ndFloat32 (0.8f), ndFloat32(0.8f), ndFloat32(0.8f), ndFloat32(0.0f));
ndVector lateralColor(ndFloat32(0.3f), ndFloat32(0.7f), ndFloat32(0.0f), ndFloat32(0.0f));
ndVector longitudinalColor(ndFloat32(0.7f), ndFloat32(0.3f), ndFloat32(0.0f), ndFloat32(0.0f));
context.DrawLine(chassisMatrix.m_posit, chassisMatrix.m_posit + weight, forceColor);

for (ndList<ndMultiBodyVehicleTireJoint*>::ndNode* node = m_tireList.GetFirst(); node; node = node->GetNext())
{
ndMultiBodyVehicleTireJoint* const tireJoint = node->GetInfo();
ndBodyKinematic* const tireBody = tireJoint->GetBody0()->GetAsBodyDynamic();
ndMatrix tireFrame(tireBody->GetMatrix());
totalMass += tireBody->GetMassMatrix().m_w;
effectiveCom += tireFrame.m_posit.Scale(tireBody->GetMassMatrix().m_w);
}

for (ndList<ndMultiBodyVehicleTireJoint*>::ndNode* node = m_tireList.GetFirst(); node; node = node->GetNext())
{
ndMultiBodyVehicleTireJoint* const tireJoint = node->GetInfo();
ndBodyKinematic* const tireBody = tireJoint->GetBody0()->GetAsBodyDynamic();

// draw upper bumper
ndMatrix upperBumberMatrix(tireJoint->CalculateUpperBumperMatrix());
//context.DrawFrame(tireJoint->CalculateUpperBumperMatrix());

ndMatrix tireBaseFrame(tireJoint->CalculateBaseFrame());
//context.DrawFrame(tireBaseFrame);

// show tire center of mass;
ndMatrix tireFrame(tireBody->GetMatrix());
//context.DrawFrame(tireFrame);
upperBumberMatrix.m_posit = tireFrame.m_posit;
//context.DrawFrame(upperBumberMatrix);

// draw tire forces
const ndBodyKinematic::ndContactMap& contactMap = tireBody->GetContactMap();
ndFloat32 tireGravities = scale /(totalMass * m_downForce.m_gravity);
ndBodyKinematic::ndContactMap::Iterator it(contactMap);
for (it.Begin(); it; it++)
{
ndContact* const contact = *it;
if (contact->IsActive())
{
const ndContactPointList& contactPoints = contact->GetContactPoints();
for (ndContactPointList::ndNode* contactNode = contactPoints.GetFirst(); contactNode; contactNode = contactNode->GetNext())
{
const ndContactMaterial& contactPoint = contactNode->GetInfo();
ndMatrix frame(contactPoint.m_normal, contactPoint.m_dir0, contactPoint.m_dir1, contactPoint.m_point);

ndVector localPosit(m_localFrame.UntransformVector(chassisMatrix.UntransformVector(contactPoint.m_point)));
ndFloat32 offset = (localPosit.m_z > ndFloat32(0.0f)) ? ndFloat32(0.2f) : ndFloat32(-0.2f);
frame.m_posit += contactPoint.m_dir0.Scale(offset);
frame.m_posit += contactPoint.m_normal.Scale(0.1f);

// normal force
ndFloat32 normalForce = -tireGravities * contactPoint.m_normal_Force.m_force;
context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_normal.Scale (normalForce), forceColor);

// lateral force
ndFloat32 lateralForce = -tireGravities * contactPoint.m_dir0_Force.m_force;
context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_dir0.Scale(lateralForce), lateralColor);

// longitudinal force
ndFloat32 longitudinalForce = tireGravities * contactPoint.m_dir1_Force.m_force;
context.DrawLine(frame.m_posit, frame.m_posit + contactPoint.m_dir1.Scale(longitudinalForce), longitudinalColor);
}
}
}
}

effectiveCom = effectiveCom.Scale(ndFloat32(1.0f) / totalMass);
chassisMatrix.m_posit = effectiveCom;
chassisMatrix.m_posit.m_w = ndFloat32(1.0f);
context.DrawFrame(chassisMatrix);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ndMultiBodyVehicle : public ndModelArticulation
D_NEWTON_API ndMultiBodyVehicleDifferential* AddDifferential(ndFloat32 mass, ndFloat32 radius, ndMultiBodyVehicleTireJoint* const leftTire, ndMultiBodyVehicleTireJoint* const rightTire, ndFloat32 slipOmegaLock);
D_NEWTON_API ndMultiBodyVehicleDifferential* AddDifferential(ndFloat32 mass, ndFloat32 radius, ndMultiBodyVehicleDifferential* const leftDifferential, ndMultiBodyVehicleDifferential* const rightDifferential, ndFloat32 slipOmegaLock);

D_NEWTON_API virtual void Debug(ndConstraintDebugCallback& context) const;
D_NEWTON_API virtual void Update(ndWorld* const world, ndFloat32 timestep);
D_NEWTON_API virtual void PostUpdate(ndWorld* const world, ndFloat32 timestep);

Expand All @@ -100,9 +101,6 @@ class ndMultiBodyVehicle : public ndModelArticulation

private:
void ApplyVehicleDynamicControl(ndFloat32 timestep, ndTireContactPair* const tires, ndInt32 tireCount);

protected:
D_NEWTON_API virtual void Debug(ndConstraintDebugCallback& context) const;

#endif
private:
Expand Down

0 comments on commit eeca24c

Please sign in to comment.