// reset all instance state public override void Reset() { // reset the vehicle base.Reset(); // max speed and max steering force (maneuverability) MaxSpeed = 2.0f; MaxForce = 8.0f; // initially stopped Speed = 0; // size of bounding sphere, for obstacle avoidance, etc. Radius = 0.5f; // width = 0.7, add 0.3 margin, take half // set initial position // (random point on path + random horizontal offset) float d = hpath.PolyPath.TotalPathLength * StaticRandom.Random(); float r = hpath.PolyPath.radius; Vector3 randomOffset = VectorUtils.RandomVectorOnUnitRadiusXZDisk() * r; Position = (hpath.PolyPath.MapPathDistanceToPoint(d) + randomOffset); // randomize 2D heading RandomizeHeadingOnXZPlane(); // pick a random direction for path following (upstream or downstream) pathDirection = (StaticRandom.Random() > 0.5) ? -1 : +1; // notify proximity database that our position has changed if (proximityToken != null) { proximityToken.UpdateForNewPosition(Position); } }
protected override void LoadContent(PloobsEngine.Engine.GraphicInfo GraphicInfo, PloobsEngine.Engine.GraphicFactory factory, IContentManager contentManager) { base.LoadContent(GraphicInfo, factory, contentManager); ///Create a Simple Model IModelo sm = new SimpleModel(factory, "..\\Content\\Model\\cenario"); ///Create a Physic Object IPhysicObject pi = new TriangleMeshObject(sm, Vector3.Zero, Matrix.Identity, Vector3.One, MaterialDescription.DefaultBepuMaterial()); pi.isMotionLess = true; ///Create a shader IShader shader = new DeferredNormalShader(); ///Create a Material IMaterial mat = new DeferredMaterial(shader); ///Create a an Object that englobs everything and add it to the world IObject obj4 = new IObject(mat, sm, pi); this.World.AddObject(obj4); lt = new LightThrowBepu(this.World, factory); ///Create a FirstPerson Camera ///This is a special camera, used in the development ///You can move around using wasd / qz / and the mouse ICamera cam = new CameraFirstPerson(GraphicInfo); this.World.CameraManager.AddCamera(cam); PointLightCircularUpdater lcu = new PointLightCircularUpdater(this); ///Create 100 moving point lights and add to the world ///IF the radius of the point light is TOO big like 250, it will make the game slower for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { PointLightPE pl0 = new PointLightPE(new Vector3(i * 5, 25, j * 5), StaticRandom.RandomColor(), 100, 2); pl0.UsePointLightQuadraticAttenuation = true; lcu.AddLight(pl0, 0.01f, 50 * i, j / StaticRandom.Random()); this.World.AddLight(pl0); } } lcu.Start(); this.RenderTechnic.AddPostEffect(new AntiAliasingPostEffect()); }
private void CreateInternalModel(String fileName) { if (!modelLoaded.ContainsKey(fileName)) { ContentBuilder.Clear(); String iname = "Model" + StaticRandom.Random(); ContentBuilder.Add(fileName, iname, null, "ModelProcessor"); String buildError = ContentBuilder.Build(); if (string.IsNullOrEmpty(buildError)) { if (!Directory.Exists(Directory.GetCurrentDirectory() + "/Content/Loaded")) { Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/Content/Loaded"); } MakeCopy(ContentBuilder.OutputDirectory, Directory.GetCurrentDirectory() + "/Content/Loaded/"); modelLoaded.Add(fileName, iname); } else { throw new Exception(buildError); } } }
float Random() { float random = (float)(StaticRandom.Random() % 1000) / 1000.0f; return(random); }
// compute combined steering force: move forward, avoid obstacles // or neighbors if needed, otherwise follow the path and wander public Vector3 DetermineCombinedSteering(float elapsedTime) { // move forward Vector3 steeringForce = Forward; // probability that a lower priority behavior will be given a // chance to "drive" even if a higher priority behavior might // otherwise be triggered. const float leakThrough = 0.1f; // determine if obstacle avoidance is required Vector3 obstacleAvoidance = Vector3.Zero; if (leakThrough < StaticRandom.Random()) { const float oTime = 6; // minTimeToCollision = 6 seconds obstacleAvoidance = SteerToAvoidObstacles(oTime, hpath.Obstacles); } // if obstacle avoidance is needed, do it if (obstacleAvoidance != Vector3.Zero) { steeringForce += obstacleAvoidance; } else { // otherwise consider avoiding collisions with others Vector3 collisionAvoidance = Vector3.Zero; const float caLeadTime = 3; // find all neighbors within maxRadius using proximity database // (radius is largest distance between vehicles traveling head-on // where a collision is possible within caLeadTime seconds.) float maxRadius = caLeadTime * MaxSpeed * 2; neighbors.Clear(); proximityToken.FindNeighbors(Position, maxRadius, ref neighbors); if (neighbors.Count > 0 && leakThrough < StaticRandom.Random()) { collisionAvoidance = SteerToAvoidNeighbors(caLeadTime, neighbors) * 10; } // if collision avoidance is needed, do it if (collisionAvoidance != Vector3.Zero) { steeringForce += collisionAvoidance; } else { // add in wander component (according to user switch) if (Globals.WanderSwitch) { steeringForce += SteerForWander(elapsedTime); } // do (interactively) selected type of path following const float pfLeadTime = 3; Vector3 pathFollow = (Globals.UseDirectedPathFollowing ? SteerToFollowPath(pathDirection, pfLeadTime, hpath.PolyPath) : SteerToStayOnPath(pfLeadTime, hpath.PolyPath)); // add in to steeringForce steeringForce += pathFollow * 0.5f; } } // return steering constrained to global XZ "ground" plane steeringForce.Y = 0; return(steeringForce); }