您好,欢迎访问三七文档
[RoboticsStudio]继续修改迷宫,加上机器人--Day262009/1/1617:51|阅读数:4829|我要推荐|3Comments|订阅嗯,之前的迷宫产生程式有点小bug,就是在分割房间的时候,如果产生的墙壁刚好挡住之前挖开的洞,这个迷宫就有可能会不通...所以加上一点小修改来弥补这样的错误,最后整个MazeEntity的程式如下(我直接把注解写在code当中了):001usingSystem;002usingSystem.Collections.Generic;003usingSystem.ComponentModel;004usingMicrosoft.Ccr.Core;005usingMicrosoft.Dss.Core.Attributes;006usingMicrosoft.Dss.ServiceModel.Dssp;007usingMicrosoft.Dss.ServiceModel.DsspServiceBase;008usingW3C.Soap;009usingsubmgr=Microsoft.Dss.Services.SubscriptionManager;010usingengine=Microsoft.Robotics.Simulation.Engine.Proxy;011usingMicrosoft.Robotics.Simulation.Engine;012usingMicrosoft.Robotics.Simulation.Physics;013usingMicrosoft.Robotics.PhysicalModel;014015016namespaceMazeGenerator017{018publicclassMazeEntity:MultiShapeEntity019{020///summary021///Defaultconstructor022////summary023publicMazeEntity(){}024025///summary026///Initializationconstructor027////summary028///paramname=shape/param029///paramname=initialPos/param030publicMazeEntity(Vector3initialPos,byte[,]blocks,floatmazeheight,floatblocksize)031{032for(intx=0;xblocks.GetLength(0);x++)033{034for(inty=0;yblocks.GetLength(1);y++)035{036if(blocks[x,y]==0)037continue;038039varboxshape=newBoxShape(040newBoxShapeProperties(041100,//massinkilograms.042newPose(newVector3(initialPos.X+x*blocksize,initialPos.Y+mazeheight/2,initialPos.Z-y*blocksize)),//relativepose043newVector3(blocksize,mazeheight,blocksize)));044045this.BoxShapes.Add(boxshape);//dimensions046}047}048049this.State.MassDensity.Mass=blocks.GetLength(0)*blocks.GetLength(1)*100;050}051052publicstaticbyte[,]GenerateMaze(intwidth,intheight)053{054byte[,]result=newbyte[width,height];055Randomr=newRandom();056for(intx=0;xwidth;x++)057for(inty=0;yheight;y++)058result[x,y]=(byte)(((x==0)||(y==0)||(x==width-1)||(y==height-1))?1:0);059060//digaholeofleft-top,andright-down061result[0,1]=0;062result[width-1,height-2]=0;063064splitMazeChamber(r,result,0,0,width-1,height-1);065066returnresult;067}068069///summary070///将一个房间切割为迷宫,前提是这个房间四周都是墙壁,但是会有一些通道,连到其他房间071////summary072///paramname=r/param073///paramname=maze/param074///paramname=left/param075///paramname=top/param076///paramname=right/param077///paramname=bottom/param078privatestaticvoidsplitMazeChamber(Randomr,byte[,]maze,intleft,inttop,intright,intbottom)079{080intwallX=0;081if(right-left3)082wallX=r.Next(left+2,right-1);083084intwallY=0;085if(bottom-top3)086wallY=r.Next(top+2,bottom-1);087088//停止条件-没得切割房间089if((wallX=0)&&(wallY=0))090return;091092//建造墙壁来隔开房间093if(wallX0)094{095for(inti=top+1;ibottom;i++)096maze[wallX,i]=1;097098//假如这道墙壁刚好盖在外部的通道,就拆掉一格墙壁099if(maze[wallX,top]==0)100maze[wallX,top+1]=0;101if(maze[wallX,bottom]==0)102maze[wallX,bottom-1]=0;103}104105if(wallY0)106{107for(inti=left+1;iright;i++)108maze[i,wallY]=1;109110//假如这道墙壁刚好盖在外部的通道,就拆掉一格墙壁111if(maze[left,wallY]==0)112maze[left+1,wallY]=0;113if(maze[right,wallY]==0)114maze[right-1,wallY]=0;115}116117//打通各个房间,然后继续切割每个房间118if((wallX0)&&(wallY0))119{120ListKeyValuePairint,intholes=newListKeyValuePairint,int();12holes.Add(newKeyValuePairint,in1t(wallX,r.Next(top+1,wallY-1)));122holes.Add(newKeyValuePairint,int(wallX,r.Next(wallY+1,bottom-1)));123holes.Add(newKeyValuePairint,int(r.Next(left+1,wallX-1),wallY));124holes.Add(newKeyValuePairint,int(r.Next(wallX+1,right-1),wallY));125holes.RemoveAt(r.Next(0,4));126holes.ForEach(hole=maze[hole.Key,hole.Value]=0);127128splitMazeChamber(r,maze,left,top,wallX,wallY);129splitMazeChamber(r,maze,wallX,top,right,wallY);130splitMazeChamber(r,maze,left,wallY,wallX,bottom);131splitMazeChamber(r,maze,wallX,wallY,right,bottom);132}133elseif(wallX0)134{135maze[wallX,r.Next(top+1,bottom-1)]=0;136137splitMazeChamber(r,maze,left,top,wallX,bottom);138splitMazeChamber(r,maze,wallX,top,right,bottom);139}140elseif(wallY0)141{142maze[r.Next(left+1,right-1),wallY]=0;143144splitMazeChamber(r,maze,left,top,right,wallY);145splitMazeChamber(r,maze,left,wallY,right,bottom);146}147}148}149}上面的程式就可以产生不会卡死的迷宫了,接下来,要放机器人上去,因为code不少,我就直接把注解写在code当中让大家看啰:001///summary002///产生一个名字后面带有Guid,这样可以避免与其他名称重复003///因为VSE当中的Entity名称是不可以重复的.004////summary005///paramname=name/param006///returns/returns007privatestringCreateNamePlusGuid(stringname)008{009returnname+_+Guid.NewGuid().ToString();010}011012///summary013///产生一个虚拟机器人Pioneer3DX到VSE当中014////summary015///paramname=position/param016///paramname=angle面对的角度/param017privatevoidAddPioneer3DX(Vector3position,floatangle)018{019Pioneer3DXrobotBaseEntity=CreatePioneer3DXMotorBase(position);020021//产生虚拟SickLRF放入机器人当中022robotBaseEntity.InsertEntity(CreateLaserRangeFinder());023024//产生虚拟的碰撞侦测器放入机器人当中025robotBaseEntity.InsertEntity(CreateBumperArray());026027//产生虚拟的WebCam放入机器人当中028robotBaseEntity.InsertEntity(CreateRobotCamera());029030//旋转角度031robotBaseEntity.State.Pose.Orientation=032TypeConversion.FromXNA(xna.Quaternion.CreateFromAxisAngle(newxna.Vector3(0,1,0),(float)((double)angle*Math.PI/(double)180)));0330
本文标题:第26天Microsoft-Robotics-Developer-Studio中文教程--继续修改迷
链接地址:https://www.777doc.com/doc-5607863 .html