Chapter 12
In This Chapter
Understanding the Physics properties
Working with fixtures
Wrapping Objects around the Room
Adding Physics to Rooms
In this chapter, I introduce you to working with physics in GameMaker: Studio. I draw upon examples from the in-software Physics Tutorial, as shown in Figure 12-1. (Choose the Tutorials tab from the New Project window that appears when you start the software; the Physics Tutorial is found under Advanced.) The tutorial provides information on how to make an Asteroids-style game.
Game physics revolves around the idea of making Objects appear more real to the player. By adding physics to your game, you change how Objects react during collision detection and how Objects move through the Room based on energy and force. Physics provide your game a more natural feel. This chapter doesn’t explain everything you can do with physics and GameMaker, but it does provide a solid foundation of how physics works.
The examples in this chapter show you how to make a player’s ship that can shoot bullets. You also write a Script that will enable both the ship and the bullets to leave one side of the Room only to reenter on the opposite side of the Room. Finally, this chapter includes a section on creating a Room that uses physics.
When creating a physics-based game, you need to create a fixture for the Object, as opposed to a collision mask or mask_index (as described in Chapter 2). You create this fixture so that you can assign physical properties to the Object. Fixtures define the area of an Object that becomes the shape the physics uses for collisions; it also contains the physical information of the Object as well.
The basic process for creating a fixture (a detailed, step-by-step procedure is in this chapter) is to give it a shape, define the physical properties, and then apply that fixture to the Object. You can create a fixture and then apply it to multiple Objects so they all have the same base properties. GameMaker has built-in tools to help you create and apply fixtures in the Object Properties window.
The following procedure has you create a new Object for the player using physics properties. You can use the premade Sprite that is included in the Physics tutorial or make a new one using a convex shape.
Before I get started with the procedure, here’s a quick list of the different physics properties that you use when setting up your Object (see Figure 12-3):
Using collision groups is a processor-heavy task and should only be used when absolutely necessary.
To create an Object using physics, follow these steps:
The Object Properties window appears.
A thumbnail of the image appears in the Sprite section and the name of the image appears in the field (see Figure 12-3).
The Physics Properties appear in the Object Properties window.
If you’re working in the Physics tutorial, Shape should have already been selected. If you aren’t working from the tutorial, GameMaker will try its best to create a fixture around the shape of your Sprite, but you’ll probably have to edit this as explained in the next step.
This opens the Physics Shape (also known as the fixture editor). This is where you create the fixture for the Object (see Figure 12-4).
In the Physics tutorial, the fixture (the yellow lines that denote the outline of the player’s ship, as indicated in Figure 12-4) was already created for you. However, in the Collision section of the Physics Shape, if you select the Circle or Box radio button, you’ll see how the fixture changes. Figure 12-4 shows how the outline changes if you select the Circle shape.
If you change the shape of the fixture and want to get back to the original Shape that was set up for you, click the X in the top-right corner of Physics Shape to close that window. Also choose the X in the Object Properties window and then select No to not save any changes. Then, when you reopen the Physics Shape, the original shape should still be there, as shown in Figure 12-5. If not, you can manually reset the fixture or simply reload a new tutorial.
To reset the fixture manually, keep in mind that you can
When you create a fixture, you want to keep the number of points as low as possible with a minimum of three points (triangle shape) with the overall shape being convex. In the Physics tutorial, the final shape of the fixture should look like Figure 12-5, in that the fixture matches the shape of the Sprite.
The green square (refer to Figure 12-4) signifies the starting point of the fixture. The blue circles represent path points, and the red circle is the currently selected path point.
The Object Properties window is saved and closed.
You’re now done with setting the physics for the Object.
After 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 or right.
To apply force to an Object to make it move, create an Up Keyboard Event as described in the following procedure. Just follow these steps:
The Object Properties window appears.
An Up Keyboard Event appears in the Events section.
An empty code window appears.
physics_apply_local_force(0, 0, 0, -50);
The code window is saved and closed.
This code uses the local coordinate system to set a vector for propulsion. The local coordinates are based on the Sprite, where the X and Y axes meet at the Origin (see Chapter 2). So, no matter which way the Object is rotated in the Room, the origin of the Sprite never changes. In that way, X = 0, Y = –1 would always be just below the Sprite, as shown in Figure 12-6. The first value in this function is for xlocal, the second value is ylocal, the third is xforce_local, and the last value represents yforce_local. By using the value –50 as the value for yforce_local, you’re placing force behind the ship that pushes the ship forward. The amount of force is based on the value, so a larger number places greater force. Experiment with this by changing the value from –50 to –5000 to see what a difference it makes. If you use a positive number — say, 50 — notice that the ship appears to move backward instead of forward.
To enable the player to turn the Object, add Left and Right Keyboard Events.
To have the player turn the Object to the left, follow these steps:
The Object Properties window appears.
A Left Keyboard Event appears in the Events section.
An empty code window appears.
phy_angular_velocity=0;
phy_rotation-=10;
The code window is saved and closed.
During the game, the player can now turn the Object to the left. This code adds or subtracts from the physics rotation, which is what controls the direction and image angle of the Object when the player presses the left-arrow key.
To have the player turn the Object to the right, follow these steps:
The Object Properties window appears.
A Right Keyboard Event appears in the Events section.
An empty code window appears.
phy_angular_velocity=0;
phy_rotation+=10;
The code window is saved and closed. This code adds or subtracts from the physics rotation, which is what controls the direction and image angle of the Object when the player presses the right-arrow key.
Another difference between the physics world in GameMaker: Studio and the traditional functions is that normally the direction and image angle are calculated counterclockwise, so adding 10 to the direction variable would rotate the Object to the left. However, when using physics, directions and angles are calculated clockwise, so adding 10 to the rotational value rotates the Object to the right.
When using physics to create your game, along with using fixtures on your Objects (see “Understanding Fixtures” and “Creating a fixture” earlier in this chapter), you need to set up the Room to accommodate for physics.
There are two properties on the Physics tab of the Room Properties window that you need to learn:
The in-software Physics tutorial has a Room for physics already set up. I’ll walk you through how and what you need to do for your own game. To set up the Room for physics, follow these steps:
The Room Properties window appears.
The Physics properties appear in the Room Properties window.
You just told GameMaker that this Room should use Physics for the Instances of Objects during game play.
You don’t want gravity in your asteroids game, because it takes place in outer space. If you do want gravity in your game, so that Instances naturally fall down, leave the default setting of 10.0.
The Room Properties window is saved and closed.
The default value for Pixels to Meters works well for this game, but make sure that both the X and Y coordinates for the Gravity vectors are set to 0 — that is, if you’re making a space game and you don’t want gravity to interfere with the player’s ship or the asteroids.
In the Physics tutorial, the player’s ship shoots bullets to break up the asteroids. The following tutorial will step you through that process.
The following procedure is based on the Physics tutorial, which has some of the Objects and Sprites in the Resource tree, waiting for you to configure. You’re encouraged to use your own Sprites for your own Objects for your own game.
To set up bullets in a physics environment, follow these steps:
The Object Properties window appears.
The Physics Shape window appears, as shown in Figure 12-9.
Make sure the yellow outline (known as the fixture) aligns with the bullet (see Figure 12-10). You may have to zoom in to see it properly. To zoom in, you can spin your mouse wheel. The fixture should be box shaped, because you chose Box for the Collision Shape in Step 3.
The Physics Shape window is saved and closed.
The value of 5 is a relatively high density. You want to use a high density for the bullet because you want the impact to really affect the Instances of Objects that it collides with during gameplay.
GameMaker calculates physics for Objects based on their size. Therefore, because the bullet Sprite is small in size compared to the asteroids, you need the bullet Object to have a high density for it to have an impact during Collision Events. Think of the impact difference a bullet would have if it were made of aluminum instead of lead.
You set these properties to 0 because you don’t want the Object to bounce, slow down over time, or curve during a collision.
By applying Friction, you’re telling the Object to slow down for each step of the game. How much the Object slows down is determined by the value you enter here and the speed of the Object.
All these properties are explained in this chapter in the “Defining the physics properties” section.
A Create Event appears in the Events section.
The code window appears.
phy_bullet=true;
alarm[0]=60;
This code uses the variable phy_bullet, which tells GameMaker this is a fast-moving bullet Object. As such, GameMaker provides the extra processing power to ensure proper collisions. This code also sets an Alarm that you set up next.
The code window is saved and closed.
The Alarm 0 Event appears in the Events section.
The code window appears.
instance_destroy();
This code destroys the Instance of the bullet Object. This code is triggered when the Alarm is triggered, which you set for 60 steps in Step 12.
The code window is saved and closed.
A Step Event appears in the Events section.
A code window appears.
phy_wrap();
This code calls the Script, which I describe earlier in this chapter. This Script enables the player Object, and now the bullet Object, to leave one side of the Room and reenter on the opposite side. Because you put this Action in a Step Event, GameMaker will check to see if the Instance leaves the Room for every step of the game.
The code window is saved and closed.
The Object Properties window for the bullet is saved and closed.
You’ve now defined a bullet Object using physics. Now you can update the player Object so that the player’s ship can shoot the bullets.
Now that you have the bullet Object defined to work with physics (see the preceding section), you need to set up the ship so that it will shoot the bullet itself.
To set up the ship to fire bullets, follow these steps:
The Object Properties window appears.
A Create Event appears in the Events section.
The code window appears.
canshoot=true;
phy_fixed_rotation=true;
The first line of code creates the variable, canshoot, which will control the rate of fire and set it to true. When canshoot equals true, that means the player can shoot bullets. The second line of code stops collisions from spinning the ship.
The code window is saved and closed.
The Alarm Event appears in the Events section.
The code window appears.
canshoot=true;
Again, you are setting the canshoot variable to true, which enables the player to shoot bullets. You’re doing this in an Alarm Event. In the next step, I show you how to trigger this Alarm to rearm the ship after the player shoots.
The code window is saved and closed.
The Space Keyboard Event appears in the Events section.
The code window appears.
if canshoot
{
with (instance_create(x,y,obj_Bullet))
{
phy_rotation=other.phy_rotation;
physics_apply_local_impulse(0, 0, 0, -100);
}
alarm[0]=15;
canshoot=false;
}
The code window is saved and closed.
This code first checks to see if the variable canshoot is set to true, which it should be at the beginning of the game, because you set canshoot to equal true in the Create Event. Because canshoot does equal true, when the player presses the spacebar, the Space Bar Event is triggered and the preceding block of code is triggered. This block of code creates the bullet at the player position through the with function. The code also sets the directional angle of the bullet to that of the ship by using the keyword other in a function. The code then applies a local impulse to the bullet to shoot it off and, thus, sets the speed. Finally, the code sets the Alarm 0 to 15 (which defines the amount of time before the player can shoot again) and sets canshoot to false (so that the player has to wait for the Alarm to be triggered before he can shoot again).
Now is a good time to save your work and run the game to see how it performs. If you want to experiment with the values you defined, you’ll discover what effect that has on your Objects as you play the game.
In Chapter 2, I discuss one way to make the asteroids by using parents and children so that the traits of the parent Object are passed on to the child Objects.
18.191.168.8