Game maker tutorial : How to make arms with joints Prerequisites

Transcription

Game maker tutorial : How to make arms with joints Prerequisites
Game maker tutorial : How to make arms with joints
Before you read further
Targeted audience
Beginners to intermediate game maker developers
Prerequisites
Understanding of base GML language. Loop ,conditional and with constructs.
Covered materials
Mathematical notions : Sine and Cosine.
Messaging between objects through instance variables.
How to set up a complex system.
Defining goal
Goal definition
We want to simulate a robot arm like the one you can see in pharmaceutics or car factories.
Analysis of such arm
As we can see in the following picture, the arm is
composed of multiple parts. The depicted one has 3
segments and 3 joints.
The next thing we will have to do is defining a data
structure to describe the arm. In Game Maker, we cant
use structured objects, unless we use empty
GameMaker Objects (GMO).
Drawing 1: An arm
state and mirror array to living GMO.
Instead we will use plain old arrays to hold all the arm
Author : [email protected]
Data Structure definition
We will hold the data in 3 arrays, one will contains the length of each segment, the other the relative
angle of the joint to the horizontal and the last one the effective angle of each segment to the horizontal.
The gray lines represent the next segment at angle “0” in relative position.
The horizontal black lines are … the horizontals.
The black sectors denotes the relatives angles.
The gray sectors denotes the absolute angles.
You can see that the gray sectors are simply the sum of the previous black and
gray sectors.
That means that you can calculate each segment angle with this formula :
S angle (i)=S angle (i−1)+S join (i)
Illustration 1: Arm
and angles
Updating data structure
We will need to compute the position of each segment and they angle to the normal.
For the last, we will use the above formula.
For the position, we will simply say that the beginning of one segment is positioned at the end of the
previous segment.
GameMaker don't offer this information, so we will calculate it.
We have the following information about the previous segment:
•
Current angle
•
Current coordinates
•
Current length
Here is how we use them :
x=x '+cos(angle)×lenght
y= y ' −sin(angle)×lenght
It's that easy ! We did do “-sin()” to go counter-clockwise like GM does.
Author : [email protected]
Game Maker implementation
In this implementation we want to be able to add segment and controls without having to add new code
(Always think like that, it's a huge time saver for the future since it ease re-usability for you and others)
Arm controller will be an object that will keep the arm in a consistent state and won't be interactive.
Controls permit to move the arm.
Note that you do not need to make one object per segment, it's only needed when you want different
length.
Author : [email protected]
Setting-up the arm object
This is enough ! The presence of the variable “arm_serial” will permit the arm_controller to properly
pick the object while initializing !
Author : [email protected]
Setting-up up/down controllers
In the create event :
target_index will be properly set by the arm_controller initialization!
In the Left Button event :
Author : [email protected]
Setting-up the arm_controller object
In the step event :
Line 4 to 7 is the application of the first formula to set-up angles.
Line 11 to 16 is the application of the second formula to set-up segments positions.
Author : [email protected]
In the create event :
We initialize the 3 arrays and the up/down controllers.
We check instance variable existence to pick up the right objects and we already left room to have
multiple arms, arm_controllers and so on ;)
Exercise 1: Set-up the whole stuff to have two independent arms.(Only some objects to duplicate then
change only some literal values)
Exercise 2a: Use inheritance to avoid duplicating whole piece of code.
Exercise 2b: Use the ability to have per instance “create event” in Game Maker to avoid using
inheritance and code duplication !
Author : [email protected]