Friday, 31 July 2015

Physics in GameMaker

Well, things have been pretty quiet around here lately. That's partly because of work, partly because of other stuff, and partly because I've been stuck. I started a new GameMaker project, but it's a physics-based one and there doesn't seem to be a lot of information around about the physics stuff in GameMaker. I'll explain the problem I had and then the solution.

The game is a bit like the old Lander games - the original 2D one. There's a browser-based version here if you want to take a look. In short, you have a little spaceship, and you can turn it (clockwise or anti-clockwise, as it's 2D), plus you have thrust, which makes you head in the direction that your craft is pointing. There's also gravity, which pulls you downwards (as gravity often does).

That sounds perfect for a physics engine, so I thought I'd try the physics in GameMaker (I did try it without physics, but modelling stuff like gravity and thrust is kind've a mathematical nightmare). The problem was that I couldn't get my player object to be controllable. I coded all the controls in - and this is fairly simple stuff, remember, just rotate and thrust - but nothing seemed to work properly. Time and time again, all that happened was my spaceship dropped down, like a gravity-enabled rock. Rotate and thrust just didn't seem to work.

So I turned to the internet, but all the articles and tutorials seemed to be about making boxes fall. I could do that. Hell, by now, I was pretty much an expert at making things fall. What I couldn't do was to make them controllable in any way. So I did the logical thing - I gave up.

Wise, wise words indeed.

After about a month, I figured I'd give it another shot. And I found something I hadn't noticed before - Forces. See, I was trying to use the non-physical way of controlling movement and direction (things like direction, point.direction and so on). These don't seem to work when the object is physics-enabled. Instead, you apply forces to the object. To move the player's spaceship, you use something like physics_apply_local_impulse instead of using speed and direction. To rotate, you'd use something like physics_apply_torque.

It's a very different method of controlling movement compared to the non-physics stuff. You have to control the movement indirectly by applying forces to an object rather than directly stating the speed and direction.

I'll write some more about this at a later date, but I wanted to put the basics out there now in case someone else has the same problem and, like me, can't seem to find the solution. Hope it helps!

Aaaaaand the promised follow-up post is here.

No comments:

Post a Comment