Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Let's Get Physical – Using GameMaker's Physics System

Save for later
  • 28 min read
  • 02 Nov 2015

article-image

 In this article by Brandon Gardiner and Julián Rojas Millán, author of the book GameMaker Cookbook we'll cover the following topics:

  • Creating objects that use physics
  • Alternating the gravity
  • Applying a force via magnets
  • Creating a moving platform
  • Making a rope

(For more resources related to this topic, see here.)

The majority of video games are ruled by physics in one way or another. 2D platformers require coded movement and jump physics. Shooters, both 2D and 3D, use ballistic calculators that vary in sophistication to calculate whether you shot that guy or missed him and he's still coming to get you. Even Pong used rudimentary physics to calculate the ball's trajectory after bouncing off of a paddle or wall. The next time you play a 3D shooter or action-adventure game, check whether or not you see the logo for Havok, a physics engine used in over 500 games since it was introduced in 2000. The point is that physics, however complex, is important in video games. GameMaker comes with its own engine that can be used to recreate physics-based sandbox games, such as The Incredible Machine, or even puzzle games, such as Cut the Rope or Angry Birds. Let's take a look at how elements of these games can be accomplished using GameMaker's built-in physics engine.

Physics engine 101

In order to use GameMaker's physics engine, we first need to set it up. Let's create and test some basic physics before moving on to something more complicated.

Gravity and force

One of the things that we learned with regards to GameMaker physics was to create our own simplistic gravity. Now that we've set up gravity using the physics engine, let's see how we can bend it according to our requirements.

Physics in the environment

GameMaker's physics engine allows you to choose not only the objects that are affected by external forces but also allows you to see how they are affected. Let's take a look at how this can be applied to create environmental objects in your game.

Advanced physics-based objects

Many platforming games, going all the way back to Pitfall!, have used objects, such as a rope as a gameplay feature. Pitfall!, mind you, uses static rope objects to help the player avoid crocodiles, but many modern games use dynamic ropes and chains, among other things, to create a more immersive and challenging experience.

Creating objects that use physics

There's a trend in video games where developers create products that have less games than play areas; worlds and simulators in which a player may or may not be given an objective and it wouldn't matter either way. These games can take on a life of their own; Minecraft is essentially a virtual game of building blocks and yet has become a genre of its own, literally making its creator, Markus Persson (also known as Notch), a billionaire in the process. While it is difficult to create, the fun in games such as Minecraft is designed by the player. If you give a player a set of tools or objects to play with, you may end up seeing an outcome you hadn't initially thought of and that's a good thing. The reason why I have mentioned all of this is to show you how it binds to GameMaker and what we can do with it. In a sense, GameMaker is a lot like Minecraft. It is a set of tools, such as the physics engine we're about to use, that the user can employ if he/she desires (of course, within limits), in order to create something funny or amazing or both.

What you do with these tools is up to you, but you have to start somewhere. Let's take a look at how to build a simple physics simulator.

Getting ready

The first thing you'll need is a room. Seems simple enough, right? Well, it is. One difference, however, is that you'll need to enable physics before we begin. With the room open, click on the Physics tab and make sure that the box marked Room is Physics World is checked. After this, we'll need some sprites and objects. For sprites, you'll need a circle, triangle, and two squares, each of a different color. The circle is for obj_ball. The triangle is for obj_poly. One of the squares is for obj_box, while the other is for obj_ground. You'll also need four objects without sprites: obj_staticParent, obj_dynamicParent, obj_button, and obj_control.

