/// <summary> /// Creates all the initial game objects /// </summary> private void CreateGameObjects() { ActiveGameObjects = new List<GameObject>(); int index = 0; foreach (PlayerInfo player in activePlayers) { //Create a Game Object for every active player GameObject playerShip; playerShip = new GameObject(); playerShip.Scale = new Vector3(0.25f, 0.25f, 0.25f); playerShip.Yaw = 1.5f; playerShip.Pitch = 0f; playerShip.Roll = 1.5f; playerShip.Health = 30; playerShip.UpdateRotationByYawPitchRoll(); playerShip.Player_Information = player; playerShip.Name = "Player Ship"; playerShip.shootingSound = soundBank.GetCue("missile"); playerShip.explosionSound = soundBank.GetCue("explosion"); Material shipMaterial = new Material(); shipMaterial.Diffuse = new Vector4(0, 0, 0, 1); shipMaterial.SpecularPower = 10; if (index == 0) { shipMaterial.Specular = Color.Red.ToVector4(); } else { shipMaterial.Specular = Color.Green.ToVector4(); } GeometryNode playerShipNode = new GeometryNode("Player Ship"); playerShipNode.Model = player.Player_Ship.Player_Ship_Model; playerShipNode.Material = shipMaterial; playerShip.Geometry = playerShipNode; playerShipNode.AddToPhysicsEngine = true; playerShipNode.Physics.Shape = ShapeType.Box; scene.RootNode.AddChild(playerShip); ActiveGameObjects.Add(playerShip); if (index == 0) { playerShip.Translation = new Vector3(-25, 20, -100); } else if (index == 1) { playerShip.Translation = new Vector3(25,-25, -100); playerShip.Pitch = 9.5f; playerShip.UpdateRotationByYawPitchRoll(); } index++; } }
/// <summary> /// Updates the rotation of an object based on it's target position /// </summary> /// <param name="player"></param> /// <param name="targetPosition"></param> private void UpdateRotation(GameObject player, Vector3 targetPosition) { Matrix rotation = Matrix.CreateFromYawPitchRoll(player.Yaw, player.Pitch, player.Roll); Vector3 pos = player.Translation + rotation.Backward; double slope = findSlope(player.Translation.X,player.Translation.Y,pos.X,pos.Y); double slopeDiff = findSlope(player.Translation.X, player.Translation.Y, targetPosition.X, targetPosition.Y); float angleDirection = (float)Math.Atan(slope); angleDirection = MathHelper.ToDegrees(angleDirection); float angleTarget = (float)Math.Atan(slopeDiff); angleTarget = MathHelper.ToDegrees(angleTarget); if (pos.X < player.Translation.X && pos.Y > player.Translation.Y) angleDirection += 180; if (pos.X < player.Translation.X && pos.Y < player.Translation.Y) angleDirection += 180; if (pos.X > player.Translation.X && pos.Y < player.Translation.Y) angleDirection += 360; if (targetPosition.X < player.Translation.X && targetPosition.Y > player.Translation.Y) angleTarget += 180; if (targetPosition.X < player.Translation.X && targetPosition.Y < player.Translation.Y) angleTarget += 180; if (targetPosition.X > player.Translation.X && targetPosition.Y < player.Translation.Y) angleTarget += 360; if (angleTarget < 1) angleTarget += 360; if (angleDirection < 1) angleDirection += 360; if ( Math.Abs(angleDirection - angleTarget) < 5) { player.turnCounter = 0; return; } else { if (player.turnCounter == 60 || player.turnCounter == -60) { player.turnCounter = 0; } if (player.turnCounter > 0) { player.Pitch += 0.1f; player.turnCounter++; } else if ( player.turnCounter < 0) { player.Pitch -= 0.1f; player.turnCounter--; } else if ( Math.Abs(angleDirection - angleTarget) <= 180.0f) { player.Pitch += 0.1f; player.turnCounter++; } else { player.Pitch -= 0.1f; player.turnCounter--; } } player.UpdateRotationByYawPitchRoll(); }