Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //returns true only if resulting bounds have volume
- static inline bool get_tile_bounds(shape_quad& bounds){
- //find the boundaries of all tiles that the player is touching
- //(+= 11 so that an east/south subtile boundary is crossed only when
- // a pixel of the player sprite actually overlaps a given tile)
- bounds.b += shape::point(11, 11);
- bounds /= 12; //convert pixels to subtiles
- //
- bounds.a.x = CLAMP(bounds.a.x, 0, TILESIZ_X*2); //skip subtiles that are off-screen
- bounds.a.y = CLAMP(bounds.a.y, 0, TILESIZ_Y*2); //(2 subtiles in a tile lengthwise)
- //
- bounds.b.x = CLAMP(bounds.b.x, 0, TILESIZ_X*2);
- bounds.b.y = CLAMP(bounds.b.y, 0, TILESIZ_Y*2);
- s32 xmin, xmax, ymin, ymax;
- if(abs(bounds.a.x)<abs(bounds.b.x)){ xmin = abs(bounds.a.x), xmax = abs(bounds.b.x); }
- else { xmin = abs(bounds.b.x), xmax = abs(bounds.a.x); }
- if(abs(bounds.a.y)<abs(bounds.b.y)){ ymin = abs(bounds.a.y), ymax = abs(bounds.b.y); }
- else { ymin = abs(bounds.b.y), ymax = abs(bounds.a.y); }
- return (xmax-xmin)>0 && (ymax-ymin)>0;
- }
- #define SO_LOOP_PATTERN 0
- bool subtiles_overlapping(shape::rect& object_rect_ref, shape::point* delta_p){
- shape::rect object_rect = object_rect_ref;
- shape_quad bounds = object_rect;
- shape::point delta(0, 0); //neutral delta by default
- if(!get_tile_bounds(bounds)){ //there are no subtiles to check; exit early
- if(delta_p) *delta_p = delta;
- return false;
- }
- shape::point min = bounds.a;
- shape::point max = bounds.b;
- shape::point sub; //current subtile position
- shape::rect col(0,0, 12,12); //subtile collider
- #if SO_LOOP_PATTERN == 1
- s32 xmid = (min.x+max.x)/2; //x midpoint
- /* bottom-to-top vertically (y = max.y-1; y >= min.y; --y)
- right-to-left on the left half (x = xmid-1; x >= min.x; --x)
- left-to-right on the right half (x = xmid ; x < max.x; ++x) */
- //(tbd)
- #else
- for(sub.y = max.y-1; sub.y >= min.y; --sub.y) //bottom-to-top vertically
- for(sub.x = min.x ; sub.x < max.x; ++sub.x) //left-to-right horizontally
- if(subtile_is_collidable(sub.x, sub.y)){
- col.x = sub.x*12;
- col.y = sub.y*12;
- if(rects_overlapping(object_rect, col, &delta))
- object_rect += delta;
- }
- #endif
- delta.x = object_rect.x - object_rect_ref.x;
- delta.y = object_rect.y - object_rect_ref.y;
- if(delta_p) *delta_p = delta;
- return delta.x!=0 || delta.y!=0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement