Gamemaker Studio 2 Physics

Gamemaker Studio 2 Physics 3,9/5 1432 reviews
  1. Gamemaker Studio 2 Physics Tutorial
  2. Game Maker Studio 2 Ball Physics
  3. Gamemaker Studio 2 Rpg Tutorial
  4. Gamemaker Studio 2 Free Download
  • In GameMaker: Studio, you get your Object defined for physics, you might want to allow the player to move the Object around the Room during the game. The following two sections show how to add movement to an Object as well as make an Object turn left.
  • The physics system in GameMaker: Studio has a number of functions to apply distinct forces within the world. In the GameMaker: Studio physics world, instances aren’t only affected by gravity and collisions, they are also affected by the forces that you choose to apply to them, and knowing the right force to apply is very important when you want to control an instance correctly within the physics simulation.
  • The physics and visualization are done with shaders, making the water simulation very fast. You can have more than a million blocks of water simulated simultaneously with great frame rates. The asset is highly customizable, letting you tweak the water extensively. The asset is compatible with GM Studio 2 and GM Studio 1.
Part 1 Part 2 Part 3 Part 4
Gamemaker Studio 2 Physics

Today's tech blog takes form of a mini-tutorial, where we will be making a small physics simulation and at the same time we'll discuss some of the issues that people come up against when transitioning from the 'traditional' GameMaker Studio 2 approach to movement and collisions to the physics approach.

Even the most advanced users can be confused when using GameMaker Studio 2 physics for the first time, so we'll start this tutorial with probably the most important piece of advice we can give.. Forget everything you know about how GameMaker Studio 2 deals with collisions and movement! Why? Well, when you enable physics in a room, you are changing how everything will interact at a very basic level within GameMaker, and this makes the built in variables like speed and direction obsolete. However, it does enable a whole bunch of specific physics variables which can be used instead.

The other major change that happens when you enable physics is that collisions will be handled automatically for you. With 'traditional' GameMaker collisions you would have a Collision Event and then have some code or action in there to deal with what happens, but - with the physics enabled - the collisions will be resolved by GameMaker automatically, with bounce angles and velocities etc.. being calculated and applied for you.

So, with that warning out of the way, lets move on and get started making a simple physics simulation.. You'll need to make a new GML project in GameMaker Studio 2 and open the Room Editor before continuing.

The physics system in GameMaker: Studio has a number of functions to apply joints between fixtures. In the GameMaker: Studio physics world, joints are used to constrain instances to the world or to each other. Typical examples in games include ragdolls, teeters, and pulleys, but joints can be combined in many different ways to create interesting motions or add realism to your game world. However note that in the physics world rotations are calculated in the opposite way to the normal GameMaker: Studio game world, meaning that vector functions like pointdirection should have there return values modified (simply making positive to negative should resolve this). Example: if physpeedx 0 physpeedy 0.

To enable physics, you must first set up the physics world. This is done in the room editor, from the physics tab:

Obviously we need to tick the option Enable Physics (otherwise any physics enabled objects placed in the room will cause errors), and then we can set the gravity and the scale for the room. The room Gravity is calculated as a vector, with the distance and the direction of the vector giving the gravity strength in meters per second and the direction of the gravity pull.

The scale option Pixels to Meters isn't quite so obvious. In the physics simulation, all reactions are based off of idealised 'real life' physics, and so the scale of the interaction is important. A 1m cube of lead will not react the same as a 1cm cube of lead, and so we need to let the physics simulation know what scale we are working with. So, here you set how many meters one pixel in the game relates to.

For simplicity we'll leave the default values for this tab and you can close the room editor for now. We'll now need to quickly make some sprites to use for this project. You'll need two block sprites (one for the floor and one to be, well, a block), a circle sprite and a triangle sprite, and make sure to center their origins. They could be any size you want, but for the sake of consistency, keep them around the 32x32px size, as that's what was used to make the Demo file available at the end of this article.

We need to define our objects now, starting with a couple of parent objects. These parent objects won't have any physics properties themselves, and will only be used to 'group' collisions together for ease of use. Now, as we stated at the start of this article, with physics enabled objects will react to collisions automatically, but we still have to tell the physics simulation which objects we want to collide. So to do that we still need to give them a Collision Event, although we will add no code to it, just a simple comment to tell the physics that these instances should interact.

Make two objects now and call them 'obj_StaticParent' and 'obj_DynamicParent'. Then add collision events in both of them checking for collisions with themselves and each other so that they look like this:

You should also add an Outside Room event to the Dynamic parent, and in that put the GML function instance_destroy() so that if any instance falls out the room it is removed.

Why do we have two parents? Well, in the physics simulation you can have have three basic types of object: static, dynamic and kinematic. Each one has slightly different properties and will behave in a different way:

  • Static - A static body is one that does not react to any external force, which means that it is unaffected by gravity or collisions and will remain immobile in the position that it is placed within the room unless explicitly moved through code. Think of the 'wall' blocks you would place in a top down game, for example - in a physics world they would be classed as static objects.

  • Dynamic - A dynamic body is one that participates in the full physics simulation and it will react to any forces applied to, like gravity or when in a collision. Anything that moves and reacts to other instances in a game would normally be dynamic.

  • Kinematic - A kinematic body is a sort of mix between static and dynamic. It is an instance that has been flagged as static, but has then been set in motion through code by setting it's speed directly (not through forces). If you think of a top down game again, then you could make any doors that slide open or closed kinematic so that they don't rotate or react to forces, but they do still move.

