OBJCOL.CPP
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "fixed.h"
  4. #include "blockmap.h"
  5. #include "objcol.h"
  6. #include "abs.h"
  7. #include "sign.h"
  8. #include "rayspr.h"
  9. MYFIXED Dis_Percent(MYFIXED obj_x, MYFIXED obj_y, MYFIXED ms_x, MYFIXED ms_y,
  10.    MYFIXED delta_x, MYFIXED delta_y, MYFIXED move_len_sq) {
  11. MYFIXED pos_on_line;
  12. pos_on_line=fixeddiv(
  13.    fixedmult(obj_x-ms_x, delta_x)+fixedmult(obj_y-ms_y, delta_y),
  14.    move_len_sq);
  15. pos_on_line=fixedsqrtHP(ABS(pos_on_line))*SIGN(pos_on_line);  
  16. return pos_on_line;
  17. }
  18. MYFIXED Obj_Dis(MYFIXED obj_x, MYFIXED obj_y, MYFIXED mf_x, MYFIXED mf_y,
  19.    MYFIXED delta_x, MYFIXED delta_y, MYFIXED move_len) {
  20. MYFIXED dis_result;
  21. dis_result=fixeddiv(
  22.    fixedmult(obj_x-mf_x, -delta_y)-fixedmult(obj_y-mf_y, -delta_x),
  23.    move_len);
  24. return dis_result;
  25. }
  26. void Stop_Obj(pobject move_obj, pobj_collision the_collision) {
  27. the_collision->delta_x=fixedmult(the_collision->dis_percent, 
  28.    the_collision->delta_x);
  29. the_collision->delta_y=fixedmult(the_collision->dis_percent, 
  30.    the_collision->delta_y);
  31. }
  32. void Slide_Obj(pobject move_obj, pobj_collision the_collision) {
  33. MYFIXED intersect_x, intersect_y;
  34. intersect_x=fixedmult(the_collision->dis_percent, the_collision->delta_x);
  35. intersect_y=fixedmult(the_collision->dis_percent, the_collision->delta_y);
  36. MYFIXED delta_y=the_collision->delta_y;
  37. the_collision->delta_y=intersect_y+SIGN(the_collision->dist_from_move)*fixedmult(
  38.    REBOUND_PERCENT, -the_collision->delta_x);
  39. the_collision->delta_x=intersect_x+SIGN(the_collision->dist_from_move)*fixedmult(
  40.    REBOUND_PERCENT, delta_y);
  41. }
  42. BOOL Intersect_Z(pobject obj_1, pobject obj_2) {
  43. if (obj_1->z >= obj_2->z + obj_2->type->height)
  44.    return FALSE;
  45. if (obj_2->z >= obj_1->z + obj_1->type->height)
  46.    return FALSE;
  47. return TRUE;
  48. }
  49. void Find_Closest_Obj_In_List(pobj_collision the_collision,
  50.  pobject_node search_list) {   
  51. if (search_list==NULL)
  52.    return;
  53. pobject_node cur_node;
  54. pobject cur_obj;
  55. MYFIXED dis_percent, dis_from_move, obj_block_percent;
  56. cur_node=search_list;
  57. while (!OL_Empty_Node(cur_node)) {
  58.     cur_obj=cur_node->data;
  59.     // do they even intersect height wise?
  60.     if (Intersect_Z(cur_obj, the_collision->move_obj)) {
  61.     
  62.     // find at what point on move the objects intersect
  63.     dis_percent=Dis_Percent(cur_obj->x, cur_obj->y,
  64.        the_collision->move_obj->x,
  65.        the_collision->move_obj->y,
  66.        the_collision->delta_x,
  67.        the_collision->delta_y, 
  68.        the_collision->move_len_sq);
  69.     obj_block_percent=fixeddiv(cur_obj->type->block_width,
  70.        the_collision->move_len);
  71.     dis_percent-=obj_block_percent;
  72.     
  73.     if (dis_percent>(-obj_block_percent) && dis_percent < ONE) {
  74.     
  75.     dis_from_move = Obj_Dis(cur_obj->x, cur_obj->y, 
  76.        the_collision->dest_x,
  77.        the_collision->dest_y,
  78.        the_collision->delta_x,
  79.        the_collision->delta_y, 
  80.        the_collision->move_len);
  81.        if (ABS(dis_from_move)<
  82.           (the_collision->move_obj->type->obj_width+cur_obj->type->obj_width)) {
  83.           if ( dis_percent < the_collision->dis_percent )
  84.           {
  85.           the_collision->found_collision=TRUE;
  86.           the_collision->col_obj=cur_obj;
  87.           the_collision->dist_from_move=dis_from_move;
  88.           the_collision->dis_percent=dis_percent;
  89.           }
  90.        }
  91.     } 
  92.     }
  93.     cur_node=OL_Next_Node(cur_node);
  94.   }
  95. }
  96. void Check_Obj_Collision(pobj_collision the_collision) {
  97. the_collision->dest_x=the_collision->move_obj->x+the_collision->delta_x;
  98. the_collision->dest_y=the_collision->move_obj->y+the_collision->delta_y;
  99. the_collision->move_len_sq=
  100.   fixedsqr(the_collision->delta_x)+fixedsqr(the_collision->delta_y);
  101. the_collision->move_len=
  102.   fixedsqrtHP(the_collision->move_len_sq);
  103. the_collision->found_collision=FALSE;
  104. the_collision->dis_percent=MAXMYFIXED;
  105. if (the_collision->move_len_sq==0)
  106.    return;
  107. USHORT block_x, block_y;
  108. MYFIXED dest_x, dest_y;
  109. dest_x=the_collision->dest_x;
  110. dest_y=the_collision->dest_y;
  111. block_x=Block_X(dest_x);
  112. block_y=Block_Y(dest_y);
  113. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x, block_y));
  114. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x, block_y+1));
  115. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x, block_y-1));
  116. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x+1, block_y));
  117. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x-1, block_y));
  118. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x+1, block_y-1));
  119. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x+1, block_y+1));
  120. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x-1, block_y-1));
  121. Find_Closest_Obj_In_List(the_collision, *Get_Block_Obj_List(block_x-1, block_y+1));
  122. }