Example #1
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
void
PhysicsActor::applyForce(const Math::Vector3& _force)
{
    NewtonBodyAddForce(m_pActor, _force.m_array);
}
    void SubmitConstraints (dFloat timestep, int threadIndex)
    {
        dMatrix tirePivotMatrix;
        dMatrix chassisPivotMatrix;

        ProjectTireMatrix();

        // calculate the position of the pivot point and the Jacobian direction vectors, in global space.
        CalculateGlobalMatrix (m_chassisLocalMatrix, m_tireLocalMatrix, chassisPivotMatrix, tirePivotMatrix);

        // Restrict the movement on the pivot point along all two orthonormal direction
        dVector centerInTire (tirePivotMatrix.m_posit);
        dVector centerInChassis (chassisPivotMatrix.m_posit + chassisPivotMatrix.m_up.Scale ((centerInTire - chassisPivotMatrix.m_posit) % chassisPivotMatrix.m_up));
        NewtonUserJointAddLinearRow (m_joint, &centerInChassis[0], &centerInTire[0], &chassisPivotMatrix.m_front[0]);
        NewtonUserJointAddLinearRow (m_joint, &centerInChassis[0], &centerInTire[0], &chassisPivotMatrix.m_right[0]);

        // get a point along the pin axis at some reasonable large distance from the pivot
        dVector pointInPinInTire (centerInChassis + chassisPivotMatrix.m_front.Scale(MIN_JOINT_PIN_LENGTH));
        dVector pointInPinInChassis (centerInTire + tirePivotMatrix.m_front.Scale(MIN_JOINT_PIN_LENGTH));
        NewtonUserJointAddLinearRow (m_joint, &pointInPinInTire[0], &pointInPinInChassis[0], &chassisPivotMatrix.m_right[0]);
        NewtonUserJointAddLinearRow (m_joint, &pointInPinInTire[0], &pointInPinInChassis[0], &chassisPivotMatrix.m_up[0]);

        //calculate the suspension spring and damper force
        dFloat dist;
        dFloat speed;
        dFloat force;

        dVector tireVeloc;
        dVector chassisVeloc;
        dVector chassisOmega;
        dVector chassisCom;
        dMatrix chassisMatrix;

        const NewtonBody* tire;
        const NewtonBody* chassis;

        // calculate the velocity of tire attachments point on the car chassis

        tire = GetBody1();
        chassis = GetBody0();

        NewtonBodyGetVelocity (tire, &tireVeloc[0]);
        NewtonBodyGetVelocity (chassis, &chassisVeloc[0]);
        NewtonBodyGetOmega (chassis, &chassisOmega[0]);
        NewtonBodyGetMatrix (chassis, &chassisMatrix[0][0]);
        NewtonBodyGetCentreOfMass (chassis, &chassisCom[0]);
        chassisCom = chassisMatrix.TransformVector(chassisCom);
        chassisVeloc += chassisOmega * (centerInChassis - chassisCom);


        // get the spring damper parameters
        speed = (chassisVeloc - tireVeloc) % chassisPivotMatrix.m_up;
        dist = (chassisPivotMatrix.m_posit - tirePivotMatrix.m_posit) % chassisPivotMatrix.m_up;
        // check if the suspension pass the bumpers limits
        if (-dist > m_suspenstionSpan* 0.5f) {
            // if it hit the bumpers then speed is zero
            speed = 0;
            NewtonUserJointAddLinearRow (m_joint, &centerInChassis[0], &centerInChassis[0], &chassisPivotMatrix.m_up[0]);
            NewtonUserJointSetRowMinimumFriction(m_joint, 0.0f);
        } else if (dist > 0.0f) {
            // if it hit the bumpers then speed is zero
            speed = 0;
            NewtonUserJointAddLinearRow (m_joint, &centerInChassis[0], &centerInChassis[0], &chassisPivotMatrix.m_up[0]);
            NewtonUserJointSetRowMaximumFriction(m_joint, 0.0f);
        }

        // calculate magnitude of suspension force
        force = NewtonCalculateSpringDamperAcceleration (timestep, m_spring, dist, m_damper, speed) * m_effectiveSpringMass;

        dVector chassisForce (chassisMatrix.m_up.Scale (force));
        dVector chassisTorque ((centerInChassis - chassisCom) * chassisForce);
        NewtonBodyAddForce (chassis, &chassisForce[0]);
        NewtonBodyAddTorque (chassis, &chassisTorque[0]);

        dVector tireForce (chassisForce.Scale (-1.0f));
        NewtonBodyAddForce(tire, &tireForce[0]);

        // apply the engine torque to tire torque
        dFloat relOmega;
        dMatrix tireMatrix;
        dVector tireOmega;

        NewtonBodyGetOmega(tire, &tireOmega[0]);
        NewtonBodyGetMatrix (tire, &tireMatrix[0][0]);

        relOmega = ((tireOmega - chassisOmega) % tireMatrix.m_front);

        // apply engine torque plus some tire angular drag
        dVector tireTorque (tireMatrix.m_front.Scale (m_enginetorque - relOmega * m_Ixx * m_angularDragCoef));
        NewtonBodyAddTorque (tire, &tireTorque[0]);

        dVector chassisReationTorque (chassisMatrix.m_right.Scale (- m_enginetorque));
        NewtonBodyAddTorque(chassis, &chassisTorque[0]);

        m_enginetorque = 0.0f;

        // add the brake torque row
        if (dAbs(m_brakeToque) > 1.0e-3f) {

            relOmega /= timestep;
            NewtonUserJointAddAngularRow (m_joint, 0.0f, &tireMatrix.m_front[0]);
            NewtonUserJointSetRowAcceleration(m_joint, relOmega);
            NewtonUserJointSetRowMaximumFriction(m_joint, m_brakeToque);
            NewtonUserJointSetRowMinimumFriction(m_joint, -m_brakeToque);
        }
        m_brakeToque = 0.0f;

    }