private void ConfigCallBacks() { Console.WriteLine(DateTime.Now.ToString() + " Setup Collisions CallBacks\n"); // Create physics material to detect collision NewtonMaterial BullGroMat = new NewtonMaterial(); // Bullet to Ground interaction BullGroMat.MaterialName1 = "Bullet"; BullGroMat.MaterialName2 = "Ground"; // Callback Function BullGroMat.ContactProcessCallback = GameLogic.CallbackBulletGround; // Add the physics Material interaction ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullGroMat); // Create physics material to detect collision NewtonMaterial BullObstMat = new NewtonMaterial(); // Bullet to Ground interaction BullObstMat.MaterialName1 = "Bullet"; BullObstMat.MaterialName2 = "Obstacle"; // Callback Function BullObstMat.ContactProcessCallback = GameLogic.CallbackBulletObst; // Add the physics Material interaction ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullObstMat); ////////////////////////////////////// // Create physics material to detect collision NewtonMaterial BullCarMat1 = new NewtonMaterial(); BullCarMat1.MaterialName1 = "Bullet"; BullCarMat1.MaterialName2 = "Car0"; BullCarMat1.ContactProcessCallback = GameLogic.CallbackBulletHitCar0; //BullCarMat1.ContactBeginCallback = puercavida; //BullCarMat1.ContactEndCallback = GameLogic.oooocall; ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullCarMat1); NewtonMaterial BullCarMat2 = new NewtonMaterial(); BullCarMat2.MaterialName1 = "Bullet"; BullCarMat2.MaterialName2 = "Car1"; BullCarMat2.ContactProcessCallback = GameLogic.CallbackBulletHitCar1; ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullCarMat2); NewtonMaterial BullCarMat3 = new NewtonMaterial(); BullCarMat3.MaterialName1 = "Bullet"; BullCarMat3.MaterialName2 = "Car2"; BullCarMat3.ContactProcessCallback = GameLogic.CallbackBulletHitCar2; ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullCarMat3); NewtonMaterial BullCarMat4 = new NewtonMaterial(); BullCarMat4.MaterialName1 = "Bullet"; BullCarMat4.MaterialName2 = "Car3"; BullCarMat4.ContactProcessCallback = GameLogic.CallbackBulletHitCar3; ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(BullCarMat4); }
private void SetupPhysics() { scene.PhysicsEngine = new NewtonPhysics(); // Make the physics simulation space larger to 500x500 centered at the origin ((NewtonPhysics)scene.PhysicsEngine).WorldSize = new BoundingBox(Vector3.One * -250, Vector3.One * 250); // Increase the gravity scene.PhysicsEngine.Gravity = 60.0f; ((NewtonPhysics)scene.PhysicsEngine).MaxSimulationSubSteps = 5; // Creates several physics material to associate appropriate collision sounds for each // different materials NewtonMaterial physMat = new NewtonMaterial(); // Domino to domino material interaction physMat.MaterialName1 = "Domino"; physMat.MaterialName2 = "Domino"; // Defines the callback function when the two materials contact/collide physMat.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { // Only play sound if the collision/contact speed is above 4 if (contactSpeed > 4f) { // If we're already playing more than the limited number of sounds, then // don't play if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { // Set the volume proportional to the collision/contact speed Sound.SetVolume("Default", contactSpeed / 4 * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(woodHitWood3Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; NewtonMaterial physMat2 = new NewtonMaterial(); // Gound to ball material interaction physMat2.MaterialName1 = "Ground"; physMat2.MaterialName2 = "Ball"; physMat2.Elasticity = 0.7f; physMat2.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 3f) { if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { Sound.SetVolume("Default", contactSpeed * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(rubberBall01Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; NewtonMaterial physMat3 = new NewtonMaterial(); physMat3.MaterialName1 = "Ground"; physMat3.MaterialName2 = "Domino"; physMat3.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 4f) { if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { Sound.SetVolume("Default", contactSpeed / 2 * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(woodHitConcrete1Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; NewtonMaterial physMat4 = new NewtonMaterial(); physMat4.MaterialName1 = "Ball"; physMat4.MaterialName2 = "Domino"; physMat4.Elasticity = 0.5f; physMat4.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 4f) { if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { Sound.SetVolume("Default", contactSpeed * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(hammerWood1Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; NewtonMaterial physMat5 = new NewtonMaterial(); physMat5.MaterialName1 = "Ball"; physMat5.MaterialName2 = "Ball"; physMat5.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 4f) { if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { Sound.SetVolume("Default", contactSpeed * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(rubberBall01Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; NewtonMaterial physMat6 = new NewtonMaterial(); physMat6.MaterialName1 = "Ball"; physMat6.MaterialName2 = "Obstacle"; physMat6.Elasticity = 0.7f; physMat6.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 4f) { if (soundsPlaying.Count >= SOUND_LIMIT) return; /*try { Sound.SetVolume("Default", contactSpeed * volume); } catch (Exception exp) { }*/ try { Sound.Instance.PlaySoundEffect(rubberBall01Sound); soundsPlaying.Add(DateTime.Now.TimeOfDay.TotalMilliseconds); } catch (Exception exp) { } } }; ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat); ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat2); ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat3); ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat4); ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat5); ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat6); }
/// <summary> /// Removes an existing physics material. /// </summary> /// <param name="physMat">A physics material that defines the physical /// material properties to be removed</param> public void RemovePhysicsMaterial(NewtonMaterial physMat) { if (!(physMat is NewtonMaterial)) throw new GoblinException("You can only remove NewtonMaterial from NewtonPhysics"); materialPairs.Remove(physMat.MaterialName1 + "_" + physMat.MaterialName2); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { base.Initialize(); #if WINDOWS // Display the mouse cursor this.IsMouseVisible = true; #endif // Initialize the GoblinXNA framework State.InitGoblin(graphics, Content, ""); #if WINDOWS_PHONE this.Activated += Sound.Instance.GameActivated; #endif //State.ThreadOption = (ushort)ThreadOptions.MarkerTracking; // Initialize the scene graph scene = new Scene(); // Set the background color to CornflowerBlue color. // GraphicsDevice.Clear(...) is called by Scene object with this color. scene.BackgroundColor = Color.CornflowerBlue; #if WINDOWS // We will use the Newton physics engine (http://www.newtondynamics.com) // for processing the physical simulation scene.PhysicsEngine = new NewtonPhysics(); #else scene.PhysicsEngine = new MataliPhysics(); #endif scene.PhysicsEngine.Gravity = 30; #if WINDOWS_PHONE ((MataliPhysics)scene.PhysicsEngine).SimulationTimeStep = 1 / 30f; #endif text3ds = new List<Text3DInfo>(); // Set up the lights used in the scene CreateLights(); // Set up the camera which defines the eye location and viewing frustum CreateCamera(); // Create 3D objects CreateObjects(); #if WINDOWS // Set up physics material interaction specifications between the shooting box and the ground NewtonMaterial physMat = new NewtonMaterial(); physMat.MaterialName1 = "ShootingBox"; physMat.MaterialName2 = "Ground"; physMat.Elasticity = 0.7f; physMat.StaticFriction = 0.8f; physMat.KineticFriction = 0.2f; // Define a callback function that will be called when the two materials contact/collide physMat.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal, float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed, Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection) { if (contactSpeed > 2) collisionCount++; // When a cube box collides with the ground, it can have more than 1 contact points // depending on the collision surface, so we only play sound and add 3D texts once // every four contacts to avoid multiple sound play or text addition for one surface // contact if (collisionCount >= 4) { // Set the collision sound volume based on the contact speed SoundEffectInstance instance = Sound.Instance.PlaySoundEffect(bounceSound); //instance.Volume = contactSpeed / 50f; // Print a text message on the screen Notifier.AddMessage("Contact with speed of " + contactSpeed); // Create a 3D text to be rendered Text3DInfo text3d = new Text3DInfo(); text3d.Text = "BOOM!!"; // The larger the contact speed, the longer the 3D text will stay displayed text3d.Duration = contactSpeed * 500; text3d.ElapsedTime = 0; // Scale down the vector font since it's quite large, and display the text // above the contact position text3d.Transform = Matrix.CreateScale(0.03f) * Matrix.CreateTranslation(contactPosition + Vector3.UnitY * 4); // Add this 3D text to the display list text3ds.Add(text3d); // Reset the count collisionCount = 0; } }; // Add this physics material interaction specifications to the physics engine ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat); #endif // Add a mouse click handler for shooting a box model from the mouse location MouseInput.Instance.MouseClickEvent += new HandleMouseClick(MouseClickHandler); State.ThreadOption = (ushort)ThreadOptions.MarkerTracking; // Show some debug information State.ShowFPS = true; // Show debugging messages on the screen State.ShowNotifications = true; // Make the debugging message fade out after 3000 ms (3 seconds) Notifier.FadeOutTime = 3000; }
/// <summary> /// Adds a physics material to this physics engine for material simulation. /// </summary> /// <param name="physMat">A physics material that defines the physical /// material properties to be added</param> public void AddPhysicsMaterial(NewtonMaterial physMat) { if (!(physMat is NewtonMaterial)) throw new GoblinException("You need to add NewtonMaterial for NewtonPhysics"); String pairName = physMat.MaterialName1 + "_" + physMat.MaterialName2; if (!materialPairs.Contains(pairName)) { int mat1ID = 0, mat2ID = 0; if (materialIDs.ContainsKey(physMat.MaterialName1)) mat1ID = materialIDs[physMat.MaterialName1]; else { mat1ID = Newton.NewtonMaterialCreateGroupID(nWorld); materialIDs.Add(physMat.MaterialName1, mat1ID); } if (materialIDs.ContainsKey(physMat.MaterialName2)) mat2ID = materialIDs[physMat.MaterialName2]; else { mat2ID = Newton.NewtonMaterialCreateGroupID(nWorld); materialIDs.Add(physMat.MaterialName2, mat2ID); } Newton.NewtonMaterialSetDefaultCollidable(nWorld, mat1ID, mat2ID, ((physMat.Collidable) ? 1 : 0)); if ((physMat.StaticFriction >= 0) && (physMat.KineticFriction >= 0) && (physMat.KineticFriction <= physMat.StaticFriction)) Newton.NewtonMaterialSetDefaultFriction(nWorld, mat1ID, mat2ID, physMat.StaticFriction, physMat.KineticFriction); if (physMat.Elasticity >= 0) Newton.NewtonMaterialSetDefaultElasticity(nWorld, mat1ID, mat2ID, physMat.Elasticity); if (physMat.Softness >= 0) Newton.NewtonMaterialSetDefaultSoftness(nWorld, mat1ID, mat2ID, physMat.Softness); IntPtr material = Marshal.AllocHGlobal(1); Newton.NewtonMaterialSetCollisionCallback(nWorld, mat1ID, mat2ID, material, (((NewtonMaterial)physMat).ContactBeginCallback != null) ? materialContactBeginCallback : null, (((NewtonMaterial)physMat).ContactProcessCallback != null) ? materialContactProcessCallback : null, (((NewtonMaterial)physMat).ContactEndCallback != null) ? materialContactEndCallback : null); if (!materials.ContainsKey(material)) materials.Add(material, (NewtonMaterial)physMat); materialPairs.Add(pairName); } }