public virtual double distanceTo(GameShape other, double[] offset) { //double[] deltas = new double[offset.Length]; double delta; double dist = 0; // offset in the x-dimension delta = Math.Abs(offset[0]); if (this.getType() == SHAPE.GAME_RECTANGLE) { delta -= this.getWidth() / 2; } if (other.getType() == SHAPE.GAME_RECTANGLE) { delta -= other.getWidth() / 2; } if (delta < 0) { delta = 0; } dist += delta * delta; // offset in the y-dimension delta = Math.Abs(offset[1]); if (this.getType() == SHAPE.GAME_RECTANGLE) { delta -= this.getWidth() / 2; } if (other.getType() == SHAPE.GAME_RECTANGLE) { delta -= other.getWidth() / 2; } if (delta < 0) { delta = 0; } dist += delta * delta; dist = Math.Sqrt(dist); // subtract off the circular radius if (this.getType() == SHAPE.GAME_CIRCLE) { dist -= ((GameCircle)this).getRadius(); } if (other.getType() == SHAPE.GAME_CIRCLE) { dist -= ((GameCircle)other).getRadius(); } if (dist < 0) { dist = 0; } return(dist); }
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()); } }
public void setShape(GameShape newShape) { this.shape = newShape; this.image.Width = newShape.getWidth(); this.image.Height = newShape.getHeight(); }