Very nice Erel. ?
- It is a bit challenging to model the non-physics game in box2d physics engine. After trying all kinds of things, I've used brute force, literally. The main problem was that the ball started moving up and down or sideways. The solution is to check the horizontal and vertical velocity and apply force if they are too slow.
There is also a check for the total speed and the ball is pushed if it is too slow.
i had a look at your code and I would do some things a little bit differently. (I hope you don't mind if I put some feedbacks here)
the reason the ball is losing energy is that in such a game where we don't really use any physic it would make more sense to give all bodies friction of 0 and restitution of 1.
when a ball with restitution 0 will fall on the ground it won't bounce back (like a bowling ball) but with restitution of 1, it will bounce back with the same velocity it hits the other body. Friction 0 will help us also to not lose any energy when the ball hits any other body. so I changed all bodies' restitution value to 1 and friction to 0 (in tiled).
i also set the ball.body.LinearDamping to 0. this will also keep the velocity of the ball and don't lose speed when it is in the air. (i set the ball to fixed.rotation so it won't rotate but it has no influence on physics)
Ball.Body.AngularDamping = 0 '<<<<
Ball.Body.LinearDamping = 0 '<<<<
we don't need Gravity in this game as you already did it with the World. gravity here:
world.Initialize("world", world.CreateVec2(0, 0))
but i also changed for all bodies the GravityScale value to 0 (in tiled). i don't think it is necessary but In such kind of game, I do that.
(gravity scale allows us to remove gravity from a dynamic body even in a world that has gravity. so it will stay in the air but can collide and also move around like it is in space)
density is also a value we should put attention to. if you are using dynamic bodies then if both bodies have the same density they will lose energy if they hit. because you have created the paddle as a dynamic body I changed the density of the ball to be lower than all other bodies. this will help it bounce back with enough power.
### JOINTS ###
i would not use any Joints in such a game. instead, i would use a kinematic body for the paddle and not a dynamic body. the advantage I will get is a body that can be moved with velocity but will not be affected if another body hits it. and this will also prevent losing energy. if you look closely at your example when the ball hits the paddle very hard the paddle moves down and up. because the only thing that holds it in place is the Motorjoint. you can also apply the effect you did with a nice stepApplyingVelocity function. (i did it in a game of mine, looks really great and you get a feeling like working with Force and not with Velocity)
anyway, it would make more sense to use a DistanceJoint to connect the Ball and the Paddle after you die. WeldJoint should be used in different scenarios
Weld - holds the bodies at the same orientation
Distance - a point on each body will be kept at a fixed distance apart
as you can see distanceJoint will hold 2 bodies with a fixed distance. btw if you want a rigid distance joint you would use:
Dim distanceDef As B2DistanceJointDef'<<<<
distanceDef.Initialize(Ship.Body, Ball.Body, Ball.Body.WorldCenter, Ball.Body.WorldCenter)'<<<<
distanceDef.DampingRatio = 1'<<<<
distanceDef.FrequencyHz = 0'<<<<
this will hold the ball very tight and no moving around
i will include the updated example of your Breakout Game.
and thank you for your great examples
Ps: i like a lot the way you shaped your paddle. i would not have thought of doing it like this. i am using a complex calculation to send the ball in a specific direction but your shape is doing it well. i changed it a little bit by adding 2 more dots but it is a very nice idea. i do think that the game needs more polishing and also if the paddle would be a kinematic body it would bounce back the ball better but this project is a good start.
(i have added '<<<< in the code for the lines i have changed)