How to do it

  1. Open obj_staticParent and add two collision events: one with itself and one with obj_dynamicParent.
  2. In each of the collision events, drag and drop a comment from the Control tab to the Actions box.
  3. In each comment, write Collision.
  4. Close obj_staticParent and repeat steps 1-3 for obj_dynamicParent.
  5. In obj_dynamicParent, click on Add Event, and then click on Other and select Outside Room.
  6. From the Main1 tab, drag and drop Destroy Instance in the Actions box. Select Applies to Self.
  7. Open obj_ground and set the parent to obj_staticParent.
  8. Add a create event with a code block containing the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2);
    physics_fixture_set_density(fixture, 0);
    physics_fixture_set_restitution(fixture, 0.2);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  9. Open the room that you created and start placing instances of obj_ground around it to create platforms, stairs, and so on. This is how mine looked like:
  10. Open obj_ball and set the parent to obj_dynamicParent.
  11. Add a create event and enter the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_circle_shape(fixture, sprite_get_width(spr_ball) / 2);
    physics_fixture_set_density(fixture, 0.25);
    physics_fixture_set_restitution(fixture, 1);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  12. Repeat steps 10 and 11 for obj_box, but use this code:
    var fixture = physics_fixture_create();
    physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2);
    physics_fixture_set_density(fixture, 0.5);
    physics_fixture_set_restitution(fixture, 0.2);
    physics_fixture_set_friction(fixture, 0.01);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  13. Repeat steps 10 and 11 for obj_poly, but use this code:
    var fixture = physics_fixture_create();
    physics_fixture_set_polygon_shape(fixture);
    physics_fixture_add_point(fixture, 0, -(sprite_height / 2));
    physics_fixture_add_point(fixture, sprite_width / 2, sprite_height / 2);
    physics_fixture_add_point(fixture, -(sprite_width / 2), sprite_height / 2);
    physics_fixture_set_density(fixture, 0.01);
    physics_fixture_set_restitution(fixture, 0.1);
    physics_fixture_set_linear_damping(fixture, 0.5);
    physics_fixture_set_angular_damping(fixture, 0.01);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  14. Open obj_control and add a create event using the following code:
    globalvar shape_select;
    globalvar shape_output;
    shape_select = 0;
  15. Add a Step and add the following code to a code block:
    if mouse_check_button(mb_left) && alarm[0] < 0 && !place_meeting(x, y, obj_button)
    {
    instance_create(mouse_x, mouse_y, shape_output);
    alarm[0] = 5;
    }
    if mouse_check_button_pressed(mb_right)
    {
        shape_select += 1;
    }
  16. Now, add an event to alarm[0] and give it a comment stating Set Timer.
  17. Place an instance of obj_control in the room that you created, but make sure that it is placed in the coordinates (0, 0).
  18. Open obj_button and add a step event.
  19. Drag a code block to the Actions tab and input the following code:
    if shape_select > 2
    {
        shape_select = 0;
    }
    if shape_select = 0
    {
        sprite_index = spr_ball;
        shape_output = obj_ball;
    }
    if shape_select = 1
    {
        sprite_index = spr_box;
        shape_output = obj_box;
    }
    if shape_select = 2
    {
        sprite_index = spr_poly;
        shape_output = obj_poly;
    }

Once these steps are completed, you can test your physics environment. Use the right mouse button to select the shape you would like to create, and use the left mouse button to create it. Have fun!

How it works

While not overly complicated, there is a fair amount of activity in this recipe. Let's take a quick look at the room itself. When you created this room, you checked the box for Room is Physics World. This does exactly what it says it does; it enables physics in the room. If you have any physics-enabled objects in a room that is not a physics world, errors will occur. In the same menu, you have the gravity settings (which are vector-based) and pixels to meters, which sets the scale of objects in the room. This setting is important as it controls how each object is affected by the coded physics. YoYo Games based GameMaker's physics on the real world (as they should) and so GameMaker needs to know how many meters are represented by each pixel. The higher the number, the larger the world in the room. If you place an object in two different rooms with different pixel to meter settings, even though the objects have the same settings, GameMaker will apply physics to them differently because it views them as being of differing size and weight.

Let's take a look at the objects in this simulation. Firstly, you have two parent objects: one static and the other dynamic. The static object is the only parent to one object: obj_ground. The reason for this is that static objects are not affected by outside forces in a physics world, that is, the room you built. Because of this, the ground pieces are able to ignore gravity and forces applied by other objects that collide with them. Now, neither obj_staticParent nor obj_dynamicParent contain any physics code; we saved this for our other objects. We use our parent objects to govern our collision groups using two objects instead of coding collisions in each object. So, we use drag and drop collision blocks to ensure that any children can collide with instances of one another and with themselves. Why did you drag comment blocks into these collision events? We did this so that GameMaker doesn't ignore them; the contents of each comment block are irrelevant. Also, the dynamic parent has an event that destroys any instance of its children that end up outside the room. The reason for this is simply to save memory. Otherwise, each object, even those off-screen, will be accounted for calculations at every step and this will slow everything down and eventually crash the program.