In this basic project, we won't be using kinematic objects, but we will need static and dynamic ones, which is why we have the two parent objects - it just simplifies collision checking a lot!

Proshow gold 8 free download. Double-click the 'Add/Remove Programs' icon.

Gamemaker Studio 2 Physics Tutorial

We now need to add four more physics enabled objects: one that is static for the 'floor', and three dynamic ones, so go ahead and create these four objects now and give each one an appropriate sprite from those we made earlier:

You need to assign the floor object to have the obj_StaticParent as its parent, and the other three objects should be children of obj_DynamicParent to ensure that the collisions are resolved correctly. We can now move on and define the fixtures for the objects.

Okay, we have our physics enabled room and our objects, but we haven't actually given our objects any physical properties yet, have we? We need to tell GameMaker Studio 2 what properties the physics body will have, which will determine how it interacts with other physics bodies in the game. This is done by defining the fixture.

You define a fixture in two parts, the first being the shape it should have and the second being the properties it the shape should have. We can do this directly from the object editor itself or from code, but either way what happens is you define the fixture and then it is bound to the instance for it to use when created in the room. This may seem confusing, so we'll just continue and add a fixture to an object and explain along the way.

Open the object obj_Base and select the Uses Physics option then click the Physics button. This will open the object physics properties window with a number of extra options, shown below:

The first thing to set is the 'collision shape', so click the Modify Collision Shape button to open up the Fixture Editor. This editor works the same as the Path Editor and permits you to move the points of the edge of the fixture to fit the sprite being used:

NOTE: We are making the fixture conform to the shape of the assigned sprite in this project, but this is NOT required. A fixture is independent of the sprite used, meaning you can define any shape fixture for an object, whether it has a sprite assigned or not, and regardless of the form of the assigned sprite.

When you have defined the shape for the floor object, you can close the Fixture Editor window to save it. It's time now to set the fixture properties. As this object is static, we need to tell the physics simulation to treat it as such, and that is done by setting the Density to 0. In real life an object with 0 density isn't possible, so this is the GameMaker way of flagging a fixture as being static. We'll quickly list the rest of the properties here now, although for this object you can leave them as their default values (except density):

  • Density: The density of something is defined as its mass per unit volume, which basically means how much mass is crammed into the space it takes up in the world.

  • Restitution: Restitution is really a way of saying how 'bouncy' the fixture is. This setting will affect how much an object 'bounces' when it collides with other objects and is co-dependant on other forces that act on the instance like gravity and friction.

  • Collision Group: The collision group can be used to force GameMaker Studio 2 to treat collisions in specific ways.

  • Linear Damping: Damping is used to reduce the physics world velocity of an instance as it moves over time (like air resistance).

  • Angular Damping: As above, only instead affecting the velocity of movement, this sets the slow down rate for the angular velocity.

  • Friction: Friction sets the loss of momentum caused by the collision of two instances with fixtures bound to them. The lower the value, the easier it is for one colliding instance to 'slide' off the other.

You can find a more complete explanation of each of these properties from the manual, so we won't go into too much depth on them here, except where required for the test project.

Close the floor object now, and open the object obj_Block. This is our first dynamic object and you should again select Uses Physics then click the 'Physics' button to open the properties editor, then adjust the box fixture shape to suit the sprite in the Fixture Editor, as we did for the floor object. The default values are fine here too - the density set to 0.5 - so close the object once you've finished and then open the object obj_Circle.

Once more, enable physics selecting the Uses Physics option then click the 'Physics' button and open the Fixture Editor. This time we need to set the collision shape to 'Circle', which can be done from the menu at the top left:

When you edit the collision shape now, you will see that it is no longer a fixture with four corner points, but rather a fixture with two points, one marking the center of the fixture and the other marking the radius. Set these to suit your sprite and close the editor when ready. Now, in the physics properties, set the density to 0.1, and set the restitution to 0.8. We do this because we want it to be lighter than the block fixture and bounce on contact with other fixtures.

