Aug-13-2018, 08:38 PM
I'm looking for a way to solve my problem.
I'm working on a game Rocks In Space. Its similar to the old Asteriods game. The craft can turn on a single axis 0 through 360. There is only one screen, If you fly off the screen the autopilot is activated to return the craft to the center of the screen. My problem is that I have a routine that fails to know when the craft has turned through a set of angles and reached its target angle (where I want it to stop).
An import part of it task is to check if an craft has rotated through a set of angles and reached the target angel, but it has to be able to work under the following conditions:
1. it doesn't neccessarily have to be called in a continuos loop.
2. when the check routine is not running the craft could have turned through a few angles.
3. The routine that checks if the craft has reached its target position only knows about the crafts current position, the angle it facing in, the target position, and some other attributes listed below.
4. The craft can turn clockwise or anticlockwise.
The following attributes are exposed and can be accessed at any time:
1. clockwise_anticlockwise # indicating which way the craft is rotating.
2. angle # the crafts position in angles (from 0 - 360)
3. target_angle # the angle the craft should stop on.
4. turning_speed # negative numbers result in an anticlockwise rotation.
5. maximum_speed_step # largest amount the turning_speed can be altered by.
Example #1.A.
A craft is turning clockwise and starts at 0 degrees and rotates clockwise, and may turn in increments of 6 degrees. If the target angle is 90 degrees, a logic expression like the one below would work
But if the craft's current position is 30 degrees and the target angle is 300 degrees, what would be the best way to see if the craft has reached the target angle?
Ive provided a listing of the main module used to manage the autopilot and my strategy used to go about it.
I'm working on a game Rocks In Space. Its similar to the old Asteriods game. The craft can turn on a single axis 0 through 360. There is only one screen, If you fly off the screen the autopilot is activated to return the craft to the center of the screen. My problem is that I have a routine that fails to know when the craft has turned through a set of angles and reached its target angle (where I want it to stop).
An import part of it task is to check if an craft has rotated through a set of angles and reached the target angel, but it has to be able to work under the following conditions:
1. it doesn't neccessarily have to be called in a continuos loop.
2. when the check routine is not running the craft could have turned through a few angles.
3. The routine that checks if the craft has reached its target position only knows about the crafts current position, the angle it facing in, the target position, and some other attributes listed below.
4. The craft can turn clockwise or anticlockwise.
The following attributes are exposed and can be accessed at any time:
1. clockwise_anticlockwise # indicating which way the craft is rotating.
2. angle # the crafts position in angles (from 0 - 360)
3. target_angle # the angle the craft should stop on.
4. turning_speed # negative numbers result in an anticlockwise rotation.
5. maximum_speed_step # largest amount the turning_speed can be altered by.
Example #1.A.
A craft is turning clockwise and starts at 0 degrees and rotates clockwise, and may turn in increments of 6 degrees. If the target angle is 90 degrees, a logic expression like the one below would work
target_angle =90 current_angle >= target_angle: # turn has completed.
But if the craft's current position is 30 degrees and the target angle is 300 degrees, what would be the best way to see if the craft has reached the target angle?
Ive provided a listing of the main module used to manage the autopilot and my strategy used to go about it.
from clsTaskManager import *
from clsGameInfo import *
from constants import *
class Autopilot:
def __init__(self, screen_obj):
self._tasks =TaskManager() # Create a new task manager.
self._time_slice =1 /GameInfo.FPS
self.parameters ={}
self.scrn_obj =screen_obj
self.variables ={}
# __init__() -----------------------------------------------------------
def _autopilot_aim_at_center_of_screen(self, obj):
'Function - designed to be called from a taks queue.'
print("[_autopilot_aim_at_center_of_screen] - Triggered successfully.")
# Get a dictionary of all the parameters
# for this routine.
par =Autopilot.parameters["_autopilot_aim_at_center_of_screen"]
# _autopilot_aim_at_center_of_screen() ----------------------------------
def _autopilot_point_craft_in_oaf(self, obj):
'Function - designed to be called from a taks queue.'
class TaskStatus:
pass
# Get a dictionary of all the parameters and variables
# for this routine.
par =self.parameters["_autopilot_point_craft_in_oaf"]
var =self.variables["_autopilot_point_craft_in_oaf"]
# _.
so =self.scrn_obj
if obj.status ==TaskManager.STATUS_TYPES.INIT:
var["failed attempts"] =0
var["completed_turn"] =False
par_obj =TaskStatus()
par_obj.status =TaskManager.STATUS_TYPES.INIT
var["par_obj"] =par_obj
self._r_t_diff =0
self._r_a_diff =0
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
if self._breaking_angle <so.angle:
# Presume its correct and update the relative
# angle differential so that more than 360
# degrees are represented.
self._r_t_diff =360
elif if self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
if self._breaking_angle >so.angle:
# Presume its correct and update the relative
# angle differential so that more than 360
# degrees are represented.
self._r_a_diff =360
print("[_autopilot_point_craft_in_oaf] - INFO #1: Initial angles set at C angle:{0} Breaking angle:{1}.".format(so.angle, self._breaking_angle))
elif obj.status ==TaskManager.STATUS_TYPES.RUNNING:
# Need to work on turning the craft in the
# opposite angle of flight.
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
if so.angle >self._breaking_angle:
print("[_autopilot_point_craft_in_oaf] - INFO #2: Craft is facing in the direction of the breaking angel.")
var["completed_turn"] =True
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
if so.angle <self._breaking_angle:
print("[_autopilot_point_craft_in_oaf] - INFO #3: Craft is facing in the direction of the breaking angel.")
var["completed_turn"] =True
# Different states:
# - turn completed
# - turn incomplete
if var["completed_turn"] ==True:
# Examine the turn speed values to determine
# if the craft is still turning.
if so._turn_speed ==0:
# The craft is NOT rotating, and there
# is nothing left to do for this task.
print("[_autopilot_point_craft_in_oaf] - INFO #4: Craft is facing in the direction of the breaking angel >>>AND<<< has stopped rotating.")
obj.status =TaskManager.STATUS_TYPES.COMPLETED
else:
# The craft is still turning.
print("[_autopilot_point_craft_in_oaf] - INFO #5: Rotation has completed but turn speed =", so._turn_speed)
update_task_status =False # Lock this task's status.
self._autopilot_stop_rotating(obj, update_task_status)
if not so._turn_speed ==0:
# Something has seriously wrong, we
# shouldn't even be roatating.
var["failed attempts"] +=1
if var["failed attempts"] >10:
# Give up and let's move onto the next task.
print("[_autopilot_point_craft_in_oaf] - WARNING #6: Failed to turn the craft to the breaking angle, and giving up.")
obj.status =TaskManager.STATUS_TYPES.COMPLETED
# Lets start this whole thing again.
self.autopilot_init()
var["failed attempts"] =0 # Reset var.
else:
# The craft has sotpped rotating and it has
# turned to and pointing in the direction where
# we can apply thrusters to nuetralise xy movement.
obj.status =TaskManager.STATUS_TYPES.COMPLETED
else:
# Point the craft in the direction of the
# breaking angle so we can start slowing down by
# apply thrusters.
# Hack into the tasked linked routine to set up variables
# (and parameters) so the routine can run.
try:
tmp_par =self.parameters["_autopilot_turn_to_angle"]
except KeyError:
tmp_par ={}
finally:
tmp_par["angle"] =self._breaking_angle
self.parameters["_autopilot_turn_to_angle"] =tmp_par
try:
tmp_var =self.variables["_autopilot_turn_to_angle"]
except KeyError:
tmp_var ={}
self.variables["_autopilot_turn_to_angle"] =tmp_var
# _.
update_task_status =False # Lock task's status
self._autopilot_turn_to_angle(var["par_obj"], update_task_status)
if var["par_obj"].status ==TaskManager.STATUS_TYPES.INIT:
# Now that the routine has been initialised lets progress
# onto running the core part of the routine.
var["par_obj"].status =TaskManager.STATUS_TYPES.RUNNING
# Save all the parameters and variables usesd by this routine.
self.variables["_autopilot_point_craft_in_oaf"] =var
self.parameters["_autopilot_point_craft_in_oaf"] =par
# _.
if obj.status ==TaskManager.STATUS_TYPES.COMPLETED:
print("[_autopilot_point_craft_in_oaf] - Completed successfully.")
print(" - current angle:{0} AOF:{1}".format(self.scrn_obj.angle, self._breaking_angle))
# _autopilot_point_craft_in_oaf() ---------------------------------------
def _autopilot_stop_moving(self, obj, update_task_status =True):
# DESCRIPTION:
# This routine is used to bring the craft to a complete stop.
#
#
# NOTES:
# # self._angle_x can be (+) or (-)
'Function - designed to be called from a taks queue.'
print("[_autopilot_stop_moving] - Triggered successfully.")
# Get a dictionary of all the parameters
# for this routine.
par =self.parameters["_autopilot_stop_moving"]
var =self.variables["_autopilot_stop_moving"]
# This phase involves reducing our speed across the x and y
# planes to zero.
so =self.scrn_obj
if so._speed_x >0:
if so._speed_x +so._angle_x >0:
# We can't set the speed to
# zero just yet.
so._speed_x +=so._angle_x
else:
so._speed_x=0
elif so._speed_x <0:
if so._speed_x +so._angle_x <0:
# We can't set the speed to
# zero just yet.
so._speed_x +=so._angle_x
else:
so._speed_x=0
if so._speed_y >0:
if so._speed_y +so._angle_y >0:
# We can't set the speed to
# zero just yet.
so._speed_y +=so._angle_y
else:
so._speed_y=0
elif so._speed_y <0:
if so._speed_y +so._angle_y <0:
# We can't set the speed to
# zero just yet.
so._speed_y +=so._angle_y
else:
so._speed_y=0
if so._speed_x ==0 and so._speed_y ==0:
craft_not_moving =True
else:
craft_not_moving =False
if update_task_status ==True:
if craft_not_moving: # Go to the next mode.
obj.status =TaskManager.STATUS_TYPES.COMPLETED
print("[_autopilot_stop_moving] - Completed successfully.")
# _autopilot_stop_moving() ----------------------------------------------
def _autopilot_stop_rotating(self, obj, update_task_status =True):
'Function - designed to be called from a taks queue.'
print("[_autopilot_stop_rotating] - Triggered successfully.")
# Get a dictionary of all the parameters
# for this routine.
par =self.parameters["_autopilot_stop_rotating"]
# _.
so =self.scrn_obj
if so._turn_speed >0: # Craft is still turning.
if so._turn_speed >self.largest_turn_factor:
so._turn_speed -=self.largest_turn_factor
else:
# Speed is at a low enough level to
# reduce it to zero.
so._turn_speed =0
elif so._turn_speed <0: # Craft is still turning.
if so._turn_speed <(0 -self.largest_turn_factor):
so._turn_speed +=self.largest_turn_factor
else:
# Speed is at a low enough level to
# reduce it to zero.
so._turn_speed =0
if update_task_status ==True:
# Check if the task has completed.
if so._turn_speed ==0:
# The craft has stopped rotating, so we can mark
# this task as being completed.
obj.status =TaskManager.STATUS_TYPES.COMPLETED
print("[_autopilot_stop_rotating] - Completed successfully.")
# _autopilot_stop_rotating() --------------------------------------------
@staticmethod
def _autopilot_zero_turn_speed(turn_speed, largest_turn_factor):
# Slow the crafts rotation as much as possible.
if turn_speed >0: # Craft is still turning.
if turn_speed >largest_turn_factor:
turn_speed -=largest_turn_factor
else:
# Speed is at a low enough level to
# reduce it to zero.
turn_speed =0
elif turn_speed <0: # Craft is still turning.
if turn_speed <(0 -largest_turn_factor):
turn_speed +=largest_turn_factor
else:
# Speed is at a low enough level to
# reduce it to zero.
turn_speed =0
return (turn_speed ==0), turn_speed
# -----------------------------------------------------------------------
def _autopilot_turn_to_angle(self, obj, update_task_status =True):
PHASE_NONBRAKING =1
PHASE_BRAKING =2
so =self.scrn_obj
par =self.parameters["_autopilot_turn_to_angle"]
var =self.variables["_autopilot_turn_to_angle"]
print("[_autopilot_turn_to_angle] - Triggerred successfully (Status:{0} Angle:{1}).".format(obj.status, so.angle))
if obj.status ==TaskManager.STATUS_TYPES.INIT:
var["routine_phase"] =PHASE_NONBRAKING
elif obj.status ==TaskManager.STATUS_TYPES.RUNNING:
# Check if the craft has completed its turn.
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
elif if self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
if var["routine_phase"] ==PHASE_NONBRAKING:
# --- Rotate Quickly ---
# Rotate the craft to the desired angle and
# do it as fast as possible, but make sure
# that the craft can come to a stop before
# overshooting the target angle.
# Need to make two versions of this while
# loop, one for rotating clockwise and
# another for rotating the other way.
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
fnd =False
#next_turning_factor_speed =self.scrn_obj._craft.max_turn_factor
next_turning_factor_speed =self.largest_turn_factor
rs =0
print("[_autopilot_turn_to_angle] - INFO #1: The autopilot turned clockwise (next turning factor:{0}).".format(next_turning_factor_speed))
while rs <=self.largest_turn_factor:
while next_turning_factor_speed >= 0:
# Find the value for the next best turning
# step increase.
new_turning_speed =so._turn_speed -rs +next_turning_factor_speed
new_angle =self.scrn_obj.angle +(new_turning_speed /GameInfo.FPS) # Advance the angle to the next time frame.
evaluation_result =self.evaluate_stop_turn(new_turning_speed, new_angle, par["angle"])
if evaluation_result ==True:
# We have found a a new turning speed
# that we can use without missing our
# target.
print("[_autopilot_turn_to_angle] - INFO #2: Found a new turning speed:{0}).".format(new_turning_speed))
fnd =True
break
next_turning_factor_speed -=1
if fnd:
break
else:
rs +=1
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
fnd =False
next_turning_factor_speed =(0 -self.largest_turn_factor)
print("[_autopilot_turn_to_angle] - INFO #3: The autopilot turned anticlockwise (next turning factor:{0}).".format(next_turning_factor_speed))
while next_turning_factor_speed >= 0:
# Find the value for the next best turning
# step increase.
new_turning_speed =so._turn_speed -rs +next_turning_factor_speed
new_angle =self.scrn_obj.angle -(new_turning_speed /GameInfo.FPS) # Advance the angle to the next time frame.
#evaluation_result =self.evaluate_stop_turn(new_turning_speed, self.scrn_obj.angle, par["angle"])
evaluation_result =self.evaluate_stop_turn(new_turning_speed, new_angle, par["angle"])
if evaluation_result ==True:
# We have found a a new turning speed
# that we can use without missing our
# target.
print("[_autopilot_turn_to_angle] - INFO #4: Found a new turning speed:{0}).".format(new_turning_speed))
fnd =True
break
next_turning_factor_speed +=1
else:
print("[_autopilot_turn_to_angle] - WARNING: The autopilot did NOT turn clockwise or anticlockwise.")
if fnd:
print("[_autopilot_turn_to_angle] - INFO #5: Turning speed:{0} (old:{1}).".format(new_turning_speed, so._turn_speed))
so._turn_speed =new_turning_speed -rs
else:
# Move to the next phase.
var["routine_phase"] =PHASE_BRAKING
print("\n[_autopilot_turn_to_angle] - INFO #6: Entered new phase 'PHASE_BRAKING'.")
elif var["routine_phase"] ==PHASE_BRAKING:
print("[_autopilot_turn_to_angle] - INFO: Turning phase #slower turn speed, turning speed:{0} (current speed:{1}) angle:{2}.".format(None, so._turn_speed, so.angle))
# Search for a turn speed that results in the craft turning as
# fast as possible yet able to stop at the desired angle without
# over-shooting.
r =0
while r <self.largest_turn_factor: # < Turn factor increase ?
# Make a prediction on the craft stopping at the desired
# angle with the current speed settings.
source_angle =so.angle
turn_speed =so._turn_speed
current_angle =source_angle +((turn_speed -r) *self._time_slice)
largest_turn_factor =self.largest_turn_factor
stop_trying =False
while stop_trying ==False:
result, turn_speed =self._autopilot_zero_turn_speed(turn_speed, largest_turn_factor)
if result ==False:
# The craft needs more time to stop the rotation.
target_angle =par["angle"]
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
if source_angle >target_angle:
target_angle +=360
if current_angle <target_angle:
# There's still a chance of stopping before
# the desired target angle.
stop_trying =False
else:
stop_trying =True
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
if source_angle <target_angle:
target_angle +=360
if current_angle >target_angle:
# There's still a chance of stopping before
# the desired target angle.
stop_trying =False
else:
stop_trying =True
else:
# Even if we did nothing at this point the craft
# can still come to a stop before over shooting
# the target.
break
if not turn_speed ==0:
# We need to apply the brakes on the crafts
# rotation because its turning to fast.
r +=1
else:
# The craft can stop the ratation in time to
# hit the target angel without over-shooting.
if r >0:
# The craft can only stop at the desired
# target if we reduce the speed now.
so._turn_speed -=r # Update the speed of rotation.
break
if update_task_status ==True:
if so._turn_speed ==0:
obj.status =TaskManager.STATUS_TYPES.COMPLETED
self.parameters["_autopilot_turn_to_angle"] =par
self.variables["_autopilot_turn_to_angle"] =var
print("[_autopilot_turn_to_angle] - Status:", obj.status)
# _.
# autopilot_turn_to_angle() ---------------------------------------------
def activate_autopilot(self):
if self._autopilot ==cls.AUTOPILOT_OFF:
self._autopilot =cls.AUTOPILOT_ON
self._autopilot_init()
# activate_autopilot() --------------------------------------------------
def autopilot_init(self):
'Creates a set of tasks to bring the craft back to the screen center.'
# Do the stuff neccessary to start the autopilot.
print("[autopilot_init] - Started ...")
# Determine angle of flight.
self._aof =self.scrn_obj.evaluate_flight_path() # Angle of Flight.
self._breaking_angle =None # Oppoosit to Angle of Flight.
self._craft_is_moving =False
self.autopilot_rotation =None # Type of rotation.
self.largest_turn_factor =None # Largest turn increment value.
self._RT =None # Rotation types.
# >>> Find the Oppoosite Angle of Flight <<<
# ----------------------------------------------
# In order to neutralise the speed to come to
# a stand still the craft needs to face in the
# opposite direction of the angle of flight,
# calculate the breaking angle.
self._breaking_angle =self._aof +180
while self._breaking_angle >360: # Attempt to rectify angle.
self._breaking_angle -=360
# _.
# >>> Turn Clockwise or Anti-Clockwise <<
# ----------------------------------------------
# In order for the craft to turn and face in
# the opposite direction to the angle of flight
# the craft needs to turn clockwise or ant-
# clockwise. Unfortunately the craft could be
# facing in any direction prior to the
# autopilot kicking in.
#
# Call the routine to determine the shortest
# turning circle.
c_val, a_val=self.scrn_obj.clockwise_anticlockwise(self._aof, self._breaking_angle )
if c_val <=a_val: # Its quicker to turn clockwise.
#my_rotation =RT.CLOCKWISE
self.autopilot_rotation =self.scrn_obj._craft.RT.CLOCKWISE
else: # Its quicker to turn anti-clockwise.
#my_rotation =RT.ANTICLOCKWISE
self.autopilot_rotation =self.scrn_obj._craft.RT.ANTICLOCKWISE
# _.
self.largest_turn_factor =self.scrn_obj._craft.max_turn_factor /GameInfo.FPS
# --- Build the Task List ---
#
# Build a task list for the autopilot to follow
# to return the craft to the center of the screen.
self._tasks.clear_tasks()
self._tasks.add(self.autopilot_stop_rotating())
self._tasks.add(self.autopilot_point_craft_in_oaf())
self._tasks.add(self.autopilot_stop_moving())
self._tasks.add(self.autopilot_aim_at_center_of_screen())
# _.
## active_task.completed =True
# _autopilot_init() ------------------------------------------------------
def autopilot_aim_at_center_of_screen(self):
'Function - generates a task (definition) and registers it in the queue.'
obj =TaskManager.definition()
obj.name ="autopilot_aim_at_center_of_screen"
obj.linked_routine = self._autopilot_stop_rotating
# Store parameter information in this
# class.
par ={}
var ={}
#par["x"] =121
self.parameters["_autopilot_aim_at_center_of_screen"] =par
self.variables["_autopilot_point_craft_in_oaf"] =var
# _.
return obj
# autopilot_aim_at_center_of_screen() -----------------------------------
def autopilot_point_craft_in_oaf(self):
'Function - generates a task (definition) and registers it in the queue.'
obj =TaskManager.definition()
obj.name ="autopilot_point_craft_in_oaf"
obj.linked_routine = self._autopilot_point_craft_in_oaf
# Store parameter information in this
# class.
par ={}
var ={}
#par["x"] =121
self.parameters["_autopilot_point_craft_in_oaf"] =par
self.variables["_autopilot_point_craft_in_oaf"] =var
# _.
return obj
# autopilot_point_craft_in_oaf() ----------------------------------------
def autopilot_stop_moving(self):
'Function - generates a task (definition) and registers it in the queue.'
obj =TaskManager()
obj.name ="autopilot_stop_moving"
obj.linked_routine =self._autopilot_stop_moving
# Store parameter information in this
# class.
par ={}
var ={}
#par["x"] =121
self.parameters["_autopilot_stop_moving"] =par
self.variables["_autopilot_stop_moving"] =var
# _.
return obj
# autopilot_stop_moving() -----------------------------------------------
def autopilot_stop_rotating(self):
'Function - generates a task (definition) and registers it in the queue.'
obj =TaskManager.definition()
obj.name ="autopilot_stop_rotating"
obj.linked_routine = self._autopilot_stop_rotating
# Store parameter information in this
# class.
par ={}
var ={}
#par["x"] =121
self.parameters["_autopilot_stop_rotating"] =par
self.variables["_autopilot_stop_rotating"] =var
# _.
return obj
# autopilot_stop_rotating() ------------------------------------------------------
def autopilot_turn_to_angle(self, angle):
'Function - generates a task (definition) and registers it in the queue.'
obj =TaskManager.definition()
obj.name ="autopilot_turn_to_angle"
obj.linked_routine = self._autopilot_turn_to_angle
# Store parameter information in this
# class.
par ={}
var ={}
par["angle"] =angle
self.parameters["_autopilot_turn_to_angle"] =par
self.variables["_autopilot_turn_to_angle"] =var
print("[autopilot_turn_to_angle] - Parameters and variables stored successfully.")
# _.
return obj
# autopilot_turn_to_angle() ---------------------------------------------
def evaluate_stop_turn(self, turning_speed, current_angle, target_angel, recurrence =False):
eval_result =False
run_it_again =False
calculated_angle =None
# Only do this section once.
if recurrence ==False:
# Preceed to calibrate the angle if neccessary.
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
if target_angel <current_angle:
# Adjudst the [target_angle] so its relative to the
# current angle.
target_angel +=360
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
if target_angel >current_angle:
# Adjust the [current_angle] so its relative to the
# target angle so we can perform a simple
# comparison.
current_angle +=360
# _.
# -- Reduce the turning speed ---
#
# Reduce the turing speed simulating maximum breaking.
#if turning_speed >0:
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
#rotation_type =rotation_types.CLOCKWISE
if turning_speed >self.scrn_obj._craft.max_turn_factor:
# Craft is rotating to fast to come to a
# complete stop at the moment.
turning_speed -=self.scrn_obj._craft.max_turn_factor
else:
# We can stop the rotation completly.
#print("[evaluate_stop_turn] - INFO: Turning clockwise we can stop the rotation completly.")
turning_speed =0
#elif turning_speed <0:
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
#rotation_type =rotation_types.ANTICLOCKWISE
if turning_speed <(0 -self.scrn_obj._craft.max_turn_factor):
# Craft is rotating to fast to come to a
# complete stop at the moment.
turning_speed +=self.scrn_obj._craft.max_turn_factor
else:
# We can stop the rotation completly.
#print("[evaluate_stop_turn] - INFO: Turning anticlockwise we can stop the rotation completly.")
turning_speed =0
else:
# Looks like the craft isn't rotating.
print("[evaluate_stop_turn] - WARNING: Turning speed is zero.")
self._autopilot_rotation =rotation_types.NIETHER
# Calculate the angle travelled by the craft over
# the set time frame.
if turning_speed ==0:
additional_angles =0
else:
additional_angles =(turning_speed /GameInfo.FPS)
# _.
if self.autopilot_rotation ==rotation_types.CLOCKWISE:
calculated_angle =current_angle +additional_angles
if calculated_angle <=target_angel:
# --- Within Range ---
# At the present rate of turning the craft
# will rotate through a number of angles over the
# set period within the specifed target range.
if turning_speed ==0:
# Our evaluation of the stopping range has
# been completed.
eval_result =True
else:
run_it_again =True
elif self.autopilot_rotation ==rotation_types.ANTICLOCKWISE:
calculated_angle =current_angle -additional_angles
if calculated_angle >=target_angel:
# --- Within Range ---
# At the present rate of turning the craft
# will rotate through a number of angles over the
# set period within the specifed target range.
if turning_speed ==0:
# Our evaluation of the stopping range has
# been completed.
eval_result =True
else:
run_it_again =True
# DEBUG information
if calculated_angle ==None:
print(" [evaluate_stop_turn] - INFO #1: Angle:{0} Turning speed:{1} Target angle:{2} (Rotation type:{3} Result:{4}).".format(current_angle, turning_speed, target_angel, self.autopilot_rotation, eval_result))
else:
if run_it_again:
print(" [evaluate_stop_turn] - INFO #2: Angle:{0} Turning speed:{1} Target angle:{2} (Rotation type:{3} Result:{4}).".format(calculated_angle, turning_speed, target_angel, self.autopilot_rotation, "n/a"))
else:
print(" [evaluate_stop_turn] - INFO #2: Angle:{0} Turning speed:{1} Target angle:{2} (Rotation type:{3} Result:{4}).".format(calculated_angle, turning_speed, target_angel, self.autopilot_rotation, eval_result))
if run_it_again ==True:
# Since the rotation speed hasn't been
# neutralised to zero we need to see how
# far the craft will rotate before coming to a
# complete stop.
# perform an evaluation on the next time frame.
#
# * Note(s)
# Setting [recurrence] =True ensures that the target
# angle can only be calibrated once.
#
#eval_result =self.evaluate_stop_turn(turning_speed, calculated_angle, target_angel)
eval_result =self.evaluate_stop_turn(turning_speed, calculated_angle, target_angel, recurrence =True)
# _.
# Return the result of the time
# frame analysis to the calling
# routine.
if eval_result ==False:
# DEBUG information
if calculated_angle ==None:
print(" [evaluate_stop_turn] - INFO #3: Routine calculated that the craft is turning to fast."
"\n * Angle:{0} Turning speed:{1} Target angle:{2}.".format(current_angle, turning_speed, target_angel))
else:
print(" [evaluate_stop_turn] - INFO #4: Routine calculated that the craft is turning to fast."
"\n * Angle:{0} Turning speed:{1} Target angle:{2}.".format(calculated_angle, turning_speed, target_angel))
return eval_result
# evaluate_stop_turn() --------------------------------------------------
