Apply gravity to bouncing ball....

Got balls.

They move along a line, following a direction vector, at some speed…
They bounce off walls and such, in an elastic reaction, ie, they never lose speed.
I use them for paddle games and place holders, and much more.

Using them currently, as a grenade, so nades bounce off walls and stuff, and explode after 3 or 4 seconds.

But they need gravity… normally, for an object to “fall”, I would just add a float for gravity, accelerate the gravity over time, and ADD the gravity to an objects normal movement, reseting gravity and stopping grav. accel. when grounded…

But these nades bounce off angled surfaces and floors, so how do I control for that, when it comes to gravity acceleration?

Opposite surface vector?

Grab the angle of the surface hit and vectorise that as a force percentage against the object.

1 Like

Ok, I’ve been trying to post back for quite some time, but end up deleting it, for rambling…

Seems something like: When the ball bounces, however much its direction just changed from that,
it should lose some momentum ie direction… AND gravity should be reduced by the same factor? (from 0 to 1)

You’re talking about two different concepts I think.

  • Gravity is a force that is constantly applied in a single direction. Unless your specific gameplay mechanics want it to, this never changes. It’s an optimization for you to disable gravity when it can no longer apply (ie, grounded), but technically it never goes away.
  • Energy loss due to imperfect elastic collision. A fraction of an object’s energy is lost when it bounces.

It’s my quick way of doing gravity, because I think “real” gravity is too fast, for the small screen real estate…

So I just have a float for “total_gravity”, which I increment by a small amount each frame, up to a max.

Then the “total gravity” is applied to the objects position.

When a thing touches floor, “total gravity” is set to zero, and it stops falling…

Works fine for most things, because they grip a surface, or explode or whatever…

  • elastic reaction balls are simple too, in concept anyway.

But combining the 2 I don’t even know where to start.
-Right now, I just set gravity to zero when I bounce on something BELOW the ball… It is a fair approximation, but it’s not as COOL as I feel a more real system would be…

here is my relevant code

BOUNCE: … this checks the impact point on the ball (cross point) to see if the bounce happened below the ball, ie a floor… IF it is floor, reset gravity… And for ANY surface hit, deposit some energy.

 if (direction.Y < 0 && target_list[0].cross_point.X > my_pos.X - 6 && target_list[0].cross_point.X < my_pos.X + 6)
   {
      gravity = 0;                           
   }
 direction.Y *= 0.75f;

and the balls movement code:

          my_pos += direction;
          gravity += 0.016f;
          direction.Y += gravity;

I think you’re maybe going a bit too far afield here. We can go back to some basics and build up from there. Let’s take bounce energy loss out of the game for a minute here and just consider gravity. There’s some physics formulas that can be useful here.

A change in position over time can be described by d = t * v, where,

  • d = a change in position (delta distance)
  • t = a change in time (delta time)
  • v = velocity, or, the amount of distance something moves for a given period of time.

A change in velocity over time can be described by v = a * t, where,

  • v = a change in velocity
  • a = acceleration, or, the amount velocity changes over time.
  • t = a change in time

Consider these and how they can move your ball by updating your ball’s velocity over time, as well as updating your ball’s position over time.

You’re welcome to keep using the approach you’re using, but it makes it a bit harder to sort out any issues because your forging your own path.

If you give the above a try and run into issues, let me know. It’s been a while since I’ve done this so I’m going to play around a bit on my own haha.

1 Like

Still these hours later, the BEST results I can achieve are by fudging it, and tweaking the values til it looks most convincing… Not ideal.

I will have to withdraw for a bit, and stare at pen and paper for this one… I’d love to hear what you figure our.

Hi BOTH you guys! I can only tag one of you, it seems-

But I THINK I got it now!

balls direction (normalized) Y component is the factor for gravity…

so, increment float gravity each frame by some nice looking number, 0.02 or so in my case…
add this float gravity to balls Y dir each frame… then for every BOUNCE, multiply gravity by a number between 0 and 1, corresponding with normalized dir Y component…

  • And then I also on bounce, scale the overall direction vector by 0.9f to deposit some energy.
Vector2 normalized_dir = direction;
normalized_dir.Normalize();

gravity *= (Math.Abs( normalized_dir.Y));

direction *= 0.9f;

IT LOOKS AND FEELS GREAT (so far, only tested for a few minutes before coming back to tell you guys)

2 Likes

I’m glad you got it working in a way that you like :slight_smile:

Consider though that I think you’re confusing gravity, an acceleration, with velocity. You should not modify gravity, but you should use gravity to modify your velocity. Velocity is what changes your position.

From what I can tell, that’s ultimately what you’re doing… you’re just using the wrong terms to describe it? Keep that in mind in case you ever need to debug this later, or share it with someone else.

Anyway, have fun!

well, the gravity number I have, is incremented at a fixed rate, and only diminished by bouncing…

Its like the accumulated motion / force / inertia DUE to gravity, rather than actual gravity…

Like something that falls for longer, moves faster and faster, accumulates more energy, and bounces higher, …

I think my model works, but my language is not up to par. It produces arched throws, and every bounce is lower than the last one…

One line of code I have to help explain this, that I did not post above, is for every frame,
gravity += 0.02f…

So the force that effects the grenade downward each frame, is incremented each frame.

after each bounce, gravity has to start accelerting the grenade back downwards again,
overcoming the new upwards Y direction…

Am I going in circles instead of making myself clear?

Replace gravity with velocity and 0.02f with gravity and you’ve got it :wink:

2 Likes

This book https://natureofcode.com/, which you can read online for free or purchase the PDF, gives a good primer on this subject.

Chapters 1 and 2 will be the most relevant.