public void Attach(Ball b) { b.Body.Position = body.Position - new Vector2(0, height / 2.0f + b.Radius + 0.5f); //b.Body.CollidesWith = Category.None; //b.Body.CollisionCategories = Category.Cat2; b.Body.Mass = 0.0001f; ballAttachments.Add(JointFactory.CreateWeldJoint(world, body, b.Body, Vector2.Zero, new Vector2(0, height / 2.0f + b.Radius))); float diameterSum = 0; foreach (WeldJoint wj in ballAttachments) { diameterSum += (wj.BodyB.UserData as Ball).Radius * 2; } float distanceSoFar = 0; for (int i = 0; i < ballAttachments.Count; i++) { ballAttachments[i].LocalAnchorB = new Vector2(distanceSoFar - diameterSum/2.0f + (ballAttachments[i].BodyB.UserData as Ball).Radius, height / 2.0f + (ballAttachments[i].BodyB.UserData as Ball).Radius); ballAttachments[i].BodyB.Position = body.Position - new Vector2(distanceSoFar - diameterSum / 2.0f, height / 2.0f + (ballAttachments[i].BodyB.UserData as Ball).Radius); distanceSoFar += (ballAttachments[i].BodyB.UserData as Ball).Radius * 2; } //world.RemoveJoint }
public void SplitBalls(Player player) { for(int i = gameObjects.Count - 1; i >= 0; i--) { if (gameObjects[i] is Ball && (gameObjects[i] as Ball).Owner == player) { float angle = (float)Game1.rand.NextDouble() * MathHelper.TwoPi; Vector2 heading = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)); (gameObjects[i] as Ball).Launch(heading); for(int j = 0; j < Game1.rand.Next(2,5); j++) { if (player.Balls >= 15) continue; angle = (float)Game1.rand.NextDouble() * MathHelper.TwoPi; heading = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)); player.AddBall(); Ball b = new Ball(player, gameObjects[i].Position + new Vector2(0, 0.001f), ConvertUnits.ToSimUnits(10.0f), ConvertUnits.ToSimUnits(screenHeight), world); gameObjects.Add(b); b.Launch(heading); } } } }
/// <summary> /// Spawns a ball and attaches it to player's paddle /// </summary> /// <param name="player"></param> public void SpawnBall(Player player) { player.AddBall(); Ball b = new Ball(player, ConvertUnits.ToSimUnits(new Vector2(screenWidth / 2.0f, screenHeight - 400.0f)), ConvertUnits.ToSimUnits(10.0f), ConvertUnits.ToSimUnits(screenHeight), world); gameObjects.Add(b); player.Paddle.Attach(b); }