Vector wallAvoidance() { //Same as above. this.createEntityFeelers(); double distanceToThis = 0; double distanceToClosest = 9999999; int closestWallIndex = -1; Vector force = new Vector(); Vector tempPoint = new Vector(); Vector closestPoint = new Vector(); ObjectBounds[] walls = WorldColliders.getObjectBoundsArray(); for(int i = 0; i < 3; ++i){ for(int j = 0; j < walls.length; j++){ for(int k = 0; k < 4; k++){ Vector2D points = walls[j].getVertices(); Vector from = new Vector(points[k].x, points[k].y); Vector to = new Vector(points[(k+1)&3].x, points[(k+1)&3].y); Storage mathStore = MathExtension.lineIntersects2dObject(owner.getPosition(), feeler[i], from, to, distanceToThis, tempPoint); if(mathStore.wasItTrue == true){ if(mathStore.dist < distanceToClosest){ closestWallIndex = j; closestPoint.setValues(tempPoint.x(), tempPoint.y()); } } distanceToThis = mathStore.dist; } if(closestWallIndex >= 0){ Vector difference = feelers[i] - closestPoint; Vector tempVec = to -from; tempVec.normalise(); tempVec.setVectorToNormal(); force = tempVec * Math.sqrt(difference.lengthSquared()); } } } return force; }
Vector obstacleAvoidance() { //The obstacles for this are taken from the global object boxLength = SteeringBehaviour.minimumDetectionLength + (Math.sqrt(owner.getCurrentSpeedSquared()/owner.getMaxSpeed())) * SteeringBehaviour.minimumDetectionLength; ArrayList<AIEntity> list = EntityManager.globalInstance().getNeighboursInRange(owner, boxLength); AIEntity closestEntity = null; double distanceOfClosestEntity = 9999999; Vector positionOfClosestEntity = new Vector(); foreach(AIEntity listIter in list){ Vector localPosition = MathExtension.convertPointToLocalPosition(listIter.getPosition(),owner.getHeadingDirection(),owner.getPerpendicularHeading(), owner.getPosition()); if(localPosition.x() >= 0){ double rad = listIter.boundingRadius() + owner.boundingRadius(); if(Math.fabs(localPosition.y()) < rad){ double dX = localPosition.x(); double dY = localPosition.y(); double sqrRoot = Math.sqrt(rad*rad - dY*dY); double temp = dX - sqrRoot; if(temp <= 0) temp = dX + sqrRoot; if(temp < distanceOfClosestEntity){ distanceOfClosestEntity = temp; closestEntity = listIter; positionOfClosestEntity = localPosition; } } } } Vector steeringRequiredToAvoid = new Vector(0,0); if(closestEntity != null){ double mult = 1 + (SteeringBehaviour.minimumDetectionLength - positionOfClosestEntity.x()) / SteeringBehaviour.minimumDetectionLength; steeringRequiredToAvoid.setValues(steeringRequiredToAvoid.x(), (closestEntity.boundingRadius() - positionOfClosestEntity.y()) * mult); double brakeVal = 0.15; steeringRequiredToAvoid.setValues((closestEntity.boundingRadius() - positionOfClosestEntity.x()) * brakeVal, steeringRequiredToAvoid.y()); } //This may need a conversion into global space, it may not. return steeringRequiredToAvoid; }