Now, as we're using physics-enabled objects, let's see how each one differs from the others. When working with the object editor, you may have noticed the checkbox labelled Uses Physics. This checkbox will automatically set up the basic physics code within the selected object, but only after assuming that you're using the drag and drop method of programming. If you click on it, you'll see a new menu with basic collision options as well as several values and associated options:

  • Density: Density in GameMaker works exactly as it does in real life. An object with a high density will be much heavier and harder to move via force than a low-density object of the same size. Think of how far you can kick an empty cardboard box versus how far you can kick a cardboard box full of bricks, assuming that you don't break your foot.
  • Restitution: Restitution essentially governs an object's bounciness. A higher restitution will cause an object to bounce like a rubber ball, whereas a lower restitution will cause an object to bounce like a box of bricks, as mentioned in the previous example.
  • Collision group: Collision grouping tells GameMaker how certain objects react with one another. By default, all physics objects are set to collision group 0. This means that they will not collide with other objects without a specific collision event. Assigning a positive number to this setting will cause the object in question to collide with all other objects in the same collision group, regardless of collision events. Assigning a negative number will prevent the object from colliding with any objects in that group. I don't recommend that you use collision groups unless absolutely necessary, as it takes a great deal of memory to work properly.
  • Linear damping: Linear damping works a lot like air friction in real life. This setting affects the velocity (momentum) of objects in motion over time. Imagine a military shooter where thrown grenades don't arc, they just keep soaring through the air. We don't need this. This is what rockets are for.
  • Angular damping: Angular damping is similar to linear damping. It only affects an object's rotation. This setting keeps objects from spinning forever. Have you ever ridden the Teacup ride at Disneyland? If so, you will know that angular damping is a good thing.
  • Friction: Friction also works in a similar way to linear damping, but it affects an object's momentum as it collides with another object or surface. If you want to create icy surfaces in a platformer, friction is your friend.

We didn't use this menu in this recipe but we did set and modify these settings through code. First, in each of the objects, we set them to use physics and then declared their shapes and collision masks. We started with declaring the fixture variable because, as you can see, it is part of each of the functions we used and typing fixture is easier than typing physics_fixture_create() every time. The fixture variable that we bind to the object is what is actually being affected by forces and other physics objects, so we must set its shape and properties in order to tell GameMaker how it should react. In order to set the fixture's shape, we use physics_set_circle_shape, physics_set_box_shape, and physics_set_polygon_shape. These functions define the collision mask associated with the object in question. In the case of the circle, we got the radius from half the width of the sprite, whereas for the box, we found the outer edges used via half the width and half the height. GameMaker then uses this information to create a collision mask to match the sprite from which the information was gathered. When creating a fixture from a more complex sprite, you can either use the aforementioned methods to approximate a mask, or you can create a more complex shape using a polygon like we did for the triangle. You'll notice that the code to create the triangle fixture had extra lines. This is because polygons require you to map each point on the shape you're trying to create. You can map three to eight points by telling GameMaker where each one is situated in relation to the center of the image (0, 0). One very important detail is that you cannot create a concave shape; this will result in an error. Every fixture you create must have a convex shape. The only way to create a concave fixture is to actually create multiple fixtures in the same object. If you were to take the code for the triangle, duplicate all of it in the same code block and alter the coordinates for each point in the duplicated code; you can create concave shapes. For example, you can use two rectangles to make an L shape. This can only be done using a polygon fixture, as it is the only fixture that allows you to code the position of individual points.

Once you've coded the shape of your fixture, you can begin to code its attributes. I've described what each physics option does, and you've coded and tested them using the instructions mentioned earlier. Now, take a look at the values for each setting. The ball object has a higher restitution than the rest; did you notice how it bounced? The box object has a very low friction; it slides around on platforms as though it is made of ice. The triangle has very low density and angular damping; it is easily knocked around by the other objects and spins like crazy. You can change how objects react to forces and collisions by changing one or more of these values. I definitely recommend that you play around with these settings to see what you can come up with.

