nezvers

PhysicsSystem grid world (draft)

Jan 29th, 2021 (edited)
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. /*
  2. Github: https://github.com/nezvers
  3. Youtube: https://www.youtube.com/channel/UCb4-Y0E6mmwjtawcitIAzKQ
  4. Twitter: https://twitter.com/NeZversStudio
  5. */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10.  
  11.  
  12. // Physics body
  13. typedef struct{
  14.     int i;
  15. }Body;
  16.  
  17. typedef struct{
  18.     int count;
  19.     Body *list;
  20. }Cell;
  21.  
  22. typedef struct{
  23.     int width;
  24.     int height;
  25.     Cell *cell;
  26. }GridMap;
  27.  
  28. void init_GridMap(GridMap *grid){
  29.     grid->cell = (Cell*)calloc(grid->width *grid->height, sizeof(Cell));
  30. }
  31.  
  32.  
  33. Cell* get_cell(GridMap *grid, int x, int y){
  34.     return &grid->cell[x + y*grid->width];
  35. }
  36.  
  37. void add_body_to_cell(Cell *cell, Body *body, int x, int y){
  38.     cell->count++;
  39.     printf("%d \n", cell->count);
  40.     Body *old = cell->list;
  41.     cell->list = (Body*)calloc(cell->count, sizeof(Body));
  42.     memcpy(cell->list, old, sizeof(Body)*(cell->count -1));
  43.     Body *p = cell->list + cell->count;
  44.     p = body;
  45.     free(old);
  46. }
  47.  
  48. void add_body_to_grid(GridMap *grid, Body *body, int x, int y){
  49.     Cell *cell = get_cell(grid, x, y);
  50.     add_body_to_cell(cell, body, x, y);
  51. }
  52.  
  53. void remove_body_from_cell(Cell *cell, Body *body, int x, int y){
  54.     cell->count--;
  55.     Body *old = cell->list;
  56.     cell->list = (Body*)calloc(cell->count, sizeof(Body));
  57.     int j = 0;
  58.     for(int i=0; i < cell->count; i++){
  59.         if(&old[i] != body){
  60.             cell->list[j] = old[i];
  61.             j++;
  62.         }
  63.     }
  64.     free(old);
  65. }
  66.  
  67. void remove_body_from_grid(GridMap *grid, Body *body, int x, int y){
  68.     Cell *cell = get_cell(grid, x, y);
  69.     remove_body_from_cell(cell, body, x, y);
  70. }
  71.  
  72. void destroy_GridMap(GridMap *grid){
  73.     for(int y = 0; y<grid->height; y++){
  74.         for(int x=0; x<grid->width; x++){
  75.             Cell *cell = get_cell(grid, x, y);
  76.             if(cell != NULL){
  77.                 free(cell->list);
  78.             }
  79.         }
  80.     }
  81.     free(grid->cell);
  82. }
  83.  
  84.  
  85.  
  86.  
  87. int main(){
  88.     GridMap grid = {0};
  89.     grid.width = 100;
  90.     grid.height = 100;
  91.     init_GridMap(&grid);
  92.    
  93.     Body body = {0};
  94.     add_body_to_grid(&grid, &body, 25, 40);
  95.     Cell *cell = get_cell(&grid, 25, 40);
  96.     cell->list[0].i = 54;
  97.    
  98.     printf("body now: %d \n", cell->list[0].i);
  99.    
  100.     destroy_GridMap(&grid);
  101.     return 0;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
Add Comment
Please, Sign In to add comment