/// <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() { // TODO: Add your initialization logic here base.Initialize(); updatables = new List <Element>(); // Display the mouse cursor this.IsMouseVisible = true; // Initialize the GoblinXNA framework State.InitGoblin(graphics, Content, ""); // Initialize the scene graph scene = new Scene(); // Use the newton physics engine to perform collision detection scene.PhysicsEngine = new NewtonPhysics(); // For some reason, it sometimes causes memory conflict when it attempts to update the // marker transformation in the multi-threaded code, so if you see weird exceptions // thrown in Shaders, then you should not enable the marker tracking thread State.ThreadOption = (ushort)ThreadOptions.MarkerTracking; // Set up optical marker tracking // Note that we don't create our own camera when we use optical marker // tracking. It'll be created automatically SetupMarkerTracking(); // Set up the lights used in the scene CreateLights(); // Enable shadow mapping // NOTE: In order to use shadow mapping, you will need to add 'MultiLightShadowMap.fx' // and 'SimpleShadowShader.fx' shader files to your 'Content' directory. Also in here, // we're creating the ShadowMap before the creation of 3D objects since we need to assign // this ShadowMap to the IShadowShader used for the 3D objects scene.ShadowMap = new MultiLightShadowMap(); // Create 3D objects CreateObjects(); // Create the ground that represents the physical ground marker array CreateGround(); // Show Frames-Per-Second on the screen for debugging State.ShowFPS = true; collisionCount = 0; // Set up physics material interaction specifications between the shooting box and the ground NewtonMaterial physMat = new NewtonMaterial(); physMat.MaterialName1 = "CannonBall"; 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 >= 8) { // Reset the count collisionCount = 0; } }; // Add this physics material interaction specifications to the physics engine ((NewtonPhysics)scene.PhysicsEngine).AddPhysicsMaterial(physMat); }
/// <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 // 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); // 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; }