public void UpdatePosition() { double foodDist = 0; double leftTrack; double rightTrack; double curSpeed; myFood = myWorld.FindClosestFood(x, y, ref foodDist, ref indMyFood); double[] input = new double[4]; input[0] = Math.Cos(dir); //could be sin also; doesn't matter aslong as its between 0 and 1 input[1] = Math.Cos(dir); input[2] = (myFood.X - x) / foodDist; //let the ant know about nearest food input[3] = (myFood.Y - y) / foodDist; //same double[] output = net.FeedData(input); //feed input into network and get output leftTrack = output[0]; rightTrack = output[1]; dir += (rightTrack - leftTrack) * (Cosmos.maxForce / 100); //find the direction curSpeed = (rightTrack + leftTrack) / 2; x += Math.Sin(dir) * Cosmos.maxSpeed * curSpeed / 10; //max speed is just a twaking parameter; don't get confused by it y -= Math.Cos(dir) * Cosmos.maxSpeed * curSpeed / 10; //try varying it in simulation finishPosition = new PointF((float)x + finishPosition.X, (float)y + finishPosition.Y); if (x < 0) { x = clientSize.Width; } if (x > clientSize.Width) { x = 0; } if (y < 0) { y = clientSize.Height; } if (y > clientSize.Height) { y = 0; } if ((myFood.X >= (x - Cosmos.foodTolerance)) && (myFood.X <= (x + Cosmos.foodTolerance)) && (myFood.Y >= (y - Cosmos.foodTolerance)) && (myFood.Y <= (y + Cosmos.foodTolerance))) { foodCollected++; myWorld.NewFood(indMyFood); } }
/*public void UpdatePosition(Polygon[] polygons, int numFood) * { * double foodDist = 0; * double leftTrack; * double rightTrack; * double curSpeed; * * * * myFood = myWorld.FindClosestFood(x, y, ref foodDist, ref indMyFood, myFoodCollected); * double[] input = new double[4]; * input[0] = Math.Cos(dir); //could be sin also; doesn't matter aslong as its between 0 and 1 * input[1] = Math.Cos(dir); * input[2] = (myFood.X - x) / foodDist; //let the ant know about nearest food * input[3] = (myFood.Y - y) / foodDist; //same * double[] output = net.FeedData(input); //feed input into network and get output * leftTrack = output[0]; * rightTrack = output[1]; * dir += (rightTrack - leftTrack) * (Cosmos.maxForce / 100); //find the direction * curSpeed = (rightTrack + leftTrack) / 2; * x += Math.Sin(dir) * antMaxSpeed * curSpeed / 10; //max speed is just a twaking parameter; don't get confused by it * y -= Math.Cos(dir) * antMaxSpeed * curSpeed / 10; //try varying it in simulation * * Vector2 velocity = new Vector2((float)x, (float)y); * * int tx, ty, lx, ly, rx, ry; * tx = (int)(Ants.AntTopX * Math.Cos(dir) - Ants.AntTopY * Math.Sin(dir) + x); * ty = (int)(Ants.AntTopY * Math.Cos(dir) + Ants.AntTopX * Math.Sin(dir) + y); * lx = (int)(Ants.AntLeftX * Math.Cos(dir) - Ants.AntLeftY * Math.Sin(dir) + x); * ly = (int)(Ants.AntLeftY * Math.Cos(dir) + Ants.AntLeftX * Math.Sin(dir) + y); * rx = (int)(Ants.AntRightX * Math.Cos(dir) - Ants.AntRightY * Math.Sin(dir) + x); * ry = (int)(Ants.AntRightY * Math.Cos(dir) + Ants.AntRightX * Math.Sin(dir) + y); * //g.FillPolygon(Cosmos.whiteBrush, new Point[] { new Point(tx, ty), new Point(lx, ly), new Point(rx, ry) }); * * Polygon agent = new Polygon(); * agent.Points.Add(new Vector2(tx, ty)); * agent.Points.Add(new Vector2(lx, ly)); * agent.Points.Add(new Vector2(rx, ry)); * agent.BuildEdges(); * Vector2 playerTranslation = velocity; * * for (int m = 0; m < polygons.Length; m++) * { * PolygonCollisionResult r = PolygonCollision(agent, polygons[m], velocity); * * if (r.WillIntersect) * { * playerTranslation = velocity + r.MinimumTranslationVector; * x = x + playerTranslation.X/2; * y = y + playerTranslation.Y/2; * * // y = y + playerTranslation.Y; * break; * } * } * * * Vector2 finish = new Vector2(); * * if (agentType != null && agentType.Equals("calm")) * { * finish = new Vector2(finishPosition.X, finishPosition.Y); * Vector2 target = new Vector2(myFood.X,myFood.Y); * Vector2 current = new Vector2((float)x,(float)y); * Vector2 steerForce = SteeringBehaviours.Seek(ref target,ref current , ref velocity, 10); * steerForce = Vector2.Truncate(steerForce, 10); * Random random = new Random(); * Vector2 acceleration = steerForce / (random.Next(20, 80)); * velocity = Vector2.Truncate(velocity + acceleration,10); * finish = Vector2.Add(velocity, finish); * finishPosition = new PointF((float)finish.X, (float)finish.Y); * } * else * finishPosition = new PointF((float)x + finishPosition.X, (float)y + finishPosition.Y); * * * * if (x < 0) * x = clientSize.Width; * if (x > clientSize.Width) * x = 0; * if (y < 0) * y = clientSize.Height; * if (y > clientSize.Height) * y = 0; * * * * if ((myFood.X >= (x - Cosmos.foodTolerance)) && * (myFood.X <= (x + Cosmos.foodTolerance)) && * (myFood.Y >= (y - Cosmos.foodTolerance)) && * (myFood.Y <= (y + Cosmos.foodTolerance))) * { * if (myFoodCollected[indMyFood] != 1) * { * foodCollected++; * myFoodCollected[indMyFood] = 1; * } * * // agent disappears when they have reached the total number of goals * if(foodCollected == numFood) * invisible = true; * myWorld.NewFood(indMyFood); * } * } */ public void UpdatePosition(Polygon[] polygons, int numFood) { double foodDist = 0; double leftTrack; double rightTrack; double curSpeed; myFood = myWorld.FindClosestFood(x, y, ref foodDist, ref indMyFood, myFoodCollected); double[] input = new double[4]; input[0] = Math.Cos(dir); //could be sin also; doesn't matter aslong as its between 0 and 1 input[1] = Math.Cos(dir); input[2] = (myFood.X - x) / foodDist; //let the ant know about nearest food input[3] = (myFood.Y - y) / foodDist; //same double[] output = net.FeedData(input); //feed input into network and get output leftTrack = output[0]; rightTrack = output[1]; double olddir = dir; dir += (rightTrack - leftTrack) * (Cosmos.maxForce / 100); //find the direction curSpeed = (rightTrack + leftTrack) / 2; x += Math.Sin(dir) * antMaxSpeed * curSpeed / 10; //max speed is just a twaking parameter; don't get confused by it y -= Math.Cos(dir) * antMaxSpeed * curSpeed / 10; //try varying it in simulation // finishPosition = new PointF((float)x + finishPosition.X, (float)y + finishPosition.Y); if (((x - 15) <= 5) || ((x + 15) >= 1090)) { if ((x - 15) <= 5) { x += 15; } else { x -= 15; } } if (((y - 15) <= 5) || ((y + 15) >= 667)) { if ((y - 15) <= 5) { y += 15; } else { y -= 15; } } Vector2 targetPosition = new Vector2(myFood); Vector2 currentPosition = new Vector2((float)x, (float)y); Vector2 desired_V = Vector2.Normalize(Vector2.Subtract(targetPosition, currentPosition)) * (int)antMaxSpeed; Vector2 velocity = new Vector2(1, 1); Vector2 steerForce = Vector2.Subtract(desired_V, velocity); steerForce = Vector2.Truncate(steerForce, (int)Cosmos.maxForce); float mass = 21; Vector2 acceleration = steerForce / mass; velocity = Vector2.Truncate(velocity + acceleration, (int)antMaxSpeed); currentPosition = Vector2.Add(velocity, currentPosition); finishPosition = new PointF((float)currentPosition.X + finishPosition.X, (float)currentPosition.Y + finishPosition.Y); x = (float)currentPosition.X; y = (float)currentPosition.Y; int tx, ty, lx, ly, rx, ry; tx = (int)(Ants.AntTopX * Math.Cos(dir) - Ants.AntTopY * Math.Sin(dir) + x); ty = (int)(Ants.AntTopY * Math.Cos(dir) + Ants.AntTopX * Math.Sin(dir) + y); lx = (int)(Ants.AntLeftX * Math.Cos(dir) - Ants.AntLeftY * Math.Sin(dir) + x); ly = (int)(Ants.AntLeftY * Math.Cos(dir) + Ants.AntLeftX * Math.Sin(dir) + y); rx = (int)(Ants.AntRightX * Math.Cos(dir) - Ants.AntRightY * Math.Sin(dir) + x); ry = (int)(Ants.AntRightY * Math.Cos(dir) + Ants.AntRightX * Math.Sin(dir) + y); Polygon agent = new Polygon(); agent.Points.Add(new Vector2(tx, ty)); agent.Points.Add(new Vector2(lx, ly)); agent.Points.Add(new Vector2(rx, ry)); agent.BuildEdges(); Vector2 playerTranslation = velocity; for (int m = 0; m < polygons.Length; m++) { if ((rx + 270) >= polygons[m].Points[3].X) { PolygonCollisionResult r = PolygonCollision(agent, polygons[m], velocity); if (r.WillIntersect) { playerTranslation = velocity + r.MinimumTranslationVector; x = x + playerTranslation.X / 2; y = y + playerTranslation.Y / 2; dir = olddir; // y = y + playerTranslation.Y; break; } } } /*for (int m = 0; m < polygons.Length; m++) * { * RectangleF rf = new RectangleF(polygons[m].Points[0].X,polygons[m].Points[0].Y, Math.Abs(polygons[m].Points[1].X - polygons[m].Points[0].X),Math.Abs(polygons[m].Points[2].Y - polygons[m].Points[1].Y)); * * if(IsIntersected(new PointF((float)x + finishPosition.X, (float)y + finishPosition.Y),(float)6, rf)) * { * // playerTranslation = velocity + r.MinimumTranslationVector; * //x = x + playerTranslation.X / 2; * //y = y + playerTranslation.Y / 2; * x = x + 50; * y = y + 50; * * // y = y + playerTranslation.Y; * break; * } * } */ //finishPosition = new PointF((float)x + finishPosition.X, (float)y + finishPosition.Y); if (x < 0) { x = clientSize.Width; } if (x > clientSize.Width) { x = 0; } if (y < 0) { y = clientSize.Height; } if (y > clientSize.Height) { y = 0; } if ((myFood.X >= (x - Cosmos.foodTolerance)) && (myFood.X <= (x + Cosmos.foodTolerance)) && (myFood.Y >= (y - Cosmos.foodTolerance)) && (myFood.Y <= (y + Cosmos.foodTolerance))) { foodCollected++; invisible = true; //myWorld.NewFood(indMyFood); } }