Remember how the ground objects are static? Notice how we still had to code them? Well, that's because they still interact with other objects but in an almost opposite fashion. Since we set the object's density to 0, GameMaker more or less views this as an object that is infinitely dense; it cannot be moved by outside forces or collisions. It can, however, affect other objects. We don't have to set the angular and linear damping values simply because the ground doesn't move. We do, however, have to set the restitution and friction levels because we need to tell GameMaker how other objects should react when they come in contact with the ground. Do you want to make a rubber wall to bounce a player off? Set the restitution to a higher level. Do you want to make that icy patch we talked about? Then, you need to lower the friction. These are some fun settings to play around with, so try it out.

Alternating gravity

Gravity can be a harsh mistress; if you've ever fallen from a height, you will understand what I mean. I often think it would be great if we could somehow lessen gravity's hold on us, but then I wonder what it would be like if we could just reverse it all together! Imagine flipping a switch and then walking on the ceiling! I, for one, think that it would be great. However, since we don't have the technology to do it in real life, I'll have to settle for doing it in video games.

Getting ready

For this recipe, let's simplify things and use the physics environment that we created in the previous recipe.

How to do it

  1. In obj_control, open the code block in the create event.
  2. Add the following code:
    physics_world_gravity(0, -10);

That's it! Test the environment and see what happens when you create your physics objects.

How it works

GameMaker's physics world of gravity is vector-based. This means that you simply need to change the values of x and y in order to change how gravity works in a particular room. If you take a look at the Physics tab in the room editor, you'll see that there are values under x and y. The default value is 0 for x and 10 for y. When we added this code to the control object's create event, we changed the value of y to -10, which means that it will flow in the opposite direction. You can change the direction to 360 degrees by altering both x and y, and you can change the gravity's strength by raising and lowering the values.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

There's more

Alternating the gravity's flow can be a lot of fun in a platformer. Several games have explored this in different ways. Your character can change the gravity by hitting a switch in a game, the player can change it by pressing a button, or you can just give specific areas different gravity settings. Play around with this and see what you can create.

Applying force via magnets

Remember playing with magnets in a science class when you were a kid. It was fun back then, right? Well, it's still fun; powerful magnets make a great gift for your favorite office worker. What about virtual magnets, though? Are they still fun? The answer is yes. Yes, they are.

Getting ready

Once again, we're simply going to modify our existing physics environment in order to add some new functionality. 

How to do it

  1. In obj_control, open the code block in the step event.
  2. Add the following code:
    if keyboard_check(vk_space)
    {
    with (obj_dynamicParent)
       {
       var dir = point_direction(x,y,mouse_x,mouse_y);
       physics_apply_force(x, y, lengthdir_x(30, dir), lengthdir_y(30, dir));
       }
    }

Once you close the code block, you can test your new magnet. Add some objects, hold down the spacebar, and see what happens.

How it works

Applying a force to a physics-enabled object in GameMaker will add a given value to the direction, rotation, and speed of the said object. Force can be used to gradually propel an object in a given direction, or through a little math, as in this case, draw objects nearer. What we're doing here is that while the Spacebar is held down, any objects in the vicinity are drawn to the magnet (in this case, your mouse). In order to accomplish this, we first declare that the following code needs to act on obj_dynamicParent, as opposed to acting on the control object where the code resides. We then set the value of a dir variable to the point_direction of the mouse, as it relates to any child of obj_dynamicParent. From there, we can begin to apply force. With physics_apply_force, the first two values represent the x and y coordinates of the object to which the force is being applied. Since the object(s) in question is/are not static, we simply set the coordinates to whatever value they have at the time. The other two values are used in tandem to calculate the direction in which the object will travel and the force propelling it in Newtons. We get these values, in this instance, by calculating the lengthdir for both x and y. The lengthdir finds the x or y value of a point at a given length (we used 30) at a given angle (we used dir, which represents point_direction, that finds the angle where the mouse's coordinates lie). If you want to increase the length value, then you need to increase the power of the magnet.

