This project has moved. For the latest updates, please go here.

# Collision between 2 bodies, second body bounces back and first body is removed

 Topics: Developer Forum, User Forum Wiki Link: [discussion:246489]
 lordregor666 Feb 17, 2011 at 2:29 PM Hi, I am trying to make a game of Breakout, I have managed to set the collision between bodies so when they collide then one of them is removed. My problem right now is that if you have all played Breakout, if you have the bouncing object and it collides against a brick, the bouncing object is then propulsed back. I have the collision sorted and it will remove the object but I don't understand how to create the sort of propulsion back, I tried to apply a linearImpulse but for some reason it didn't work and the ball just went through eliminating the brick and never bouncing back. This is my code so far for the object(bricks) and the collision+update:   ``` private void LoadObstacles() { //creates body and properties for (int i = 0; i < myLimit; i++) { myRectangleRed[i] = FixtureFactory.CreateRectangle(World, 2, 2.0f, 1, MaterialColour(Color.Red)); myRectangleRed[i].Body.IsStatic = true; myRectangleRed[i].OnCollision += OnCollision; startPosition = new Vector2(0, 0); endPosition = new Vector2(20, 0); myRectangleRed[i].Body.Position = Vector2.Lerp(startPosition, endPosition, i / (float)(myLimit - 1)); } } private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact) { Body body1 = fixtureA.Body; Body body2 = fixtureB.Body; float mass1 = body1.Mass; float mass2 = body2.Mass; if (mass1 > 0.0f && mass2 > 0.0f) { if (mass2 > mass1) { if (!_removeBodies.Contains(body1)) { _removeBodies.Add(body1); } return false; } } return true; } public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { if (IsActive) { for (int i = 0; i < _removeBodies.Count; i++) { World.RemoveBody(_removeBodies[i]); } } _removeBodies.Clear(); base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); } I tried to use a linear impulse after the first collision but this didn't seem to work.If anyone can give me some help on this it would be of great help.Thank you,``` Elsch Developer Feb 17, 2011 at 2:48 PM Edited Feb 17, 2011 at 2:54 PM You could try to store the object in a list in your OnCollision delegate instead of removing it right there. Then after world.step() you go through that list and call world.remove() for every object. They will then be removed in the next time step i.e. one frame later but my guess would be that should be ok and you would get a physically correct collision response for your ball without manually fiddling with it ;) Edit: Sry just saw you already do that. I think you just have to remove the "return false;" in your OnCollsion method and everything should be fine. Why should you cancel the collision when you actually want it to happen? lordregor666 Feb 22, 2011 at 9:49 AM Hi Elsch, Sorry for late reply, I got the OnCollision from somewhere else and thought the return false or true was feasable when the instance of the object was the collision state otherwise it continues but as I see now it means it allows collision. Thank you for the help, it works nicely now :) lordregor666 Feb 22, 2011 at 12:02 PM Hi, Sorry but I have another question about collisions and this time I have a question about the world borders. As I am looking into the classes, I find little commenting on it and I can't find where the world borders are. I would like to delete the default borders later on as well but basically what I want to do now is that when the ball collides with the left hand border then "do something" The reason why I would like border position is because of the dimensions of the screen can change between a minimised screen to a maximised screen so I can't solve a limit for where the border is initially (saddly). I've got some pseudocode but essencially it is not on collide but possition of the ball so any help on how to get collision with the borders would be of great help. Thanks again. Elsch Developer Feb 23, 2011 at 9:18 AM At the moment it is created in the physicsgamescreen class. Have a look at border.cs If you want to change its size i'd recommend removing it completely from the world and creating a new one with the new size. In the new samples framework i changed the border to use a loop shape (it is in the source) so you can also have a look at that. It is quite easy to create an arbitrarily shaped border using a loop shape. If you want special OnCollision handling per edge use several edge shapes/fixtures (duh) to form your border instead of one loop shape. Then you can assign individual OnCollision events (on a per fixture basis). lordregor666 Feb 24, 2011 at 10:37 AM Great, that helped a lot :)