Tuesday, 19 May 2015

Unity Level Editor for a special case - Editor Extension (Source Code)

I needed lately to design levels of a special characteristics where everything is made of tiles and there shouldn't be restrictions on angles between different surfaces -- a group of tiles that lie on the same plane.

So I built a special editor that would help me do such a thing easily, of course using Unity editor scripting, and I'm sharing the code with everyone.

As you can notice from the Video, the editor let you lay out copies of a targeted GameObject on imaginary plane, doing that feels like painting. To create a new plane, you need to toggle on "addNewPlane", I know very awful design, but you can alter that to fit your needs.

I'm sorry I didn't comment the code, it was a one purpose editor, but I built it in away that could be expanded to achieve more than its purpose as always, I just like doing that. All the data needed to drive the Editor is in the "Lib.cs" file, this will let you alter the behavior safely. You can contact me if you have any question.

Source Code - BitBucket Repository

Problems :
1) Uncommented Code.
2) Most of the Behavior implementation of this Editor is in one File (EditorWindow.cs), this make things a little messy, I'm not sure if you can follow my code, but as I said contact me.
3) The UI and the Gizmo are bad and ugly. As you can see from the video the circle with an arrow pointing randomly is so awful, and I must change it.

Good Points :
1) The needed Data structure of the Editor are under the NameSpace "Lib" (Must change this name in Future versions), They're well structured so you can alter the behavior safely.
2) If you followed the code carefully, you will learn about Editor Scripting and Quaternions.
2) You can contact me :).

Source Code - BitBucket Repository

Wednesday, 6 May 2015

"Android & Microcontrollers Bluetooth plugin" future native support

Calling any Native/Java method is computationally expensive. It is highly advisable to keep the number of transitions between managed and native/Java code to a minimum, for the sake of performance and also code clarity" This applies for any plugin available for Unity, or any Managed to Unmanaged code communication. So I designed the plugin to do so without your intervention most of the time!. All reading methods won't actually make any plugin call unless there's data available, and that call is just to notify the plugin that data has been read. But who is responsible for data communication between the Plugin and Unity?, data will be sent automatically when needed according to your configurations of the "BtConnector UI Editor". Defining how many bytes you need in the buffer and when they're sent to your app is very important and critical for performance, using the "BtConnector UI Editor --BtConnector inspector" is easy, just try to alter its options, and you will understand.

This approach works nicely for most of us, minimizing data transition isn't an option for others, because they need real-time data, and they deal with high Baud Rates, even though you can do that with the current plugin, you will sacrifice a drop in Frame Rate.

The solution will be to let you add your own code to the plugin with ease, so you will be able to code your own data logic handling functionality away from unity, and that's what I'm working on now. I'm really bad with deadlines, because this isn't my main job, so I won't say anything about when this will be released. Also notice that I might never release it, if I found out that it just complicate the usability of the plugin, but till now I can't see why it couldn't be an easy and handy functionality for you.

Monday, 4 May 2015

Challenges of parallel/concurrent programming

With this article, I'll talk about some of the issues facing programmers who develop concurrent or parallel programs. These days, multi-core machines are everywhere. Almost every computer shipped with a multi-core CPU, so I think it is an important topic for most programmers. I'll be using the word "parallelism" to refer to parallel/concurrent development.

Most of our known algorithms are sequential, but that is not useful for our parallel machines. One of the basic challenges is that Parallelism needs parallel algorithms and data structures which are fundamentally different from the sequential ones. Simply, There is no successful automated tool to convert a sequential algorithm to a parallel one. Human creativity is the only way to make such conversion.

Fortunately for us, some algorithms have what is called inherent parallelism where the algorithm can be broken into completely independent computations that can run in parallel. For example, a lot of image processing algorithms have this feature because each pixel is independent of the other, so those are easy to parallelize. On the other hand, we still don't have efficient parallel algorithms for a lot of problems, either because these problems are hard to break into tasks without causing an overhead of communication between them, or they have a complex data structure that we can't distribute across the available threads. {Programmers who use these algorithms, perhaps through libraries should expect them to run perfectly on the current devices, that's why it's an important challenge for the industry as well.}

Another challenge is the faulty non-deterministic results of the program. Parallelism is about speeding up on multi-core machines, but we must make sure that we still maintain the same right results every time we run the program; for example, multiplying two matrices should give us the same result every time. Some bugs that programmers are doomed to make has a non-deterministic feature, which means a program might run as the programmer expected but sometimes it will do otherwise. A death accidents caused by radiation therapy machine, Therac-25, was caused by such bugs. One example is race condition which happens when a wrong order of events cause incorrect results (Sometimes that wouldn't happennon-deterministic). Another example is data race which happens when two or more threads access the same memory location at the same time and at least one of them is writing without any synchronization between them (Sometimes that wouldn't happennon-deterministic). They're hard to debug because their occurrence is arbitrary, sometimes they happen, sometimes not, so debugging them is really hard and almost all released products have these hidden flaws.

