/// <summary> /// This is called when the game should draw itself. /// </summary> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Create camera matrices, making the object spin. float time = (float)gameTime.TotalGameTime.TotalSeconds; float yaw = time * 0.4f; float pitch = time * 0.7f; float roll = time * 1.1f; Vector3 cameraLookat = new Vector3(0.0f, 0.0f, 2.5f); float aspect = GraphicsDevice.Viewport.AspectRatio; Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); Matrix view = Matrix.CreateLookAt(cameraLookat, Vector3.Zero, Vector3.Up); Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 100); Matrix worldTranslation = Matrix.CreateTranslation(0.0f, 0.0f, -5.0f); world *= worldTranslation; // Draw the current primitive. GeometricPrimitive currentPrimitive = primitive; // Since we know where the ground plane is, generate a very simple but correct shadow // by squashing the geometry into the XZ plane and rendering it at the ground plane directly // below the sphere Matrix shadowMatrix = Matrix.Identity; shadowMatrix.M12 = 0.0f; shadowMatrix.M22 = 0.0f; shadowMatrix.M23 = 0.0f; Matrix matScale; for (int i = 0; i < numSpheres; i++) { // Generate a scale matrix matScale = Matrix.CreateScale(spheres[i].Radius / 0.5f); Matrix worldX = world * matScale; // Translate worldTranslation = Matrix.CreateTranslation(spheres[i].Position); worldX *= worldTranslation; currentPrimitive.Draw(worldX, view, projection, spheres[i].Color, false); // Render shadow worldX *= shadowMatrix; worldX.M42 = -1.0f; currentPrimitive.Draw(worldX, view, projection, Color.Black, true); } base.Draw(gameTime); }
/// <summary> /// Load your graphics content. /// </summary> protected override void LoadContent() { // Initialize the Acelerometer wrapper Accelerometer.Initialize(); primitive = new SpherePrimitive(GraphicsDevice); Random random = new Random(); int numsphereColors = sphereColors.Length; float xpos = -10.0f, zpos = -2.0f, ypos = floorPlaneHeight; for (int i = 0; i < numSpheres; i++) { Sphere newSphere = new Sphere(); // Generate a a randomized velocity impulse newSphere.Velocity.X = 0.2f * random.Next(-10, 10); newSphere.Velocity.Z = 0.2f * random.Next(-10, 10); newSphere.Velocity.Y = 0.2f * random.Next(-3, 3); // Generate a random color newSphere.Color = sphereColors[i % numsphereColors]; // first 2 spheres are large, rest are small. newSphere.Radius = 0.10f + ((float)(random.Next(100)) / 100.0f) * 0.15f; // Set initial position newSphere.Position.X = xpos; newSphere.Position.Y = ypos + newSphere.Radius * 6.0f; newSphere.Position.Z = zpos; // Set mass based on size (sphere volume function) newSphere.Mass = (float)(Math.PI) * (newSphere.Radius * newSphere.Radius * newSphere.Radius); spheres.Add(newSphere); // Set new position xpos += 1.5f; if (xpos > 20.0f) { xpos = -10.0f; zpos -= 1.5f; } } }