Monthly Archives: March 2012

Unity First Impression

I m using unity from quite a while now. I started with unity much before i started this blog. As my first post in Unity Section of my blog i want to share the first impression i got when i was getting started with Unity3D.

I have played with many Game Engines once in a while like Source Engine , C4, Irrlitch, etc and some couple of Gameloft’s internal or in-house game engines. But i have never felt so comfortable and easy get started with any game engine as Unity3D.

It was so simple and intuitive to use the Editor and programming API was also very good and easy to use. In couple of days I got working knowledge to start any kind of game in it. In just couple of days while getting used to I was able to pull of a playable FPS with couple of weapons and 2D air tennis kind of game.

Just for an example of how easy things are in unity – defining a property that we can edit in Editor , In most on Game Engines it means creating .H & .CPP files writing long code , then edit 1-2 scrip files and then it shows up in the Editor. But in Unity3D its like – int property; , yea that’s it.

I also like the way unity handle resources. Just drag and drop textures, models, sound, etc and it’s done (of course we may have to modify some parameters in some cases but using GUI). It provides such an easy and quick workflow that saves a lot of time.

Am afraid if i keep working with Unity3D only it might makes me lazy. I mean i have spent nights implementing collision detection etc by myself in my engine but in Unity3D its just drag n drop of a physics component.

 

What am working on nowdayz

Nowadays am working on implementing a Collision Detection System in my game engine (Just Another Game Engine).

I have written collision code multiple times while working on different games and each time i made it from scratch. Those systems were designed for specific primitives e.g. rectangle, circle, cube, sphere etc for specific games. So before this system i do had collision in my engine but it was for specific purposes for games it was made. and those special cases aren’t the ideal way to do collision detection in 3d environments.

So for 3D i mainly had collision detection for height-map terrains and some pillar (or cylindrical objects) placed on terrains. And that system assumed everything is aligned to xyz axes and gravity is along -y direction. So i can have a character as AABB or cylinder walking on terrain and colliding with cylindrical objects (with height along y).

For making the character walk on terrain i was just fetching the height (y value) of terrain at particular position (x and z) from height-map using this simple algorithm –

  • Get Player World Position
  •  Use x and z value to get the cell C in which player is
  • Get height value from height-map at four vertices making up that cell
  •  Interpolate the values on x-z plane to get height on that point
  • Scale the value with height-map scale factor
  • Assign the value to Y of player position

If you want a detailed example of how to make and render terrains based on height maps with code check these links : –

This is the most simple and useful method to make a character walk on terrain. In most of testing and maybe in some game/apps it can be a easy and quick way to do collision detection. We can even replace terrain by plane by making the function that returns height-map pixel value to return height of plane.

Also there are some issues related with this algorithm that can’t be avoided as shown in screenshots below : –

(notice that player object is walking up an almost vertical clip. Also its somewhat inserted into the heightmap)

Also using this algorithm ignores the slopes , walls ,etc and player we continue the hills , walks etc like spider man.

For intersection of player with pillars i was using collisions between cylinders aligned along y-axis. Here is the algorithm i was using –

  • Given 2 cylinders A & B . Check y value of both start and end position of A if any of them lies inside B end points.
  • Check same for B
  •  If any of above test return true just check the collision between 2 circles along xz plane

Again, an easy and quick way to do collision with any object that can be imagined as cylinder. But this algorithm assumes cylinders are aligned along y axes. And we often need more precise collision detection than with cylindrical bounding volumes.

Of course these algorithms can be improved to give better results in more generic cases but considering the amount of work involved and couple of more things that i wanted to do (I will discuss it in detail in next post), I thought it would be better to implement the proper collision detection system as it’s done in commercial game engines .

(In my next post in couple of days i will be posting more details on what i wanted to do and the progress of collision detection system. )

Does using too many () in C++ slows down the application?

Recently a friend of mine suggested me that your program may if we using too many parentheses or (). I thought it will be an interesting blog post featuring what’s happening behind the scenes when we use () in C++ and whether it’s a myth or reality ?



Note –Parentheses or () are used for many purposes in C/C++ and other languages but Scope of discussion in this post is only limited to use of parentheses in arithmetic expressions to slice them in sub expressions. Also am going to use () rather than typing whole word ‘parentheses from now on’.
Also am posting this only because I found it interesting to do some research on the topic and felt would be a good and knowledgeable thing to share.

So what was suggested by giving an example is , if we are given two statements –

  • A : (((a+b)+c)+d)
  • B : (a+b)+(c+d)

B will be faster than A and if we run both of them like 1000000+ times we will see than B is significantly faster than A.

Question 1:What do you think?

  1. B is faster than A
  2. B and A both are same

Question 2: Using many () affect the runtime performance?

  1. Yes (means B is faster than A)
  2. No (means either of 2 or 3 in above question)

Also, what’s the reason behind whatever option you think is correct?

I selected Ques1 – option 2 and Ques2 – option 2 before actually performing some real experiment.Now let’s see what really happens behind the scene and find the right answer and reason behind it. Also if I was right or wrong.

