Skip to main content

Useful simple maths collection for games development

Here, I've collected the useful mathematics calculation to be used in everyday game development. Keep me posted if you have other useful daily mathematics calculate useful in game.

Calculating percentage from current and maximum value

float percent =(current / maximum) * 100.0f

Calculating value from the current and percentage

float value = (current * percentage) / 100.0f

Calculating Row And Columns in 2D array

     /// Get the row number of the grid cell in a given index  
    public int GetRowFromIndex(int index, int numRows)  
    {  
      int row =  Mathf.FloorToInt((float)index / (float)numColumns); 
      return row;  
    }  
    /// Get the column number of the grid cell in a given index  
    public int GetColumnFromIndex(int index, int numColumns)  
    {  
      int col = index % numColumns;  
      return col;  
    }  
    /// Get the index from the given row and column  
    public int GetIndexFromRowCol(int curRow, int curCol, int numRows, int numColumns)  
    {  
      int index = (curRow * numColumns) + curCol;  
      return index;  
    }  


Comments

Popular posts from this blog

Optimising Unity new UI System

UI/Sprite textures aren't packed into a texture atlas by default. A tag is needed to group them. Read the documentation / tutorial on the sprite packer. Overlapping text/graphic boundaries with another text/graphic will create additional draw calls, even if the actual visual graphics do not overlap. Grids (other layouts too I presume) need minimum 1 pixel spacing between items, else items are considered overlapping and will create additional draw calls. Images with alpha 0 are still rendered. They are rendered with alpha 0 even though they are not seen. Unity currently does not support non-rectangle shapes as Sprites, so using the TightSpitePacker policy will cause image artifacts. When UI objects are offscreen, they are still batched (rendered as well?). Possible solutions: Change parent to non-UI parent, as UI Camera will no longer detect it Change layer of panel so UI Camera will no longer detect it Disable gameobject when off-screen ScrollRect performance tuning S...

Using XAML on Monogame for WinRT (Part 2)

Integrating Monogame with XAML Metro UI If you're coming from the previous section, you now have a metro UI application project with blank page and also have monogame.windows8 library reference to the project. Now Let's fix some source code for the  pages to get XAML working with monogame. Step01 ( Hacking App.xaml.cs file) App.xaml.cs delivers the most important start point while rendering a particular page in Metro UI application. I added gamePage and mainPage as  a public variables as they become accessible from other classes to for page nagivations. Here is the source code for to see what it looks like for the final App.xaml.cs file. sealed partial class App : Application     {         public ContentManager Content { get; set; }         public GameServiceContainer Services { get; set; }         public GamePage gamePage;         public MainPage mainPage;   ...

Integrating apple id signin with Unity

1 - Download Unity apple id sign in package 2 - Build the sample project file to XCode 3 - Add Authentication services frameworking inside UnityFramework->General->Framework&Libraries 4 - Mark as optional for the new services in UnityFramework->BuildPhases->LinkBinaryWithLibraries 5 - Add capability from Unity-iPhone -> Signing&Capabilities -> Add -> Sign in with Apple Build the app and now completed!