On My Way to OOP – Collision Ball Class Library

After 6 month of non-stop online course in coursera that made me spending my it-should be-happy-but-it-is-not Sunday for doing the homework and the programming assignment, I decided to take a break and decided to have more fun with my free time. So, last month, I didn’t enroll to any courses after my last courses ended somewhere in the early April. Well, actually, I am currently enrolled in one course but that was just for the course resources. I didn’t watch any of the lectures or do any of the exercises/homeworks/assignments. Last month was leisure month.

During those 6 month, I did learn a lot of things, but what I’ve learnt most from those 6 month is OOP or Object-Oriented Programming, mainly with Java. Since I started my programming career (not sure if I can call that career though:D) with MATLAB and only using it for scientific computing or other silly stuff such as the MATLAB Fun Toolbox, I kinda having a hard time in completing the programming assignments which must be written in Java and of course in OOP style. I do have little experience in C# and Python, but at that time, I was doing what so called maybe-not-perfectly-right-but-what-the-hell-as-long-as-the-job’s-done kinda learning for the C# and Python because I have to meet a deadline (See: Why MATLAB stands for Matrix Laboratory). I kinda know a little, but it wasn’t really helpful.

The good thing is, as the assignment started easy and gets harder a little by little, and it also required the student to utilize the OOP to the fullest. While it is a bad news for a first timer java like me, I now have better understanding of OOP (still suck at it, but I suck less now). Now, at least I now what the hell was public, private, static, and final keywords are for. Last time (when I was programming in C#), I just blindly added and/or changed those keywords until the programs worked as I wanted.

During the month of leisure, an idea/project/things-that-I-want-to-make-but-probably-will-be-left-behind-after-some-point-before-it-is-finished. While this idea thingy won’t be described in this post (I will post again somewhere in the future about this idea), I realized that this idea requires me or at least I thought that it will be easier to be realized if I can implement OOP style programming in MATLAB. Having zero experience of doing such things, I decided to make a simple code about elastic collision in OOP style as practice for future endeavor. This code is about putting several moving balls in a confined space and animating them as they move and collide with each other. I made it in such way that it uses every OOP features as much as possible, such as from static/non-static variables, public/private/protected methods. Long story short, here it is, several simulation results of the collision ball code – OOP style.

While the animation in the videos above looks nice, in order to keep the code simple, this code was made with 2 major faults in it. First,  I used brute force method to find collision between the balls. This means that I have to check a ball’s position relative to other balls and repeat that for all other balls. Thus, for n-ball object, I have to do n! (n! = n factorial = n*(n-1)*(n-2)*… and so on) check. If the number of balls is fairly low, the computer will still be able to keep up the  calculation with the pace of the animation. However, if it is not, the animation will get lagged. Second, I assumed that collision can only occur between two balls, while on the real cases, collision between three or more balls may happen. Thus, when three ball collision occur, it will be calculated as two consecutive two balls collisions. When an interval time between calculation is too short, or when a ball’s speed is quite high, this might cause two balls (the last collision to be resolved) to get stuck with each other because those balls has already overlapped with each other for a significant portion. While I’d love to fix that, it is quite hard to debug this thing, so for a quick fix, I just made a speed threshold to reduce the occurrence of ball stuck (it still happen quite a lot though). I might fix this somewhere in the future, I might not. The purpose of this code was OOP practice in MATLAB, so I can live with those flaw.

Actually, I’m kinda indecisive whether I should put this code on my MATLAB Fun Toolbox or not. For one things, it is not an application like other entry in the toolbox. Instead, it is a class library (You know, OOP style), so, that user must actually type something and do a little programming first before he/she can use it. It is also not a game. But then, it is still fun watching balls colliding and bouncing around (Yeah, my definition of fun is kinda weird), so I’m just gonna put this code there. You can download this code from my MATLAB Fun Toolbox page. The code was poorly documented (I’m too lazy for this stuff), so I also put up some example scripts that are used to create the videos above, so that you know how to use the class library easily through these example scripts.

Well, this post and the code marked my first real product in 2013. It is slow since it is already June, but I hope that I still can meet the resolution that I’ve set earlier this year. Hope the code is useful.



This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s