Creating a moving platform

We've now seen both static and dynamic physics objects in GameMaker, but what happens when we want the best of both the worlds? Let's take a look at how to create a platform that can move and affect other objects via collisions but is immune to said collisions.

Getting ready

Again, we'll be using our existing physics environment, but this time, we'll need a new object. Create a sprite that is128 px wide by 32 px high and assign it to an object called obj_platform. Also, create another object called obj_kinematicParent but don't give it a sprite. Add collision events to obj_staticParent, obj_dynamicParent, and itself. Make sure that there is a comment in each event.

How to do it

  1. In obj_platform, add a create event.
  2. Drag a code block to the actions box and add the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2);
    physics_fixture_set_density(fixture, 0);
    physics_fixture_set_restitution(fixture, 0.2);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
    phy_speed_x = 5; 
    Add a Step event with a code block containing the following code:
    if (x <64) or (x > room_width-64)
    {
        phy_speed_x = phy_speed_x * -1;
    }
  3. Place an instance of obj_platform in the room, which is slightly higher than the highest instance of obj_ground.

Once this is done, you can go ahead and test it. Try dropping various objects on the platform and see what happens!

How it works

Kinematic objects in GameMaker's physics world are essentially static objects that can move. While the platform has a density of 0, it also has a speed of 5 along the x axis. You'll notice that we didn't just use speed equal to 5, as this would not have the desired effect in a physics world. The code in the step simply causes the platform to remain within a set boundary by multiplying its current horizontal speed by -1. Any static object to which a movement is applied automatically becomes a kinematic object.

Making a rope

Is there anything more useful than a rope? I mean besides your computer, your phone or even this book. Probably, a lot of things, but that doesn't make a rope any less useful. Ropes and chains are also useful in games. Some games, such as Cut the Rope, have based their entire gameplay structure around them. Let's see how we can create ropes and chains in GameMaker.

Getting ready

For this recipe, you can either continue using the physics environment that we've been working with, or you can simply start from scratch. If you've gone through the rest of this chapter, you should be fairly comfortable with setting up physics objects. I completed this recipe with a fresh .gmx file.

Before we begin, go ahead and set up obj_dynamicParent and obj_staticParent with collision events for one another. Next, you'll need to create the obj_ropeHome, obj_rope, obj_block, and obj_ropeControl objects. The sprite for obj_rope can simply be a 4 px wide by 16 px high box, while obj_ropeHome and obj_block can be 32 px squares. Obj_ropeControl needs to use the same sprite as obj_rope, but with the y origin set to 0. Obj_ropeControl should also be invisible. As for parenting, obj_rope should be a child of obj_dynamicParent and obj_ropeHome, and obj_block should be children of obj_staticParent, and obj_ropeControl does not require any parent at all. As always, you'll also need a room in which you need to place your objects.

How to do it

  1. Open obj_ropeHome and add a create event.
  2. Place a code block in the actions box and add the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2); physics_fixture_set_density(fixture, 0);
    physics_fixture_set_restitution(fixture, 0.2);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  3. In obj_rope, add a create event with a code block.
  4. Enter the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2);
    physics_fixture_set_density(fixture, 0.25);
    physics_fixture_set_restitution(fixture, 0.01);
    physics_fixture_set_linear_damping(fixture, 0.5);
    physics_fixture_set_angular_damping(fixture, 1);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  5. Open obj_ropeControl and add a create event.
  6. Drag a code block to the actions box and enter the following code:
    setLength = image_yscale-1;
    ropeLength = 16;
    rope1 = instance_create(x,y,obj_ropeHome2);
    rope2 = instance_create(x,y,obj_rope2);
    physics_joint_revolute_create(rope1, rope2, rope1.x, rope1.y, 0,0,0,0,0,0,0);
    repeat (setLength)
    {
        ropeLength += 16;
        rope1 = rope2;
        rope2 = instance_create(x, y+ropeLength, obj_rope2);
        physics_joint_revolute_create(rope1, rope2, rope1.x, rope1.y, 0,0,0,0,0,0,0);
    }
  7. In obj_block, add a create event.
  8. Place a code block in the actions box and add the following code:
    var fixture = physics_fixture_create();
    physics_fixture_set_circle_shape(fixture, sprite_get_width(spr_ropeHome)/2);
    physics_fixture_set_density(fixture, 0);
    physics_fixture_set_restitution(fixture, 0.01);
    physics_fixture_set_friction(fixture, 0.5);
    physics_fixture_bind(fixture, id);
    physics_fixture_delete(fixture);
  9. Now, add a step event with the following code in a code block:
    phy_position_x = mouse_x;
    phy_position_y = mouse_y;
  10. Place an instance of obj_ropeControl anywhere in the room. This will be the starting point of the rope. You can place multiple instances of the object if you wish.
  11. For every instance of obj_ropeControl you place in the room, use the bounding box to stretch it to however long you wish. This will determine the length of your rope.
  12. Place a single instance of obj_block in the room.

