/// <summary> /// Updates the support position in accordance with the current state. /// </summary> /// <param name="supportPosition">The support position.</param> private void UpdatePosition(SupportPosition supportPosition) { supportPosition.Score = 0; // these other scores may be used for debugging supportPosition.ShootScore = 0; supportPosition.DistanceScore = 0; supportPosition.PassScore = 0; var controlling = AI.MyTeam.ControllingPlayer; if (controlling != null) { if (AI.MyTeam.IsPassFromControllingSafe(supportPosition.Position)) { supportPosition.Score += Parameters.PassSafeFromControllingPlayerWeight; supportPosition.PassScore += Parameters.PassSafeFromControllingPlayerWeight; } supportPosition.Score += Parameters.DistanceFromControllingPlayerWeight * GetDistanceFromControllingScore(supportPosition.Position); supportPosition.DistanceScore += Parameters.DistanceFromControllingPlayerWeight * GetDistanceFromControllingScore(supportPosition.Position); } if (IsShotOnGoalPossible(supportPosition.Position)) { supportPosition.Score += Parameters.ShotOnGoalPossibleWeight; supportPosition.ShootScore += Parameters.ShotOnGoalPossibleWeight; } // distance from opponent }
void SetUpSupportRegions() { //Calculate the offsets for each axis float XOffSet = (Mathf.Abs((TopRightVec.x - BottomLeftVec.x)) / (WidthDivisions + 1)); float YOffSet = (Mathf.Abs((TopRightVec.y - BottomLeftVec.y)) / (HeightDivisions + 1)); //starting point is bootom left of the pitch Vector2 CurrentPos = BottomLeftVec; //Loop for each Division on Each Axis for (int x = 1; x <= WidthDivisions; x++) { for (int y = 1; y <= HeightDivisions; y++) { //Add the offsets to the current position CurrentPos = new Vector2(BottomLeftVec.x + (XOffSet * x), BottomLeftVec.y + (YOffSet * y)); //Create a Support position at that location SupportPosition NewPosition = new SupportPosition(); NewPosition.Position = CurrentPos; NewPosition.Weighting = DefaultWeighting; NewPosition.obj = new GameObject(); NewPosition.obj.transform.position = CurrentPos; //Add to the support posiiton list SupportPositions.Add(NewPosition); } } }
public Vector2 DetermineBestSupportingPosition() { if (DebugOn) { // Debug.Log("Determining Best Support Position For Attacker"); } Vector2 BestPoition = new Vector2(0, 0); float BestScoreSoFar = 0.0f; foreach (SupportPosition SP in PitchRef.SupportPositions) { //reset the current Weighing SP.Weighting = PitchRef.GetDefaultWeighting(); //Calculate the Passing Score to position if (IsPassSafeFromAllOpponents(ControllingPlayer.transform.position, SP.Position, SP.obj, ControllingPlayer.PassingForce)) { SP.Weighting += PitchRef.GetSafePassScore(); } //Determine if a goal can be scored from the posiiton Vector2 Outtarget = new Vector2(); if (CanShoot(SP.Position, out Outtarget, 4)) { SP.Weighting += PitchRef.GetShootingChanceScore(); } //Check to See if the supporting player is close if (SupportingPlayer) { float OptimalDisitance = 1.0f; Vector2 PassingVector = SP.Position - (Vector2)SupportingPlayer.transform.position; float dist = PassingVector.magnitude; if (dist < OptimalDisitance) { SP.Weighting += (OptimalDisitance - dist) / 4f; } } //If the current position has a better score make it so if (SP.Weighting > BestScoreSoFar) { BestScoreSoFar = SP.Weighting; BestSupportingSpot = SP; } } return(BestPoition); }
public override void sampleAt(TrackSegment4 trackSegment, float t) { base.sampleAt(trackSegment, t); Vector3 normal = trackSegment.getNormal(t); Vector3 trackPivot = getTrackPivot(trackSegment.getPoint(t), normal); Vector3 tangentPoint = trackSegment.getTangentPoint(t); Vector3 normalized = Vector3.Cross(normal, tangentPoint).normalized; Vector3 middlePoint = trackPivot + normalized * trackWidth / 2f; Vector3 middlePoint2 = trackPivot - normalized * trackWidth / 2f; Vector3 vector = trackPivot + normal * getCenterPointOffsetY(); if (useTopperTrack) { topperLeftRailExtruder.extrude(middlePoint, tangentPoint, normal); topperLeftPlankExtruder_1.extrude(middlePoint - (normal * -0.034561f), tangentPoint, normal); topperLeftPlankExtruder_2.extrude(middlePoint - (normal * -0.050133f), tangentPoint, normal); topperLeftPlankExtruder_3.extrude(middlePoint - (normal * -0.065763f), tangentPoint, normal); topperLeftPlankExtruder_4.extrude(middlePoint - (normal * -0.081394f), tangentPoint, normal); topperLeftPlankExtruder_5.extrude(middlePoint - (normal * -0.097025f), tangentPoint, normal); topperLeftPlankExtruder_6.extrude(middlePoint - (normal * -0.112511f), tangentPoint, normal); topperRightRailExtruder.extrude(middlePoint2, tangentPoint, normal); topperRightPlankExtruder_1.extrude(middlePoint2 - (normal * -0.034561f), tangentPoint, normal); topperRightPlankExtruder_2.extrude(middlePoint2 - (normal * -0.050133f), tangentPoint, normal); topperRightPlankExtruder_3.extrude(middlePoint2 - (normal * -0.065763f), tangentPoint, normal); topperRightPlankExtruder_4.extrude(middlePoint2 - (normal * -0.081394f), tangentPoint, normal); topperRightPlankExtruder_5.extrude(middlePoint2 - (normal * -0.097025f), tangentPoint, normal); topperRightPlankExtruder_6.extrude(middlePoint2 - (normal * -0.112511f), tangentPoint, normal); } else { iboxLeftRailExtruder.extrude(middlePoint, tangentPoint, normal); iboxRightRailExtruder.extrude(middlePoint2, tangentPoint, normal); } collisionMeshExtruder.extrude(trackPivot, tangentPoint, normal); if (liftExtruder != null) { liftExtruder.extrude(vector - normal * (0.16f + chainLiftHeight / 2f), tangentPoint, normal); } //calculating beams if (t == 0f) { supportPosts[trackSegment.getStartpoint()] = new List <SupportPosition>(); float supportInterval = trackSegment.getLength(0) / (float)Mathf.RoundToInt(trackSegment.getLength(0) / this.beamSpacing); float pos = 0; while (pos <= trackSegment.getLength(0) + 0.1f) { float tForDistance = trackSegment.getTForDistance(pos, 0); normal = trackSegment.getNormal(tForDistance); tangentPoint = trackSegment.getTangentPoint(tForDistance); Vector3 binormal = Vector3.Cross(normal, tangentPoint).normalized; trackPivot = base.getTrackPivot(trackSegment.getPoint(tForDistance, 0), normal); SupportPosition position = new SupportPosition(); position.topBarVisible = false; position.bottomBarVisible = false; float trackDirection = Mathf.Repeat(Mathf.Atan2(tangentPoint.x, tangentPoint.z) * Mathf.Rad2Deg, 360.0f); trackDirection += 45; bool trackFacingXPositive = false; bool trackFacingXNegative = false; bool trackFacingZPositive = false; bool trackFacingZNegative = false; if (trackDirection < 90) { trackFacingZPositive = true; } else if (trackDirection < 180) { trackFacingXPositive = true; } else if (trackDirection < 270) { trackFacingZNegative = true; } else { trackFacingXNegative = true; } float trackBanking = 0f; Vector3 bottomBeamDirection = new Vector3(); if (trackFacingXPositive) { trackBanking = Mathf.Repeat(Mathf.Atan2(normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg; if (trackBanking > 180) { trackBanking -= 360; } Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z); Vector2 normalProjection = Rotate(tangentProjection, 90); bottomBeamDirection.z = normalProjection.y; bottomBeamDirection.x = normalProjection.x; bottomBeamDirection.Normalize(); bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f; } if (trackFacingXNegative) { trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg; if (trackBanking > 180) { trackBanking -= 360; } bottomBeamDirection.z = tangentPoint.x; bottomBeamDirection.x = tangentPoint.z; Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z); Vector2 normalProjection = Rotate(tangentProjection, 90); bottomBeamDirection.z = normalProjection.y; bottomBeamDirection.x = normalProjection.x; bottomBeamDirection.Normalize(); bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f; } if (trackFacingZPositive) { trackBanking = Mathf.Repeat(Mathf.Atan2(normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg; if (trackBanking > 180) { trackBanking -= 360; } bottomBeamDirection.z = tangentPoint.x; bottomBeamDirection.x = tangentPoint.z; Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z); Vector2 normalProjection = Rotate(tangentProjection, 90); bottomBeamDirection.z = normalProjection.y; bottomBeamDirection.x = normalProjection.x; bottomBeamDirection.Normalize(); bottomBeamDirection *= Math.Abs(trackBanking) <= 90 ? -1.0f : 1.0f; } if (trackFacingZNegative) { trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg; if (trackBanking > 180) { trackBanking -= 360; } bottomBeamDirection.z = tangentPoint.x; bottomBeamDirection.x = tangentPoint.z; Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z); Vector2 normalProjection = Rotate(tangentProjection, 90); bottomBeamDirection.z = normalProjection.y; bottomBeamDirection.x = normalProjection.x; bottomBeamDirection.Normalize(); bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f; } position.trackBanking = trackBanking; //track beam Vector3 startPoint = trackPivot + normal * 0.159107f + binormal * (beamWidth / 2); Vector3 endPoint = trackPivot + normal * 0.159107f - binormal * (beamWidth / 2); bool equalHeight = Mathf.Abs(startPoint.y - endPoint.y) < 0.97f; //Bottom beam calculation Vector3 bottomBeamPivot = new Vector3(trackPivot.x, Mathf.Min(startPoint.y, endPoint.y), trackPivot.z); Vector3 bottomBeamStart = new Vector3(); Vector3 bottomBeamEnd = new Vector3(); Vector3 bottomBeamBinormal = bottomBeamDirection.normalized; Vector3 planePosition = new Vector3(); Vector3 planeSpanVector1 = endPoint - startPoint; Vector3 planeSpanVector2 = tangentPoint; Vector3 bottomLinePosition = new Vector3(); Vector3 topLinePosition = new Vector3(); Vector3 lineSpanVector = bottomBeamDirection; bool attachToStartPoint = false; if (((trackFacingXNegative || trackFacingXPositive) ? -1.0f : 1.0) * ((Mathf.Abs(trackBanking) <= 90) ? -1.0f : 1.0f) * trackBanking < 0) { bottomBeamStart.x = endPoint.x; bottomBeamStart.z = endPoint.z; bottomBeamStart.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y; bottomBeamEnd = bottomBeamStart - bottomBeamDirection.normalized * beamWidth; planePosition = endPoint; bottomLinePosition = bottomBeamStart; topLinePosition = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z); attachToStartPoint = false; } else { bottomBeamEnd.x = startPoint.x; bottomBeamEnd.z = startPoint.z; bottomBeamEnd.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y; bottomBeamStart = bottomBeamEnd + bottomBeamDirection.normalized * beamWidth; planePosition = startPoint; bottomLinePosition = bottomBeamEnd; topLinePosition = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z); attachToStartPoint = true; } if (Mathf.Abs(trackBanking) > 90) { bottomBeamStart.y -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace; bottomBeamEnd.y -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace; position.topBarVisible = true; } if (Mathf.Abs(trackBanking) > iBeamBankingSwitch) { position.bottomBarVisible = true; } position.bottomBarLeft = bottomBeamStart; position.bottomBarRight = bottomBeamEnd; position.topBarLeft = new Vector3(bottomBeamEnd.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamEnd.z); position.topBarRight = new Vector3(bottomBeamStart.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamStart.z); position.barsTangent = -1f * bottomBeamBinormal; if (Mathf.Abs(trackBanking) < 90) { position.topBarLeft.y += (1 - (Mathf.Abs(trackBanking) / 90)) * invertHeadSpace; position.topBarRight.y += (1 - (Mathf.Abs(trackBanking) / 90)) * invertHeadSpace; } Vector3 projectedTangentDirection = tangentPoint; projectedTangentDirection.y = 0; projectedTangentDirection.Normalize(); Vector3 intersectionPoint = new Vector3(); if (Math.Abs(trackBanking) > 90 && position.topBarVisible) { intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, topLinePosition, lineSpanVector); if (!float.IsNaN(intersectionPoint.x)) { if (attachToStartPoint) { endPoint = intersectionPoint; } else { startPoint = intersectionPoint; } } } else if (Math.Abs(trackBanking) > 0.1) { intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, bottomLinePosition, lineSpanVector); } if (true) { WriteToFile("IntersectionPoint:" + intersectionPoint); WriteToFile("PlanePosition:" + planePosition); WriteToFile("Magnitude:" + (intersectionPoint - planePosition).magnitude); WriteToFile("Difference:" + (intersectionPoint - planePosition)); WriteToFile("TrackBanking" + trackBanking); WriteToFile("planeSpanVector1" + planeSpanVector1); WriteToFile("planeSpanVector2" + planeSpanVector2); WriteToFile("topLinePosition" + topLinePosition); WriteToFile("bottomLinePasition" + bottomLinePosition); WriteToFile("lineSpanVector" + lineSpanVector); } if (!float.IsNaN(intersectionPoint.x) && (intersectionPoint - planePosition).magnitude > 0.5 && (intersectionPoint - planePosition).magnitude < 1.5) { if (attachToStartPoint) { endPoint = intersectionPoint; } else { startPoint = intersectionPoint; } } Vector3 leftVerticalSupportPost = bottomBeamEnd; Vector3 rightVerticalSupportPost = bottomBeamStart; Vector3 middleOfBeam = (bottomBeamStart + bottomBeamEnd) / 2; if (!position.topBarVisible) { if (trackBanking < iBeamBankingSwitch && trackBanking > -90f) { leftVerticalSupportPost = ((bottomBeamEnd - middleOfBeam) * 0.8f) + middleOfBeam; } if (trackBanking > -iBeamBankingSwitch && trackBanking < 90f) { rightVerticalSupportPost = ((bottomBeamStart - middleOfBeam) * 0.8f) + middleOfBeam; } } if (Mathf.Abs(trackBanking) > 90) { leftVerticalSupportPost.y = Mathf.Max(startPoint.y, endPoint.y); rightVerticalSupportPost.y = Mathf.Max(startPoint.y, endPoint.y); } else { leftVerticalSupportPost.y = startPoint.y; rightVerticalSupportPost.y = endPoint.y; } if (pos > 0f) { if (Mathf.Abs(trackBanking) <= iBeamBankingSwitch) { float distance = 1 / Mathf.Sin((90 - Mathf.Abs(trackBanking)) * Mathf.Deg2Rad); if (attachToStartPoint) { endPoint = startPoint - ((startPoint - endPoint) * distance); } else { startPoint = endPoint - ((endPoint - startPoint) * distance); } } position.iBeamLeft = startPoint; position.iBeamRight = endPoint; position.iBeamTangent = -1f * binormal; position.iBeamNormal = equalHeight ? Vector3.up : normal; } position.verticalSupportPostLeft = leftVerticalSupportPost; position.verticalSupportPostRight = rightVerticalSupportPost; position.verticalSupportPostTangent = projectedTangentDirection; supportPosts[trackSegment.getStartpoint()].Add(position); customBuildVolumeMeshExtruder.setWidth(Vector3.Distance(position.bottomBarLeft, position.bottomBarRight)); customBuildVolumeMeshExtruder.setHeight(Vector3.Distance((position.bottomBarLeft + position.bottomBarRight) / 2, (position.topBarLeft + position.topBarRight) / 2) + (Mathf.Abs(position.trackBanking) > 90 ? supportBeamExtension : 0f)); Vector3 middle = (position.topBarLeft + position.topBarRight + position.bottomBarLeft + position.bottomBarRight) / 4; if (pos > 0f) { customBuildVolumeMeshExtruder.extrude(new Vector3(middle.x, middle.y + (Mathf.Abs(position.trackBanking) > 90 ? supportBeamExtension / 2 : 0f), middle.z), Vector3.Cross(position.barsTangent, Vector3.up) * (Mathf.Abs(position.trackBanking) > 90 ? 1f : -1f), Vector3.up); } pos += supportInterval; } customBuildVolumeMeshExtruder.end(); } }