Ejemplo n.º 1
0
    public override AINode getNextNode(Character body)
    {
        GameObject other = this.getNeighborOfType(body, this.itsTypeToCheck);
        double     forwardDist, lateralDist, speed;

        if (other == null)
        {
            return(base.chooseRightChild());
            //forwardDist = lateralDist = 0;
        }
        else
        {
            // This is an approximation based on the assumption that both body and other are circles.
            // Eventually I'll make it better


            // calculate relative positions
            double[] myPosition    = body.getCenter();
            double[] theirPosition = other.getCenter();
            double   dx            = theirPosition[0] - myPosition[0];
            double   dy            = theirPosition[1] - myPosition[1];
            double   dist          = Math.Sqrt(dx * dx + dy * dy);

            // calculate relative velocity
            double[] myVelocity    = body.getVelocity();
            double[] theirVelocity = other.getVelocity();
            double   vx            = myVelocity[0] - theirVelocity[0];
            double   vy            = myVelocity[1] - theirVelocity[1];
            speed = Math.Sqrt(vx * vx + vy * vy);

            // calculate sizes. If the shapes aren't actually circles, then we kind of make something up for the moment
            GameShape myShape     = body.getShape();
            double    myRadius    = Math.Max(myShape.getWidth() / 2, myShape.getHeight() / 2);
            GameShape theirShape  = other.getShape();
            double    theirRadius = Math.Max(theirShape.getWidth() / 2, theirShape.getHeight() / 2);


            // Compute the point at which the objects will be closest to each other
            if (vx == 0 && vy == 0)
            {
                forwardDist = dist - myRadius - theirRadius;
                lateralDist = dist - myRadius - theirRadius;
            }
            else
            {
                forwardDist = (dx * vx + dy * vy) / speed - myRadius - theirRadius;
                lateralDist = Math.Abs((dx * vy - dy * vx) / speed) - myRadius - theirRadius;
            }
        }
        // At faster speeds it's easier for there to be a collision, so scale the threshold by the speed (Therefore, the threshold represents a number of seconds)
        if (forwardDist * this.itsForwardScale + lateralDist * this.itsLateralScale < itsThreshold * speed)
        {
            return(base.chooseLeftChild());
        }
        else
        {
            return(base.chooseRightChild());
        }
    }
Ejemplo n.º 2
0
 public void setShape(GameShape newShape)
 {
     this.shape        = newShape;
     this.image.Width  = newShape.getWidth();
     this.image.Height = newShape.getHeight();
 }