For our final object, the triangle, we need to create a polygon fixture. This is a fixture that we explicitly define the points for, and in a very specific way. A polygon fixture has a minimum of 3 points and a maximum of 8 points and it cannot be concave. This is important to note as to make concave shapes you need to bind multiple fixtures which can only be done through code (and we'll cover it in a future tech blog). So, enable physics for the object then click the 'Physics' button and open the Fixture Editor to edit the collision shape. In the editor, select the Convex Shape option for the collision shape. Now, not only can you move points around, but you can add and remove them to create your fixture:

Add and adjust three points now to suit the triangle sprite that we have assigned to this object, then close the editor. With that done, set the fixture properties to have a high density (0.8) and a high linear and rotational damping (0.6), but a low restitution (0.1) so it behaves slightly differently from the previous objects.

We are almost ready to test these objects and see how they react to each other, but before we can we need to make a controller object, so make a new object and call it obj_Control. Give it an Alarm[0] event with a comment, like 'Spawn Timer' or something. This is simply to limit the number of instances of our physics objects spawned. Now add a Step Event with the following:

Place this object in the room we created at the start, along with a number of the static obj_Base objects all on the 'Instances' layer. Now run the project! If all has gone correctly you should see something like this:

Congratulations! You've just created your first physics simulation in GameMaker Studio 2!

To round off this article, let's just run through some of the important points:

  • When physics is enabled, all collisions and movement are governed by the special physics variables and functions, and not the normal ones.

  • Physics bodies can be defined by one of three types: static, dynamic or kinematic.

  • For physics bodies to collide, the instance that has physics enabled must have a collision event, even if that event only has a comment.

  • An object is physics enabled when it has a fixture bound to it.

  • A fixture is a combination of a collision shape and basic physical properties.

Hopefully with this article you can see that basic physics is quite easy to set up and start working with. However you'll notice that apart from creating the objects themselves, there is no other interaction in the example. For that we need to start using the dedicated physics functions through the code editor, and we'll be covering that along with a few other more complex themes in a future tech blog.

You can download a .yyz for this tech blog showing the finished example here.

The GameMaker: Studio physics system has a number of functions for defining fixtures which can then be applied to instances.

Before an instance in GameMaker: Studio can have physical properties it must have a fixture assigned to it. A fixture binds a shape or form to the instance and this adds material properties such as density, friction, and restitution. Fixtures can be boxes, circles, polygons or any other simple shape you wish as GameMaker: Studio provides functions that enable you to define your own shape point by point, and then once you have your shape you can go ahead and define its physical properties too. These properties are what govern the way the instance with the fixture assigned to it will react with other instance in the physics world and are listed below:

  • Density: A material's density is defined as its mass per unit volume. It is, essentially, a measurement of how tightly matter is crammed together. If you wish your fixture to be static (ie: immovable) then the density should be set to 0, essentially making it infinitely dense. Kinematic objects that move with constant velocity and are not physically affected by collisions are also defined using a density of 0.

  • Friction: Friction is the force resisting the relative motion of solid surfaces, fluid layers, and material elements sliding against each other.

  • Linear Damping: This is the generalised force that causes a moving object to lose momentum. In the 'real world' things don't generally happen forever. For example, A paper aeroplane will fly for a short while but eventually it will fall due to the air friction that acts upon it causing it to slow down. In the physics world that we create such things are difficult to model and so each fixture is given its own linear damping to simulate this type of 'generalised friction'.

  • Angular Damping: As above, this is a generalised force that is applied to an object to stop it spinning eternally.

  • Restitution: In physics, restitution is defined as 'the return of an object or system to its original state after elastic deformation', but as the fixtures in the GameMaker: Studio are really rigid bodies and cannot be deformed, think of this as basically how 'bouncy' the fixture is.

So as you can see, with just a few simple functions, GameMaker: Studio can help you to create complex systems and interactions which will give your games an unprecedented sense of realism.
Note: You can define one fixture and have it bound to multiple instances (think of a pool table, where all the balls have the same physical properties, yet each one is a discreet entity)

Creating Fixtures

The following functions are for creating, binding, setting collisions and then deleting fixtures:

  1. physics_fixture_create
  2. physics_fixture_bind
  3. physics_fixture_bind_ext
  4. physics_fixture_set_collision_group
  5. physics_fixture_delete
  6. physics_remove_fixture

Defining Fixture Shapes

A fixture must be given a shape or else it will not be detected by the physics world, and this shape can be defined by the following functions:

  1. physics_fixture_set_box_shape
  2. physics_fixture_set_circle_shape
  3. physics_fixture_set_edge_shape
  4. physics_fixture_set_chain_shape
  5. physics_fixture_set_polygon_shape
  6. physics_fixture_add_point

Fixture Properties

In order for your physics enabled instance to react properly to the world around it, the fixtures you use must have specific properties defined that will give the fixture bounce, friction, mass etc.. The following functions are used to set these properties of the fixture:

Game Maker Studio 2 Ball Physics

  1. physics_fixture_set_density
  2. physics_fixture_set_friction
  3. physics_fixture_set_linear_damping
  4. physics_fixture_set_angular_damping
  5. physics_fixture_set_restitution
  6. physics_fixture_set_sensor
  7. physics_fixture_set_kinematic
  8. physics_fixture_set_awake

You can also set certain properties of the fixture after it has been bound to an instance. When binding the 'base' fixture, you can choose to store the unique index for the bound physical properties in a variable. This can then be used in the following functions to change certain properties, or to get the current values for them:

Gamemaker Studio 2 Rpg Tutorial

  1. physics_get_friction
  2. physics_get_density
  3. physics_get_restitution
  4. physics_set_friction
  5. physics_set_density
  6. physics_set_restitution
  7. physics_mass_properties


Next: Joints
Gamemaker Studio 2 Physics
© Copyright YoYo Games Ltd. 2018 All Rights Reserved

Gamemaker Studio 2 Free Download