/* ** AIRSHIP proto version ** Public Domain/Domaine Publique author Robin Fercoq date 00/01 category real time voxel automata rendering fly through engine source C++ -raw- compiler DJGPP IDE Rhide libs Allegro exec dos compatible */ /* ship.cc class ship methods: pseudo inertia drive, camera movement primitives mouse navigation/piloting */ #include "airship.h" ship::ship(){ twa=roa=tia=moved=strx=stry=0; strr=0; freeway=0; } void norm_vect(fixed *dx,fixed *dy,fixed *dz){ float x,y,z; x=fixtof(*dx);y=fixtof(*dy);z=fixtof(*dz); normalize_vector_f(&x,&y,&z); *dx=ftofix(x);*dy=ftofix(y);*dz=ftofix(z); } void ship::normcam(){ fixed d; norm_vect(&c.uf, &c.vf, &c.wf); d=dot_product(c.uf,c.vf,c.wf,c.ut,c.vt,c.wt); c.ut-=fmul(d,c.uf); c.vt-=fmul(d,c.vf); c.wt-=fmul(d,c.wf); norm_vect(&c.ut,&c.vt,&c.wt); } void ship::normcam(fixed*ul,fixed*vl,fixed*wl){ normcam(); cross_product(c.ut,c.vt,c.wt,c.uf,c.vf,c.wf,ul,vl,wl); } void ship::twist(fixed a){ MATRIX r; normcam(); get_vector_rotation_matrix(&r,c.ut,c.vt,c.wt,a); apply_matrix(&r,c.uf,c.vf,c.wf,&c.uf,&c.vf,&c.wf); } void ship::roll(fixed a){ MATRIX r; fixed ul,vl,wl; normcam(&ul,&vl,&wl); get_vector_rotation_matrix(&r,ul,vl,wl,a); apply_matrix(&r,c.uf,c.vf,c.wf,&c.uf,&c.vf,&c.wf); //apply_matrix(&r,c.ut,c.vt,c.wt,&c.ut,&c.vt,&c.wt); } void ship::tilt(fixed a){ MATRIX r; //normalize_vector(&c.uf, &c.vf, &c.wf); get_vector_rotation_matrix(&r,c.uf,c.vf,c.wf,a); apply_matrix(&r,c.ut,c.vt,c.wt,&c.ut,&c.vt,&c.wt); //normalize_vector(&c.ut, &c.vt, &c.wt); } void ship::strafh(fixed d){ fixed ul,vl,wl; normcam(&ul,&vl,&wl); c.x-=fmul(d,ul); c.y-=fmul(d,vl); c.z-=fmul(d,wl); } void ship::strafv(fixed d){ normcam(); c.x+=fmul(d,c.ut); c.y+=fmul(d,c.vt); c.z+=fmul(d,c.wt); } void ship::straflh(fixed d){ strafh(d); twist( fmul(fdiv(-d,tl),rad2fa) ); } void ship::straflv(fixed d){ strafv(d); roll( fmul(fdiv(-d,tl),rad2fa) ); } void ship::move(fixed d){ normcam(); c.x+=fmul(d,c.uf); c.y+=fmul(d,c.vf); c.z+=fmul(d,c.wf); } void ship::move_(fixed d){ move(d); tl-=d; if (tlmanip(); v->action(); //S.pilot(itofix(mmx),itofix(mmy)); poll_joystick(); if (key[KEY_PGUP]) tl+=itofix(1)>>1; if (key[KEY_PGDN]){ tl-=itofix(1)>>1; if (tlitofix(105))c.fov=itofix(105);} } if (key[KEY_PLUS_PAD]){ if (key_shifts&KB_SHIFT_FLAG) c.fov=itofix(35); else{ c.fov-=itofix(1);if(c.fov7) freeway=7; //if (freeway==-2) freeway=1; } if (key[KEY_SLASH_PAD]){ freeway--; if (freeway<-7) freeway=-7; //if (freeway==0) freeway=-3; } if (mouse_b&2 && !(mouse_b&1)){ if (!(mouse_b&4)) roa=+mmy>>4; twa=mmx>>4; } if (!joy[0].button[1].b && num_joysticks) twa=fmul((joy[0].stick[0].axis[0].pos)<<8,(1<<16)+16*abs(moved));//10 if (!joy[0].button[0].b && num_joysticks) roa=fmul((joy[0].stick[0].axis[1].pos)<<8,(1<<16)+16*abs(moved));//10 if (roa) roll(roa); if (twa) twist(twa); if (mouse_b&4 && !(mouse_b&1)){ moved=-mmy>>6; if (!(mouse_b&2)) tia=-mmx>>4; } if (joy[0].button[0].b) moved-=(joy[0].stick[0].axis[1].pos)<<4;//8 if (key_shifts&KB_CTRL_FLAG) move_(moved); else move(moved); if (joy[0].button[1].b) tia=-(joy[0].stick[0].axis[0].pos)<<10;//8 if (tia) tilt(tia); if (mouse_b&1){ if (key_shifts&KB_SHIFT_FLAG){ strx=mmx>>8; stry=-mmy>>8; strr=1; } else{ strx=mmx>>6; stry=-mmy>>6; strr=0; } } if (strr){ straflh(strx); straflv(stry); } else{ strafh(strx); strafv(stry); } if (key[KEY_LEFT]||key[KEY_RIGHT]||key[KEY_UP]||key[KEY_DOWN]){ if (key[KEY_LEFT]||key[KEY_RIGHT]){ get_vector_rotation_matrix(&r,c.ut,c.vt,c.wt, key[KEY_LEFT]?5<<16:-5<<16); apply_matrix(&r,Lu,Lv,Lw,&Lu,&Lv,&Lw); } if (key[KEY_UP]||key[KEY_DOWN]){ normcam(&ul,&vl,&wl); get_vector_rotation_matrix(&r,ul,vl,wl, key[KEY_UP]?-5<<16:5<<16); apply_matrix(&r,Lu,Lv,Lw,&Lu,&Lv,&Lw); } if (key_shifts&KB_CTRL_FLAG) randpal(); updatepalette(); } if ( (cr=v->isup(&c)) ){ v=cr->pointer; apply_matrix(&cr->position,c.x,c.y,c.z,&c.x,&c.y,&c.z); } if ( (cr=v->isdown(&c)) ){ v=cr->pointer; apply_matrix(&cr->position,c.x,c.y,c.z,&c.x,&c.y,&c.z); } clear_to_color(scrbuf,1); //memset(RLE[0],0,RLE_); //memset(PART[0],0,PART_); v->renderflat(scrbuf,&c,freeway,1); if (key[KEY_F11]) textprintf(scrbuf,font,10,10,255,"%d",totframe++); else totframe=0; if (key[KEY_F12]) textprintf(scrbuf,font,10,30,255,"%d %d %d", c.x>>16,c.y>>16,c.z>>16); hline(scrbuf,0,0,L,0); vline(scrbuf,0,0,H,0); blit(scrbuf,screen,0,0,0,0,L,H); }