Custom Movement Done Easily and Correctly! (Part 1)
Author: | Silveraura
|
Submitted: | 2nd April, 2006
|
Views: | 7792
| Rated: |
|
|
Introduction
So it’s about time you decided you wanted to learn custom movement? Tired of people smashing down your game because you are using the default built in movement that comes with TGF/MMF?
Here in Part 1 of my Custom Movement Article, we are going to be going over how to completely re-make the 8 directional movement that’s built into MMF (or TGF). In addition, I’m going to show you how to customize every aspect of the custom movement, and how to make things editable, event while the game is running, to create things like rough terrain or ice!
Well first things first, let me explain that learning custom movement is going to by far, one of the most thrilling things for a new or even experience MMF user. The secret to custom movement is this… MMF plays a very small part in it doing so, custom movement is basically movement that can be done in any other program or engine that allows you to move an objects position, relative to its own position.
Some key points before you start:
Custom Movement is nothing but movement that you control completely through very simple math, adding, subtracting… etc.
Custom Movement is movement that relies very little on MMF’s easy game design structure, and what I mean by this is. Once you learn custom movement fluently, you will be able to take what you learn into coding, and getting past a few translation hiccups, you’ll find that everything will be very fluent. In other words, Custom Movement is how games are designed in real coding!
Custom Movement can make an object do just about anything you want, from re-making all of MMF’s built in movement, to making your own!
Almost all issues that occur with custom movement, such as objects getting stuck or not doing something correctly, its almost always your fault. This means that if your engine is at fault, you have the ability to fix it, unlike in MMF’s default movement, if something goes wrong, you cant fix it and rarely can you find a logical method to get around it!
Early Confusion Killer
The first main thing you need to know about TGF/MMF, which will often scare some people, because it will sound like they are getting into something that they never wanted to get into. The first thing you need to remember is, it’s a lot easier then you think.
Now the main thing you need to know is, think of TGF/MMF as a huge grid, and each pixel is 1. The X coordinate is all the pixels going left and right, and the Y coordinate is all the pixels going up and down. I find this easy to remember, because if you look at the Y, it looks like a V with a tail going up and down. So Y would stand for up and down. In addition, going into 3D, Z would go Back and Forth, showing depth, but we wont get into that. Anyway, if you look at the field as a large grid, the top left and corner is ZERO.
With this said, looking at the top left hand corner of the visible area in MMF, knowing that this is Zero, anything to the right of this will be Positive X, anything to the left of this will be Negative X, anything to the Bottom of this will be Positive Y, and everything to the Top of this will be Negative Y.
Now knowing this, you should quickly adapt to the fact that the farther you get away from this point, the larger the number gets. So 100 pixels to the Right of 0 will be (100,0) meaning that you are 100 pixels on the X axis. If you were 50 pixels to the Left of 0, it will be (-50,0) meaning that you were -50 pixels on the X axis. Same goes with the Y axis. This is very important to know as you go on through custom movement, but if you don’t understand it exactly right now, don’t worry. It will start to come clear as you go through the tutorial, trust me.
Now to get started.
First things first, what we need to do is go over the bare basics of how custom movement ties in with math and taking in player input.
After creating an object, create the event: Repeat while (Player 1) Moved Right.
Now what we need to do is go under the object, and go to: Position > Set X Coordinate.
Now in this new box here, what we need to put in here is: X( "Active" ) + 2
Confused already? Well maybe, maybe not… but I’ll explain anyway. What we are doing here is the fallowing:
Once the player holds down the Right button, the Active object will then set its position, to its own position, plus two. So the Active object will now move 2 pixels to the Right of its current position.
Why to the right though? Well as I said earlier in the article, the entire frame is like a huge grid, the farther you go from 0, the higher the numbers get. So lets say the position of the Active object is (5,5) in the format (X,Y). This means that the object is sitting 5 pixels from 0 in both the X and Y Axis. Now if we added 2 to the X axis, this means that 5 will turn to 7, and the Active object will be sitting on (7,5), which is 2 pixels to the right of its original position of (5,5). Now lets say we go back to (5,5) and rather then adding 2, we remove 2. This will set the position from (5,5) to (3,5) which will move the Active object 2 pixels to the Left of its original position of (5,5)! This same principle works with the Y axis. Hopefully this make sense for anyone.
Now this is all fine and dandy, but just because this might not be 100% clear, I’ll show you how Left, Up, and Down would look.
Repeat while (Player 1) Moved Left
-(Active) Set X position to X( "Active" ) – 2
Repeat while (Player 1) Moved Down
(Active) Set Y position to Y( "Active" ) + 2
Repeat while (Player 1) Moved Up
(Active) Set Y position to Y( "Active" ) – 2
Alterable Variable Crash Course!
Now before I jump straight into this, let me go into a crash course of Alterable Variables.
Alterable Variables are like little counters that are mounted into Active objects. Each active object, even copies of the same active object, all have different variables.
So say you have two Enemies, they are the same Active object, they are just copies of each other. Now lets say both of these Enemies have the Alterable Variable of five. So now we have two enemies with the alterable value of five. Now lets say we make an event that says that if we click on one of the two enemies, we will subtract two points from the alterable value. The enemy we click on will have an alterable variable of three, but the enemy you DIDN’T click on, will still have five! Go figure! It’s the ultimate way to do health! Wow… amazing, isn’t it? Anyway… alterable values come with a letter, all this means is that each active object can have up to 21 alterable variables, A through Z.
Great uses of these alterable variables is to have things like an objects Acceleration, Health, Gravity, etc which I will start to explain later. The point to learning Alterable Variables is because it is a much more effective method of giving active objects their own built in counter, rather then having hundreds of hidden counters spread around your frame. Now as you start working with alterable variables, you will start to notice that you need some more flexibility with your variables, like giving them names or a default starting. These options can be found by right clicking on the object, going to Properties, and Alterable Variables. In here, you can give each variable a name and an initial starting number.
Of course, Alterable Variables don’t have the ability to have a Max and Minimum, but no fear. You can build you own, and here is how.
First things first, when you go into New condition, right click on the object you want to get the variable from, go into Alterable Variables, then go into Compare to one of the alterable variables. In this new window, select the variable you want, then change it to Greater, then change the number in here, to the Max number you want the alterable variable to reach. Now click ok, and in this new event, go under the object with the variable, and change the variable to the Max. What this will do is make it so that any time that variable goes higher then the Max, it will set the variable to the Max, forcing the variable to stay at or below it at all times. This same thing goes for lower then. In the end, you will have 2 events that look like this:
Alterable Variable A of (Active) > 5
(Active) Set Alterable Variable A to 5
Alterable Variable A of (Active) < 0
(Active) Set Alterable Variable A to 0
The above will force the Alterable Variable A to stay within a range of zero through five.
Getting Our Hands Dirty!
Alright, Article finished… What? Wasn’t this good enough? Oh wait, you want to do things like Acceleration now?
Alright… now thanks to the above crash course to Alterable Variables, I can safely assume that you know as much as you’ll need to know, to do what I am about to show you.
First things first, explaining how this works. As many of you know, Acceleration can mean both speeding up and slowing down, though to most, slowing down is Deceleration. What we will be doing is making it so that the position of the Active object, will move relative to its current position, based on the Alterable Variables we set up, rather then just adding numbers to it, you’ll understand later, don’t worry.
Anyway, everything we did above… delete it. What we are going to do, is make two alterable variables. X Acceleration, and Y Acceleration, so before we go any farther, do the fallowing:
Right click on Active, go to Properties, then Alterable Value.
Remove: “Alterable Variable A” and type in “X Acceleration”
Now change the Current Value to Value number 1.
Remove: “Alterable Variable B” and type in “Y Acceleration”
What we are doing is preparing ourselves for a very easy to work with, setup for our Acceleration. See, what we need to do is have two alterable variables, one to control the X Acceleration and one to control the Y Acceleration, both of which will be controlled by player input.
Now what we need to do is go into the Event Editor and create the event:
Repeat while (Player 1) Moved Right
+ X Acceleration of (Active) < 5
Now under this event, we are going to add one to the X Acceleration of Active.
So what you should have so far is:
Repeat while (Player 1) Moved Right
+ X Acceleration of (Active) < 5
(Active) : Add 1 to X Acceleration
Now lets copy this event and change the input from Right to Left, and make the X Acceleration of (Active) Greater then Negative Five. Then change the result from Adding 1 to X Acceleration, to Subtracting 1. You should have this:
Repeat while (Player 1) Moved Left
+ X Acceleration of (Active) > -5
(Active) : Subtract 1 to X Acceleration
Now so far what we have here is the player controlling the Alterable Variable of our Active object. If the player holds in right, the variable X Acceleration is going to progressively add from zero to five and stop because the event is only live if its below five. The same will happen when they press Left, it will progressively subtract from five or zero, until it hits negative five, which is where we told it to stop. If you would like to visually see how this is all playing together, you can create a counter and set the counter value to X Acceleration of Active.
Always
(Counter) : Set Counter to X Acceleration (Active)
This will allow you to actively see what’s going on behind the scenes.
Now first things first, before we move our object, we should make it so that the alterable variable automatically accelerates back to zero if the player releases either the left or right key. To do this, just make the fallowing events: (The [X] in front of events, means that they are the opposite, or Negate. You can do this by right clicking on them and selecting Negate.)
[X] Repeat While (Player 1) Moved Right
+[X] Repeat While (Player 1) Moved Left
+X Acceleration of (Active) > 0
(Active) : Subtract 1 to X Acceleration
[X] Repeat While (Player 1) Moved Right
+[X] Repeat While (Player 1) Moved Left
+X Acceleration of (Active) < 0
(Active) : Add 1 to X Acceleration
Now if this was done correctly, what these two events will do, is make it so that if the player is not pressing left or right, and the X Acceleration is above zero, it will subtract one, or if the X Acceleration is below zero, it will add one. If you have a counter showing what’s going on, now would be a good time to run it. If done correctly, when you release the key, the counter should move back to zero, showing that the variable is resetting itself.
With this all set up, its time to make things interesting!
Create an Always event. (If you made a counter with an Always event, just use this one.)
Now create the condition Set X Coordinate of Active to: X( "Active" ) + X Acceleration( "Active" )
You should have the fallowing event:
Always
(Active) : Set X Position to X(“Active”) + X Acceleration(“Active”)
What this event is saying is, MMF will always set the X Position of the Active object, to the position of the active object, plus the X Acceleration. So if the X Acceleration is 5, the object is going to set its position to the position of itself, plus 5. If the X Acceleration is -3, the object is going to set its position to the position of itself, plus -3, or minus 3. Now run this sucker and check it out!
At this point, you will notice that the Active object moves fluently left and right, and only moves so fast. This is because what we did is we made it so that as you hold in the left and right key, the X Acceleration, which is always being added to the objects relative position, will add and subtract from zero to five (or negative five) meaning that it will require to flow through two, three, and four on its way. This gives the object acceleration, rather then instantly jumping from zero to five. Now the reason the object only moves so fast, is because when we made this. We made it so that it would only add or subtract, if it stayed within the range of negative 5 to 5, giving us a range of ten. I will show you how to customize this later in the article. Right now, we need to add in Up and Down! To do this, first we want to copy the first two events we have, the events that read input from the player pressing Left and Right. Copy these two events and make the fallowing changes to them:
Change Right to Down
Change Left to Up
Change All X Acceleration to Y Acceleration
(Acceleration > -5 or < 5 , and Add or Subtract from Acceleration)
Now we need to copy the other two events that check to see if the player is holding Left or Right. Once you copy them, make the fallowing changes:
Change Right to Down
Change Left to Up
Change All X Acceleration to Y Acceleration
(Acceleration > or < 0, and Add or Subtract from Acceleration)
Now what we need to do, is make it so that the Y Acceleration, effects the Active object. So we need to go back into our Always event and add the fallowing Condition under our Active object.
(Active) : Set Y Position to Y(“Active”) + Y Acceleration(“Active”)
So our Always event should look like this so far:
Always
(Active) : Set X Position to X(“Active”) + X Acceleration(“Active”)
(Active) : Set Y Position to Y(“Active”) + Y Acceleration(“Active”)
Now run it and see how it goes.
At this rate, you should notice that you Active object moves around in all 8 directions, just as planned! Congratulations, you just completed your first custom movement engine that is just like MMF’s built in engine, only much smoother.
Tweak to Perfection: The Ultimate CUSTOM Movement!
Doesn’t the name custom ring any bells yet? Well by now it should, but just incase you haven’t gotten the message yet, the entire point to custom movement is the ability to control every single aspect of the movement, giving you the ultimate custom control. This not only allows you to fix any problems that arise, but this also allows you to customize every little tid-bit of your engine to perfection.
Now tweaking our engine is going to be as fallow. If you recall, we set a limit for our objects speed to be 5, meaning that the fastest our object can move is 5 pixels every loop. What if we wanted to speed or slow this down though? Well if we wanted our engine to be faster, we could change that value to 8. This will be very tedious though, having to change every single 5 into an 8, and every single -5 into a -8! So I’m going to give a crash course in what I like to call ‘Constants’. Constants are nothing built into MMF, so TGF and MMF users alike, will have no problem using them. All constants are in my opinion, are counters (or variables) that allow you to give out a general variable that you would like to change, so all you need to do is change it in 1 location, and it will effect several locations all at once. Here is what I mean. Rather then going back in setting the speed every single time we want to tweak it up some, we could create a counter. Then what we can do is go in, and change every single one of those 5’s to the variable of the counter, and every single negative 5, to the variable of the counter, times negative one. That way we could make an event for the start of the frame, to set the counter to 8, and that would automatically set the Acceleration to 8! Lets do that now!
First, create a counter (or remove the condition for the current counter, if you have one already). Now what we need to do is go into the event editor and make the fallowing changes:
Change the’ < 5’ to ‘< value( "Counter" )’
Change the ‘> -5’ to ‘> value( "Counter" ) * -1’
What this is going to do, is make it so that rather then the Max or Minimum being 5, its going to be the value of the counter. So if the counter equal 6, the ‘< value( "Counter" )’ is actually going to be saying ‘< 6’. Now as for the negative five, rather then having two counters, one for positive and one for negative, we are just going to use our current counter, and multiply it by negative one, which will turn our positive number into a negative. Meaning that ‘> value( "Counter" ) * -1’ will actually mean ‘> -6’ if the counter equals 6!
Now to make all this work, make a new event, at the Start of Frame, change the counter value to 8, and run. (Make sure if you are recycling your original counter, that you remove any conditions that may change the counter to something different!)
After doing this, you should quickly notice that our Active object is moving much faster! Now one of the best parts about this method is the fact that the speed can now be changed at runtime! So lets make two small events to demonstrate this. Repeat while (Player 1) Pressed fire 1, and the same event, only negate. Now remove the Start of Frame event, and under Repeat while (Player 1) Pressed fire 1, change the counter to value 8. Then under the [X] Repeat while (Player) Pressed fire 1, change the counter value to 5. Now run and notice how you move slower. Now hold down button 1 (Shift by Default). You should notice the Active object is moving much quicker while you are holding button 1! Let go of button 1 and start moving again, you’ll suddenly notice you don’t move as fast.
Now acceleration makes things more interesting. There are two main methods that I prefer, both are great, one is a little more complex, but a lot more customizable! Lets go over the easy method first.
The bases of this one is just adding inserting the event: Every 00”-0x The more milliseconds there is in between the loop, the slower it will loop, and the longer it will take to accelerate! So lets add in the event: Every 00”-05 into all eight of our acceleration events. This will mean that it takes 0.05 seconds to speed up 1 more acceleration point. When you run it, you should notice that it takes a little longer to speed up or slow down.
Now how about my more complex method?
Well first things first, undo everything you did before (if you fallowed the last example)
Now lets add in a new counter, this counter will be a main loop if you will. In side the event editor, under the Always event, under Counter 2, make it always add 1. Now lets make our counter loop, create a new event that says if our counter is greater then 5, set the counter back to 0. Now in all eight of our acceleration events, insert the event: (Counter 2) = 0
What is going on here is, the counter is rapidly looping from 0 to 5, back to 0 again. This means that it has to rapidly pass through 0 over and over again. The larger the loop, the longer it takes for it to revert back to 0. Every time the counter reverts back to 0, the acceleration events will be triggered. So if the loop is 5, then it will take 5 loops to make 1 acceleration trigger. If the loop is 2, it will take 2 loops, thus acceleration will be much quicker. If the loop is 0, then it should accelerate no differently then it would without any loops at all.
This is great for two reasons, 1 reason is because its sort of like a constant in itself, because now you only have to change 1 number, rather then 8 different times for each of the acceleration events. This is also great because this allows you to change the time at run-time (while the game is playing). Lets try this out!
In the event: (Counter 2) > 5 Change the 5 to 2, and Insert Repeat while (Player 1) Pressed fire 2. Now copy the event and then change the 2 to 5 and negate the Insert Repeat while (Player 1) Pressed fire 2. You should have something that looks like this:
(Counter 2) > 2
+ Repeat while (Player 1) Pressed fire 2
(Counter 2) : Set Counter to 0
(Counter 2) > 5
+ [X]Repeat while (Player 1) Pressed fire 2
(Counter 2) : Set Counter to 0
What this is doing is making it so that when you are not holding in button 2 (Ctrl by default) then the loop will be 5, but if you are holding it in, then the loop will be 2, thus faster. Give it a run, as you move around, you should notice that your acceleration is rather slow and slippery. Now hold down button 2, and move around. You should notice now that your acceleration is a lot quicker and a lot easier to make quick bends and turns! Releasing Ctrl will return the loop back to 5, and make things slow and slippery again.
At this rate, not only have you learned how to tweak your custom movement engine to perfection, but you have learned how to change your movement through “runtime” giving you the ability to add things such as speed power ups, environmental changes like slowing down while moving over harsh terrain, or slippery while moving around on ice!
Overall, my goal in this article was to hopefully get you started in making your own customizable custom movement. (Heh... ) And help boost new and intermediate MMF (or TGF) users alike, up to a new level that releases them from the deadly grip of default movement.
What to expect in Part 2!
In Part 1, we learned how to actually put the movement together, and how to customize it the way you want, but we didn’t really go over how to actually do things like collisions and as stupid as this may be, collisions are a little different in custom movement. They require things such as triggers and then you have to get into fast loops, and all kinds of messy stuff.
Part 1 was to get you started on making working custom movement, and how to tweak it up, we’ll go deeper into things like collisions and many more methods to make custom movement work, other then just 8 directional movement, like particles (Example: Glass Shattering, Earthquake, Explosions, etc.)
I hope you enjoyed reading my article, and I hope I helped some people out their in making the field of custom movement, a lot less scary to walk into. It’s not hard, it’s just different!
If you have any questions, just PM me or E-Mail/IM(MSN) me at CassataGames(at)Comcast.net [Replace (at) with @]
You can find the entire custom movement engine from the above article, fully commented, right here:
http://www.sitesled.com/members/arcanetale/CustomMovementTutorial.cca
No credit required, just playing my part to share knowledge with people who need it!
|
|
|