Make a power bar and apply force like pool/billiard/golf

Description

Hello i just started gdevelop and Im trying to make a similar concept like pool or golf where a player holds the mouse button to aim where to shoot and there would be a power bar as well to estimate the force on the ball.

Problem

I already have the part where a player hold the mouse to aim and when the mouse button is released, the ball will shoot. But the problem is the power bar. How do i apply force based on the power bar? A simple tutorial would be good to

What is the expected result

The expected result is that the player can shoot the ball based on the power bar.

You would make variable for that power bar
Now question is how you adjust power bar?
How you charge it?
Let’s assume you hold the bar to charge it from 0 to 100 and then from 100 to 0

So 1st thing to do is make number scene variable called Power
And so you need formula (same as you would for HP bar) to calculate bar filled based that variable
Which would be
BarWidth / 100 * Variable(Power)
And you change width of your bar based on that
So for example your bar is 326 width
Then equation would be
363 / 100 * Variable(Power)
In my case bar width was 200
image

So now that we have bar which will display power properly (bar itself is just visualization while variable is what actually matters)
We need need a way to charge bar
And most simple way would be

As you see i am not checking if cursor is on bar (1st condition)
But bar frame
Since bar shrinks based on power variable value
When it is 0 means bar itself have 0 pixels so there is nothing to hover over
That’s why i needed another object and bar frame is perfect for it

While when cursor is over it and LMB is pressed i add 1 to Power variable
Where mod(Variable(Power),101)
Means cycle/loop trough 101 numbers or more likely from 0 to 100 since starting number is 0 then if we put 100 there instead of 101 it would cycle trough 0 - 99
If we put there 5 there instead of 101 it would cycle trough 0 1 2 3 4 since that is 5 digits including 0
And in short of it it goes from 0 to 100 and then back to 0

Of course you most likely will want different way of charging your bar but for sake of this example it is enough

Now part which you were waiting for

When RMB is released we add permanent force to our boomerang toward some angle (you will go toward something else than me or even a position but whatever)
And instead of force we put there Variable(Power)
Since we are using trigger once in condition force needs to be permanent instead of instant
AND if scale of force for you (for me it was) is too low you simply multiply that variable by something
For example
image
Or going beyond you could multiply it by another variable
And set that variable to various values
For example player power variable which would be based on player stats that comes from level
AND/OR club/stick you use to hit the ball
Different items could have different values
And so you have total control over how strong hit would be

Effect?

LMB to charge while on bar
RMB to throw boomerang

1 Like

Thank you for the long detailed explanation! I will try to apply it to my game but it seems like you boomerang doesnt stop even if i shoot it on a low percentage… Is it possible to use the action “apply force (angle)” so like when you shoot, it can stop unless it hits another ball

Did you just insult my boomerang?

Just kidding
In this version click LMB anywhere and it will throw boomerang with current value of power bar
If you hold LMB it will automatically throw boomerang when bar is at 100

In previous link i disabled it to demonstrate to you how adding force from power bar works

NOW
What you ask for is perfectly doable
There is action to remove all forces from object
So you could go with object A is in collision with object B
Action stop (remove all forces) from object A

But another question do you want constant speed like in my previous link
Or you want to have your moving object loose speed the longer it travels
Cause that is done simply by subtracting some number from object power variable
While you simply add condition like object variable power is greater than 0
So when it is 0 it simply stops
Don’t go with equal or greater than
Or you will be able to go below 0 like -1 and so it will start moving backwards

Throw boomerang into tree and it will instantly come back
Cause i made action to multiply power variable by -1
If it is still flying away
Multiplying by -1 will change any number from negative to positive and vice-versa
For example

5 * - 1 = - 5 
-5 * - 1 = 5

A lot of ways to achieve what you want to do
You could also go beyond what i have with my boomerang in link in this message

I am constantly subtracting from power variable so it is linear subtraction of power var
BUT you could add some multiplier there
So it subtract some number + current power variable divided by some number
For example subtract 3 + Boomerang(Power)/10
And now lower boomerang power var will be (meaning the longer boomerang travels)
It will lose it speed slower
That is kinda what you would wish for Ball rolling
So it do not have constant loosing speed but gradually lose less and less speed over time

BUT idk what you are doing there
Maybe you are making a bomb who knows?
So these are just few ideas for you depending on which route you want to take

Hello sorry for the long update! I was dealing some problems but i managed to get it work with your concept! Thank you for your help!

1 Like