Skip to content

Commit

Permalink
re enabling vehicle (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Jan 3, 2025
1 parent 0725b13 commit 564242d
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 202 deletions.
145 changes: 64 additions & 81 deletions newton-4.00/applications/ndSandbox/demos/ndBasicVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon
ndDemoEntity* const vehicleEntity = vehicleEntityDummyRoot->GetFirstChild();
vehicleEntity->ResetMatrix(vehicleEntity->CalculateGlobalMatrix() * matrix);

ndPhysicsWorld* const world = scene->GetWorld();
//ndPhysicsWorld* const world = scene->GetWorld();

// 1- add chassis to the vehicle mode
// create the vehicle chassis as a normal rigid body
AddChassis(ndSharedPtr<ndBody>(CreateChassis(scene, vehicleEntity, m_configuration.m_chassisMass)));
ndBodyDynamic* const chassis = m_chassis->GetAsBodyDynamic();
Expand All @@ -270,12 +271,7 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon
com += m_localFrame.m_right.Scale(m_configuration.m_comDisplacement.m_z);
chassis->SetCentreOfMass(com);

//// 1- add chassis to the vehicle mode
//SetChassis(chassis);
//ndSharedPtr<ndBody> bodyPtr(chassis);
//world->AddBody(bodyPtr);

// 2- each tire to the model,
// 2- each tire
// create the tire as a normal rigid body
// and attach them to the chassis with a tire joints
ndVehicleDectriptor::ndTireDefinition rr_tireConfiguration(m_configuration.m_rearTire);
Expand All @@ -292,17 +288,9 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon
ndMultiBodyVehicleTireJoint* const fr_tire = AddTire(fr_tireConfiguration, fr_tire_body);
ndMultiBodyVehicleTireJoint* const fl_tire = AddTire(fl_tireConfiguration, fl_tire_body);

//ndSharedPtr<ndBody> rr_tire_body_Ptr(rr_tire_body);
//ndSharedPtr<ndBody> rl_tire_body_Ptr(rl_tire_body);
//ndSharedPtr<ndBody> fr_tire_body_Ptr(fr_tire_body);
//ndSharedPtr<ndBody> fl_tire_body_Ptr(fl_tire_body);
//world->AddBody(rr_tire_body_Ptr);
//world->AddBody(rl_tire_body_Ptr);
//world->AddBody(fr_tire_body_Ptr);
//world->AddBody(fl_tire_body_Ptr);

m_currentGear = sizeof(m_configuration.m_transmission.m_forwardRatios) / sizeof(m_configuration.m_transmission.m_forwardRatios[0]) + 1;

// 3- add differential
// add the slip differential
ndMultiBodyVehicleDifferential* differential = nullptr;
switch (m_configuration.m_differentialType)
Expand Down Expand Up @@ -333,78 +321,76 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon
break;
}
}
ndTrace(("xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"));
#if 0

// add a motor

// 4- add a motor
ndMultiBodyVehicleMotor* const motor = AddMotor(m_configuration.m_motorMass, m_configuration.m_motorRadius);
motor->SetMaxRpm(m_configuration.m_engine.GetRedLineRadPerSec() * dRadPerSecToRpm);
motor->SetFrictionLoss(m_configuration.m_engine.GetTorque(0.0f) * 0.5f);

// add the gear box
// 5- add the gear box
ndMultiBodyVehicleGearBox* const gearBox = AddGearBox(differential);
gearBox->SetIdleOmega(m_configuration.m_engine.GetIdleRadPerSec() * dRadPerSecToRpm);

