Lephisto/src/collision.c
Allanis acf4150079 [Add] Per pixel collision.
[Fix] Fleets now start at random location.
    -- Still needs improvement.
2013-02-08 18:29:07 +00:00

51 lines
1.6 KiB
C

#include "main.h"
#include "log.h"
#include "collision.h"
// Collide sprite at (asx, asy) int 'at' at pos 'ap' with sprite at (bsx,bsy) in 'bt' at 'bp'
// at - Texture a.
// asx - Position of x of sprite a.
// asy - Position of y of sprite a.
// ap - Position in space of sprite a.
// bt - Texture b.
// bsx - Position of x of sprite b.
// bsy - Position of y of sprite b.
// bp - Position in space of sprite b.
int CollideSprite(const gl_texture* at, const int asx, const int asy, const Vec2* ap,
const gl_texture* bt, const int bsx, const int bsy, const Vec2* bp) {
int x,y;
// a - cube coords.
int ax1 = (int)VX(*ap) - (int)(at->sw)/2;
int ay1 = (int)VY(*ap) - (int)(at->sh)/2;
int ax2 = ax1 + (int)(at->sw) - 1;
int ay2 = ay1 + (int)(at->sh) - 1;
// b - cube coords.
int bx1 = (int)VX(*bp) - (int)(bt->sw)/2;
int by1 = (int)VY(*bp) - (int)(bt->sh)/2;
int bx2 = bx1 + (int)(bt->sw) - 1;
int by2 = by1 + (int)(bt->sh) - 1;
// Check if bounding boxes intersect.
if((bx2 < ax1) || (ax2 < bx1)) return 0;
if((by2 < ay1) || (ay2 < by1)) return 0;
// Define the remaining binding box.
int inter_x0 = MAX(ax1, bx1);
int inter_x1 = MIN(ax2, bx2);
int inter_y0 = MAX(ay1, by1);
int inter_y1 = MIN(ay2, by2);
for(y = inter_y0; y <= inter_y1; y++)
for(x = inter_x0; x <= inter_x1; x++)
// Computer offsets for surface before passing to TransparentPixel test.
if((!gl_isTrans(at, asx*(int)(at->sw) + x-ax1, asy*(int)(at->sh) + y-ay1)) &&
(!gl_isTrans(bt, bsx*(int)(bt->sw) + x-bx1, bsy*(int)(bt->sh) + y-by1)))
return 1;
return 0;
}