What I think is () are just use to recognize different sub expressions in a complex expression at compiling stage. Means () tells the compiler that all the stuff inside () should be treated as a single entity and should be evaluated first before applying any operator in parent expression. Compilers then use this order to generate machine level instructions to actually perform those ordered operations at runtime.  So  there is no effect of () on runtime execution.

e.g. a = b*(c+d); here () tells compiler that it should treat (c+d) as a single entity in parent expression and value of c+ d should be multiplied by b. If we don’t use () and the expression is a = b*c+d; then it will be evaluated as (b*c) + d. (This comes from operator precedence).

So (((a+b)+c)+d) is same as (a+b)+(c+d) so option2 for 1st question and option 2 for 2nd question. () will used only at compile time to recognize sub expressions and assembly code for both statements will be same. So that was my thinking/reasoning behind my answers.

Now to verify this I written code in VC++ 2010 and checked its assembly code that was generated, for both the expressions :-

A : r = (((a+b)+c)+d);
00D313C1 mov eax,dword ptr [a]
00D313C4 add eax,dword ptr [b]
00D313C7 add eax,dword ptr [c]
00D313CA add eax,dword ptr [d]
00D313CD mov dword ptr [r],eax

B : r = (a+b)+(c+d);
00D313D0 mov eax,dword ptr [a]
00D313D3 add eax,dword ptr [b]
00D313D6 mov ecx,dword ptr [c]
00D313D9 add ecx,dword ptr [d]
00D313DC add eax,ecx
00D313DE mov dword ptr [r],eax

C : r = a + b + c + d;
00E213E1 mov eax,dword ptr [a]
00E213E4 add eax,dword ptr [b]
00E213E7 add eax,dword ptr [c]
00E213EA add eax,dword ptr [d]
00E213ED mov dword ptr [r],eax

Let’s analyze what’s really happening in both of them.

  • In case of A: r = (((a+b)+c)+d);      –        mov,add,add,add,mov
  • In case of B: r = (a+b)+(c+d);         –        mov,add, mov, add,add,mov
  • In case of C: r = a + b + c + d           –        mov,add,add,add,mov

So as you can see there’s an extra mov operation in B which was supposed to be faster but it got extra mov instruction which tells us that it may be slower than A by time required to process one extra mov instruction (if do takes noticeable time).

Also assembly code for a + b + c + d  is same as that for A and it confirms that there is no presence of () in runtime code generated so it doesn’t affect the performance. Hence case (((a+b)+c)+d) , (a+b)+(c+d) and a+ b + c + d   should take same time at execution.

That been said sometimes improper use of () can generate some extra instructions like in B where it generates an extra mov instruction but whether it affects the performance in some noticeable way that have been left for reader as an exercise, I would love to hear if anyone finds something interesting there also.

So what I would like to suggest is we should use as many () as possible in complex expressions to clearly identify/separate sub expressions. Yes some people may say we can just remember BODMAS rule but expressions may contain a lot more operators than +,-,/,* , so again one may go and say we can learn this . But again let me ask you how many of you remember it ?

Am not saying we shouldn’t know the precedence and order of evaluation, what am saying is no matter how much experienced you are if you read a complex expression without () you will pause for sure to figure out order of execution of operations inside any particular complex expression. Also in office environments working fast and under pressure or maybe for any reason we may write something thinking it should work ignoring all those things mentioned above in a hurry , whereas in reality things will be different and may introduce bugs in your program.

Like you may write something like –

a = b+c*d%e when you actually meant a = b+(c*(d%e))

So to avoid any such troubles we must use () specially in expressions involving more than one operators to improve readability and avoid unnecessary mistakes and introduce bugs.

First Blood

Well I have been thinking to start blogging from a long time but life kept me busy but finally here’s the FirstBlood. I have been working as a Game Developer from last couple of years and have been learning about “How to make Games” from a very long time. So this will be blog about my game development experiences and i would also share useful info, tutorials etc. Apart from development I am also planning to share my views on the games i play. Ideally i would life to write something everyday but with full time job and other research projects mayn’t be able to but lets see how active i can be on this blog.

I am also working on a game engine of my own called “Just Another Game Engine” (till i make it standout from others in some way)

How am planning to organize the blog –

Basically am planning to categorize things in –

  • Game Development – This will include subcategories like GameDesign,Graphics, Physics, etc.
  • JustAnotherGameEngine – For posts related to my game engine and its progress including some demos
  • Programming – This will contain some general programming things
  • Algorithms – I did fair amount of work in algorithms and planning to do some more as algorithms are FUN.
  • Games – My views on some of the games i played or play.
  • General – General Stuff
  • Same Categories like Unity3D, Source, CryEngine, etc. where i will share some tutorials, demos, useful links and my experience with some the Game Engines available out there.

OLD – I have so many projects/apps i created in past i would share so those things along with appropriate categories will also be categorized and marked as old (things that i have done last month/ year etc, basically things that have been done and aren’t active projects/apps any more)