# Platform Physics

12 replies to this topic

### #1 brod

brod

Brian RODriguez

• GMC Member
• 2021 posts
• Version:GM8

Posted 30 December 2009 - 06:59 PM

Alright, so recently I thought it'd be a good idea to take a new approach to platform elements: Using forces instead of simply plugging in hspeed and vspeed.

By that, I mean, say when you collide with a block, instead of simply setting your vspeed to 0, you add a force (a.k.a. Normal Force) to the player, which would effectively stop it. However, I'm having trouble figuring out just exactly they'd be found, I figured using data structures would be a good way to find the net force. But that raises another important question, what would be an effective way to find normal force? As you may or may not know, normal force is perpendicular to the surface applying it, but how would that be found if you just have random squiggles?

So, these are the current topics that come to mind
• Using forces, a good or a bad idea?
• Normal Force, how would it be found?

Hopefully more topics will be added as the discussion continues, if it's even allowed, that is.

Of course there are already plenty of great physic dlls and the like available, but it'd be great if we could accomplish it in GML.
• 0

### #2 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 30 December 2009 - 09:50 PM

Newtonian Physics at its heart is simply the axiom that the net sum of the forces F = ma. If anything should be included in a physics engine, it should be this relationship. Alternatively, one can start with the definition of momentum p = mv, which can then be used to derive F = ma. These two notions should be equivalent. One key important point is that Newton's laws only hold in inertial reference frames. However, Game Maker's coordinate system is an inertial reference, so this isn't a problem.

Forces can depend on various things, and often require methods of differential equations to solve. One example is the spring force: The net force when the spring is released is proportional to the distance for which it is stretched. In other words, ma = -kx. (The minus sign is used to indicate direction. k is the constant of proportionality, often referred to as a spring constant.) After realizing that this is the differential equation m d^2x / dt^2 = -kx(t), we can solve this for the spring's position as a function of time:
x(t) = Acos(wt), where w = sqrt(k / m), and is the angular frequency of the object.

When we are trying to mimic physics in Game Maker, there are two approaches. The first is to use (and know) the equations of motion, and perform calculations to where the object should be and how it should react. The other is to try and mimic the differential equation to reproduce the equations of motion. This is more valuable because this essentially is what F = ma is all about -- F = ma tells us how the system will act at a later time.

As an example of these two methods, let's look at an object that starts at y = room_height / 2, x = room_width / 2. We want to mimic the object moving up and down in the y-direction as a wave. The two methods look like this:

Method 1
Create Event
```mass = 1
time = 0; //Time parameter

x_start = room_width / 2
x = x_start

y_start = room_height / 2
y = y_start
y_wave_amp = room_width / 4
y_wave_period = room_speed * 4
y_wave_frequency = 2 * pi / y_wave_period //this is angular frequency.```

Step Event
```time += 1

x = x_start
y = y_start + y_wave_amp * cos(y_wave_frequency * time)```

Method 2
Create Event
```mass = 1

x_start = room_width / 2
x = x_start
x_speed = 0;
x_acceleration = 0;

y_start = room_height / 2
y_speed = 0;
y_wave_amp = room_width / 4
y_wave_period = room_speed * 4
y = y_start + y_wave_amp
y_wave_frequency = 2 * pi / y_wave_period //this is angular frequency.
y_acceleration = 0```

Step Event
```x_acceleration = 0 //this corresponds to no forces in x direction
x_speed += x_acceleration
x += x_speed

y_acceleration = -sqr(y_wave_frequency) * (y - y_start)
y_speed += y_acceleration
y += y_speed```

Try these. They should give identical results, up to small errors. There are key differences between the two. The first equation requires a time parameter, and is quite rigid--it cannot be combined with any other "forces" unless a new position function is generated. The second, however, requires only that it starts an initial amount away, and uses the differential equation to generate the motion, independent of a time parameter. This second method should combine better with forces, simply by adding or subtracting to the acceleration.

If a general physics engine is to be created, then method 2 should be strived for.
• 0

### #3 inarma

inarma

GMC Member

• GMC Member
• 596 posts

Posted 30 December 2009 - 09:59 PM

Lol, this is kinda cool. Yea I know I'm not the brightest person ever so this is gonna sound kinda dumb, but you should really work towards this you could recreate the Earth's physics and make them easily more open to the GMC without needing such an expansive knowledge of their calculations. Keep at it I'd like to see where this goes.
• 0

### #4 Tepi

Tepi

GMC Member

• Global Moderators
• 4201 posts
• Version:GM8.1

Posted 30 December 2009 - 09:59 PM

• Using forces, a good or a bad idea?