Programming languages that encourage a purely functional paradigm, like Haskell, offer some help in this area [Before proceeding, I do have to say I am not an expert in any functional language]. Regular imperative languages and OOP (e.g., C++, C#, Java, etc) in which your program might has objects that you can call methods on them that would change their state; On the other hand, purely functional programming give us functions that simply has no state. It simply guarantees that calling the same function on the same variables will always return the same results (deterministic). This means no side effects, so there are no data races between different functions running in parallel. Of course, at some point, you need to create effects and change the state, but functional programming makes that a hard choice for programmers.

Moreover, Functional languages like Haskell makes a clear difference between concurrency and parallelism. Concurrency is not about harnessing the power of parallel machines, but it's part of the program's design to run on multiple threads. For example, a server responding to clients. The server must run on multiple threads to serve multiple clients, it's part of the program's design. Popular imperative languages fail to make such difference, and they use the same programming approach for concurrency and parallelism. Haskell seems to do a good job in this area.

Still, I think OOP and imperative programming paradigm would appeal to a wider variety of people and would do a simpler job in most areas. For example, video game development is a simpler task when a language like C++ is used, due to the extensive real-time state change of the different objects in a video game. There's another programming paradigm called Data Parallelism that distributes or maps data across different computational processes, a famous example is Map-Reduce model which is a good solution when it comes to big data. Apparently, there are different programming approaches that should be used for different problems, and these different approaches are not available in a single programming language.

Another issue is the lack of a clear cost model, which is a way to give the programmer an idea of how much an operation could cost in resources (time and space), or sometimes the cost model is too simple and ignore a lot of important details. Cost models are important because they inform the programmer which parts of their program worth parallelizing and which is not in order to reach an optimal use of the machine. Cost models are hard to form theoretically. Thus, usually, a dynamic program analysis on the targeted machine is needed. In other words, costs are better understood by executing your program on the targeted machine.

Last put not least, problems related to different machine architectures. Abstracting the programming languages from the hardware provides a lot of benefits and ease to programmers, but sometimes programmers should be exposed to the hardware. For example, different CPUs need a different number of threads. having more threads than needed may degrade performance or just increase power consumption. That might be caused by problems like over switching between threads on the same core, saturated off-chip bandwidth, or competing for shared resources where a lot of threads ends up waiting for a single process to finish from a critical section. Solutions to such problems could be achieved by providing a feedback while the program running to control the number of threads or how we map data to the available threads. Something that would be of interest is granularity of your software, which is related to the ratio of computation to the amount of communication (read more here). Granularity is an important topic that programmers porting programs for completely different architectures (MIMD, SIMD ...etc) should worry about. Furthermore, both parallel and serial programmers should care about locality of data by keeping accessed data close to each other in memory in order to optimise cache use.

All in all, A single solution for all these challenges is not available yet, but different solutions are scattered between different technologies. Maybe someday we will have a language that could tackle all of these different challenges. In the meanwhile, let's embrace diversity.

Friday, 30 May 2014

Prototyping Smooth Curves - Quadratic Bézier Curves

Often, you need smooth curves in your game. One of the popular solutions is implementing cubic Bézier curves. Cubic curves let you feel like there are two points in control of the curve.

Animation of a cubic Bézier curve, t in [0,1]
Cubic Bézier Curve
For an artist who avoids coding, cubic curves provide a great feel of what the end result will look like. In case you're building a tool for artists, you better provide them with those curves. However, for prototyping, what I end up using a lot is quadratic curves. They're very intuitive to include in your code. 
Animation of a quadratic Bézier curve, t in [0,1]
Quadratic Bézier Curves
They're intuitive because every angle formed by those points (e.g. P0P1, and P2 in the figure above) represents a bend in the curve. It feels like you're drawing the same curve, but with edges.

Intutive use of Quadratic Curves

Actually, that is why I started this blog post. Just to tell you how intutive they are. Ok! I'll write a little on how to use them, but allow me to do some copying from Wikipedia.

A quadratic Bézier curve is the path traced by the function B(t), given points P0P1, and P2,

If you happened to use Unity, then P0P1, and P2  could be represented using Vector3 or Vector2. In Unity, Vector3 or Vector2 can be multiplied by a scalar ( ). could be a float variable from 0 to 1. = 0 gives the first point P0 (first point in the curve segment). = 1 gives the last point P(last point in the curve segment).
I guess that is all you need to get started. Any point on the curve (the red curve in the figure above) can be calculated given a t (notice = 0.50 in the figure above) and a 3 points ( P0, P1, and P2 in the figure above).

void Curve (float t, Vector2 P0,Vector2 P1,Vector2 P2)
     return (1-t)*( (1-t)*P0 + t*P1 ) + t*( (1-t)*P1 + t*P2 );

Well, this is just one segment. How do you add more segments? You need to build a datastructure that could capture those multiple segments. There are many examples that have good Bézier implementation. Pick any one you like, it doesn't matter what kind of Bézier curves is being used (Quadratic or Cubic). They have a lot of similar ideas. For example, check out this tutorial.

Sunday, 4 May 2014

Next-Gen Engines

New Game Engines have been recently released and more are coming, news and rumors about other new game engines are spreading out. It's the next generation of gaming, so lets check out some of the most exciting reviews and trailers.

Lets start with this amazing video from TheDeserterGamerHD channel. This video focuses on the new incredible Dream Game Next-Gen Engine by VAYA Studio. I suspect it's a real game engine, but imagine if it's real.

"the next generation gaming era is finally coming" #from the video

Snowdrop Next-Gen engine is the engine in which Tom Clancy's The Division is being developed in, check out there trailer, so exciting.

"be prepared to enter the next generation of gaming'" #from the video

FOX Engine and MGS5, here is a part of their tech demo

Please take a deep breath, because this will blow your mind it's a whole planet rendered by the Game Engine I-Novae.

I will not forget mobile games, they are a huge thing lately, so check out Unity Game Engine 5

Now I will leave you with these leaked screenshots of Source 2 Next-Gen Engine from VALVE. Half life 2 created using the old source engine, so I hope they will use this new technology to create Half life 3.