//#pragma argsused #define ARRIBA 72 // NUMEROS ASOCIADOS A LAS FLECHAS DEL TECLADO #define IZQUIERDA 75 #define DERECHA 77 #define ABAJO 80 #define ESC 27 #define ENTER 13 int backcolor = 0; int dir = 0; int x = 39, y = 22; int anteriorpx, anteriorpy; char tecla; int pillTimer = -1; Point2D pillPosition = Point2D(0,0); long int puntos = -5; int vidas = 3; void setCColor(int color) { static HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color | (backcolor * 0x10 + 0x100)); } int color[7] = { 0x009,
Point2D Point2D::operator+ (const Point2D& rhs) const { return Point2D(x+rhs.x,y+rhs.y); }
bool Point2D::equals(int xx, int yy) const { return equals(Point2D(xx,yy)); }
int main( int argc, char* args[] ) { if (InitSDL()) { WindowSurface screen(800,600); MusicHandler musicHandler; SoundEffectsHandler sfxHandler; Graphics graphics; Sounds sounds; Settings setting; Maps levels; GlobalSettings gSettings; GameStateManager gameStates(&setting); SettingSaverLoader ssl; ssl.LoadSettings(gSettings); long Timer = clock();//For Frame Independent Movement bool gameRunning=true; FPS fps(30); SDL_Event sEvent; musicHandler.SetNewMusic(&sounds.titleScreen); sfxHandler.AddSoundEffect(&sounds.death); sfxHandler.AddSoundEffect(&sounds.bell); sfxHandler.AddSoundEffect(&sounds.damage); sfxHandler.AddSoundEffect(&sounds.enemyDeath); gameStates.NewState(GSIntro); int introCount=0; float logoPositionY = 0; Surface surface; surface.LoadImage("Images/icon.png", 255, 255, 0); SDL_WM_SetIcon(surface, NULL); screen.SetCaption("A Rat's Tale"); EnemyHandler enemies; Map map(&graphics.tileSheet, 50, 50, "Map/map1.txt"); levels.count++; map.AddTile('A', 0, 0); map.AddTile('B', 50, 0); map.AddTile('C', 100, 0); map.AddTile('D', 0, 50); map.AddTile('E', 50, 50); map.AddTile('F', 100, 50); map.AddTile('G', 0, 100); map.AddTile('H', 50, 100); map.AddTile('I', 100, 100); map.AddTile('J', 0, 150); map.AddTile('K', 50, 150); map.AddTile('L', 100, 150); map.AddTile('M', 150, 0); map.AddTile('N', 150, 50); map.AddTile('O', 150, 100); map.AddTile('P', 200, 0); map.AddTile('Q', 250, 0); map.AddTile('R', 200, 50); map.AddTile('S', 250, 50); map.AddTile('T', 200, 100); map.AddTile('U', 250, 100); map.AddTile('V', 150, 150); map.AddTile('W', 200, 150); map.AddTile('X', 250, 150); map.AddTile('Y', 150, 200); map.AddTile('Z', 200, 200); map.AddTile('(', 250, 200); map.AddTile(')', 300, 200); map.AddTile('>', 300, 0, 50, 0); map.AddTile('<', 300, 50, 0, 50); map.AddTile('\\', 300, 100, 50, 0); map.AddTile('/', 300, 150, 0, 50); map.AddTile('a', 350, 50); map.AddTile('b', 400, 50); map.AddTile('c', 450, 50); map.AddTile('d', 350, 100); map.AddTile('e', 400, 100); map.AddTile('f', 450, 100); map.AddTile('g', 350, 150); map.AddTile('h', 400, 150); map.AddTile('i', 450, 150); map.AddTile('j', 350, 0); map.AddTile('k', 400, 0); map.AddTile('l', 450, 0); map.AddTile('w', 500, 0, 50, 0); map.AddTile('v', 500, 50, 0, 50); map.AddTile('x', 500, 100, 50, 0); map.AddTile('y', 500, 150, 0, 50); map.AddTile('m', 0, 200, 0, 50); map.AddTile('n', 50, 200); map.AddTile('o', 100, 200, 50, 0); map.AddTile('p', 350, 200, TStop); map.AddTile('q', 400, 200, TSleft); map.AddTile('r', 450, 200, TSright); map.AddTile('s', 500, 200, TSbottom); map.AddTile('u', 200, 250, false, true); map.AddTile('z', 50, 250, false, true); map.AddTile('@', 150, 250); map.AddTile('!', 100, 250); Player player(&graphics.player, map.GetSpawnLocation().X, map.GetSpawnLocation().Y, 3, 3, 2); player.SetVelocity(200, 300); //If Timer is set in draw of player (50 pixels per second) else (50pixels per frame) Menu menu("Main menu", &setting, 255, 255, 255); menu.ShowHeader(false); menu.SetCenter(true); menu.SetVerticalSpace(20); menu.AddButtonChild("New game",false,255,255,255, &Settings::OnClickNewGame); menu.AddButtonChild("Load game",false,255,255,255, &Settings::OnClickLoadGame); menu.AddChild("Options", true); menu.GetChild(2)->SetVerticalSpace(20); menu.GetChild(2)->AddChild("Graphics",true); menu.GetChild(2)->GetChild(0)->AddChild("FPS Setting"); menu.GetChild(2)->GetChild(0)->AddChild("Resolution"); menu.GetChild(2)->AddChild("Sounds",true); menu.GetChild(2)->GetChild(1)->AddChild("Master Volume"); menu.GetChild(2)->GetChild(1)->AddSliderChild(256,10,true,155,155,155,&SetNewVolume,&gSettings); ((SliderMenuItem*)(menu.GetChild(2)->GetChild(1)->GetChild(1)))->SetStatus((int)(gSettings._volume*256)); menu.GetChild(2)->GetChild(1)->AddChild("SFX-Music"); menu.GetChild(2)->GetChild(1)->AddSliderChild(256,10,true,30,200,30,&SetNewProportion,&gSettings); ((SliderMenuItem*)(menu.GetChild(2)->GetChild(1)->GetChild(3)))->SetStatus((int)(gSettings._sfxMusicProportion*256)); menu.GetChild(2)->AddChild("Keys"); menu.AddButtonChild("Exit",false,255,255,255, &Settings::OnClickExitGame); Menu newLevelMenu("Level completed", &setting); newLevelMenu.SetCenter(true); newLevelMenu.SetVerticalSpace(20); newLevelMenu.AddChild("Congratulations"); newLevelMenu.AddChild(""); newLevelMenu.AddChild("What do you want to do?"); std::vector<std::string> opt; opt.push_back("Main menu"); opt.push_back("Restart"); opt.push_back("Next level"); newLevelMenu.AddOptionChild(opt, " ",false,255,255,255,&Settings::NewLevelOptions); Menu gameOverMenu("Game Over!", &setting); gameOverMenu.SetCenter(true); gameOverMenu.SetVerticalSpace(20); gameOverMenu.AddChild("Your journey has ended. You want to restart?"); std::vector<std::string> opts; opts.push_back("Yes"); opts.push_back("No"); opts.push_back("Quit"); gameOverMenu.AddOptionChild(opts, " ", false, 255, 255, 255, &Settings::GameOverOptions); Menu inGameMenu("Pause",&setting,255,0,0); inGameMenu.SetCenter(true); inGameMenu.SetVerticalSpace(20); inGameMenu.AddButtonChild("Resume" ,true,255,255,255,&Settings::OnClickResume); inGameMenu.AddChild("Show statistics" ,true,255,255,255); inGameMenu.GetChild(1)->AddChild("Current level: "); inGameMenu.GetChild(1)->AddChild("Remaining Lives: "); inGameMenu.GetChild(1)->AddChild("Progression: "); inGameMenu.GetChild(1)->AddChild("Enemies killed: "); inGameMenu.GetChild(1)->AddChild("Game completion: "); inGameMenu.GetChild(1)->AddChild("Total distance drawn: "); inGameMenu.AddChild("Options", true); inGameMenu.GetChild(2)->SetVerticalSpace(20); inGameMenu.GetChild(2)->AddChild("Graphics",true); inGameMenu.GetChild(2)->GetChild(0)->AddChild("FPS Setting"); inGameMenu.GetChild(2)->GetChild(0)->AddChild("Resolution"); inGameMenu.GetChild(2)->AddChild("Sounds",true); inGameMenu.GetChild(2)->GetChild(1)->AddChild("Master Volume"); inGameMenu.GetChild(2)->GetChild(1)->AddSliderChild(256,10,true,155,155,155,&SetNewVolume,&gSettings); ((SliderMenuItem*)(menu.GetChild(2)->GetChild(1)->GetChild(1)))->SetStatus((int)(gSettings._volume*256)); inGameMenu.GetChild(2)->GetChild(1)->AddChild("SFX-Music"); inGameMenu.GetChild(2)->GetChild(1)->AddSliderChild(256,10,true,30,200,30,&SetNewProportion,&gSettings); ((SliderMenuItem*)(menu.GetChild(2)->GetChild(1)->GetChild(3)))->SetStatus((int)(gSettings._sfxMusicProportion*256)); inGameMenu.GetChild(2)->AddChild("Keys"); inGameMenu.AddChild("Quit",true,255,0,0); inGameMenu.GetChild(3)->AddButtonChild("Exit to main menu",true,0,255,0, &Settings::OnClickNewGame); inGameMenu.GetChild(3)->AddButtonChild("Exit game",true,255,255,255, &Settings::OnClickExitGame); int NewLevelID = 0; int LevelCounter = 0; bool actionButtonPressed = false; //((OptionMenuItem*)newLevelMenu.GetChild(3))->GetOption(1)->Enabled = false; while (gameRunning) { while (SDL_PollEvent(&sEvent)) { if (sEvent.type==SDL_QUIT || setting.GetResult() == MRExitGame){ gameRunning=false; setting.Finish(); } if (sEvent.type == SDL_VIDEORESIZE ) {screen.CreateWindowSurface( sEvent.resize.w,sEvent.resize.h);} switch(gameStates.CurrentState()) { case GSIntro: if (sEvent.type == SDL_KEYDOWN||sEvent.type == SDL_MOUSEBUTTONDOWN) { logoPositionY=20; introCount=256; graphics.gameLogo.SetTransparency(256); } case GSMenuMain: if(sEvent.type == SDL_KEYDOWN){ if(sEvent.key.keysym.sym == SDLK_ESCAPE) {gameStates.BackState();menu.Reset();} } menu.HandleEvent(sEvent); break; case GSPause: { if(sEvent.type == SDL_KEYDOWN){ if(sEvent.key.keysym.sym == SDLK_ESCAPE) {gameStates.BackState();inGameMenu.Reset();} } inGameMenu.HandleEvent(sEvent); break; } case GSGame: if(sEvent.type == SDL_KEYDOWN) { if(sEvent.key.keysym.sym == SDLK_ESCAPE) {gameStates.PushState(GSPause);} else if(sEvent.key.keysym.sym == SDLK_s||sEvent.key.keysym.sym == SDLK_DOWN) {actionButtonPressed=true;} } if(sEvent.type == SDL_KEYUP){ if(sEvent.key.keysym.sym == SDLK_s||sEvent.key.keysym.sym == SDLK_DOWN) {actionButtonPressed=false;} } player.HandleEvent(sEvent); map.HandleEvent(sEvent); break; case GSMenuNewLevel: newLevelMenu.HandleEvent(sEvent); break; case GSGame_Over: gameOverMenu.HandleEvent(sEvent); break; } } if(clock() > Timer + 500) Timer = clock(); if(gameStates.CurrentState() == GSNone) gameStates.PushState(GSMenuMain); if(gameStates.CurrentState() == GSGame && player.Health <= 0) { gameStates.PushState(GSGame_Over); sounds.forest.SetVolumeModifier(0.1f); sounds.death.Play(false); } screen.ClearWindow(); float musicPercentage=2*gSettings._sfxMusicProportion; if (musicPercentage>1.0){musicPercentage=1.0;} float sfxPercentage=2-2*gSettings._sfxMusicProportion; if (sfxPercentage>1.0){sfxPercentage=1.0;} musicHandler.SetGlobalVolume((int)(gSettings._volume*musicPercentage*128)); sfxHandler.SetGlobalVolume((int)(gSettings._volume*sfxPercentage*128)); switch(gameStates.CurrentState()) { case GSIntro: if(introCount > 255){ logoPositionY -= 5; if(logoPositionY <= 20){ logoPositionY = 20; gameStates.NewState(GSMenuMain); } Timer = clock(); graphics.gameLogo.Draw(screen, screen.GetWidth()/2 - graphics.gameLogo.GetWidth()/2, (unsigned int)logoPositionY); } else { graphics.gameLogo.SetTransparency(introCount); logoPositionY = (float)screen.GetHeight()/2 - (float)graphics.gameLogo.GetHeight()/2; Timer = clock(); graphics.gameLogo.Draw(screen, screen.GetWidth()/2 - graphics.gameLogo.GetWidth()/2, (unsigned int)logoPositionY); } introCount+=2; break; case GSMenuMain: if(setting.GetResult() == MRNewGame){ musicHandler.SetNewMusic(&sounds.forest); levels.count = 0; LevelCounter = 0; map.Reset(); map.NewMap(levels.levels[0]); enemies.PopulateEnemies(&map, &graphics); player.Reset(map.GetSpawnLocation(), true); gameStates.NewState(GSGame); setting.Finish(); }else if (setting.GetResult()==MRLoadGame) { Loader load; if (load.StartAndCheck("Save1.sav")) { int newMapId; load.LoadCheckpoint(LevelCounter,newMapId); if(levels.maxCount <= newMapId || newMapId < 0) newMapId = 0; levels.count = newMapId; load.LoadMap(map); load.LoadPlayer(player); load.Close(); map.NewMap(levels.levels[newMapId]); enemies.PopulateEnemies(&map, &graphics); gameStates.NewState(GSGame); musicHandler.SetNewMusic(&sounds.forest); player.Reset(map.GetSpawnLocation(), false); } setting.Finish(); } musicHandler.Update(); Timer = clock(); graphics.gameLogo.Draw(screen, screen.GetWidth()/2 - graphics.gameLogo.GetWidth()/2, (unsigned int)logoPositionY); menu.Open(screen, graphics.another, Point2D(50, logoPositionY + graphics.gameLogo.GetHeight())); break; case GSPause: { if(setting.GetResult() == MRResume){ gameStates.BackState(); inGameMenu.Reset(); setting.Finish(); }else if(setting.GetResult() == MRLoadGame) { Loader load; if (load.StartAndCheck("tempOldSave.sav")) { int newMapId; load.LoadCheckpoint(LevelCounter,newMapId); if(levels.maxCount <= newMapId || newMapId < 0) newMapId = 0; levels.count = newMapId; load.LoadMap(map); load.LoadPlayer(player); load.Close(); map.NewMap(levels.levels[newMapId]); enemies.PopulateEnemies(&map, &graphics); gameStates.NewState(GSGame); musicHandler.SetNewMusic(&sounds.forest); player.Reset(map.GetSpawnLocation(), false); Saver save; save.StartAndOpen(); save.SaveCheckpoint(LevelCounter,levels.count); save.SaveMap(map); save.SavePlayer(player); save.EndAndClose("Save1.sav"); } if (load.StartAndCheck("tempSave.sav")) { int newMapId; load.LoadCheckpoint(LevelCounter,newMapId); if(levels.maxCount <= newMapId || newMapId < 0) newMapId = 0; levels.count = newMapId; load.LoadMap(map); load.LoadPlayer(player); load.Close(); map.NewMap(levels.levels[newMapId]); enemies.PopulateEnemies(&map, &graphics); gameStates.NewState(GSGame); musicHandler.SetNewMusic(&sounds.forest); player.Reset(map.GetSpawnLocation(), false); Saver save; save.StartAndOpen(); save.SaveCheckpoint(LevelCounter,levels.count); save.SaveMap(map); save.SavePlayer(player); save.EndAndClose("Save1.sav"); } if (load.StartAndCheck("Save1.sav")) { int newMapId; load.LoadCheckpoint(LevelCounter,newMapId); if(levels.maxCount <= newMapId || newMapId < 0) newMapId = 0; levels.count = newMapId; load.LoadMap(map); load.LoadPlayer(player); load.Close(); map.NewMap(levels.levels[newMapId]); enemies.PopulateEnemies(&map, &graphics); gameStates.NewState(GSGame); musicHandler.SetNewMusic(&sounds.forest); player.Reset(map.GetSpawnLocation(), false); } setting.Finish(); }else if (setting.GetResult() == MRNewGame) { gameStates.NewState(GSMenuMain); musicHandler.SetNewMusic(&sounds.titleScreen); setting.Finish(); } musicHandler.Update(); Timer = clock(); map.DrawBackground(screen, &graphics); map.Draw(screen, LevelCounter); player.DrawHealthBar(screen, 1, 5, 5, 100, 20, &graphics.another); player.DrawInkBar(screen, 1, 5, 35, 100, 20, &graphics.another); inGameMenu.Open(screen, graphics.another, Point2D(0, 50)); break; } case GSGame: if(gameStates.CurrentState() != GSMenuNewLevel && actionButtonPressed){ if(map.NewMapEnabled(player.GetBoundR(), NewLevelID)) gameStates.PushState(GSMenuNewLevel); actionButtonPressed=false; } player.Update(&map, screen.GetWidth(), screen.GetHeight(),sounds, Timer); enemies.Update(&map, &player,sounds, Timer); map.Update(player.GetBoundR(-map.GetMapPosition().X, -map.GetMapPosition().Y),player.InkPool); musicHandler.Update(); Timer = clock(); map.DrawBackground(screen, &graphics); map.Draw(screen, LevelCounter); enemies.Draw(screen, map.GetMapPosition()); player.Draw(screen, map.GetMapPosition()); player.DrawHealthBar(screen, 1, 5, 5, 100, 20, &graphics.another); player.DrawInkBar(screen, 1, 5, 35, 100, 20, &graphics.another); break; case GSMenuNewLevel:{ if(levels.count > LevelCounter) LevelCounter++; if(NewLevelID < 0) levels.count++; else if(NewLevelID >= levels.maxCount) levels.count = levels.maxCount-1; else levels.count = NewLevelID; map.NewMap(levels.levels[levels.count]); enemies.PopulateEnemies(&map, &graphics); player.Reset(map.GetSpawnLocation(), false); gameStates.BackState(); sounds.bell.Play(false); Saver saver; saver.StartAndOpen(); saver.SaveCheckpoint(LevelCounter,levels.count); saver.SaveMap(map); saver.SavePlayer(player); saver.EndAndClose("Save1.sav"); break; } case GSGame_Over: musicHandler.Update(); Timer = clock(); gameOverMenu.Open(screen, graphics.another, Point2D(0, 50)); if(setting.GetResult() != MRExitGame && setting.GetResult() != MRNone) { if(setting.GetResult() == MRMainMenu) { musicHandler.SetNewMusic(&sounds.titleScreen); sounds.forest.SetVolumeModifier(0.4f); gameStates.NewState(GSMenuMain); } else if(setting.GetResult() == MRNewGame){ levels.count = 1; map.NewMap(levels.levels[0]); enemies.PopulateEnemies(&map, &graphics); player.Reset(map.GetSpawnLocation(), false); player.Health =(float) 100; player.InvulnerableTime = 0; sounds.forest.SetVolumeModifier(0.4f); gameStates.NewState(GSGame); } setting.Finish(); } break; } screen.UpdateWindow(); fps.Delay(); } } ClearSDL(); return 0; };
Point2D Point2D::operator* (const double rhs) const { return Point2D(x*rhs,y*rhs,w*rhs); }
const Point2D Point2D::operator - (const double& a_Value) const { return Point2D(x - a_Value, y - a_Value); }
const Point2D Point2D::operator / (const double& a_Value) const { return Point2D(x / a_Value, y / a_Value); }
void Viewer::reset() { if ( !m_initflag ) { update_mode( MODELROTATE ); } // Default mouse information m_button1 = false; m_button2 = false; m_button3 = false; m_ixpos = 0.0; m_xpos = 0.0; m_iypos = 0.0; m_ypos = 0.0; m_txpos = 0.0; // Initialize viewport for ( int i = 0; i < 4; i += 1 ) { m_viewport[i] = ( Point2D() ); } // Default FOV of 30 m_fov = 30.0; // Set the default far and near plane values m_near = 2.0; m_far = 20.0; // Initialize all the transformation matrices m_projection = Matrix4x4(); m_modelling = Matrix4x4(); m_viewing = Matrix4x4(); m_scaling = Matrix4x4(); // Start off by pushing the cube back into the screen Vector4D row1, row2, row3, row4; row1 = Vector4D( 1, 0, 0, 0 ); row2 = Vector4D( 0, 1, 0, 0 ); row3 = Vector4D( 0, 0, 1, 8 ); row4 = Vector4D( 0, 0, 0, 1 ); m_viewing = Matrix4x4( row1, row2, row3, row4 ); // Initialize the unit cubes m_unitCube[0] = ( Point3D( 1.0, -1.0, -1.0) ); m_unitCube[1] = ( Point3D(-1.0, -1.0, -1.0) ); m_unitCube[2] = ( Point3D(-1.0, 1.0, -1.0) ); m_unitCube[3] = ( Point3D( 1.0, 1.0, -1.0) ); m_unitCube[4] = ( Point3D(-1.0, -1.0, 1.0) ); m_unitCube[5] = ( Point3D( 1.0, -1.0, 1.0) ); m_unitCube[6] = ( Point3D( 1.0, 1.0, 1.0) ); m_unitCube[7] = ( Point3D(-1.0, 1.0, 1.0) ); for ( int i = 0; i < 8; i += 1 ) { m_unitCubeTrans[i] = ( Point3D() ); } // Initialize the gnomons m_gnomon[0] = ( Point3D(0.0, 0.0, 0.0) ); m_gnomon[1] = ( Point3D(0.5, 0.0, 0.0) ); m_gnomon[2] = ( Point3D(0.0, 0.5, 0.0) ); m_gnomon[3] = ( Point3D(0.0, 0.0, 0.5) ); for ( int i = 0; i < 4; i += 1 ) { m_gnomonTrans[i] = ( Point3D() ); } m_viewflag = false; m_initflag = false; // Initialize the perspective set_perspective( m_fov, 1, m_near, m_far ); }
Point2D Point2D::operator+(const Point2D &p) const { return Point2D(x + p.x, y + p.y); }
void UIRectangle::dropShadowRounded( float dens, float rad) { glPushMatrix( ); glTranslated( 0.08, -0.08, 0 ); int div = 6; double span = TWO_PI * 0.25 / ( double)div; int i; if ( rad <= 0 ) rad = 0.5 * min ( w( ), h( ) ); Point2D p; double il = tl[0] + rad; double ir = br[0] - rad; double it = tl[1] - rad; double ib = br[1] + rad; //center block glColor4d ( 0,0,0,dens); glBegin( GL_QUADS); glVertex2d ( il , it ); glVertex2d ( ir , it ); glVertex2d ( ir , ib ); glVertex2d ( il , ib ); //upper glColor4d ( 0,0,0,0); glVertex2d ( il , tl[1] ); glVertex2d ( ir , tl[1] ); glColor4d ( 0,0,0,dens); glVertex2d ( ir , it ); glVertex2d ( il , it ); //lower glColor4d ( 0,0,0,0); glVertex2d ( il , br[1] ); glVertex2d ( ir , br[1] ); glColor4d ( 0,0,0,dens); glVertex2d ( ir , ib ); glVertex2d ( il , ib ); //left glColor4d ( 0,0,0,0); glVertex2d ( tl[0] , it ); glVertex2d ( tl[0] , ib ); glColor4d ( 0,0,0,dens); glVertex2d ( il , ib ); glVertex2d ( il , it ); //right glColor4d ( 0,0,0,0); glVertex2d ( br[0] , it ); glVertex2d ( br[0] , ib ); glColor4d ( 0,0,0,dens); glVertex2d ( ir , ib ); glVertex2d ( ir , it ); glEnd( ); p = tl + Point2D( rad, -rad ); glBegin( GL_TRIANGLE_FAN); glColor4d ( 0,0,0,dens); glVertex2d ( p[0], p[1]); glColor4d ( 0,0,0,0.0); for ( i = 0 ; i <= div ; i++ ) { glVertex2d ( p[0] - rad * cos( ( double)i * span ) , p[1] + rad * sin( ( double)i * span ) ); } glEnd( ); p = Point2D( br[0] - rad, tl[1] -rad ); glBegin( GL_TRIANGLE_FAN); glColor4d ( 0,0,0,dens); glVertex2d ( p[0], p[1]); glColor4d ( 0,0,0,0.0); for ( i = 0 ; i <= div ; i++ ) { glVertex2d ( p[0] + rad * sin( ( double)i * span ) , p[1] + rad * cos( ( double)i * span )); } glEnd( ); p = br + Point2D( - rad, rad ); glBegin( GL_TRIANGLE_FAN); glColor4d ( 0,0,0,dens); glVertex2d ( p[0], p[1]); glColor4d ( 0,0,0,0.0); for ( i = 0 ; i <= div ; i++ ) { glVertex2d ( p[0] + rad * cos( ( double)i * span ) , p[1] - rad * sin( ( double)i * span )); } glEnd( ); p = Point2D( tl[0] + rad, br[1] + rad ); glBegin( GL_TRIANGLE_FAN); glColor4d ( 0,0,0,dens); glVertex2d ( p[0], p[1]); glColor4d ( 0,0,0,0.0); for ( i = 0 ; i <= div ; i++ ) { glVertex2d ( p[0] - rad * sin( ( double)i * span ) , p[1] - rad * cos( ( double)i * span )); } glEnd( ); glPopMatrix( ); }
bool Viewer::on_expose_event( GdkEventExpose* /*event*/ ) { Glib::RefPtr<Gdk::GL::Drawable> gldrawable = get_gl_drawable(); if ( !gldrawable ) { return false; } if ( !gldrawable->gl_begin(get_gl_context()) ) { return false; } // Start drawing draw_init( get_width(), get_height() ); // Transform the world gnomon for( int i = 0; i < 4; i += 1 ) { m_gnomonTrans[i] = m_viewing * m_gnomon[i]; } // Draw the world gnomon set_colour( Colour(0.1, 0.1, 1.0) ); draw_line2D( m_gnomonTrans[0], m_gnomonTrans[1] ); draw_line2D( m_gnomonTrans[0], m_gnomonTrans[2] ); draw_line2D( m_gnomonTrans[0], m_gnomonTrans[3] ); // Draw the modelling gnomon set_colour( Colour(0.1, 1.0, 0.1) ); draw_modellingGnomon(); // Draw the unit cube set_colour( Colour(0.1, 0.1, 0.1) ); draw_unitCube(); // Initialize the viewport if ( !m_viewflag ) { m_viewport[0] = ( Point2D(get_width() * 0.05, get_height() * 0.05) ); m_viewport[1] = ( Point2D(get_width() * 0.95, get_height() * 0.05) ); m_viewport[2] = ( Point2D(get_width() * 0.95, get_height() * 0.95) ); m_viewport[3] = ( Point2D(get_width() * 0.05, get_height() * 0.95) ); m_viewflag = true; } // Draw the viewport set_colour( Colour(0.1, 0.1, 0.1) ); draw_line( m_viewport[0], m_viewport[1] ); draw_line( m_viewport[1], m_viewport[2] ); draw_line( m_viewport[2], m_viewport[3] ); draw_line( m_viewport[3], m_viewport[0] ); // Finish drawing draw_complete(); // Update the information bar update_infobar(); // Swap the contents of the front and back buffers so we see what we // just drew. This should only be done if double buffering is enabled. gldrawable->swap_buffers(); gldrawable->gl_end(); return true; }
void ConsoleWindow::reshape( ) { DisplayWindow::reshape(); UIRect &bw = _baseWindow; //these are all relative to basewindow coordinates! double pad = _marginSize; double pad2 = 2.0 * _marginSize; double cspan = max ( 1.5 * bw.h() - pad2, min ( 1.2, 0.4 * bw.w() ) ); _cubeWindow.moveto ( pad, -pad ); _cubeWindow.resize ( cspan - pad2 , bw.h() - pad2 ); _contentWindow.moveto ( _cubeWindow.right() + pad2 , _cubeWindow.top() ); _contentWindow.resize ( bw.w() - ( cspan + pad2 ) , _cubeWindow.h() ); if ( _content ) setContentArea(); _vScrollBar.moveto ( _contentWindow.right(), _contentWindow.top() ); _vScrollBar.resize ( pad2, _contentWindow.h() ); _hScrollBar.moveto ( _contentWindow.left(), _contentWindow.bottom() ); _hScrollBar.resize ( pad , _contentWindow.w() ); _sizeBox.moveto ( -pad , - bw.h() + pad ); _sizeBox.resize ( pad2, pad2 ); _curLabel.moveto ( cspan + pad, pad * 1.5 ); _curLabel.resize ( pad2 * 3.0, pad * 3.0 ); _curLabel.setLabel("current face"); _curLabel.fitLabel(); _curDisplay.moveto ( cspan - pad * 1.5, pad * 1.5 ); _curDisplay.resize ( pad * 3.0 , pad * 3.0 ); _prevLabel.resize ( pad2 * 3.0, pad2 ); _prevLabel.setLabel( "previous face" ); _prevLabel.fitLabel(); _prevLabel.moveto ( cspan - ( _prevLabel.w() + pad ) , - bw.h() + pad ); _prevDisplay.moveto ( cspan - pad * 1.5, -bw.h() + pad * 1.25 ); _prevDisplay.resize ( pad * 3.0 , pad * 2.5 ); _prevDisplay.setLabel("0"); double timex = cspan + 0.5 * ( bw.w() - cspan ); _timeDisplay.moveto ( timex + pad, pad ); _timeDisplay.resize ( pad2 * 3.0, pad2 ); _timeLabel.resize ( pad * 3.0 , pad * 3.0 ); _timeLabel.fitLabel ( ); _timeLabel.moveto ( timex - ( pad + _timeLabel.w() * 0.5 ) , pad * 1.5 ); _timeDisplay.setx( _timeLabel.right() - pad * 0.5 ); double kdim = min ( _cubeWindow.h() * 0.25 , _cubeWindow.w() * 0.125 ); for ( int i = 0 ; i < 6; i++ ) { //resize _cubeSides[i].resize( kdim, kdim ); } //this is always the same. //depending on the current face, each //cubeside will move to one of these positions... _cubePos[ Audicle::UP ] = Point2D ( kdim, 0 ); _cubePos[ Audicle::DOWN ] = Point2D ( kdim, -2.0 * kdim ); _cubePos[ Audicle::LEFT ] = Point2D ( 0, -kdim ); _cubePos[ Audicle::RIGHT ] = Point2D ( 2.0 * kdim, -kdim ); _cubePos[ Audicle::BACK ] = Point2D ( 3.0 * kdim, -kdim ); _cubePos[ Audicle::CUR ] = Point2D ( kdim, -kdim ); setCubes(); }
bool CanvasViewport::contains( Point2D p ) const { return p > location && p < Point2D( location.x + width, location.y + height ); }
void fantasma::mover_fantasma(){ borrar_fantasma(); choque_pacman(); float nearerDistance = calculateDistance(Point2D(_x*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); float newDistance = nearerDistance; float fuzzyRate = fuzzyReaction(nearerDistance); //printf("%f", fuzzyRate); int bolx = _x, boly = _y; if (mapa[_y][_x] == '|'){ fdir = rand() % 4; } if (mapa[_y][_x] == '*'){ fdir = 0; } // fuzzy Bonus if ((fuzzyRate > .50 && fuzzyRate <= 1.0) || (fuzzyRate > .25 && rand() % 10 == 3)){ for (int i = 0; i < 4; i++){ if (i == 2){ newDistance = calculateDistance(Point2D((_x+1)*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); if(mapa[_y][_x + 1] != 'X' && mapa[_y][_x + 1] != 'A' && mapa[_y][_x + 1] != 'Y' && mapa[_y][_x + 1] != 'B' && mapa[_y][_x + 1] != 'C' && mapa[_y][_x + 1] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } } if (i == 3){ newDistance = calculateDistance(Point2D((_x - 1)*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y][_x - 1] != 'X' && mapa[_y][_x - 1] != 'A' && mapa[_y][_x - 1] != 'Y' && mapa[_y][_x - 1] != 'B' && mapa[_y][_x - 1] != 'C' && mapa[_y][_x - 1] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } } if (i == 0){ newDistance = calculateDistance(Point2D(_x*1.0, (_y-1)*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y - 1][_x] != 'X' && mapa[_y - 1][_x] != 'A' && mapa[_y - 1][_x] != 'Y' && mapa[_y - 1][_x] != 'B' && mapa[_y - 1][_x] != 'C' && mapa[_y - 1][_x] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } } if (i == 1){ newDistance = calculateDistance(Point2D(_x*1.0, (_y + 1)*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y + 1][_x] != 'X' && mapa[_y + 1][_x] != 'A' && mapa[_y + 1][_x] != 'Y' && mapa[_y + 1][_x] != 'B' && mapa[_y + 1][_x] != 'C' && mapa[_y + 1][_x] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } } } } if (fdir == 2){ if (mapa[_y][_x + 1] != 'X' && mapa[_y][_x + 1] != 'A' && mapa[_y][_x + 1] != 'Y' && mapa[_y][_x + 1] != 'B' && mapa[_y][_x + 1] != 'C' && mapa[_y][_x + 1] != 'D') _x++; else fdir = rand() % 4; } if (fdir == 3){ newDistance = calculateDistance(Point2D((_x - 1)*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y][_x - 1] != 'X' && mapa[_y][_x - 1] != 'A' && mapa[_y][_x - 1] != 'Y' && mapa[_y][_x - 1] != 'B' && mapa[_y][_x - 1] != 'C' && mapa[_y][_x - 1] != 'D') _x--; else fdir = rand() % 4; } if (fdir == 0){ if (mapa[_y - 1][_x] != 'X' && mapa[_y - 1][_x] != 'A' && mapa[_y - 1][_x] != 'Y' && mapa[_y - 1][_x] != 'B' && mapa[_y - 1][_x] != 'C' && mapa[_y - 1][_x] != 'D') _y--; else fdir = rand() % 4; } if (fdir == 1){ if (mapa[_y + 1][_x] != 'X' && mapa[_y + 1][_x] != 'A' && mapa[_y + 1][_x] != 'Y' && mapa[_y + 1][_x] != 'B' && mapa[_y + 1][_x] != 'C' && mapa[_y + 1][_x] != 'D') _y++; else fdir = rand() % 4; } if (fuzzyRate < .50 || (rand() % 10 == 3 && fuzzyRate <= .75)){ if (fuzzyRate >.50 || (fuzzyRate > .25 && rand() % 10 == 3)){ for (int i = 0; i < 4; i++){ if (i == 2){ newDistance = calculateDistance(Point2D((_x + 1)*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y][_x + 1] != 'X' && mapa[_y][_x + 1] != 'A' && mapa[_y][_x + 1] != 'Y' && mapa[_y][_x + 1] != 'B' && mapa[_y][_x + 1] != 'C' && mapa[_y][_x + 1] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer > 0 && newDistance > nearerDistance){ fdir = i; } } else{ fdir = rand() % 4; } } if (i == 3){ newDistance = calculateDistance(Point2D((_x - 1)*1.0, _y*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y][_x - 1] != 'X' && mapa[_y][_x - 1] != 'A' && mapa[_y][_x - 1] != 'Y' && mapa[_y][_x - 1] != 'B' && mapa[_y][_x - 1] != 'C' && mapa[_y][_x - 1] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } else{ fdir = rand() % 4; } } if (i == 0){ newDistance = calculateDistance(Point2D(_x*1.0, (_y - 1)*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y - 1][_x] != 'X' && mapa[_y - 1][_x] != 'A' && mapa[_y - 1][_x] != 'Y' && mapa[_y - 1][_x] != 'B' && mapa[_y - 1][_x] != 'C' && mapa[_y - 1][_x] != 'D'){ if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } else{ fdir = rand() % 4; } } if (i == 1){ newDistance = calculateDistance(Point2D(_x*1.0, (_y + 1)*1.0), Point2D(x*1.0, y*1.0)); if (mapa[_y + 1][_x] != 'X' && mapa[_y + 1][_x] != 'A' && mapa[_y + 1][_x] != 'Y' && mapa[_y + 1][_x] != 'B' && mapa[_y + 1][_x] != 'C' && mapa[_y + 1][_x] != 'D') { if (pillTimer < 0 && newDistance < nearerDistance){ fdir = i; } else if (pillTimer >= 0 && newDistance > nearerDistance){ fdir = i; } } } } if (fdir == 2){ if (mapa[_y][_x + 1] != 'X' && mapa[_y][_x + 1] != 'A' && mapa[_y][_x + 1] != 'Y' && mapa[_y][_x + 1] != 'B' && mapa[_y][_x + 1] != 'C' && mapa[_y][_x + 1] != 'D') _x++; else fdir = rand() % 4; } if (fdir == 3){ if (mapa[_y][_x - 1] != 'X' && mapa[_y][_x - 1] != 'A' && mapa[_y][_x - 1] != 'Y' && mapa[_y][_x - 1] != 'B' && mapa[_y][_x - 1] != 'C' && mapa[_y][_x - 1] != 'D') _x--; else fdir = rand() % 4; } if (fdir == 0){ if (mapa[_y - 1][_x] != 'X' && mapa[_y - 1][_x] != 'A' && mapa[_y - 1][_x] != 'Y' && mapa[_y - 1][_x] != 'B' && mapa[_y - 1][_x] != 'C' && mapa[_y - 1][_x] != 'D') _y--; else fdir = rand() % 4; } if (fdir == 1){ if (mapa[_y + 1][_x] != 'X' && mapa[_y + 1][_x] != 'A' && mapa[_y + 1][_x] != 'Y' && mapa[_y + 1][_x] != 'B' && mapa[_y + 1][_x] != 'C' && mapa[_y + 1][_x] != 'D') _y++; else fdir = rand() % 4; } } } if (mapa[boly][bolx] == '_') { setCColor(color[1]); gotoxy(bolx, boly); printf("%c", 250); } // rutina atajo if (_x <= 17) _x = 61; else if (_x >= 62) _x = 18; dibujar_fantasma(); }
const Point2D Point2D::operator - (const Point2D& a_Value) const { return Point2D(x - a_Value.x, y - a_Value.y); }
Point2D Point2D::operator-(const Point2D &p) const { return Point2D(x - p.x, y - p.y); }
const Point2D Point2D::operator + (const double& a_Value) const { return Point2D(x + a_Value, y + a_Value); }
Point2D Point2D::operator* (float f) const { return Point2D(f * x, f * y); }
const Point2D Point2D::operator * (const double& a_Value) const { return Point2D(x * a_Value, y * a_Value); }
Point2D Point2D::operator/ (float f) const { assert(f!=0); float inv = 1.f/f; return Point2D(inv*x, inv*y); }
void Test() { int n = 5; Point2D* p = new Point2D[n]; p[0] = Point2D(0,1); p[1] = Point2D(1,0); p[2] = Point2D(3,0); p[3] = Point2D(3,1); p[4] = Point2D(2,2); int m = 6; Point2D* q = new Point2D[m]; q[0] = Point2D(4,1); q[1] = Point2D(6,0); q[2] = Point2D(7,0); q[3] = Point2D(8,1); q[4] = Point2D(7,2); q[5] = Point2D(5,2); int t0[2], t1[2]; CommonTangents(p,n,q,m,t0,t1); printf("Common Tangents:\n"); printf("(%d,%d)\n",t0[0],t1[0]); printf("(%d,%d)\n",t0[1],t1[1]); }
/* -------------------------------------------------------------------------------------------------- - constructor -------------------------------------------------------------------------------------------------- */ PlanesPhysicHandler::PlanesPhysicHandler(const std::string filename, LocalActorsHandler * LAH, ExternalActorsHandler * EAH) : _localAH(LAH), _externalAH(EAH) { std::ifstream file(filename.c_str()); if(!file.is_open()) return; int sizePlanes, sizewallX, sizewallZ, sizeStairs, sizecornerStairs; file>>sizePlanes; file>>sizewallX; file>>sizewallZ; file>>sizeStairs; file>>sizecornerStairs; for(int i=0; i<sizePlanes; ++i) { NormalPlane np; file>>np.Layer; float sx, sz, ex, ez; file>>sx; file>>sz; file>>ex; file>>ez; np.Square._minX = MIN(sx, ex); np.Square._maxX = MAX(sx, ex); np.Square._minZ = MIN(sz, ez); np.Square._maxZ = MAX(sz, ez); file>>np.IsWater; _floors[np.Layer].push_back(np); } for(int i=0; i<sizewallX; ++i) { NormalPlane np; file>>np.Layer; float sx, sz, ex, ez; file>>sx; file>>sz; file>>ex; file>>ez; np.Square._minX = MIN(sx, ex); np.Square._maxX = MAX(sx, ex); np.Square._minZ = MIN(sz, ez); np.Square._maxZ = MAX(sz, ez); _wallsX[np.Layer].push_back(np); } for(int i=0; i<sizewallZ; ++i) { NormalPlane np; file>>np.Layer; float sx, sz, ex, ez; file>>sx; file>>sz; file>>ex; file>>ez; np.Square._minX = MIN(sx, ex); np.Square._maxX = MAX(sx, ex); np.Square._minZ = MIN(sz, ez); np.Square._maxZ = MAX(sz, ez); _wallsZ[np.Layer].push_back(np); } for(int i=0; i<sizeStairs; ++i) { StairPlane sp; file>>sp.C1.x; file>>sp.C1.y; file>>sp.C1.z; file>>sp.C2.x; file>>sp.C2.y; file>>sp.C2.z; file>>sp.C3.x; file>>sp.C3.y; file>>sp.C3.z; file>>sp.C4.x; file>>sp.C4.y; file>>sp.C4.z; sp.Normal = ((sp.C2 - sp.C1).cross(sp.C3-sp.C1)).unit(); sp.D = sp.Normal.dot(sp.C1); // calculate min max X { sp.minX = sp.C1.x; sp.maxX = sp.C1.x; if(sp.C2.x > sp.maxX) sp.maxX = sp.C2.x; if(sp.C2.x < sp.minX) sp.minX = sp.C2.x; if(sp.C3.x > sp.maxX) sp.maxX = sp.C3.x; if(sp.C3.x < sp.minX) sp.minX = sp.C3.x; if(sp.C4.x > sp.maxX) sp.maxX = sp.C4.x; if(sp.C4.x < sp.minX) sp.minX = sp.C4.x; } // calculate min max Z { sp.minZ = sp.C1.z; sp.maxZ = sp.C1.z; if(sp.C2.z > sp.maxZ) sp.maxZ = sp.C2.z; if(sp.C2.z < sp.minZ) sp.minZ = sp.C2.z; if(sp.C3.z > sp.maxZ) sp.maxZ = sp.C3.z; if(sp.C3.z < sp.minZ) sp.minZ = sp.C3.z; if(sp.C4.z > sp.maxZ) sp.maxZ = sp.C4.z; if(sp.C4.z < sp.minZ) sp.minZ = sp.C4.z; } _stairs.push_back(sp); } for(int i=0; i<sizecornerStairs; ++i) { CornerStairPlane sp; file>>sp.C1.x; file>>sp.C1.y; file>>sp.C1.z; file>>sp.C2.x; file>>sp.C2.y; file>>sp.C2.z; file>>sp.C3.x; file>>sp.C3.y; file>>sp.C3.z; sp.tr_wh_y = Triangle2D(Point2D(sp.C1.x, sp.C1.z), Point2D(sp.C2.x, sp.C2.z), Point2D(sp.C3.x, sp.C3.z)); sp.Normal = ((sp.C2 - sp.C1).cross(sp.C3-sp.C1)).unit(); _corner_stairs.push_back(sp); } }
Truck::Truck():Car() { setPosition(Point2D(10, 20)); }
// **************************************************************************** // draws the string centred on cds void MolDraw2DSVG::drawString(const std::string &str, const Point2D &cds) { unsigned int fontSz = scale() * fontSize(); double string_width, string_height; getStringSize(str, string_width, string_height); double draw_x = cds.x - string_width / 2.0; double draw_y = cds.y - string_height / 2.0; #if 0 // for debugging text output DrawColour tcolour =colour(); setColour(DrawColour(.8,.8,.8)); std::vector<Point2D> poly; poly.push_back(Point2D(draw_x,draw_y)); poly.push_back(Point2D(draw_x+string_width,draw_y)); poly.push_back(Point2D(draw_x+string_width,draw_y+string_height)); poly.push_back(Point2D(draw_x,draw_y+string_height)); drawPolygon(poly); setColour(tcolour); #endif std::string col = DrawColourToSVG(colour()); Point2D draw_coords = getDrawCoords(Point2D(draw_x, draw_y)); d_os << "<svg:text"; d_os << " x='" << draw_coords.x; d_os << "' y='" << draw_coords.y << "'"; d_os << " style='font-size:" << fontSz << "px;font-style:normal;font-weight:normal;fill-opacity:1;stroke:none;" "font-family:sans-serif;text-anchor:start;" << "fill:" << col << "'"; d_os << " >"; TextDrawType draw_mode = TextDrawNormal; // 0 for normal, 1 for superscript, 2 for subscript std::string span; bool first_span = true; for (int i = 0, is = str.length(); i < is; ++i) { // setStringDrawMode moves i along to the end of any <sub> or <sup> // markup if ('<' == str[i] && setStringDrawMode(str, draw_mode, i)) { if (!first_span) { d_os << span << "</svg:tspan>"; span = ""; } first_span = false; d_os << "<svg:tspan"; switch (draw_mode) { case TextDrawSuperscript: d_os << " style='baseline-shift:super;font-size:" << fontSz * 0.75 << "px;" << "'"; break; case TextDrawSubscript: d_os << " style='baseline-shift:sub;font-size:" << fontSz * 0.75 << "px;" << "'"; break; default: break; } d_os << ">"; continue; } if (first_span) { first_span = false; d_os << "<svg:tspan>"; span = ""; } span += str[i]; } d_os << span << "</svg:tspan>"; d_os << "</svg:text>\n"; }
Point2D Point2D::operator/ (const double rhs) const { return Point2D(x/rhs,y/rhs,w/rhs); }
Point2D Spiral::getPointFromRadius(Point2D center, float radius, float angle) { float x = center.x + radius * cos((double) angle * MY_PI / 180), y = center.y - radius * sin((double) angle * MY_PI / 180); return Point2D(x, y); }
Point2D Point2D::operator- (const Point2D& rhs) const { return Point2D(x-rhs.x,y-rhs.y); }
const Point2D Point2D::operator + (const Point2D& a_Value) const { return Point2D(x + a_Value.x, y + a_Value.y); }
void LocalizationModule::processFrame() { auto& ball = cache_.world_object->objects_[WO_BALL]; auto& self = cache_.world_object->objects_[cache_.robot_state->WO_SELF]; // Process the current frame and retrieve our location/orientation estimate // from the particle filter pfilter_->processFrame(); self.loc = pfilter_->pose().translation; self.orientation = pfilter_->pose().rotation; log(40, "Localization Update: x=%2.f, y=%2.f, theta=%2.2f", self.loc.x, self.loc.y, self.orientation * RAD_T_DEG); if(ball.seen) { // Compute the relative position of the ball from vision readings auto relBall = Point2D::getPointFromPolar(ball.visionDistance, ball.visionBearing); auto belief = ball_filter_.x; if (ball.visionDistance > 5000) { seen_last_frame_ = false; frames_since_last_seen_ ++; ball_filter_.predict(); } else { BallFilter::MeasurementVector observation; observation(0) = relBall.x; observation(1) = relBall.y; // if (seen_last_frame_) // { // observation(2) = (relBall.x - belief(0)) * 1.0 + belief(2) * 0.0; // observation(3) = (relBall.y - belief(1)) * 1.0 + belief(3) * 0.0; // } // else // { // observation(2) = belief(2); // observation(3) = belief(3); // } if (frames_since_last_seen_ < SIGHT_THRESHOLD) { observation(2) = (relBall.x - last_seen_ball.x) / (frames_since_last_seen_ + 1); observation(3) = (relBall.y - last_seen_ball.y) / (frames_since_last_seen_ + 1); } else { observation(2) = 0; observation(3) = 0; } frames_since_last_seen_ = 0; seen_last_frame_ = true; last_seen_ball.x = relBall.x; last_seen_ball.y = relBall.y; // std::cout << "Observation: " << observation(0) << ", " << observation(1) << ", " << observation(2) << ", " << observation(3) << std::endl; ball_filter_.predict(); ball_filter_.correct(observation); } } else { frames_since_last_seen_ ++; seen_last_frame_ = false; ball_filter_.predict(); } auto new_belief = ball_filter_.x; // std::cout << "New State: " << new_belief(0) << ", " << new_belief(1) << ", " << new_belief(2) << ", " << new_belief(3)<< std::endl; auto relBall = Point2D(new_belief(0), new_belief(1)); auto relVel = Point2D(new_belief(2), new_belief(3)); // std::cout << self.loc.x << ", " << self.loc.y << std::endl; auto globalBall = relBall.relativeToGlobal(self.loc, self.orientation); auto globalVel = relVel; globalVel.rotate(self.orientation); // Update the ball in the WorldObject block so that it can be accessed in python ball.loc = globalBall; ball.relPos = relBall; ball.distance = relBall.getMagnitude(); //ball.visionDistance; ball.bearing = relBall.getDirection(); //ball.visionBearing; ball.absVel = globalVel; ball.relVel = relVel; // Update the localization memory objects with localization calculations // so that they are drawn in the World window cache_.localization_mem->state[0] = ball.loc.x; cache_.localization_mem->state[1] = ball.loc.y; cache_.localization_mem->covariance = ball_filter_.P.topLeftCorner<2, 2>() * 10000; }
void GLBox::bresenhamCircle(Point2D center, int radius, Color color) { int x = 0; int y = radius; int d = 5 - 4*radius; setPoint(Point2D(x + center.x,y + center.y),color); setPoint(Point2D(x + center.x,-y + center.y),color); setPoint(Point2D(y + center.x,-x + center.y), color); setPoint(Point2D(-y + center.x,-x + center.y),color); setPoint(Point2D(-x + center.x,-y + center.y),color); setPoint(Point2D(-x + center.x,y + center.y),color); setPoint(Point2D(-y + center.x,x + center.y),color); setPoint(Point2D(y + center.x,x + center.y),color); while(y > x) { if (d <= 0) { d += 4*(2*x + 3); } else { d += 4*(2*(x - y) + 5); y--; } x++; setPoint(Point2D(x + center.x,y + center.y),color); setPoint(Point2D(x + center.x,-y + center.y),color); setPoint(Point2D(y + center.x,-x + center.y), color); setPoint(Point2D(-y + center.x,-x + center.y),color); setPoint(Point2D(-x + center.x,-y + center.y),color); setPoint(Point2D(-x + center.x,y + center.y),color); setPoint(Point2D(-y + center.x,x + center.y),color); setPoint(Point2D(y + center.x,x + center.y),color); } }