$old_direction = $horse_detail->get_direction(); $heading_x = $old_pos_x + cos($old_direction) * 20; $heading_y = $old_pos_y - sin($old_direction) * 20; // find the angle between the heading and the target $angle = $track->find_angle($heading_x, $heading_y, $old_pos_x, $old_pos_y, $target_pt[0], $target_pt[1]); // Calculate new heading (old direction adjusted by the angle between heading and target $new_direction = $old_direction - $angle / 20; // optional debug information /*if ($horseid == 1) { print "TS: $ts pos=($old_pos_x, $old_pos_y) tar=(${target_pt[0]}, ${target_pt[1]}) head=($heading_x, $heading_y) lane=(${lane_pt[0]},${lane_pt[1]}) [$old_lane] dir=$new_direction $ angle = $angle\n"; }*/ // TODO: adjust current speed // check collision with horse ahead and slow down if needed $distance_to_my_nose = $track->distance_along_lane($old_lane, $lane_pt[0], $lane_pt[1]); // adjust for number of laps around $distance_to_my_nose += $track->lane_length($old_lane) * $horse_detail->get_lap_counter(); foreach ($horses as $h) { /** @var horse_details $hd */ $hd = $game_state->get_details($h->get_id()); if ($h->get_id() == $horseid) { continue; } if ($hd->get_lane() != $old_lane) { continue; } $distance_to_their_nose = $track->distance_along_lane($hd->get_lane(), $hd->get_position_x(), $hd->get_position_y()); // adjust for number of laps around $distance_to_their_nose += $track->lane_length($hd->get_lane()) * $hd->get_lap_counter(); if ($distance_to_their_nose < $distance_to_my_nose) { continue; }