public override void Run() { base.Run(); if (updateCount == 20) { updateCount = 1; } else { updateCount++; } if (coolDown > 0) { coolDown--; } if (audioCoolDown > 0) { audioCoolDown--; } foreach (var item in Shapes3D) { if (item.Key == "target" && updateCount > 15) { item.Value.IsVisible = false; } else if (item.Key == "target" && updateCount <= 15) { item.Value.IsVisible = true; } if (item.Key == "sphereEnvCubeMap") { float speed = 5f * 20f / MyGame.UPS; Shapes3D["sphereEnvCubeMap"].Physic.Vxyz = new Vector3(Shapes3D["sphereEnvCubeMap"]. Physic.Vxyz.X + MyGame.TableZAngle * speed * (float)Math.Cos(MyGame.TableZAngle), Shapes3D["sphereEnvCubeMap"]. Physic.Vxyz.Y, // + // MyGame.TableXAngle * speed * // ((float)Math.Sin(MyGame.TableXAngle) //- (float)Math.Sin(MyGame.TableZAngle)), Shapes3D["sphereEnvCubeMap"]. Physic.Vxyz.Z + MyGame.TableXAngle * speed * (float)Math.Cos(MyGame.TableXAngle)); float friction = 0.85f; item.Value.Physic.Vxyz = new Vector3( item.Value.Physic.Vxyz.X * friction, item.Value.Physic.Vxyz.Y * friction, item.Value.Physic.Vxyz.Z * friction); } if (item.Key == "basePanel" || item.Key == "target") { item.Value.Quaternion = Quaternion. FromAxisAngle( Vector3.UnitX, MyGame.TableXAngle - MathHelper.PiOver2); item.Value.Quaternion = Quaternion. FromAxisAngle( Vector3.UnitZ, -MyGame.TableZAngle) * item.Value.Quaternion; } else if (item.Key != "skyBox" && item.Key != "sphereEnvCubeMap" && item.Key != "spotLight" && item.Key != "sphereEnvM" && item.Key != "pointSprites" && !item.Key.Contains("notTable")) { item.Value.Quaternion = Quaternion.FromAxisAngle( Vector3.UnitX, MyGame.TableXAngle); item.Value.Quaternion = Quaternion.FromAxisAngle( Vector3.UnitZ, -MyGame.TableZAngle) * item.Value.Quaternion; } float initXAngle = 0; float initZAngle = 0; double rx = 0; double rz = 0; if (item.Key != "sphereEnvCubeMap") { initXAngle = (float)Math.Atan2(item.Value.FirstPosition.Y, item.Value.FirstPosition.Z); initZAngle = (float)Math.Atan2(item.Value.FirstPosition.Y, item.Value.FirstPosition.X); rx = Math.Sqrt(Math.Pow(item.Value.FirstPosition.Y, 2) + Math.Pow(item.Value.FirstPosition.Z, 2)); rz = Math.Sqrt(Math.Pow(item.Value.FirstPosition.Y, 2) + Math.Pow(item.Value.FirstPosition.X, 2)); } else { initXAngle = (float)Math.Atan2(item.Value.Position.Y, item.Value.Position.Z); initZAngle = (float)Math.Atan2(item.Value.Position.Y, item.Value.Position.X); rx = Math.Sqrt(Math.Pow(item.Value.Position.Y, 2) + Math.Pow(item.Value.Position.Z, 2)); rz = Math.Sqrt(Math.Pow(item.Value.Position.Y, 2) + Math.Pow(item.Value.Position.X, 2)); } double x; double y; double z; if (item.Key == "sphereEnvCubeMap") { y = -(item.Value as Sphere3D).R + rx * Math.Sin(MyGame.TableXAngle - initXAngle); z = rx * Math.Cos(MyGame.TableXAngle - initXAngle); y -= rz * Math.Sin(-MyGame.TableZAngle - initZAngle); x = rz * Math.Cos(-MyGame.TableZAngle - initZAngle); } else { y = rx * Math.Sin(MyGame.TableXAngle - initXAngle); z = rx * Math.Cos(MyGame.TableXAngle - initXAngle); y += rz * Math.Sin(MyGame.TableZAngle - initZAngle); x = rz * Math.Cos(MyGame.TableZAngle - initZAngle); } if (item.Key != "skyBox" && item.Key != "basePanel" && item.Key != "spotLight" && item.Key != "sphereEnvM" && item.Key != "pointSprites" && !item.Key.Contains("notTable")) { item.Value.Position = new Vector3((float)x, -(float)y, (float)z); } // point light and spot light demo float totalTicks = 15708 * 1.5f; if (pointLightCount == totalTicks) { pointLightCount = 1; } else { pointLightCount++; } //float pointLightAngleTween = // Tween.Solve(Tween.Function.Circular, // Tween.Ease.Out, // 0, // MathHelper.TwoPi, // totalTicks, // pointLightCount); //PointLight.Position = // new Vector3(40f * (float)Math.Cos(pointLightAngleTween), // PointLight.Position.Y, // 40f * (float)Math.Sin(pointLightAngleTween)); pointLightAngle += 0.0004f; //PointLight.Position = // new Vector3(40f * (float)Math.Cos(pointLightAngle), // PointLight.Position.Y, // 40f * (float)Math.Sin(pointLightAngle)); //if (item.Key == "pointLight") // item.Value.Position = PointLight.Position; //SpotLight.ConeDirection = // new Vector3((float)Math.Sin(pointLightAngle * 0.3f), -1, // (float)Math.Cos(pointLightAngle * 0.3f)); //SpotLight.Position = // new Vector3(-40f * (float)Math.Cos(pointLightAngle * 0.3), // 7f, // -40f * (float)Math.Sin(pointLightAngle * 0.3)); item.Value.SpotLight = SpotLight; item.Value.Light.DirectionalLightPosition = SunLightPosition; if (item.Key.Contains("Cube")) { item.Value.IsUsingNormalMap = CubesHaveNormalMap; item.Value.Light.Ambient = Vector3.One * 1.5f; } else if (item.Key == "basePanel") { item.Value.IsUsingNormalMap = CubesHaveNormalMap; item.Value.Light.Ambient = Vector3.One * 1.1f; } //float intensity = (float)Math.Cos(pointLightAngle * 0.3f); //if (Math.Abs(intensity) < 0.4f) //{ // if (audioCoolDown == 0 && Math.Abs(intensity) < 0.1f) // { // Audio.Play(5, Vector3.Zero); // audioCoolDown = 30; // } // item.Value.PointLight.Intensity = intensity; //} //else // item.Value.PointLight.Intensity = 0.2f; // shape manager ShapeManager.Manage(item.Value, Shapes3D, Shapes2Remove); // main update item.Value.Update(Camera.View, MyGame.ProjectionMatrix, Shapes3D, Camera, GameWindow); } List <Collision> collisions = PhysicHelp.CheckCollisionsOneShape( Shapes3D, "sphereEnvCubeMap"); if (collisions.Count != 0) { foreach (var item in collisions) { // end level condition if (item.Shape3Da == "target") { MyGame.Debug2 = "TARGET"; Audio.Stop(0); Audio.Stop(6); Audio.Play(3, Vector3.Zero); IsFinishedWithSuccess = true; CurrentState = State.Finishing; FinishCamera.Position = Camera.Position; FinishCamera.Quaternion = Camera.Quaternion; pointLightCount = 0; return; } else { MyGame.Debug2 = item.Shape3Da; } } float velIni = Shapes3D["sphereEnvCubeMap"].Physic.Vxyz.Length; PhysicHelp.SolveCollisionsOneShape(collisions, Shapes3D, "sphereEnvCubeMap", "target"); float velFin = Shapes3D["sphereEnvCubeMap"].Physic.Vxyz.Length; float deltaV = velIni - velFin; if (deltaV > 0.05f && audioCoolDown == 0) { Audio.Play(1, Shapes3D["sphereEnvCubeMap"].Position, false, 0.3f * (deltaV * 5)); audioCoolDown = 4; MyGame.Debug = ": " + deltaV.ToString(); } } #region text render if (TextRender.Visible && (updateCount % 10 == 0)) { Vector3 v1 = new Vector3(Camera.CameraUVW.Row0); Vector3 v2 = new Vector3(Camera.CameraUVW.Row0.X, 0f, Camera.CameraUVW.Row0.Z); v2.Normalize(); float CameraUangle = Vector3.Dot(v2, v1); Vector3 axis = new Vector3(Camera.CameraUVW.Row2.X, Camera.CameraUVW.Row2.Y, Camera.CameraUVW.Row2.Z); float angle = (float)Math.Acos(Convert.ToDouble(CameraUangle)); TextRender.Update("FPS : " + GameWindow.RenderFrequency + "\n" + "UPS : " + GameWindow.UpdateFrequency + "\n" + "WIDTH : " + MyGame.Width + "\n" + "HEIGHT : " + MyGame.Height + "\n" + // "COLLISIONS : " + collisions.Count + "\n" + "Camera : " + "X= " + Camera.Position.X + "; " + "Y= " + Camera.Position.Y + "; " + "Z= " + Camera.Position.Z + "\n" + "Q= " + Camera.Quaternion + "\n" + "SKYBOX : " + "X= " + Shapes3D["skyBox"].Position.X + "; " + "Y= " + Shapes3D["skyBox"].Position.Y + "; " + "Z= " + Shapes3D["skyBox"].Position.Z + "\n" + "BALL : " + "X= " + Shapes3D["sphereEnvCubeMap"].Position.X + "; " + "Y= " + Shapes3D["sphereEnvCubeMap"].Position.Y + "; " + "Z= " + Shapes3D["sphereEnvCubeMap"].Position.Z + "\n" + "Uangle : " + CameraUangle + " = " + (float)Math.Acos(Convert.ToDouble(CameraUangle)) + " = " + MathHelper.RadiansToDegrees((float)Math.Acos(Convert.ToDouble(CameraUangle))) + "\n" + "Shapes : " + Shapes3D.Count + "\n" + "SunLight_X : " + SunLightPosition.X + "\n" + "SunLight_Y : " + SunLightPosition.Y + "\n" + "SunLight_Z : " + SunLightPosition.Z + "\n" + "Table_X_Angle : " + MathHelper.RadiansToDegrees(MyGame.TableXAngle) + "\n" + "Table_Z_Angle : " + MathHelper.RadiansToDegrees(MyGame.TableZAngle) + "\n" + "DEBUG : " + MyGame.Debug + "\n" + "DEBUG1 : " + MyGame.Debug1 + "\n" + "DEBUG2 : " + MyGame.Debug2 + "\n" ); } #endregion Clock = 80 + ((int)(DateTimeClock.Ticks - DateTime.Now.Ticks) / 10000000); // level fail condition if (Clock <= 0) { CurrentState = State.Finishing; IsFinishedWithSuccess = false; Audio.Stop(0); Audio.Stop(6); Audio.Play(4, Vector3.Zero); } else if (Clock <= 10 && !IsLoadedAudioClockTick) { Audio.Play(6, Vector3.Zero, true, 2f); IsLoadedAudioClockTick = true; } TextRenderClock.Update(Clock.ToString()); Shapes3D["skyBox"].Position = new Vector3( -Camera.Position.X, -Camera.Position.Y, -Camera.Position.Z); ShadowMap.Update(Shapes3D, MyGame.ProjectionMatrix, GameWindow); // ball track if (Shapes3D["sphereEnvCubeMap"].Position != ballUpdateOldPosition) { var sw = new Stopwatch(); sw.Start(); Texture.DeleteTexture(Textures["bump_texture"]); using (Graphics grfx = Graphics.FromImage(basePanelNormalMap)) { float deltaX = Shapes3D["sphereEnvCubeMap"].Position.X - ballUpdateOldPosition.X; float deltaZ = Shapes3D["sphereEnvCubeMap"].Position.Z - ballUpdateOldPosition.Z; int rep = 4; for (int i = rep; i != 0; i--) { grfx.DrawImageUnscaled(ballTrackNormalMap, (int)((Shapes3D["sphereEnvCubeMap"].Position.X - (i * deltaX / rep) - 1.5f) * (1024f / 55f) + 1024f), (int)(2048 - ((Shapes3D["sphereEnvCubeMap"].Position.Z - (i * deltaZ / rep) + 1.5f) * (1024f / 55f) + 1024f)), ballTrackNormalMap.Width * 2, ballTrackNormalMap.Height * 2); } ballUpdateOldPosition = Shapes3D["sphereEnvCubeMap"].Position; } Textures["bump_texture"] = Texture.Load(basePanelNormalMap); sw.Stop(); MyGame.Debug2 = (((float)sw.ElapsedTicks / Stopwatch.Frequency) * 1000000).ToString(); } }
public override void Finish() { base.Finish(); Shapes3D["sphereEnvCubeMap"].Position = new Vector3(TargetPosition.X, Shapes3D["sphereEnvCubeMap"].Position.Y, TargetPosition.Y); Shapes3D["sphereEnvCubeMap"].Physic.Vxyz = Vector3.Zero; int totalCameraTicks = 20; if (totalCameraTicks != endLevelCameraMovCount) { float zTween = Tween.Solve(Tween.Function.Cubic, Tween.Ease.InOut, Camera.Position.Z, -80.20699f, totalCameraTicks, endLevelCameraMovCount); FinishCamera.Position = new Vector3(Camera.Position.X, Camera.Position.Y, zTween); FinishCamera.Rotate(Vector3.UnitX, -0.025f); endLevelCameraMovCount++; } FinishCamera.Update(); /* * Camera.Position = new Vector3(FinishCamera.Position.X, * FinishCamera.Position.Y, * FinishCamera.Position.Z); * * Camera.Quaternion = new Quaternion(FinishCamera.Quaternion.X, * FinishCamera.Quaternion.Y, * FinishCamera.Quaternion.Z, * FinishCamera.Quaternion.W); * * Camera.Update(); */ float initXAngle = (float)Math.Atan2(Shapes3D["sphereEnvCubeMap"].Position.Y, Shapes3D["sphereEnvCubeMap"].Position.Z); float initZAngle = (float)Math.Atan2(Shapes3D["sphereEnvCubeMap"].Position.Y, Shapes3D["sphereEnvCubeMap"].Position.X); double rx = Math.Sqrt(Math.Pow(Shapes3D["sphereEnvCubeMap"].Position.Y, 2) + Math.Pow(Shapes3D["sphereEnvCubeMap"].Position.Z, 2)); double rz = Math.Sqrt(Math.Pow(Shapes3D["sphereEnvCubeMap"].Position.Y, 2) + Math.Pow(Shapes3D["sphereEnvCubeMap"].Position.X, 2)); double y = -(Shapes3D["sphereEnvCubeMap"] as Sphere3D).R + rx * Math.Sin(MyGame.TableXAngle - initXAngle); double z = rx * Math.Cos(MyGame.TableXAngle - initXAngle); y -= rz * Math.Sin(-MyGame.TableZAngle - initZAngle); double x = rz * Math.Cos(-MyGame.TableZAngle - initZAngle); Shapes3D["sphereEnvCubeMap"].Position = new Vector3((float)x, -(float)y, (float)z); Shapes3D["target"].IsVisible = true; foreach (Shape3D item in Shapes3D.Values) { // level end with success if (IsFinishedWithSuccess) { float totalTicks = 80f * (MyGame.UPS / 20); if (pointLightCount >= totalTicks) { PointLight.Intensity = 1; } if (item.Name == "pointSprites" && pointLightCount != totalTicks && totalCameraTicks == endLevelCameraMovCount) { item.IsVisible = true; if (pointLightCount == 1) { Audio.Play(7, Vector3.Zero); } PointLight.Intensity = 1 / Tween.Solve( Tween.Function.Linear, Tween.Ease.In, 5, 35f, totalTicks, pointLightCount); item.Scale(Tween.Solve(Tween.Function.Quintic, Tween.Ease.In, 0, 40f, totalTicks, pointLightCount)); //item.TranslateWC(0, // Tween.Solve(Tween.Function.Cubic, // Tween.Ease.In, // 0, // -2f, // totalTicks, // pointLightCount), // 0); PointLight.Position = item.Position; pointLightCount++; } else { Shapes3D["pointSprites"].IsVisible = false; } item.PointLight.Intensity = PointLight.Intensity; item.PointLight.Position = PointLight.Position; item.PointLight.Color = PointLight.Color; } item.Update(FinishCamera.View, MyGame.ProjectionMatrix, Shapes3D, FinishCamera, GameWindow); } ShadowMap.Update(Shapes3D, MyGame.ProjectionMatrix, GameWindow); }