public static RaceCar AddRaceCar(Scene scene, TransformNode parentTrans) { TransformNode transNode = new TransformNode(); transNode.Translation = new Vector3(0, 10, 10); Material carMat = new Material(); carMat.Diffuse = Color.Pink.ToVector4(); carMat.Specular = Color.White.ToVector4(); carMat.SpecularPower = 10; GeometryNode carNode = new GeometryNode("Race Car"); carNode.Model = new Box(3, 1.0f, 2); carNode.Material = carMat; carNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; NewtonPhysics physicsEngine = (NewtonPhysics)scene.PhysicsEngine; RaceCar car = new RaceCar(carNode, physicsEngine); for (int i = 0; i < 4; i++) { car.Tires[i] = CreateTire((TireID)Enum.ToObject(typeof(TireID), i), car.TireTransformNode[i], carNode, scene.PhysicsEngine.Gravity); } car.Collidable = true; car.Interactable = true; carNode.Physics = car; carNode.Physics.NeverDeactivate = true; carNode.AddToPhysicsEngine = true; parentTrans.AddChild(transNode); transNode.AddChild(carNode); Newton.NewtonSetBodyLeaveWorldEvent(physicsEngine.NewtonWorld, car.LeaveWorldCallback); return(car); }
public static RaceCar AddRaceCar(Scene scene, TransformNode Marke, Vector3 CarPos, GeometryNode CModel, int IdPlayer) { TransformNode transNode = new TransformNode(); transNode.Name = "Tcar:" + IdPlayer; transNode.Translation = CarPos; CModel.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; NewtonPhysics physicsEngine = (NewtonPhysics)scene.PhysicsEngine; RaceCar car = new RaceCar(CModel, physicsEngine); for (int i = 0; i < 4; i++) { car.Tires[i] = CreateTire((TireID)Enum.ToObject(typeof(TireID), i), car.TireTransformNode[i], CModel, scene.PhysicsEngine.Gravity); } car.Collidable = true; car.Interactable = true; car.StartPos = CarPos; //Physic material Name car.MaterialName = "Car" + IdPlayer; CModel.Physics = car; CModel.Physics.NeverDeactivate = true; CModel.AddToPhysicsEngine = true; //Add To node transNode.AddChild(CModel); Marke.AddChild(transNode); Newton.NewtonSetBodyLeaveWorldEvent(physicsEngine.NewtonWorld, car.LeaveWorldCallback); return(car); }
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); }; }
public static RaceCar AddRaceCarOld(Scene scene, TransformNode parentTrans, Vector3 CarPos, Vector4 CarColor, CarType_Enum CType) { TransformNode transNode = new TransformNode(); //transNode.Scale = new Vector3(2.8f, 2.8f, 2.8f); transNode.Translation = CarPos; Material carMat = new Material(); carMat.Diffuse = CarColor; carMat.Specular = Color.White.ToVector4(); carMat.SpecularPower = 10; GeometryNode carNode = new GeometryNode("Race Car"); //Intento cargar un modelo ModelLoader loader = new ModelLoader(); //Cargo el modelo Dependiendo del tipo switch (CType) { case CarType_Enum.Basic: carNode.Model = new Box(3, 1.0f, 2); carNode.Material = carMat; break; case CarType_Enum.Ferrary: carNode.Model = (Model)loader.Load("Models", "ferrari"); ((Model)carNode.Model).UseInternalMaterials = true; break; case CarType_Enum.Tank: carNode.Model = (Model)loader.Load("Models", "Osirian_Battle_Tank"); ((Model)carNode.Model).UseInternalMaterials = true; break; default: carNode.Model = new Box(3, 1.0f, 2); carNode.Material = carMat; break; } carNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; NewtonPhysics physicsEngine = (NewtonPhysics)scene.PhysicsEngine; RaceCar car = new RaceCar(carNode, physicsEngine); for (int i = 0; i < 4; i++) { car.Tires[i] = CreateTire((TireID)Enum.ToObject(typeof(TireID), i), car.TireTransformNode[i], carNode, scene.PhysicsEngine.Gravity); } car.Collidable = true; car.Interactable = true; car.StartPos = CarPos; carNode.Physics = car; carNode.Physics.NeverDeactivate = true; carNode.AddToPhysicsEngine = true; parentTrans.AddChild(transNode); transNode.AddChild(carNode); Newton.NewtonSetBodyLeaveWorldEvent(physicsEngine.NewtonWorld, car.LeaveWorldCallback); return(car); }