// http://math.stackexchange.com/questions/1098487/atan2-faster-approximation // atan2(y,x) // a := min (|x|, |y|) / max (|x|, |y|) // s := a * a // r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a // if |y| > |x| then r := 1.57079637 - r // if x < 0 then r := 3.14159274 - r // if y < 0 then r := -r sll sllatan2(sll y, sll x) { sll abs_x = sllabs(x); sll abs_y = sllabs(y); sll maxyx = MAX(abs_x, abs_y); sll minyx = MIN(abs_x, abs_y); sll a = slldiv(minyx, maxyx); sll s = sllmul(a, a); sll r_1 = sllmul(dbl2sll((-0.0464964749)), s); sll r_2 = slladd(r_1, dbl2sll(0.15931422)); sll r_3 = sllsub(sllmul(r_2, s), dbl2sll(0.327622764)); sll r = slladd(sllmul(sllmul(r_3, s), a), a); if(sllabs(y) > sllabs(x)) { r = sllsub(CONST_PI_2, r); } if(x < CONST_0) { r = sllsub(CONST_PI, r); } if(y < CONST_0) { r = sllneg(r); } return r; }
static __inline__ double dbl(double x) { sll sval = dbl2sll(x); return *(double*)&sval; }
void phase2_process(void) { sll inc=dbl2sll(0.0022); phase2_ship_x=128; phase2_ship_y=147; phase2_shoot.x=0; phase2_shoot.y=0; phase2_shoot.state=100; phase2_bad.x=0; phase2_bad.y=300; phase2_bad.state=105; phase2_global_x=0; phase2_global_y=0; music_play_fade(MUSIC_LEVEL); init_frame_time(); theend=0; while (!theend) { getKey(); if (hit1_pressed) if (phase2_shoot.state>=25) { sll angle=sllsub(mode7_angle,sllmul(inc,int2sll((128-phase2_ship_x)<<1))); sll multi=int2sll((256-phase2_ship_y)/24); phase2_shoot.dx=sllmul(sllrotl(sllcos(angle),3),SLL_CONST_PI); phase2_shoot.dy=sllmul(sllrotl(sllsin(angle),3),SLL_CONST_PI); phase2_shoot.x=slladd(mode7_x,sllmul(phase2_shoot.dx,multi)); phase2_shoot.y=slladd(mode7_y,sllmul(phase2_shoot.dy,multi)); angle=sllsub(mode7_angle,sllmul(inc,int2sll((128-phase2_ship_x)<<2))); phase2_shoot.dx=sllmul(sllrotl(sllcos(angle),3),SLL_CONST_PI); phase2_shoot.dy=sllmul(sllrotl(sllsin(angle),3),SLL_CONST_PI); phase2_shoot.alt=(phase2_ship_y-128)>>2; phase2_shoot.state=24; play_sound(SND_SHIP_LASER); } if (left_pressed) phase2_ship_x-=8; else if (right_pressed) phase2_ship_x+=8; else if (phase2_ship_x<128) phase2_ship_x+=4; else if (phase2_ship_x>128) phase2_ship_x-=4; if (phase2_ship_x>=192) phase2_ship_x-=8; else if (phase2_ship_x<=64) phase2_ship_x+=8; if (up_pressed) { if (mode7_z<512) { mode7_z += 32; phase2_ship_y-=8; } } else if (down_pressed) { if (mode7_z>256) { mode7_z -= 32; phase2_ship_y+=8; } } else if (mode7_z>400) { mode7_z-=16; phase2_ship_y+=4; } else if (mode7_z<368) { mode7_z+=16; phase2_ship_y-=4; } mode7_process(sllsub(mode7_angle,sllmul(inc,int2sll((128-phase2_ship_x)>>1)))); phase2_global_x= sll2int(mode7_x); phase2_global_y= sll2int(mode7_y); phase2_draw(); delay_frame_time(); }