switch (m_configuration.m_torsionBarType)
{
case ndVehicleDectriptor::m_noWheelAxle:
{
// no torsion bar
break;
}

case ndVehicleDectriptor::m_rearWheelAxle:
{
ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
torsionBar->AddAxel(rl_tire->GetBody0(), rr_tire->GetBody0());
torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
break;
}

case ndVehicleDectriptor::m_frontWheelAxle:
{
ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
torsionBar->AddAxel(fl_tire->GetBody0(), fr_tire->GetBody0());
torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
break;
}

case ndVehicleDectriptor::m_fourWheelAxle:
{
ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
torsionBar->AddAxel(rl_tire->GetBody0(), rr_tire->GetBody0());
torsionBar->AddAxel(fl_tire->GetBody0(), fr_tire->GetBody0());
torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
break;
}
}
//switch (m_configuration.m_torsionBarType)
//{
// case ndVehicleDectriptor::m_noWheelAxle:
// {
// // no torsion bar
// break;
// }
//
// case ndVehicleDectriptor::m_rearWheelAxle:
// {
// ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
// torsionBar->AddAxel(rl_tire->GetBody0(), rr_tire->GetBody0());
// torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
// break;
// }
//
// case ndVehicleDectriptor::m_frontWheelAxle:
// {
// ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
// torsionBar->AddAxel(fl_tire->GetBody0(), fr_tire->GetBody0());
// torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
// break;
// }
//
// case ndVehicleDectriptor::m_fourWheelAxle:
// {
// ndMultiBodyVehicleTorsionBar* const torsionBar = AddTorsionBar(world->GetSentinelBody());
// torsionBar->AddAxel(rl_tire->GetBody0(), rr_tire->GetBody0());
// torsionBar->AddAxel(fl_tire->GetBody0(), fr_tire->GetBody0());
// torsionBar->SetTorsionTorque(m_configuration.m_torsionBarSpringK, m_configuration.m_torsionBarDamperC, m_configuration.m_torsionBarRegularizer);
// break;
// }
//}

// set a soft or hard mode
SetVehicleSolverModel(m_configuration.m_useHardSolverMode ? true : false);

// prepare data for animating suspension
m_rearAxlePivot = vehicleEntity->Find("rearAxlePivot");
m_frontAxlePivot = vehicleEntity->Find("frontAxlePivot");
m_rr_tire = rr_tire;
m_rl_tire = rl_tire;
m_fr_tire = fr_tire;
m_fl_tire = fl_tire;

// load all engine sound channels
ndSoundManager* const soundManager = world->GetSoundManager();

m_startEngine = false;
m_startEngineMemory = false;
m_startSound = soundManager->CreateSoundChannel(engineSounds[0]);
m_engineRpmSound = soundManager->CreateSoundChannel(engineSounds[1]);
m_skipMarks = soundManager->CreateSoundChannel(engineSounds[2]);

m_startSound->SetAttenuationRefDistance(20.0f, 40.0f, 50.0f);
m_engineRpmSound->SetAttenuationRefDistance(20.0f, 40.0f, 50.0f);

m_engineRpmSound->SetLoop(true);
m_skipMarks->SetLoop(true);
#endif
//// prepare data for animating suspension
//m_rearAxlePivot = vehicleEntity->Find("rearAxlePivot");
//m_frontAxlePivot = vehicleEntity->Find("frontAxlePivot");
//m_rr_tire = rr_tire;
//m_rl_tire = rl_tire;
//m_fr_tire = fr_tire;
//m_fl_tire = fl_tire;
//
//// load all engine sound channels
//ndSoundManager* const soundManager = world->GetSoundManager();
//
//m_startEngine = false;
//m_startEngineMemory = false;
//m_startSound = soundManager->CreateSoundChannel(engineSounds[0]);
//m_engineRpmSound = soundManager->CreateSoundChannel(engineSounds[1]);
//m_skipMarks = soundManager->CreateSoundChannel(engineSounds[2]);
//
//m_startSound->SetAttenuationRefDistance(20.0f, 40.0f, 50.0f);
//m_engineRpmSound->SetAttenuationRefDistance(20.0f, 40.0f, 50.0f);
//
//m_engineRpmSound->SetLoop(true);
//m_skipMarks->SetLoop(true);
}

