Science&Art Enthusiast

Life is short, no time to think



I got inspired by tutorials on youtube, about building java 2D games, so I started  project “MedBoy”.  I  will not build a game engine, or use any game library.  Building game engine instead of building games makes more sense, but it’s not easy for beginner like me.

This is an ongoing project, and check the project out in MedBoy github repository.

2D Games contains these parts:
Rendering:  Stuffs about displaying things on screen.
Sprite: Images needed in games.
Logic: Logic of how users interact with resources in games.
Sound: Sound system, controls how sounds are played.
Physics: Simulate how objects interact with each other in real world.
Inputs: User control commands, such as mouse clicks and key pressing.
File System: Storing and accessing data of game.

If we have an idea of what 2D games need, then we can come down to building each details. But before that, we need to understand how games work.

How games work:
Basically, games always run in a loop, and in each loop it receives player commands, with that information, the game updates data and render a frame. These codes shows the idea.


MedBoy is a platform game, so the input function needs to get information of arrow keys. If right arrow key is pressed, we need to update the boy’s position. I don’t want to scroll the background directly. Scrolling background is not intuitive, and if I want to add some speed change, then it becomes disaster, the relative movement becomes impossible to understand. Instead, I imagine a camera on top of background. The input from the user changes boy’s relative position to background. And the camera follow the boy with some lerp. The camera is always trying to center the boy, but the speed of the camera is constantly changing.


Level Update


In the previous version, the level was generated by random numbers.  All the tiles are represented by numbers, saving into a 2D array, integer 0 representing background while 1 means tiles.

This time I also save the level into an array, like this.

{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

Saving levels inside codes is not convenient for later change, I will come back to this topic later.

Debug Info Update


I always need to know what is going on with some variables, so by displaying on top, I get these info.

Taking Care of Collision

This part is really not that simple to implement. The guide to implementing 2D platformers explains 4 ways of doing collision detection.

Car Wheel

Engineering Equation Generation System

Engineers analyse vibration systems through writing equations on papers or tuning parameters in software. Before deriving equations of motion engineers draw free body diagrams. My work simplifies this procedure by reducing the need for writing equations, allowing users sketch vibration diagrams directly without writing any equations. The Matlab program convert sketches into vibration systems in state space form. We can design vibration controllers with the state space form system, but currently controller design is not included in the program. To visualize the response of the system, the program plays the animation of system response to initial speed.

Welcome to download Matlab code.

IMU Orientation Visualization


It starts from the quadcopter I am building

I was building a quadcopter using stm32f103.  My goal is to let the quadcopter keep balance while receiving 4 channel controlling commands of throttle, yaw, roll and pitch from 2.4G 6 channel remote controller.  I only need 4 channels, so I leave the other 2 channels behind, and they do nothing.  But the RC receiver does receive all 6 channels of signal from remote controller, so the other 2 channels can be reserved for later use.

What I want is to keep quadcopter balanced

All the controlling commands are reference signals for the quadcopter, so the quadcopter adjust its attitude to the reference signals.  This is to say, if my reference signal of throttle is 50%, supposing 50% of reference throttle corresponding to brushless motor 1000 rpm ,then the throttle of all the motors are 1000 rpm.  Similarly, a reference throttle of 100% gives us 1500 rpm.  Same applies to roll, yaw and pitch.  But we want the quadcopter to keep balance while receiving controlling commands, and what keep balance means in here?

Keeping balance means the quadcopter does not fall.  And what makes it fall.  Too much pitch, too much roll or too little throttle.  So, the quadcopter must get information of roll and pitch.  I do not consider throttle in here since I consider the quadcopter to be balanced as long as it does not flip over.  The throttle determines the altitude, and altitude is not a big concern in early stage quadcopter design.

Ok, but what if the reference roll is large and makes it flip over?  Well, if the quadcopter flips over, then it is different story, and I do not want to consider it now, what I need is to get the quadcopter fly soon.  So, I set maximum roll and pitch within safe value, so I make them small.

Now, our reference signal of pitch and roll is small enough, and they won’t get the quadcopter flips over.  What we need next is to keep the quadcopter balanced.  Imagine our quadcopter receives a reference pitch of 10 degrees, after that, the 4 motors change speed to get 10 degrees of pitch.  But the quadcopter can not reach that 10 degree instantly, it needs some time.  You can think of you stand on the ground, and someone tells you to rotate to 200 degrees, and you start to rotate, then it may take you 0.5 seconds to reach 200 degrees.  And when you reach 200 degrees, you stop.  But you can not stop instantly due to momentum, there has to be some overshoot, say 10 degrees.  If you spin much faster, then you probably spin 100 degrees more, and you have to rotate backward.  However, you may rotate slowly, so you stop right on 200 degrees.  Now you are confused, how fast should you spin when someone tells you to reach 200 degrees?  It depends, if time is not a problem, then you can rotate slowly, however, if that person gives you a time limitation, then you have to do it fast.  So same thing happens to our quadcopter, we command it to have a 10 degrees of pitch, and it has to decide to rotate fast or slow.   Fast rotation makes the quadcopter hard to stop right at the reference pitch angle, while a slow rotation may take few minutes until the quadcopter reach 10 degrees, which probably means that the quadcopter is already crashed into something before it response to your command.  So, we need a method to control how fast the rotation should be.

Algorithm that keep quadcopter balanced

What the method should be?  Actually, there is a well known algorithm call PID controller.  Basically, under thus context, it controls how fast the rotation should be.  But we do not need to talk about it now, since we have not reached that step yet.  But there is still something we need to know,  this PID controller needs 3 parameters, the set angle, the current angle and how fast we are reaching the angle.

Algorithm need information from MPU6050

After understanding all these, we know we need set angles of roll and pitch, current roll and pitch, and how fast we are rolling and pitching.  Set angles of roll and pitch are reference signals from remote controller, so it is not a big problem.  What about current roll and pitch angles?  Well, roll and pitch angles can be calculated from gyrometer.  Gyrometer gives us information of how fast it is spinning, so if we integrate it over time, we get the angles.  But integrating gyrometer readings we get drifited results. So, we need other information to calibrate the current angles.  Luckily, accelerometer can help us with this.  We can calculate pitch and  roll angles from 3-axis accelerometer.  So, MPU6050 is the right IMU for this project since it contains a 3-axis gyrometer and a 3-axis accelerometer.

GUI help me understand this IMU

You probably say, OK , use it on quadcopter.  But I got stucked!  I have this MPU6050 which is good , but  I have no idea of the raw data I get from it.  And I have no idea of its local axis.  So I build this GUI to help me understand the raw data, and its local axis.

About this GUI

This program decodes and visualize data from serial port of format “%d %d %d %d %d %dn”, the IMU I use is MPU6050. MPU6050 give us 6DOF information, 3 acceleration, 3 angular velocity.

The codes were uploaded to here.

Create a free website or blog at

Up ↑