Solving "Sag" Physics for 2D Games

  • Thread starter Thread starter sftrabbit
  • Start date Start date
  • Tags Tags
    Physics
AI Thread Summary
The discussion focuses on programming a 2D game feature that simulates ground sagging under a player's weight using physics. The key recommendation is to utilize the hyperbolic cosine function (cosh) to model the sagging effect, with parameters that define depth, width, and severity of the sag. Participants suggest modifying the function to allow for specific inputs for depth and severity while maintaining a defined width for the sagging area. The conversation also touches on the importance of adjusting parameters A, B, and D to achieve the desired visual effect. Overall, the goal is to create a realistic representation of ground deformation in response to player interaction.
sftrabbit
Messages
12
Reaction score
0
Erm... "Sag" physics?

Hi,

I'm trying to program a little 2D game to test some physics out.

What I want is a ground formed from a set of co-ordinates, with lines joining point to point to make the ground. Each point is separated 5 pixels apart.

Code:
_______________________________________

Now, when the player object sits upon this line, I want the line to sag down. Like a cushion. I want it to compress inwards.

Code:
____________             _____________
            ''.       -''
               '-,_,-'

... for example. Where the player is at the bottom of the ditch.

Is there an article (or maybe you wouldn't mind doing it) that could help me with being able to calculate the position of all the points of the ground to give the illusion that the ground is bending beneath the player?

Oh, it'd be really nice if you put it in high school/secondary school terms. I'm not too familiar with extensive Physics and Mathematics vocabulary.

Any idea?
Thanks a bunch.
 
Physics news on Phys.org
The function you want is a hyperbolic cosine function or cosh function. This function will describe the path of a string or a chain suspended by two points. The cosh function is related to exponential functions as follows;

cosh(x) = (e^(x) + e^(-x))/2

So if you use a function to describe the height of the ground as a function of position, you want a function that is constant everywhere except for the "sag region". The sag region will take the form of the cosh function defined above.

The cosh function in its most general form looks like this;

A.cosh(Bx + C) + D

There are 4 parameters that define the position and "severity" of the sagging.

A - Defines how deep the sagging is for a constant sag region.
B - Defines the "concavity" of the sagging. In other words, it defines, for a given sag depth, the width of the sag region. (If you like, heavier objects will result in a more concave sag).
C - Defines the horizontal position of the sag.
D - Defines the vertical position of the sag. Will also determine sag depth, but will also increase the size of the sag region.

I recommend experimenting with these parameters to get a feel for the effect each has on function shape.

For a 2D sag effect, the ground height will simply be the product of two cosh functions, one dependent on the X coordinate, and the other on the Y coordinate.

That's about all I can muster at a pinch, let me know how you go.

Claude.
 
Last edited:
If the sag is due to a point mass (one that has no size ) or a weight hanging down from the horizontal wire then it will come to a point.
If you are putting an object with a know width then you just start the calculation from each side of the object. So you would use the above formula but then set the middle (say 10 points) to the value at point 45 or 55
 
Aha. Good point.

Ok, so now how do I specify the width of the sagging area? I need to be able to say, for example, that the sag should start dipping down at variable w points away from the center. Where should this go in the formula?
 
You will need to define a new function, something like (For an object of width w);

Acosh(Bx+C)+D for x < -w/2+C
Some constant for -w/2+C < x < w/2+C
Acosh(Bx+C)+D for x > w/2+C

So w would be your new parameter that defines the width of the flat region in the centre of the sag.

I should emphasize though that this is just one way to accomplish this.

Claude.
 
Ok, so I have a function that works quite well, however it's depth seems to be dependent on the width of the sagging area, which I don't want it to be. Is there any way I can make the function so that I simply input a depth (from the original horizontal line to the point at the bottom of the sag), a width (from the center of the sag to the point where it levels off) and maybe a severity level (how quickly it sags from the outside).

To make it easier (hopefully) for you to understand, I'll describe my situation better.

I have a series of 100 dots, each one represented by two co-ordinates. The first dot's x-value is represented by ground[0,0] and it's y-value by ground[0,1]. I will describe the dot itself as being simply ground[0]. The second dot is then (ground[1,0],ground[1,1]) or ground[1].

So I've created a function called sag().

This sag function, at the moment, takes simply a width argument and a position argument. Width being from the edge of the sag to the center, and position being the number of the dot that will act as the center of the sag. For example, I could give 49 as the position, and it will be ground[49] that acts as the center of the sag.

So what my function does is, it creates a variable i and loops, increasing i each iteration until it reaches width. So for the loop to continue i < width must be true.

In the first loop, i = 0, so it performs my equation on ground[position] (which will be the central point). It then increases i and performs the equation on the two points next to the center. The equation it performs is:

cosh((width-i)/2)

I do "width-i" because the cosh function must be given a higher number as it is closer to the center so that it sags more in the middle. So, for the two points next to the center, if the width was perhaps 20, it would supply 20-1 (since i equals 1 at this point), 19. And for the next two outwards, it would be 20-2, so 18.

The problem with this equation, however, is that I am only giving it a width. And the depth and severity seems to depend entirely on this width. Here is an image with two sags created, the only difference between them being their widths:

http://i50.photobucket.com/albums/f317/sixfoottallrabbit/catenaries.jpg

So what do I change the equation to if I want to be able to supply a specific depth. The severity of the sag would be a plus, but it's not quite as important. Basically, the equation should be able to calculate all of it's values from just width, depth and severity.

I hope you understand.
 
Last edited by a moderator:
I get the basic gist of your problem, the solution is to tinker with the parameters A, B and possibly D, expressing them as a function of width, rather than a set constant.

Other than the trial and error approach mentioned above, the other option is to change your sag function, perhaps expressing it as a convolution of a square function of width w and the cosh function. I'm not sure how computationally intensive this might be though.

Claude.
 

Similar threads

Replies
3
Views
2K
Replies
5
Views
3K
Replies
30
Views
5K
Replies
4
Views
3K
Replies
1
Views
3K
Replies
2
Views
3K
Replies
80
Views
68K
Replies
2
Views
4K
Back
Top