~ndBasicMultiBodyVehicle()
Expand Down Expand Up @@ -464,8 +450,6 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon

void SetCamera(ndDemoEntityManager* const manager, ndFloat32)
{
ndAssert(0);
#if 0
ndDemoCamera* const camera = manager->GetCamera();
ndDemoEntity* const chassisEntity = (ndDemoEntity*)m_chassis->GetNotifyCallback()->GetUserData();
ndMatrix camMatrix(camera->GetNextMatrix());
Expand All @@ -477,7 +461,6 @@ class ndBasicMultiBodyVehicle : public ndVehicleCommon
camOrigin -= frontDir.Scale(10.0f);

camera->SetNextMatrix(camMatrix, camOrigin);
#endif
}

void ApplyInputs(ndWorld* const, ndFloat32)
Expand Down Expand Up @@ -712,8 +695,8 @@ void ndBasicVehicle (ndDemoEntityManager* const scene)
//world->AddModel(vehicle3);

//ndBasicMultiBodyVehicle* const vehicle = (ndBasicMultiBodyVehicle*)*vehicle3;
//ndBasicMultiBodyVehicle* const vehicle = (ndBasicMultiBodyVehicle*)*vehicle0;
//vehicle->SetAsPlayer(scene);
ndBasicMultiBodyVehicle* const vehicle = (ndBasicMultiBodyVehicle*)*vehicle0;
vehicle->SetAsPlayer(scene);
matrix.m_posit.m_x += 5.0f;
TestPlayerCapsuleInteraction(scene, matrix);

Expand Down
4 changes: 1 addition & 3 deletions newton-4.00/applications/ndSandbox/toolbox/ndVehicleUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,6 @@ void ndVehicleUI::RenderUI()
{
return;
}
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
ndAssert(0);
//ndFloat32 gageSize = 200.0f;
//ndFloat32 y = (ndFloat32)m_scene->GetHeight() - (gageSize / 2.0f + 20.0f);

Expand All @@ -448,7 +446,7 @@ void ndVehicleUI::RenderUI()
ndFloat32 maxRpm = m_vehicle->m_configuration.m_engine.GetRedLineRadPerSec() * dRadPerSecToRpm;
maxRpm += 500.0f;

ndAssert(0);
//ndAssert(0);
//ndFloat32 rpm = (m_vehicle->m_motor->GetRpm() / maxRpm) * 2.85f;
//
//glUseProgram(m_shaderHandle);
Expand Down
132 changes: 63 additions & 69 deletions newton-4.00/sdk/dNewton/dModels/dVehicle/ndMultiBodyVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,6 @@ ndMultiBodyVehicleTireJoint* ndMultiBodyVehicle::AddAxleTire(const ndMultiBodyVe
//return *tireJoint;
}


//ndMultiBodyVehicleMotor* ndMultiBodyVehicle::AddMotor(ndFloat32 mass, ndFloat32 radius)
ndMultiBodyVehicleMotor* ndMultiBodyVehicle::AddMotor(ndFloat32, ndFloat32)
{
ndAssert(0);
return nullptr;

//ndAssert(m_chassis);
//ndSharedPtr<ndBody> motorBody (CreateInternalBodyPart(mass, radius));
//m_internalBodies.Append(motorBody);
//m_motor = ndSharedPtr<ndMultiBodyVehicleMotor>(new ndMultiBodyVehicleMotor(motorBody->GetAsBodyKinematic(), this));
//return *m_motor;
}

//ndMultiBodyVehicleGearBox* ndMultiBodyVehicle::AddGearBox(ndMultiBodyVehicleDifferential* const differential)
ndMultiBodyVehicleGearBox* ndMultiBodyVehicle::AddGearBox(ndMultiBodyVehicleDifferential* const)
{
ndAssert(0);
return nullptr;

//ndAssert(*m_motor);
//m_gearBox = ndSharedPtr<ndMultiBodyVehicleGearBox>(new ndMultiBodyVehicleGearBox(m_motor->GetBody0(), differential->GetBody0(), this));
//return *m_gearBox;
}

