Пример #1
0
        public RaceCar(Object container, NewtonPhysics engine)
            : base(container)
        {
            this.engine = engine;

            //Crea los cuatro nodos de transformacion para las 4 Ruedas
            tireTransNode = new TransformNode[4];
            for (int i = 0; i < 4; i++)
                tireTransNode[i] = new TransformNode();

            // set the mass of the race car - Propiedad heredada
            mass = VEHICLE_MASS;

            // lower the center of the mass for a race car
            centerOfMass = -Vector3.UnitY * 1.5f;

            // sets up the callback function when body moves in the simulation
            transformCallback = delegate(IntPtr body, float[] matrix)
            {
                Matrix mat = MatrixHelper.FloatsToMatrix(matrix);

                // set the transformation of the vehicle body
                PhysicsWorldTransform = mat;

                Matrix invMat = Matrix.Invert(mat);
                float[] tireMatrix = new float[16];
                NewtonTire tire = null;
                float sign = 0;
                float angle = 0;
                float brakePosition = 0;

                // set the global matrix for each tire
                for (IntPtr tyreId = Newton.NewtonVehicleGetFirstTireID(joint);
                    tyreId != IntPtr.Zero; tyreId = Newton.NewtonVehicleGetNextTireID(joint, tyreId))
                {
                    int tireID = (int)GetTireID(tyreId);
                    tire = tires[tireID];
                    Newton.NewtonVehicleGetTireMatrix(joint, tyreId, tireMatrix);

                    // calculate the local matrix
                    Matrix tireMat = MatrixHelper.GetRotationMatrix(
                        MatrixHelper.FloatsToMatrix(tireMatrix) * invMat) * tire.TireOffsetMatrix;
                    tire.TireMatrix = tireMat;
                    tireTransNode[tireID].WorldTransformation = Matrix.CreateRotationX(MathHelper.PiOver2)
                        * tireMat;

                    // calcualte the parametric brake position
                    brakePosition = tireMat.Translation.Y - tire.TireRefHeight;
                    tire.BrakeMatrix = Matrix.CreateTranslation(0, tire.BrakeRefPosition.Y + brakePosition, 0);

                    // set suspensionMatrix
                    sign = (tire.BrakeRefPosition.Z > 0) ? 1 : -1;
                    angle = (float)Math.Atan2(sign * brakePosition, Math.Abs(tire.BrakeRefPosition.Z));
                    Matrix rotationMatrix = new Matrix(
                        1, 0, 0, 0,
                        0, (float)Math.Cos(angle), (float)Math.Sin(angle), 0,
                        0, -(float)Math.Sin(angle), (float)Math.Cos(angle), 0,
                        0, 0, 0, 1);
                    tire.AxelMatrix = rotationMatrix * tire.AxelMatrix;
                    tire.SuspensionTopMatrix = rotationMatrix * tire.SuspensionTopMatrix;
                    tire.SuspensionBottomMatrix = rotationMatrix * tire.SuspensionBottomMatrix;
                }
            };

            forceCallback = delegate(IntPtr body)
            {
                float Ixx = 0, Iyy = 0, Izz = 0, tmpMass = 0;

                Newton.NewtonBodyGetMassMatrix(body, ref tmpMass, ref Ixx, ref Iyy, ref Izz);
                tmpMass *= (1.0f + (float)Math.Abs(GetSpeed()) / 20.0f);
                float[] force = Vector3Helper.ToFloats(engine.GravityDirection * engine.Gravity * tmpMass);

                Newton.NewtonBodySetForce(body, force);
            };

            tireUpdate = delegate(IntPtr vehicleJoint)
            {
                NewtonTire tire = null;

                for (IntPtr tyreId = Newton.NewtonVehicleGetFirstTireID(vehicleJoint);
                    tyreId != IntPtr.Zero; tyreId = Newton.NewtonVehicleGetNextTireID(vehicleJoint, tyreId))
                {
                    tire = tires[(int)GetTireID(tyreId)];

                    // If the tire is a front tire
                    if ((tire == tires[(int)TireID.FrontLeft]) || (tire == tires[(int)TireID.FrontRight]))
                    {
                        float currSteerAngle = Newton.NewtonVehicleGetTireSteerAngle(vehicleJoint, tyreId);
                        Newton.NewtonVehicleSetTireSteerAngle(vehicleJoint, tyreId,
                            currSteerAngle + (tire.Steer - currSteerAngle) * 0.035f);
                    }
                    else // if the tire is a rear tire
                    {
                        Newton.NewtonVehicleSetTireTorque(vehicleJoint, tyreId, tire.Torque);

                        if (tire.Brakes > 0)
                        {
                            // ask Newton for the precise acceleration needed to stop the tire
                            float brakeAcceleration =
                                Newton.NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicleJoint, tyreId);

                            // tell Newton you want this tire stoped but only if the torque need it is less than
                            // the brakes pad can withstand (assume max brake pad torque is 500 newton * meter)
                            Newton.NewtonVehicleTireSetBrakeAcceleration(vehicleJoint, tyreId,
                                brakeAcceleration, 10000.0f);

                            // set some side slip as function of the linear speed
                            float speed = Newton.NewtonVehicleGetTireLongitudinalSpeed(vehicleJoint, tyreId);
                            Newton.NewtonVehicleSetTireMaxSideSleepSpeed(vehicleJoint, tyreId, speed * 0.1f);
                        }
                    }
                }
            };

            bodyLeaveCallback = delegate(IntPtr body)
            {
                Respawn(body);
            };
        }
Пример #2
0
 /// <summary>
 /// A callback function that will be called when the box and sphere model collides
 /// </summary>
 /// <param name="pair"></param>
 private void BoxSphereCollision(NewtonPhysics.CollisionPair pair)
 {
     Console.WriteLine("Box and Sphere has collided");
 }
 private void humveeCollision(NewtonPhysics.CollisionPair pair)
 {
     label = "Humvee";
     Console.WriteLine(label);
     ((Model)gears.Model).ShowBoundingBox = false;
     ((Model)cup.Model).ShowBoundingBox = false;
     ((Model)g36c.Model).ShowBoundingBox = false;
     ((Model)humvee.Model).ShowBoundingBox = true;
 }