For impulse, a bad idea, unless your platforms are very soft (and the time that is taken for the normal force to stop the object isn't "instant"). For typical platforms, the player will stop instantly and because it won't seem to take any time, it doesn't need to be calculated as if it took significant time. The significant part is the state in which it is in the next step, and that is the same in all platform collisions.

For anything else, good idea. Forces are the elements that drive the simulation, so it's necessary to 'use' forces. If not in the form of forces, at least in accelerations.

• Normal Force, how would it be found?

As far as I know you can only find the normal force indirectly as the magnitude of the (component of) vector it cancels out (if it does cancel something out).
• 0

• GMC Member
• 1129 posts
• Version:GM8

Posted 30 December 2009 - 11:35 PM

Lol, this is kinda cool. Yea I know I'm not the brightest person ever so this is gonna sound kinda dumb, but you should really work towards this you could recreate the Earth's physics and make them easily more open to the GMC without needing such an expansive knowledge of their calculations. Keep at it I'd like to see where this goes.

Recreate the universe's physics, you mean? I guess it would be sort of interesting to somehow implement special relativity into Game Maker.

As far as recreating physics on a human-scale in Game Maker, I would suppose the first step would be to find the edges of the shape in question. Of course this would take a while to find on 'random squiggles' but I think that this would be the best approach. When a collision occurs, only the mass, acceleration, and areas of collision need to be taken into account for most objects. Knowing where the outlines intersect between the objects can determine a fairly accurate movement.
• 0

### #6 inarma

inarma

GMC Member

• GMC Member
• 596 posts

Posted 01 January 2010 - 07:20 PM

Sounds like a start
• 0

### #7 xDanielx

xDanielx

GMC Member

• GMC Member
• 1001 posts
• Version:Unknown

Posted 02 January 2010 - 05:05 AM

Normal forces aren't really a useful concept in the rigid body model because there is no penetration. If we wanted to explain a rigid body collision in terms of forces, we would have to say that both bodies experience an infinite normal force for an infinitesimal time.

Really, the ground holds me up because my floor is full of soft bodies, gravity causes them to become deformed, and solids tend to resist deformation. I think explaining the underlying physics faithfully would require us to at least look at Coulomb's law on the atomic scale. Since emulating that isn't computationally feasible, engines like ODE use measures like "softness" and "restitution" to handle collisions between basically-rigid bodies.
• 0

### #8 gandalf20000

gandalf20000

GMC Member

• New Member
• 478 posts

Posted 02 January 2010 - 11:33 PM

Perhaps what you could do is use primitives? Create a basic primitive collision shape using GM's primitive creation methods, and use it basically like a collision mask for the sprite. Then, find out which edge you collided with, and get the two vertices that form the edge. Get the slope of the edge ((y1-y2)/(x1-x2)). We'll assume that after this, to make math simpler, y1-y2=y3, and x1-x2=x3. Get the perpendicular slope, which is equal to -x3/y3. After that, use the inverse tangent (converting to degrees, of course) to get the angle, and use that as the normal, which you can use for the physics calculations. I don't know, it's just an idea.
• 0

### #9 griffi

griffi

GMC Member

• New Member
• 243 posts

Posted 08 January 2010 - 07:41 PM

Lol, this is kinda cool. Yea I know I'm not the brightest person ever so this is gonna sound kinda dumb, but you should really work towards this you could recreate the Earth's physics and make them easily more open to the GMC without needing such an expansive knowledge of their calculations. Keep at it I'd like to see where this goes.

Recreate the universe's physics, you mean? I guess it would be sort of interesting to somehow implement special relativity into Game Maker.

As far as recreating physics on a human-scale in Game Maker, I would suppose the first step would be to find the edges of the shape in question. Of course this would take a while to find on 'random squiggles' but I think that this would be the best approach. When a collision occurs, only the mass, acceleration, and areas of collision need to be taken into account for most objects. Knowing where the outlines intersect between the objects can determine a fairly accurate movement.

Moment would also need to be considered for the purpose of balance and rotation, wouldn't it?
• 0

### #10 petenka

petenka

The Chosen One

• New Member
• 911 posts

Posted 28 January 2010 - 04:42 PM

If you want to have complex shapes, try representing your platform as a series of lines.
From here you can take the vector of the player's movement, and calculate collisions precisely.
My platformer physics engine uses such a method and it's given me the power I need.
There's probably a lot more variables you can incorporate into it such as mass and such, but the collision code I use is a great place to start.

Edited by petenka, 28 January 2010 - 04:44 PM.

• 0

### #11 Tepi

Tepi

GMC Member

• Global Moderators
• 4201 posts
• Version:GM8.1

Posted 29 January 2010 - 12:02 AM

If you want to have complex shapes, try representing your platform as a series of lines.

Actually it doesn't matter which shape you model the objects to be as long as the model is mathematically defined and differentiable (allowing you to derive the normal at any position at which it's differentiable) provided, of course, it's supposed to be static and rigid. otherwise you're going to have to calculate other properties such as moment of inertia, which luckily isn't very difficult (but you have to incorporate other models such as a function for density etc.).

As for modelling the whole universe, don't even think about it. It would require way too much memory.
• 0

### #12 ynspyred

ynspyred

GMC Member

• New Member
• 328 posts

Posted 06 February 2010 - 04:02 PM

If you want to have complex shapes, try representing your platform as a series of lines.

Why? You can get the normal to any polynomial easily, and also to logarithmic and trigonometric functions. What if I wanted to have a curvy children's slide in the game? I would represent a slide as cos(x) where 0 <= x <= pi. (0 <= x <= 180 degrees). Scaled around so you can actually see it, of course! Combined with xDanielx's infinite force over infinitesimal time (which I have no idea of how you would go about implementing.*), I'd get a beautiful smooth curve motion. Approximating it as a series of lines, though, would lead to ugly bouncing, and if the slide was long, the player/object would bounce all over the shop.
Using other curves, you could make other shapes as well. A parabola or semicircle would do for a halfpipe, and so on.

@inarma: Recreating the earth's physics would be impossible, as we don't know everything about how the insides of atoms work, etc. Particles could be approximated as 'dots', with position and motion, but the memory required would be too great, and the computation, as every single particle in a system affects every other.

*Perhaps I could cheat by setting the motion to the tangent to the curve at that point?
• 0

### #13 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 06 February 2010 - 05:52 PM

As for modelling the whole universe, don't even think about it. It would require way too much memory.

It's an impossible task anyway. In order to simulate the entire universe, your simulation would have to include a simulation of itself. Of course, that's for a simulation of this universe.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users