//ndMultiBodyVehicleTorsionBar* ndMultiBodyVehicle::AddTorsionBar(ndBodyKinematic* const sentinel)
ndMultiBodyVehicleTorsionBar* ndMultiBodyVehicle::AddTorsionBar(ndBodyKinematic* const)
{
Expand Down Expand Up @@ -195,49 +170,6 @@ void ndMultiBodyVehicle::ApplyAerodynamics()
//}
}

//void ndMultiBodyVehicle::SetVehicleSolverModel(bool hardJoint)
void ndMultiBodyVehicle::SetVehicleSolverModel(bool)
{
ndAssert(0);

//ndJointBilateralSolverModel openLoopMode = hardJoint ? m_jointkinematicOpenLoop : m_jointIterativeSoft;
//
//ndAssert(m_chassis);
//const ndBodyKinematic::ndJointList& chassisJoints = m_chassis->GetJointList();
//for (ndBodyKinematic::ndJointList::ndNode* node = chassisJoints.GetFirst(); node; node = node->GetNext())
//{
// ndJointBilateralConstraint* const joint = node->GetInfo();
// const char* const className = joint->ClassName();
// if (!strcmp(className, "ndMultiBodyVehicleTireJoint") ||
// !strcmp(className, "ndMultiBodyVehicleDifferential") ||
// !strcmp(className, "ndMultiBodyVehicleMotor"))
// {
// joint->SetSolverModel(openLoopMode);
// }
//}
//
//ndJointBilateralSolverModel driveTrainMode = hardJoint ? m_jointkinematicCloseLoop : m_jointIterativeSoft;
//for (ndReferencedObjects<ndMultiBodyVehicleDifferential>::ndNode* node = m_differentialList.GetFirst(); node; node = node->GetNext())
//{
// ndJointBilateralConstraint* const joint = *node->GetInfo();
// const ndBodyKinematic::ndJointList& jointList = joint->GetBody0()->GetJointList();
// for (ndBodyKinematic::ndJointList::ndNode* node1 = jointList.GetFirst(); node1; node1 = node1->GetNext())
// {
// ndJointBilateralConstraint* const axle = node1->GetInfo();
// const char* const clasName = axle->ClassName();
// if (strcmp(clasName, "ndMultiBodyVehicleDifferential"))
// {
// axle->SetSolverModel(driveTrainMode);
// }
// }
//}
//
//if (*m_torsionBar)
//{
// m_torsionBar->SetSolverModel(driveTrainMode);
//}
}

