public void Subdevide(Vector2 pos, int gen, int w, int h /*, List<Ball> balls*/) { // TODO test SUBDIVIDE (debug!!!) top left is not the correct top left id++; branches[0] = new QuadTreeSection(pos, gen, (int)(w * 0.5f), (int)(h * 0.5f), id /*, balls*/); id++; branches[1] = new QuadTreeSection(new Vector2(pos.X + w * 0.5f, pos.Y), gen, (int)(w * 0.5f), (int)(h * 0.5f), id /*, balls*/); id++; branches[2] = new QuadTreeSection(new Vector2(pos.X, pos.Y + h * 0.5f), gen, (int)(w * 0.5f), (int)(h * 0.5f), id /*, balls*/); id++; branches[3] = new QuadTreeSection(new Vector2(pos.X + w * 0.5f, pos.Y + h * 0.5f), gen, (int)(w * 0.5f), (int)(h * 0.5f), id /*, balls*/); locked = true; devided = true; }
//private void Load(GraphicsDevice graphicsDevice) //{ // texture = new Texture2D(graphicsDevice, 1, 1, false, SurfaceFormat.Color); // texture.SetData(new[] { Color.White }); //} public void CheckForColisions(QuadTreeSection branch, List <Ball> balls) { for (int i = 0; i < branch.branches.Length; i++) { if (branch.branches[i] != null && branch.branches[i].devided) { CheckForColisions(branch.branches[i], balls); } else { for (int j = 0; j < branch.branches[i].curentContainedBalls.Length; j++) { if (branch.branches[i].curentContainedBalls[j] == null) { continue; } for (int k = j; k < branch.branches[i].curentContainedBalls.Length; k++) { if (branch.branches[i].curentContainedBalls[k] == null || k == j) { continue; } float temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8; int id1, id2; id1 = branch.branches[i].curentContainedBalls[j].ID; id2 = branch.branches[i].curentContainedBalls[k].ID; temp1 = branch.branches[i].curentContainedBalls[k].center.X - branch.branches[i].curentContainedBalls[k].r2; temp2 = branch.branches[i].curentContainedBalls[j].center.X + branch.branches[i].curentContainedBalls[j].r2; temp3 = branch.branches[i].curentContainedBalls[k].center.X + branch.branches[i].curentContainedBalls[j].r2; temp4 = branch.branches[i].curentContainedBalls[j].center.X - branch.branches[i].curentContainedBalls[j].r2; temp5 = branch.branches[i].curentContainedBalls[k].center.Y - branch.branches[i].curentContainedBalls[k].r2; temp6 = branch.branches[i].curentContainedBalls[j].center.Y + branch.branches[i].curentContainedBalls[k].r2; temp7 = branch.branches[i].curentContainedBalls[k].center.Y + branch.branches[i].curentContainedBalls[j].r2; temp8 = branch.branches[i].curentContainedBalls[j].center.Y - branch.branches[i].curentContainedBalls[j].r2; if (temp1 > temp2 || temp3 < temp4 || temp5 > temp6 || temp7 < temp8) // Sumim da je krivo to da niso na najvišjem nivoju v drevesu ampak da so še nek uspodej to bo treba vržt vn na veje tm pa da se pregleda { continue; } else { balls.Remove(branch.branches[i].curentContainedBalls[k]); balls.Remove(branch.branches[i].curentContainedBalls[j]); } } } } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } quadTreeRoot = null; quadTreeRoot = new QuadTreeSection(Vector2.Zero, 0, DISPLAY_SIZE, DISPLAY_SIZE, 0 /*, balls*/); for (int i = 0; i < balls.Count; i++) { quadTreeRoot.Insert(balls[i]); } quadTreeRoot.CheckForColisions(quadTreeRoot, balls); if (!pause) { for (int i = 0; i < balls.Count; i++) { balls[i].Update(DISPLAY_SIZE); } } //if (!on) //{ // on = true; //} if (Keyboard.GetState().IsKeyDown(Keys.Down) && PreviousKState.IsKeyUp(Keys.Down)) { balls.Add(new Ball(DISPLAY_SIZE, Content, insertID)); insertID++; quadTreeRoot.Insert(balls[balls.Count - 1]); } if (Keyboard.GetState().IsKeyDown(Keys.P) && PreviousKState.IsKeyUp(Keys.P)) { pause = !pause; } PreviousKState = Keyboard.GetState(); //FPS = (1 / gameTime.ElapsedGameTime.TotalSeconds); base.Update(gameTime); }