a439c31d99a44b27fa6d40acf6c6df6beea880bc
[applet-bots.git] / src / appletbots / directedballs / DirectedKicker.java
1 /*
2  * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
3  */
4 package appletbots.directedballs;
5
6 import appletbots.balls.Ball;
7 import appletbots.balls.Kicker;
8 import appletbots.geometry.Vector;
9
10 import java.awt.*;
11
12 /**
13  * A directed kicker agent chooses a visible ball at random and tries to "kick"
14  * it so that the ball will move in the the bot's goal direction.  The agent
15  * calculates the spot on the ball it needs to hit to knock the ball in the
16  * right direction.  If it senses that moving directly towards the target spot
17  * will result in kicking the ball in another direction, the agent accelerates
18  * around the bot (orthagonal to the vector towards the target spot) until it
19  * can hit the target spot.
20  *
21  * @author Erik Rasmussen
22  */
23 public class DirectedKicker extends Kicker
24 {
25         /**
26          * The goal direction
27          */
28         protected Vector direction;
29
30         /**
31          * Constructs a new DirectedKicker with the given parameters
32          *
33          * @param direction       The agent's goal direction
34          * @param color           The agent's color
35          * @param size            The agent's radius
36          * @param sight           The distance the agent can see
37          * @param maxSpeed        The maximum speed the agent can travel
38          * @param maxAcceleration The maximum acceleration for this agent
39          */
40         public DirectedKicker(final Vector direction, final Color color, final int size, final int sight, final double maxSpeed, final double maxAcceleration)
41         {
42                 super(size, sight, maxSpeed, maxAcceleration);
43                 this.direction = direction;
44                 this.color = color;
45                 direction.normalize();
46         }
47
48         /**
49          * Observes the world, and follows the Directed Kicker Algorithm.
50          */
51         public void observeWorld()
52         {
53                 final Ball ball = chooseBall();
54                 if (ball != null)
55                 {
56                         Vector toBallCenter = world.getVectorToObject(this, ball);
57                         toBallCenter = toBallCenter.setLength(toBallCenter.getLength() + ball.getSize());
58                         Vector fromCenterOfBallToContactPoint = direction.multiply(-1);
59                         fromCenterOfBallToContactPoint = fromCenterOfBallToContactPoint.setLength(ball.getSize() + getSize());
60                         final Vector toContactPoint = toBallCenter.add(fromCenterOfBallToContactPoint);
61
62                         if (toContactPoint.dotProduct(fromCenterOfBallToContactPoint) > 0)
63                         {
64                                 // there's not a straight path to the contact point
65                                 // we need to go around the ball and hit it from the other side
66                                 Vector orthagToContactPoint = new Vector(toContactPoint.y, toContactPoint.x);
67                                 if (orthagToContactPoint.dotProduct(direction.multiply(-1)) < 0)
68                                         orthagToContactPoint = orthagToContactPoint.multiply(-1);
69                                 setAcceleration(orthagToContactPoint);
70                         }
71                         else
72                                 setAcceleration(toBallCenter);
73                 }
74                 else
75                 {
76                         // continue in straight path until we find a ball to kick
77                         accelerationVectorColor = null;
78                         setAcceleration(new Vector(0, 0));
79                 }
80         }
81 }