Skip to content

Instantly share code, notes, and snippets.

@drakeirving
Created July 15, 2018 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakeirving/98b64b6023f9e7bec02e60e308ead131 to your computer and use it in GitHub Desktop.
Save drakeirving/98b64b6023f9e7bec02e60e308ead131 to your computer and use it in GitHub Desktop.
Camera Snap Bullet Cancel
function snap(ox, oy, angle, size){
let RECT_RATIO = 2/3; // rect width/height ratio
let rect_ang = atan(RECT_RATIO); // angle offset to corners
// prepare rect vertices
let ax = ox + size*cos(angle - rect_ang);
let ay = oy + size*sin(angle - rect_ang);
let bx = ox + size*cos(angle + rect_ang);
let by = oy + size*sin(angle + rect_ang);
let cx = ox + size*cos(angle - rect_ang + 180);
let cy = oy + size*sin(angle - rect_ang + 180);
// prepare edge vectors and magnitudes
let abx = bx-ax;
let aby = by-ay;
let ab2 = abx*abx + aby*aby; // ab . ab
let bcx = cx-bx;
let bcy = cy-by;
let bc2 = bcx*bcx + bcy*bcy; // bc . bc
function is_collided_rect2(px, py){
let proj = (px-ax)*abx + (py-ay)*aby; // ap . ab
if(proj < 0 || proj > ab2){ return false; }
proj = (px-bx)*bcx + (py-by)*bcy; // bp . bc
if(proj < 0 || proj > bc2){ return false; }
return true;
}
// get bullets to check collision for
// broad-phase: only bullets within circle circumscribing rect
let shots = GetShotIdInCircleA2(ox, oy, size, TARGET_ENEMY);
// check collision for all bullets
ascent(i in 0..length(shots)){
if(is_collided_rect2(ObjMove_GetX(shots[i]), ObjMove_GetY(shots[i]))){
Obj_Delete(shots[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment