public TanksOnAHeightmapGame() { graphics = new GraphicsDeviceManager(this); device = GraphicsDevice; Content.RootDirectory = "Content"; music = Content.Load<Song>("Sound/windbell"); // Light Manager lightManager = new LightManager(); lightManager.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f); Services.AddService(typeof(LightManager), lightManager); // Light 1 lightManager.Add("Light1", new PointLight(new Vector3(0, 1000.0f, 0), Vector3.One)); //tank = new Tank(this, Content, graphics); terrain = new TanksOnAHeightmap.GameBase.Shapes.Terrain(this, Content, graphics); //InputHelper inputHelper = new InputHelper(PlayerIndex.One); Services.AddService(typeof(InputHelper), inputHelper); // Create the chase camera camera = new ChaseCamera(this); // Set the camera offsets //camera.DesiredPositionOffset = new Vector3(0.0f, 600.0f, 800.0f);//150,150 //camera.LookAtOffset = new Vector3(0.0f, 80.0f, 0.0f);//80 camera.DesiredPositionOffset = new Vector3(0.0f, 45.0f, 20.0f);//150,150 camera.LookAtOffset = new Vector3(0.0f, 35.0f, -50.0f);//80 // Set camera perspective camera.NearPlaneDistance = 10.0f; camera.FarPlaneDistance = 4000.0f; this.Services.AddService(typeof(TanksOnAHeightmap.GameBase.Shapes.Terrain), terrain); this.Services.AddService(typeof(ChaseCamera), camera); rand = new Random(0); // Construct our particle system components. explosionParticles = new ExplosionParticleSystem(this, Content); ParticleSettings settings = new ParticleSettings(); this.Services.AddService(typeof(ExplosionParticleSystem), explosionParticles); explosionSmokeParticles = new ExplosionSmokeParticleSystem(this, Content); this.Services.AddService(typeof(ExplosionSmokeParticleSystem), explosionSmokeParticles); projectileTrailParticles = new ProjectileTrailParticleSystem(this, Content); //Trees trees = new Trees[NUMBER_OF_TREES]; for (int i = 0; i < trees.Length; i++) { trees[i] = new Trees(this, Content, graphics); //Components.Add(trees[i]); } explosionSmokeParticles.DrawOrder = 200; projectileTrailParticles.DrawOrder = 300; explosionParticles.DrawOrder = 400; terrain.DrawOrder = 102; //Trees this.graphics.PreferredBackBufferWidth = 1024; this.graphics.PreferredBackBufferHeight = 768; start(); }
public Vector3 FindNextObstacle(List<Trees> nearTrees) { Matrix inverseTransformation = Matrix.Invert(tank.WorldMatrix); var localPosition = new Vector3[nearTrees.Count]; int i = -1; foreach (Trees tree in nearTrees) { i += 1; localPosition[i] = Vector3.Transform(tree.Position, inverseTransformation); } float objectRadius; float DistToClosestIP = float.MaxValue; int closest = -1; Vector3 boundingMax; Vector3 boundingMin; for (i = 0; i < localPosition.Length; i++) { if (localPosition[i].Z <= 100) { //1 boundingMax = nearTrees[i].BoundingBox.Max; boundingMin = nearTrees[i].BoundingBox.Min; boundingMax.Y = 0; boundingMin.Y = 0; objectRadius = (boundingMax - boundingMin).Length()/2.0f; float ExpandedRadius = objectRadius + (DetectionBox.Max.X - DetectionBox.Min.X)/2.0f; if (Math.Abs(localPosition[i].X) < ExpandedRadius) { float cX = -localPosition[i].Z; float cY = localPosition[i].X; var SqrtPart = (float) Math.Sqrt(ExpandedRadius*ExpandedRadius - cY*cY); float ip = cX - SqrtPart; if (ip <= 0.0) //! { ip = cX + SqrtPart; } if (ip < DistToClosestIP) { DistToClosestIP = ip; closest = i; } } } } var SteeringForce = new Vector3(0, 0, 0); if (closest != -1) { //the closer the agent is to an object, the stronger the //steering force should be boundingMax = nearTrees[closest].BoundingBox.Max; boundingMin = nearTrees[closest].BoundingBox.Min; boundingMax.Y = 0; boundingMin.Y = 0; objectRadius = (boundingMax - boundingMin).Length()/2.0f; float multiplier = 1.0f + (DetectionDistance - localPosition[closest].Z)/ DetectionDistance; //calculate the lateral force ClosestIntersectingObstacle->BRadius() //SteeringForce.X = (objectRadius - localPosition[closest].X) * multiplier * 0.01f; //SteeringForce.X = -1.0f/(localPosition[closest].X) * multiplier*10; //SteeringForce.X = -1.0f / (localPosition[closest].X) * multiplier * 10; SteeringForce.X = ((DetectionBox.Max.X - DetectionBox.Min.X)/2.0f) + objectRadius; if (localPosition[closest].X < 0.0f) { SteeringForce.X = SteeringForce.X + localPosition[closest].X; } else { SteeringForce.X = -1.0f*(SteeringForce.X - localPosition[closest].X); } SteeringForce.X *= multiplier*0.05f; //apply a braking force proportional to the obstacles distance from //the vehicle. //float BrakingWeight = 0.1f; // SteeringForce.Z = (objectRadius - // localPosition[closest].Z) * // BrakingWeight; //System.Console.WriteLine(SteeringForce); //return Vector3.Transform(SteeringForce, tank.Orientation * Transformation); //return SteeringForce; tank.SteeringForce = Vector3.Transform(SteeringForce, tank.Orientation); closesdObstacle = nearTrees[closest]; return tank.SteeringForce; } else { //return Vector3.Zero; tank.SteeringForce = Vector3.Zero; closesdObstacle = null; return Vector3.Zero; } }