Once you've completed these steps, you can go ahead and test them.

How it works

This recipe may seem somewhat complicated but it's really not. What you're doing here is that we are taking multiple instances of the same physics-enabled object and stringing them together. Since you're using instances of the same object, you only have to code one and the rest will follow.

Once again, our collisions are handled by our parent objects. This way, you don't have to set collisions for each object. Also, setting the physical properties of each object is done exactly as we have done in previous recipes. By setting the density of obj_ropeHome and obj_block to 0, we're ensuring that they are not affected by gravity or collisions, but they can still collide with other objects and affect them. In this case, we set the physics coordinates of obj_block to those of the mouse so that, when testing, you can use them to collide with the rope, moving it.

The most complex code takes place in the create event for obj_ropeControl. Here, we not only define how many sections of a rope or chain will be used, but we also define how they are connected. To begin, the y scale of the control object is measured in order to determine how many instances of obj_rope are required. Based on how long you stretched obj_ropeControl in the room, the rope will be longer (more instances) or shorter (fewer instances). We then set a variable (ropeLength) to the size of the sprite used for obj_rope. This will be used later to tell GameMaker where each instance of obj_rope should be so that we can connect them in a line. Next, we create the object that will hold the obj_ropeHome rope. This is a static object that will not move, no matter how much the rope moves. This is connected to the first instance of obj_rope via a revolute joint. In GameMaker, a revolute joint is used in several ways: it can act as part of a motor, moving pistons; it can act as a joint on a ragdoll body; in this case, it acts as the connection between instances of obj_rope. A revolute joint allows the programmer to code its angle and torque; but for our purposes, this isn't necessary. We declared the objects that are connected via the joint as well as the anchor location, but the other values remain null. Once the rope holder (obj_ropeHome) and initial joint are set up, we can automate the creation of the rest. Using the repeat function, we can tell GameMaker to repeat a block of code a set number of times. In this case, this number is derived from how many instances of obj_rope can fit within the distance between the y origin of obj_ropeControl and the point to which you stretched it. We subtract 1 from this number as GameMaker will calculate too many in order to cover the distance in its entirety. The code that will be repeated does a few things at once. First, it increases the value of the ropeLength variable by 16 for each instance that is calculated. Then, GameMaker changes the value of rope1 (which creates an instance of obj_ropeHome) to that of rope2 (which creates an instance of obj_rope). The rope2 variable is then reestablished to create an instance of obj_rope, but also adds the new value of ropeLength so as to move its coordinates directly below those of the previous instance, thus creating a chain. This process is repeated until the set length of the overall rope is reached.

There's more

Each section of a rope is a physics object and acts in the physics world. By changing the physics settings, when initially creating the rope sections, you can see how they react to collisions. How far and how quickly the rope moves when pushed by another object is very much related to the difference between their densities. If you make the rope denser than the object colliding with it, the rope will move very little. If you reverse these values, you can cause the rope to flail about, wildly. Play around with the settings and see what happens, but when placing a rope or chain in a game, you really must consider what the rope and other objects are made of. It wouldn't seem right for a lead chain to be sent flailing about by a collision with a pillow; now would it?

Summary

This article introduces the physics system and demonstrates how GameMaker handles gravity, friction, and so on. Learn how to implement this system to make more realistic games.

Resources for Article:


Further resources on this subject: