Saturday, 1 August 2015

A Bit More Detail (on GameMaker Physics)

The Story So Far...


My last post covered a little about how to get a physics-enabled, player-controlled object moving about using GameMaker, but it probably assumes some level of familiarity with GameMaker that not everyone will have. I was a bit excited at just getting things working and wanted to get those thoughts down quick. As promised, here's a bit more detail about the topic.

Now, my own project is a Lander-style game, so this tutorial will be mostly about rotation and thrust, but the concepts should be similar for any 2D physics-based game.



Setting Things Up

The very first thing you should do is to enable physics in the world. To do this, look at your assets in the left pane, go to Rooms, then select the room you're working on (or create a new one). Once you have it, there's a tab on the top left, labelled Physics. Click it, then tick the box marked 'Room is Physics World':
The key to it all
Now GameMaker knows to use physics in this room, so now you'll need to set up a couple of objects to interact with. Now, this page was a great help to me in figuring this stuff out, so what I'm going to post is likely to be found there too. One quick note - my game is a side-on 2D game, so I have gravity enabled (which makes things drop down the screen). If you were doing a top-down game, you'd probably have gravity off.

Firstly, we'll make two objects, one called oStaticParent and one called oDynamicParent. I mean, you can call them whatever you like, but those are the names I'm using here. They don't need sprites or anything, so don't worry about that. What they will need is collision events with themselves and each other:
I'm not drawing boobies, I'm highlighting the important stuff!

If you don't know, the way to add those is to click the Add Event button, then select Collision, then choose the object (oStaticParent and oDynamicParent, in this case). Now there's another event on there, the Outside Room event. I flat-out copied that from that tutorial page, 'cos it's a good idea.

Does this count as pirating?

Adding this is slightly different to the collision events - you click Add Event > Other > then select Outside Room from the list. Once you have that, you need to drag the Destroy Instance object into the actions window (Destroy Instance is in the Main1 tab).

That's pretty much it for the general setting up. Now you get to do the more game-specific stuff.

Funner Stuff

The room is now set up for physics, but needs some objects to physically interact. At a minimum, you'll need probably ground, a player character and maybe some crates or something to move around. You do this in the usual way, create sprites, assign them to objects and blah blah. The important bits here are:

- tick the Uses Physics box
- For immovable objects, set the parent to oStaticParent
- For the stuff that's moving around, like the player object, set the parent to oDynamicParent

::edit::
 Nearly forgot - it's a good idea at this stage to make sure your object has the correct collision shape. To do this, head to the object properties and click the Modify Collision Shape button. You want the collision shape to more or less match up to the sprite, so drag the little 'handles' of the collision shape to match. You can find more detail on this here.

Adding Movement Controls

All that's left to do now is add in movement. This is the bit I was getting massively stuck on before, because it's very different to non-physics control. Basically, there's a bunch of physics-specific commands related to movement. you can find the list of them here (or within GameMaker's help thing), but I'll post them here anyway:
  1. physics_apply_force
  2. physics_apply_impulse
  3. physics_apply_local_force
  4. physics_apply_local_impulse
  5. physics_apply_torque
  6. physics_apply_angular_impulse
So you'll assign these to keypresses to directly control player movement - for example, in my Lander-style game, pressing space activates the thrusters, which uses physics_apply_local_impulse to move the player object. Or A and D rotate the player's craft using physics_apply_torque.

My step code for controlling the craft is as follows:

// Rotation

if keyboard_check (ord('A')) {
    physics_apply_torque(-7);
    //physics_apply_angular_impulse(-0.5);
    }
if keyboard_check (ord('D')) {
    physics_apply_torque(+7);
    //physics_apply_angular_impulse(0.5);
    }


// Thrust

if keyboard_check (vk_space) {
    sprite_index = sPlayerBaseThrusters; //Show flames
    physics_apply_local_force(-12, -2, +80, 0);
   

}

if keyboard_check_released (vk_space) {
    sprite_index = sPlayerBase;
}

The commented out parts are just me experimenting with similar-but-different forces.

Anyway, I think I've covered basic movement control about as much as I'm able to at the moment, so I hope it's of some use to someone out there. I'll leave you all with the latest screenshot:

Working GameMaker physics - 'tis a thing of beauty indeed.

No comments:

Post a Comment