Games [XUI2D] Breakout example

Erel

B4X founder
Staff member
Licensed User
Longtime User
l2kB1R3CMf.gif


Interesting points:
- The ship movements is managed in the same way it is managed in the Space Shooter example. A motor joint moves the ship to the touch position.
In B4J it can also be moved with the keyboard.
B4X:
If touch.IsInitialized = False Then
        ShipMotor.MaxMotorForce = 0 'disable the motor
    Else
        'the touch panel is wider than the game screen so we need to fix the offsets and clip the value to the game screen (ivForeground)
        Dim x As Float = Min(ivForeground.Width, Max(0, touch.X - (ivForeground.Left - PanelForTouch.Left)))
        Dim v As B2Vec2 = X2.ScreenPointToWorld(X, touch.Y)
        ShipMotor.LinearOffset = X2.CreateVec2(v.X, ShipMotor.LinearOffset.Y)
        ShipMotor.MaxMotorForce = 50
    End If

- 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.
- The spritesheet is a bit strange and requires some work to extract the correct images.
- No doubt that moving to B4XPages makes the game lifecycle simpler.

I've used the resources from Ilan's example: https://www.b4x.com/android/forum/threads/game-making-video-series.130388/. Thank you Ilan!

Example is available in github: https://www.b4x.com/android/forum/threads/xui2d-x2-v2-0-2021.131279/#content
 

ilan

Expert
Licensed User
Longtime User
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)

B4X:
    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:

B4X:
 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:

B4X:
    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)
 

Attachments

  • Breakout.zip
    244.4 KB · Views: 326
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I've only tried your version at this time, @ilan (I haven't tried Erel's).

Some problems:

1 - the speed of the ball sometimes decreases too much (several seconds to walk the screen vertically, no angle or almost)

2 - sometimes the angle is almost zero (the ball slides horizontally)

3 - maybe because my screen setting is deliberately at "low" resolution (1366 x 768) but the distance between the pad and the bricks is too short (I will try a bigger resolution)

4 - finally, after a huge slowdown in the speed of the ball, it continued to slam into a corner, staying there (you can hear the bounce, though):
Clipboard02.jpg
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Now the ball has stabilized vertically (strange noises), still very slow.
a.gif

I cannot attach a more lasting animation, due to the 500kb


(I set the resolution to the maximum; the distance between pad and bricks has the same proportion).

One thing I would like to know: how the electric discharge is made?!
 

ilan

Expert
Licensed User
Longtime User
i have uploaded the example in post #2. instead of applying an impulse I just set the ball velocity. (on game start when pressing spacebar key)
it makes more sense to work with velocity than impulse in a non-physic game!

B4X:
        'Ball.Body.ApplyLinearImpulse(X2.CreateVec2(0.25, 0.25), Ball.Body.WorldCenter)
        Ball.Body.LinearVelocity = X2.CreateVec2(4, 4)

it should work better now.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
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've tried it during development and it didn't work better. The ball will still lose some energy.
 

ilan

Expert
Licensed User
Longtime User
I've tried it during development and it didn't work better. The ball will still lose some energy.

yes and I think it is because the paddle is a dynamic body and not kinematic body. but I understand why you did that because you wanted to use a MotorJoint and this requires the body to be dynamic.

But if we would like to keep it like it is (using a dynamic body for the paddle) it can be fixed as you did in your example by checking the velocity and adding force.
i made a Pong game a long time ago for iOS and after reading a lot of tutorials and how such a game should work i came to the conclusion i wrote above.
BUT what i also do in my Pong game is every time the ball hits the Paddle i check the position where it hits the paddle and according to that, i set the Velocity of the ball.

working with Velocity is simpler and you will have more control than working with impulse and force because at the end we would like our ball to move at a constant speed and with impulses and forces, it is harder to control than just set the velocity of the ball.

1623389884514.png


in the middle it would be:
B4X:
= X2.CreateVec2(0, 4)

left corner:
B4X:
= X2.CreateVec2(-4, 4)

right corner:
B4X:
= X2.CreateVec2(4, 4)

i think you get the idea. like this, we will always get a constant speed for the ball.
 
Last edited:

Magma

Expert
Licensed User
Longtime User
.... can i ask a question... may be will be needed to make it at a different thread (a new one)... but i ll give a try here...

back1.jpg


This nice backround ... is it possible to be moving like walking-running to the lines ?
actually create an illusion with lines moving or something ?

ps: This is a baby dream had from old days... :)
 

Magma

Expert
Licensed User
Longtime User
..well i ve made something in some minutes... but i think need more mathematical mind :) (Erel's may be)

Animation.gif


Here the code.. if someone can optimize it :) or need to use it.. anyway...
 

Attachments

  • linescoming.zip
    2.3 KB · Views: 245

AnandGupta

Expert
Licensed User
Longtime User
WOW ! So much to learn to make so simple game.
I am betting on your tutorials @ilan as I still find myself in sea.

Edit: Sorry f this not the right place to post for ilan , but I felt it right.
 

ilan

Expert
Licensed User
Longtime User
.... can i ask a question... may be will be needed to make it at a different thread (a new one)... but i ll give a try here...

View attachment 114775

This nice backround ... is it possible to be moving like walking-running to the lines ?
actually create an illusion with lines moving or something ?

ps: This is a baby dream had from old days... :)

it is better to start a new thread for that. i have already :)

 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Top