




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
...wd......wd......wd...實驗一追逐與攔截實驗報告一、實驗目的掌握游戲中追逐與攔截的人工智能算法二、實驗儀器Windows7系統(tǒng)MicrosoftVisualStudio2015三、實驗原理及過程//描述追逐與攔截的算法原理//描述程序實現(xiàn)時的思路包括對每個調用的API進展詳細說明〔1〕描述追逐與攔截的算法原理:連續(xù)環(huán)境中的視線追逐是最簡單的追逐算法,但是追逐者的移動不僅有線速度,而且還有角速度。算法思路就是:首先根據(jù)角速度把方向轉到視線方向,然后向目標追過去。完整追逐/閃躲由三局部組成:首先,作出追或逃的決策判斷。其次,開場追或逃〔本章重點〕最后,避開障礙物。攔截算法的根本原理是可以預測獵物未來的位置,然后直接到那個位置去,讓追擊者和獵物同時到達同一個位置。為了找出追擊者和獵物能同時到達的點,不僅要考慮他們的移動方向,還要考慮他們的速度。〔2〕Main和winmain進展函數(shù)的定義。RigidBody2D類:進展物體的質量,慣性,關系,坐標,長高寬即外形的定義,用向量表示他們移動的方向。UpdateSimulation函數(shù)對于物體1,2的移動進展反響和控制。DoCraft2Chase函數(shù)對于物體1,2追逐進展判斷,DoCraft2Evade函數(shù)對于物體1,2躲避進展判斷。DoCraft2InterceptAlt函數(shù)對于物體1,2攔截進展判斷。DoAttractCraft2函數(shù)判斷是否攻擊。四、實驗結果五、實驗心得(需包括有何缺乏如何改良)我認為目前的追逐與攔截的缺乏之處在于:本次實驗做的是連續(xù)環(huán)境中的視線追逐與攔截。相比磚塊環(huán)境中的追逐與攔截,肯定是要靈活變通很多,但是箭頭老要跑到屏幕外面去,這就非常為難了。獵物的速度向量和初始位置向量是固定的,而且靠攏時間的計算是需要相對位移和相對速度的,容易得到兩者不相遇的為難情況。如何改良:計算采用靠攏時間的方法,此外,再想方法讓箭頭留在屏幕以內,這樣視覺感受會比擬好。六、主要代碼main.cpp#include"main.h"#include"time.h"http://---------------------------------------------------------------------------/*Book:AIforGameDevelopersAuthors:DavidM.Bourg&GlennSeemannExample:Chasingandevadingincontinuousenvironments,Chapter2*///---------------------------------------------------------------------------//GlobalVariables:int FrameCounter=0;RigidBody2D Craft1,Craft2;Vector Target;#define _TIMESTEP 0.001#define _TOL 1e-10#define _FWDTIME 10#define _THRUSTFACTOR 3#define_CHASESETUP truebool Initialize(void){ Craft1.fMass=10; Craft1.fInertia=10; Craft1.fInertiaInverse=1/10; Craft1.vPosition.x=_WINWIDTH-60; Craft1.vPosition.y=_WINHEIGHT*0.8; Craft1.fWidth=10; Craft1.fLength=20; Craft1.fHeight=5; Craft1.fOrientation=135; Craft1.CD.y=-0.12*Craft1.fLength; Craft1.CD.x=0.0f; //coordinatesofthebodycenterofdrag Craft1.CT.y=-0.50*Craft1.fLength; Craft1.CT.x=0.0f; //coordinatesofthepropellerthrustvector Craft1.CPT.y=0.5*Craft1.fLength; Craft1.CPT.x=-0.5*Craft1.fWidth; //coordinatesoftheportbowthruster Craft1.CST.y=0.5*Craft1.fLength; Craft1.CST.x=0.5*Craft1.fWidth; //coordinatesofthestarboardbowthruster Craft1.ProjectedArea=(Craft1.fLength+Craft1.fWidth)*Craft1.fHeight; Craft1.ThrustForce=_THRUSTFORCE*1; Craft2.fMass=10; Craft2.fInertia=10; Craft2.fInertiaInverse=1/10; if(_CHASESETUP) { Craft2.vPosition.x=40; Craft2.vPosition.y=_WINHEIGHT*0.8; }else{ Craft2.vPosition.x=Craft1.vPosition.x-Craft1.fLength*8; Craft2.vPosition.y=Craft1.vPosition.y-Craft1.fLength*4; } Craft2.fWidth=10; Craft2.fLength=20; Craft2.fHeight=5; if(_CHASESETUP) Craft2.fOrientation=-135; else Craft2.fOrientation=135; Craft2.CD.y=-0.12*Craft2.fLength; Craft2.CD.x=0.0f; //coordinatesofthebodycenterofdrag Craft2.CT.y=-0.50*Craft2.fLength; Craft2.CT.x=0.0f; //coordinatesofthepropellerthrustvector Craft2.CPT.y=0.5*Craft2.fLength; Craft2.CPT.x=0.5*Craft2.fWidth; //coordinatesoftheportbowthruster Craft2.CST.y=0.5*Craft2.fLength; Craft2.CST.x=-0.5*Craft2.fWidth; //coordinatesofthestarboardbowthruster Craft2.ProjectedArea=(Craft2.fLength+Craft2.fWidth)*Craft2.fHeight; Craft2.ThrustForce=_THRUSTFORCE*_THRUSTFACTOR; returntrue;}void UpdateSimulation(void){ double dt=_TIMESTEP; RECT r; Craft1.SetThrusters(false,false); if(IsKeyDown(VK_UP)) Craft1.ModulateThrust(true); if(IsKeyDown(VK_DOWN)) Craft1.ModulateThrust(false); if(IsKeyDown(VK_RIGHT)) Craft1.SetThrusters(true,false); if(IsKeyDown(VK_LEFT)) Craft1.SetThrusters(false,true); //Docraft2AI Craft2.Fa.x=0; Craft2.Fa.y=0; Craft2.Pa.x=0; Craft2.Pa.y=0; if(BasicChase) { DoCraft2Chase(); DoCraft2ModulateThrust(); } if(BasicEvade) DoCraft2Evade(); if(InterceptChase) { //DoCraft2Intercept(); //DoCraft2ModulateThrust(); DoCraft2InterceptAlt(); } if(PotentialChase) DoAttractCraft2(); //updatethesimulation Craft1.UpdateBodyEuler(dt); Craft2.UpdateBodyEuler(dt); if(FrameCounter>=_RENDER_FRAME_COUNT) { //updatethedisplay if(!ShowTrails) ClearBackBuffer(); DrawCraft(Craft1,RGB(0,0,255)); DrawCraft(Craft2,RGB(255,0,0)); RECT r; r.left=(int)(Target.x-3); r.top=(int)(Target.y-3); r.right=(int)(Target.x+3); r.bottom=(int)(Target.y+3); DrawEllipse(&r,1,RGB(0,255,0)); CopyBackBufferToWindow(); FrameCounter=0; }else FrameCounter++; if(Craft1.vPosition.x>_WINWIDTH)Craft1.vPosition.x=0; if(Craft1.vPosition.x<0)Craft1.vPosition.x=_WINWIDTH; if(Craft1.vPosition.y>_WINHEIGHT)Craft1.vPosition.y=0; if(Craft1.vPosition.y<0)Craft1.vPosition.y=_WINHEIGHT; if(Craft2.vPosition.x>_WINWIDTH)Craft2.vPosition.x=0; if(Craft2.vPosition.x<0)Craft2.vPosition.x=_WINWIDTH; if(Craft2.vPosition.y>_WINHEIGHT)Craft2.vPosition.y=0; if(Craft2.vPosition.y<0)Craft2.vPosition.y=_WINHEIGHT;}void DrawCraft(RigidBody2D craft,COLORREFclr){ Vector vList[5]; double wd,lg; int i; Vector v1; wd=craft.fWidth; lg=craft.fLength; vList[0].y=lg/2; vList[0].x=wd/2; vList[1].y=-lg/2; vList[1].x=wd/2; vList[2].y=-lg/2; vList[2].x=-wd/2; vList[3].y=lg/2; vList[3].x=-wd/2; vList[4].y=lg/2*1.5;vList[4].x=0; for(i=0;i<5;i++) { v1=VRotate2D(craft.fOrientation,vList[i]); vList[i]=v1+craft.vPosition; } DrawLine(vList[0].x,vList[0].y,vList[1].x,vList[1].y,2,clr); DrawLine(vList[1].x,vList[1].y,vList[2].x,vList[2].y,2,clr); DrawLine(vList[2].x,vList[2].y,vList[3].x,vList[3].y,2,clr); DrawLine(vList[3].x,vList[3].y,vList[4].x,vList[4].y,2,clr); DrawLine(vList[4].x,vList[4].y,vList[0].x,vList[0].y,2,clr); if(ShowVectors) { Vector v,u; double f=5; //Showvelocityvectorsingreen DrawLine(craft.vPosition.x,craft.vPosition.y,craft.vPosition.x+craft.vVelocity.x,craft.vPosition.y+craft.vVelocity.y,3,RGB(0,255,0)); //Showforcevectorsinblack //thrustvector v.x=0; v.y=craft.ThrustForce*f; v=VRotate2D(craft.fOrientation,v); u.x=craft.CT.x; u.y=craft.CT.y; u=VRotate2D(craft.fOrientation,u); DrawLine(craft.vPosition.x+u.x,craft.vPosition.y+u.y,craft.vPosition.x+u.x+v.x,craft.vPosition.y+u.y+v.y,1,RGB(0,0,0)); //portsteeringforce v.x=craft.PThrust.x*f; v.y=craft.PThrust.y*f; v=VRotate2D(craft.fOrientation,v); u.x=craft.CPT.x; u.y=craft.CPT.y; u=VRotate2D(craft.fOrientation,u); DrawLine(craft.vPosition.x+u.x,craft.vPosition.y+u.y,craft.vPosition.x+u.x+v.x,craft.vPosition.y+u.y+v.y,1,RGB(0,0,0)); //stbdsteeringforce v.x=craft.SThrust.x*f; v.y=craft.SThrust.y*f; v=VRotate2D(craft.fOrientation,v); u.x=craft.CST.x; u.y=craft.CST.y; u=VRotate2D(craft.fOrientation,u); DrawLine(craft.vPosition.x+u.x,craft.vPosition.y+u.y,craft.vPosition.x+u.x+v.x,craft.vPosition.y+u.y+v.y,1,RGB(0,0,0)); //appliedforce v.x=craft.Fa.x*f; v.y=craft.Fa.y*f; v=VRotate2D(craft.fOrientation,v); u.x=craft.Pa.x; u.y=craft.Pa.y; u=VRotate2D(craft.fOrientation,u); DrawLine(craft.vPosition.x+u.x,craft.vPosition.y+u.y,craft.vPosition.x+u.x+v.x,craft.vPosition.y+u.y+v.y,1,RGB(0,0,0)); }}void DoCraft2Chase(void){ Vector u,v; bool p=false; bool s=false; u=VRotate2D(-Craft2.fOrientation,(Craft1.vPosition-Craft2.vPosition)); u.Normalize(); Target=Craft1.vPosition; if(u.x<-_TOL) p=true; elseif(u.x>_TOL) s=true; Craft2.SetThrusters(p,s);}void DoCraft2Evade(void){ Vector u,v; bool p=false; bool s=false; u=VRotate2D(-Craft2.fOrientation,(Craft1.vPosition-Craft2.vPosition)); u.Normalize(); if(u.x>0) p=true; elseif(u.x<0) s=true; Craft2.SetThrusters(p,s); Target=Craft2.vPosition;}void DoCraft2Intercept(void){ Vector u1,u2,u; Vector s1,s2; VectorVr; double t1,t2; Vector s1unit,s2unit; bool p=false; bool s=false; Vr=Craft1.vVelocity-Craft2.vVelocity; s2=GetVelocityIntersection()-Craft2.vPosition; s2unit=s2; s2unit.Normalize(); u2=VRotate2D(-Craft2.fOrientation,s2); t2=s2.Magnitude()/(Vr*s2unit); s1=Craft1.vPosition-Craft2.vPosition; s1unit=s1; s1unit.Normalize(); u1=VRotate2D(-Craft2.fOrientation,s1); t1=s1.Magnitude()/(Vr*s1unit); if(t1<0.0) { u=u2; Target=s2+Craft2.vPosition; }elseif(t2<0.0){ u=u1; Target=s1+Craft2.vPosition; }elseif(t2<t1) { u=u2; Target=s2+Craft2.vPosition; }else{ u=u1; Target=s1+Craft2.vPosition; } u.Normalize(); if(u.x<-_TOL) p=true; elseif(u.x>_TOL) s=true; Craft2.SetThrusters(p,s);}void DoCraft2InterceptAlt(void){ Vector u; Vector s1,s2,s12; bool p=false; bool s=false; double tClose; Vector Vr12; double vr; //turnaroundifwegetaheadoftheprey... s12=Craft1.vPosition-Craft2.vPosition; u=VRotate2D(-Craft2.fOrientation,s12); if(u.y<-_TOL) { //if(GetRandomNumber(0,10,true)<5) p=true; //else // s=true; Craft2.SetThrusters(p,s); Target=Craft2.vPosition; return; } Vr12=Craft1.vVelocity-Craft2.vVelocity;//closingvelocity s12=Craft1.vPosition-Craft2.vPosition;//rangetoclose tClose=s12.Magnitude()/Vr12.Magnitude();//timetoclose s1=Craft1.vPosition+(Craft1.vVelocity*tClose); Target=s1; s2=s1-Craft2.vPosition; u=VRotate2D(-Craft2.fOrientation,s2); u.Normalize(); if(u.x<-_TOL) p=true; elseif(u.x>_TOL) s=true; Craft2.SetThrusters(p,s);}void DoAttractCraft2(void){ //ApplyLenard-JonespotentialforcetoCraft2 Vector r=Craft2.vPosition-Craft1.vPosition; Vectoru=r; u.Normalize(); doubleU,A,B,n,m,d; A=2000; B=4000; n=2; m=3; d=r.Magnitude()/Craft2.fLength; U=-A/pow(d,n)+B/pow(d,m); Craft2.Fa=VRotate2D(-Craft2.fOrientation,U*u); Craft2.Pa.x=0; Craft2.Pa.y=Craft2.fLength/2; Target=Craft1.vPosition;}Vector GetVelocityIntersection(void){ doubles,t,num,denom; Vector a,b,c,d; a=Craft1.vPosition; b=a+Craft1.vVelocity; c=Craft2.vPosition; d=c+Craft2.vVelocity; denom=a.x*(d.y-c.y)+ b.x*(c.y-d.y)+ d.x*(b.y-a.y)+ c.x*(a.y-b.y); if(denom==0) returnVector(a.x,a.y,0); num= a.x*(d.y-c.y)+ c.x*(a.y-d.y)+ d.x*(c.y-a.y); s=num/denom; num= -(a.x*(c.y-b.y)+ b.x*(a.y-c.y)+ c.x*(b.y-a.y)); t=num/denom; if((s>=0)&&(t>=0)) returnVector(a.x+s*(b.x-a.x),a.y+s*(b.y-a.y),0); else returnVector(a.x,a.y,0);}intGetRandomNumber(intmin,intmax,boolseed){ int number; if(seed) srand((unsigned)time(NULL));number=(((abs(rand())%(max-min+1))+min));if(number>max) number=max;if(number<min) number=min; returnnumber;}void DoCraft2ModulateThrust(void){ Vector r=Craft1.vPosition-Craft2.vPosition; double dmax=Craft2.fLength*10; if((Craft2.PThrust.Magnitude()>0)||(Craft2.SThrust.Magnitude()>0))//turning { if(r.Magnitude()>dmax) Craft2.ThrustForce=_MAXTHRUST; else Craft2.ThrustForce=r.Magnitude()/dmax*_MAXTHRUST; }else{ //todo:checkhowclosewearetotargetandadjustspeedtostaywithit Craft2.ThrustForce=_MAXTHRUST; }}RigidBody2D.cpp#include"RigidBody2D.h"RigidBody2D::RigidBody2D(void){}voidRigidBody2D::CalcLoads(void){Vector Fb; //storesthesumofforcesVector Mb; //storesthesumofmomentsVector Thrust; //thrustvector//resetforcesandmoments: vForces.x=0.0f; vForces.y=0.0f; vForces.z=0.0f; //alwayszeroin2D vMoment.x=0.0f; //alwayszeroin2D vMoment.y=0.0f; //alwayszeroin2D vMoment.z=0.0f; Fb.x=0.0f; Fb.y=0.0f; Fb.z=0.0f; Mb.x=0.0f; Mb.y=0.0f; Mb.z=0.0f;//Definethethrustvector,whichactsthroughthecraft'sCG Thrust.x=0.0f; Thrust.y=1.0f; Thrust.z=0.0f;//zeroin2D Thrust*=ThrustForce;//Calculateforcesandmomentsinbodyspace:Vector vLocalVelocity;float fLocalSpeed;Vector vDragVector; float tmp;Vector vResultant; Vector vtmp; //Calculatetheaerodynamicdragforce://Calculatelocalvelocity://Thelocalvelocityincludesthevelocityduetolinearmotionofthecraft,//plusthevelocityateachelementduetotherotationofthecraft. vtmp=vAngularVelocity^CD;//rotationalpart vLocalVelocity=vVelocityBody+vtmp;//Calculatelocalairspeed fLocalSpeed=vLocalVelocity.Magnitude();//Findthedirectioninwhichdragwillact.//Dragalwaysactsinlinewiththerelativevelocitybutintheopposingdirectionif(fLocalSpeed>tol) { vLocalVelocity.Normalize(); vDragVector=-vLocalVelocity; //Determinetheresultantforceontheelement.doublef;if((Thrust*vLocalVelocity)/(Thrust.Magnitude()*vLocalVelocity.Magnitude())>0) f=2; else f=1; tmp=0.5f*rho*fLocalSpeed*fLocalSpeed*ProjectedArea*f; vResultant=vDragVector*_LINEARDRAGCOEFFICIENT*tmp;//simulatefuselagedrag//Keeparunningtotaloftheseresultantforces(totalforce) Fb+=vResultant;//CalculatethemomentabouttheCGofthiselement'sforce//andkeeparunningtotalofthesemoments(totalmoment) vtmp=CD^vResultant; Mb+=vtmp; }//CalculatethePort&Starboardbowthrusterforces://Keeparunningtotaloftheseresultantforces(totalforce) Fb+=3*PThrust;//CalculatethemomentabouttheCGofthiselement'sforce//andkeeparunningtotalofthesemoments(totalmoment) vtmp=CPT^PThrust; Mb+=vtmp;//Keeparunningtotaloftheseresultantforces(totalforce) Fb+=3*SThrust;//CalculatethemomentabouttheCGofthiselement'sforce//andkeeparunningtotalofthesemoments(totalmoment) vtmp=CST^SThrust; Mb+=vtmp;//dootherappliedforceshere Fb+=Fa; vtmp=Pa^Fa; Mb+=vtmp;//Calculaterotationaldragif(vAngularVelocity.Magnitude()>tol) { vtmp.x=0; vtmp.y=0; tmp=0.5f*rho*vAngularVelocity.z*vAngularVelocity.z*
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025建筑工程合同書 范本
- 2025農產品加工類合同參考格式
- 2025股票交易合同范本
- 2025年中文版國際貿易合同模板
- 2025商務英語合同的語法與用詞特點
- 2025標準租賃合同協(xié)議書范本
- 2025租賃合同法要點解析
- 2025的建筑材料購銷合同
- 2025瓷磚批發(fā)銷售合同范本
- 《應力分析及其計算方法》課件
- 2025屆山東省濟南市高三下學期一模英語試題(原卷版+解析版)
- 2025年(四川)公需科目(心理健康與職業(yè)發(fā)展主題)題庫及答案
- 肺功能課件完整版本
- 2025年河南省洛陽市洛寧縣中考一模道德與法治試題(含答案)
- 2025年蘭考三農職業(yè)學院高職單招職業(yè)適應性測試歷年(2019-2024年)真題考點試卷含答案解析
- 掘進爆破、爆破安全知識
- 《計算機網(wǎng)絡基礎》課件-OSI參考模型
- 2025年吉林省長春市中考一模歷史模擬試題(含答案)
- 貴州民族建筑知到智慧樹章節(jié)測試課后答案2024年秋貴州民族大學
- 2022年全國森林、草原、濕地調查監(jiān)測技術規(guī)程-附錄
- 【數(shù)學】第1課時 不等式的性質教學設計++2024-2025學年人教版數(shù)學七年級下冊
評論
0/150
提交評論