Creating an object based on maximum height another object can reach


#1

I am making a game where a ball bounces on bouncers. The ball goes up due to the bouncers and down due to the gravity. I use Physics 2. The restitution of the ball is 1.2. I want my app to create an object (in this case another bouncer) at the maximum height the ball can reach after colliding with a bouncer. I have two questions:

  • How can I do this? I know how to define the x-coordinate, but not how to define the y-coordinate.The speed of the ball in increases (in general) after each bounce, so the maximum height too.
  • How can I trigger this event only once per bouncer? (Current situation: the ball bounces on the same bouncer twice and creates two new bouncers, but I want to create one new bouncer no matter how many times the ball is colliding with the same bouncer.)
    Thanks in advance!

#2

Store current Y position of the ball in a variable, and update it as long as current Y is lower than stored value. This way, you’ll get the max height reached.
If necessary, reset the max height when colliding with a bouncer.

For the second point, assign a collided variable to the bouncer, change it to 1 when the ball bounces off it, and change it to 2 when a new bouncer is created. Then use that variable to decide if a bounce should produce a bouncer or not (if ball is colliding with bouncer and bouncer var collided is = 0 (or 1), change ball variable ActivateBouncerBuilding to 1)


#3

Thanks for your answer! I am new to gdevelop and found it difficult to implement what you are saying. I have this for my first point now:

But unfortunately it doesn’t work. It creates a new bouncer at the current Y position of the ball and it doesn’t create a bouncer at the max Y position the ball can reach directly after bouncing. Can you help me out? Thanks in advance, again!


#4

Do you want the new bouncer to be created at the moment the ball is colliding with bouncer?


:thinking:
I didn’t understand that from your first post.
And you want it at the highest point the ball reached during the previous bounce?


#5

Yes, I want to create a new bouncer (let’s say bouncer 2) at the moment the ball is colliding with bouncer 1. I want bouncer 2 to be created at the highest point the ball can reach after bouncing on bouncer 1. So somehow I need to define the maximum height the ball can reach after bouncing on bouncer 1, not the highest point the ball reached during the previous bounce. Thanks for helping me out.


#6

OK, you want to predict the future :sweat_smile:
Good luck:
https://www.iforce2d.net/b2dtut/projected-trajectory


#7

Thanks! It works now for point 1, but triggering the ‘‘create bouncer event’’ only once per bouncer doesn’t work for me. The situation is still that the ball bounces on the same bouncer twice and creates two new bouncers, but not one new bouncer no matter how many times the ball is colliding with the same bouncer. Can you please give me some help on this. I tried variables but it doesn’t work. Any suggestions? I really appreciate your help, thanks!


#8

I can’t tell you what you (or I) did wrong if you don’t show your events.


#9

I got it to work. Thanks for your time and effort!