public override bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget) { // Don't override the timeToTarget if there's already another valid plan. if (timeToTarget > 0) { return(false); } PrincipalProjectile principalProjectile; PrincipalSpace3D principalSpace3D = PrincipalSpace3D.Create(projectile3D, initialPosition, targetPosition, out principalProjectile); Vector2 principalTargetPosition = principalSpace3D.ToPrincipalPosition(targetPosition); float newTimeToTarget = PrincipalTimePlanners.GetTimeToTargetRGivenInitialSpeedS( principalProjectile, principalTargetPosition, initialSpeed, highArc); if (newTimeToTarget > 0) { timeToTarget = newTimeToTarget; return(true); } return(false); }
public override bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget) { if (timeToTarget > 0) { return(false); } PrincipalProjectile principalProjectile; PrincipalSpace3D principalSpace3D = PrincipalSpace3D.Create(projectile3D, initialPosition, targetPosition, out principalProjectile); Vector2 principalTargetPosition = principalSpace3D.ToPrincipalPosition(targetPosition); float slope = principalSpace3D.ToPrincipalSlope(targetSlope); if (!float.IsNaN(slope)) { float newTimeToTarget = PrincipalTimePlanners.GetTimeToTargetRGivenTargetSlopeA( principalProjectile, principalTargetPosition, slope); if (newTimeToTarget > 0) { timeToTarget = newTimeToTarget; return(true); } } return(false); }
public override bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget) { Vector3 targetDir = slopeBaseTransform.TransformDirection(new Vector3(0, slopeRelativeToBase, 1)); if (timeToTarget > 0) { Vector3 initialVelocity = projectile3D.GetInitialVelocityGivenRelativeTargetAndTime( targetPosition - initialPosition, timeToTarget); Vector3 validAngleDirection = Vector3.Cross(targetDir, slopeBaseTransform.right); if (Vector2.Dot(validAngleDirection, initialVelocity) > 0) { return(false); } } PrincipalProjectile principalProjectile; PrincipalSpace3D principalSpace3D = PrincipalSpace3D.Create(projectile3D, initialPosition, targetPosition, out principalProjectile); Vector2 principalTargetPosition = principalSpace3D.ToPrincipalPosition(targetPosition); float slope = principalSpace3D.ToPrincipalSlope(targetDir); if (!float.IsNaN(slope)) { float newTimeToTarget = PrincipalTimePlanners.GetTimeToTargetRGivenInitialSlopeA( principalProjectile, principalTargetPosition, slope); ////////////////// //Vector3 initialVelocity2 = // projectile3D.GetInitialVelocityGivenRelativeTargetAndTime( // targetPosition - initialPosition, newTimeToTarget); //Debug.Log(initialVelocity2.y / Mathf.Sqrt(initialVelocity2.x * initialVelocity2.x + initialVelocity2.z * initialVelocity2.z)); if (newTimeToTarget > 0) { timeToTarget = newTimeToTarget; return(true); } } return(false); }
// See the base class for an explanation of the parameters. public override bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget) { // Don't override the timeToTarget if there's already another valid plan. if (timeToTarget > 0) { return(false); } timeToTarget = this.timeToTarget; return(true); }
// Plan a trajectory in terms of flight time from initialPosition and targetPosition // using the settings in projectile3D. It's the responsibility of each implementated // subclass to decide whether or not to override provided timeToTarget, making it // possible to use the same interface for simple planners, backup planning and plan // modifiers. The function returns true if it modified the timeToTarget. To convert // the (new) timeToTarget to a 3D initial velocity use the projectile3D's // GetInitialVelocityGivenRelativeTargetAndTime() function. public abstract bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget);
public override bool PlanTimeToTarget(Projectile3D projectile3D, Vector3 initialPosition, Vector3 targetPosition, ref float timeToTarget) { // Allow this planner to only modify an already existing plan, not create a new one. if (timeToTarget <= 0) { return(false); } // Convert the problem to principal space PrincipalProjectile principalProjectile; PrincipalSpace3D principalSpace3D = PrincipalSpace3D.Create(projectile3D, initialPosition, targetPosition, out principalProjectile); Vector2 principalTargetPosition = principalSpace3D.ToPrincipalPosition(targetPosition); PrincipalTrajectory principalTrajectory = new PrincipalTrajectory(principalProjectile); principalTrajectory.v0 = principalTrajectory.GetInitialVelocityGivenRelativeTargetAndTime( principalTargetPosition, timeToTarget); // notify helper classes/structs of the trajectory to test _trajectoryOverGameObjectHelper.SetupTrajectory(principalSpace3D, principalTrajectory, principalTargetPosition, marginDistance); // (Re-)check all the gameobjects until no further intersections are detected, or until // an intersecting gameobject is found that can't have a trajectory passing over it. int lastIntersectingGameObjectIndex = -1; //bool foundAnotherIntersection; //do { //foundAnotherIntersection = false; for (int index = 0; index < obstacles.Length; ++index) { if (index != lastIntersectingGameObjectIndex) { float foundTimeToTarget = _trajectoryOverGameObjectHelper.GetTimeToTargetOverGameObject(obstacles[index]); if (foundTimeToTarget >= 0) // found intersection { //foundAnotherIntersection = true; lastIntersectingGameObjectIndex = index; timeToTarget = foundTimeToTarget; if (timeToTarget == 0) { return(true); // trajectory over the object is impossible, so give up } // use the new initial velocity in the tested trajecory principalTrajectory.v0 = principalTrajectory.GetInitialVelocityGivenRelativeTargetAndTime( principalTargetPosition, timeToTarget); // notify helper classes/structs of the new trajectory to test _trajectoryOverGameObjectHelper.SetupTrajectory(principalSpace3D, principalTrajectory, principalTargetPosition, marginDistance); } } } } //while (foundAnotherIntersection); return(lastIntersectingGameObjectIndex >= 0); // true iff the plan changed }