void ndMultiBodyVehicle::ApplyalignmentAndBalancing()
{
ndAssert(0);
Expand Down Expand Up @@ -948,6 +880,11 @@ ndMultiBodyVehicle::ndMultiBodyVehicle(const ndVector& frontDir, const ndVector&
////,m_differentialList()
//,m_downForce()
{
m_motor = nullptr;
m_gearBox = nullptr;
m_chassis = nullptr;
m_torsionBar = nullptr;

m_tireShape->AddRef();
m_localFrame.m_front = (frontDir & ndVector::m_triplexMask).Normalize();
m_localFrame.m_up = upDir & ndVector::m_triplexMask;
Expand All @@ -967,6 +904,42 @@ void ndMultiBodyVehicle::AddChassis(const ndSharedPtr<ndBody>& chassis)
ndAssert(m_chassis);
}

void ndMultiBodyVehicle::SetVehicleSolverModel(bool hardJoint)
{
ndAssert(m_chassis);
ndJointBilateralSolverModel openLoopMode = hardJoint ? m_jointkinematicOpenLoop : m_jointIterativeSoft;
for (ndNode* node = GetRoot()->GetFirstChild(); node; node = node->GetNext())
{
ndJointBilateralConstraint* const joint = *node->m_joint;
const char* const className = joint->ClassName();
if (!strcmp(className, "ndMultiBodyVehicleTireJoint") ||
!strcmp(className, "ndMultiBodyVehicleDifferential") ||
!strcmp(className, "ndMultiBodyVehicleMotor"))
{
joint->SetSolverModel(openLoopMode);
}
}

ndJointBilateralSolverModel driveTrainMode = hardJoint ? m_jointkinematicCloseLoop : m_jointIterativeSoft;
for (ndList<ndNode>::ndNode* node = m_closeLoops.GetFirst(); node; node = node->GetNext())
{
ndModelArticulation::ndNode& closeLoop = node->GetInfo();
ndJointBilateralConstraint* const joint = *closeLoop.m_joint;
const char* const clasName = joint->ClassName();
if (strcmp(clasName, "ndMultiBodyVehicleDifferential") || strcmp(clasName, "ndMultiBodyVehicleGearBox"))
{
joint->SetSolverModel(driveTrainMode);
}
}

if (m_torsionBar)
{
ndAssert(0);
//m_torsionBar->SetSolverModel(driveTrainMode);
}
}


ndMultiBodyVehicleTireJoint* ndMultiBodyVehicle::AddTire(const ndMultiBodyVehicleTireJointInfo& desc, const ndSharedPtr<ndBody>& tire)
{
ndAssert(m_chassis);
Expand Down Expand Up @@ -1049,7 +1022,6 @@ ndMultiBodyVehicleDifferential* ndMultiBodyVehicle::AddDifferential(ndFloat32 ma
return (ndMultiBodyVehicleDifferential*) *differential;
}


ndMultiBodyVehicleDifferential* ndMultiBodyVehicle::AddDifferential(ndFloat32 mass, ndFloat32 radius, ndMultiBodyVehicleDifferential* const leftDifferential, ndMultiBodyVehicleDifferential* const rightDifferential, ndFloat32 slipOmegaLock)
{
ndAssert(m_chassis);
Expand Down Expand Up @@ -1079,3 +1051,25 @@ ndMultiBodyVehicleDifferential* ndMultiBodyVehicle::AddDifferential(ndFloat32 ma
AddCloseLoop(rightAxle);
return (ndMultiBodyVehicleDifferential*)*differential;
}

ndMultiBodyVehicleMotor* ndMultiBodyVehicle::AddMotor(ndFloat32 mass, ndFloat32 radius)
{
ndAssert(m_chassis);
ndSharedPtr<ndBody> motorBody (CreateInternalBodyPart(mass, radius));
//m_internalBodies.Append(motorBody);
//m_motor = ndSharedPtr<ndMultiBodyVehicleMotor>(new ndMultiBodyVehicleMotor(motorBody->GetAsBodyKinematic(), this));
ndSharedPtr<ndJointBilateralConstraint> motorJoint(new ndMultiBodyVehicleMotor(motorBody->GetAsBodyKinematic(), this));
AddLimb(GetRoot(), motorBody, motorJoint);
m_motor = (ndMultiBodyVehicleMotor*)*motorJoint;
return m_motor;
}

ndMultiBodyVehicleGearBox* ndMultiBodyVehicle::AddGearBox(ndMultiBodyVehicleDifferential* const differential)
{
ndAssert(m_motor);
//m_gearBox = ndSharedPtr<ndMultiBodyVehicleGearBox>(new ndMultiBodyVehicleGearBox(m_motor->GetBody0(), differential->GetBody0(), this));
ndSharedPtr<ndJointBilateralConstraint> gearBox(new ndMultiBodyVehicleGearBox(m_motor->GetBody0(), differential->GetBody0(), this));
AddCloseLoop(gearBox);
m_gearBox = (ndMultiBodyVehicleGearBox*)*gearBox;
return m_gearBox;
}
Loading

0 comments on commit 564242d

Please sign in to comment.