Solving "Sag" Physics for 2D Games

  • Thread starter sftrabbit
  • Start date
  • Tags
    Physics
In summary: So in short, all you need to do is input the width and position of the sag (relative to the original horizontal line), and the function will do the rest.Claude.Ok, so what's the severity level?The severity level is simply how quickly the sag decreases from the outside.
  • #1
sftrabbit
12
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
  • #2
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:
  • #3
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
 
  • #4
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?
 
  • #5
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.
 
  • #6
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:
  • #7
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.
 

1. What is "sag" physics in 2D games?

"Sag" physics in 2D games refers to the simulation of gravity and its effect on objects in a game world. This includes how objects fall, bounce, and interact with each other based on the laws of physics.

2. Why is solving "sag" physics important in 2D games?

Solving "sag" physics is important because it adds a level of realism and immersion to the game. It allows for more realistic movement and interactions between objects, making the game more engaging for players.

3. How do you solve "sag" physics in 2D games?

Solving "sag" physics in 2D games involves using mathematical equations and algorithms to calculate the behavior of objects in the game world. This includes factors such as mass, velocity, and acceleration to determine how objects will move and interact with each other.

4. What are some challenges in solving "sag" physics for 2D games?

Some common challenges in solving "sag" physics for 2D games include accurately representing the effects of gravity and collisions, optimizing performance for complex simulations, and accounting for different types of objects and environments.

5. How can developers improve "sag" physics in their 2D games?

Developers can improve "sag" physics in their 2D games by conducting thorough research and testing, using realistic mathematical models and algorithms, and constantly fine-tuning and optimizing the physics engine. They can also seek feedback from players to make adjustments and improvements.

Similar threads

Replies
3
Views
2K
  • Other Physics Topics
Replies
5
Views
2K
Replies
31
Views
2K
Replies
2
Views
1K
  • STEM Career Guidance
Replies
4
Views
1K
Replies
4
Views
1K
Replies
124
Views
14K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top