// this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            if (lastpos.Count < 1)
            {
                action.accelerate = 1f;
                steeringfactor    = (float)new Random().NextDouble() * 2 - 1;
                return(action);
            }
            lastpos.Add(this.positionV);

            if (distance(lastpos[0].X, lastpos[0].Y, currentGoalV.X, currentGoalV.Y) < distance(positionX, positionY, currentGoalV.X, currentGoalV.Y))
            {
                action = lastactions[0];
            }
            else
            {
                action            = lastactions[0];
                action.accelerate = action.steer * -1;
            }
            action.accelerate = 1f;

            // insanely awesome bot code goes here
            Random random = new Random();

            random.Next(-1, 1);


            // set action.<> values to steer your bot
            action.accelerate = 1f;

            //action.steer = steerLeft() ? -steeringfactor : steeringfactor;
            action.steer = steeringfactor;

            action.brake = (float)Math.Round(0.2f); // use Math. for math calculations

            // type this. to see what you have access to
            //this.<>
            //this.currentGoalV f.ex.

            humanIntervene(action);
            lastpos.Clear();
            lastactions.Add(action);
            return(action);
        }
Beispiel #2
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // insanely awesome bot code goes here

            // set action.<> values to steer your bot
            action.accelerate = rand.NextFloat();
            action.steer      = 1f;
            action.brake      = (float)Math.Round(0.2f); // use Math. for math calculations

            // type this. to see what you have access to
            //this.<>
            //this.currentGoalV f.ex.

            return(action);
        }
 private EnvCarRace.Action check_actions(EnvCarRace.Action action, Ranges range)
 {
     if (values[range.value_idx, 3] == 0)
     {
         if (values[range.value_idx, 0] >= range.start && values[range.value_idx, 0] <= range.end)
         {
             action = apply_action(action, range.acition);
         }
     }
     else
     {
         if ((range.end > 0.5f && range.start > values[range.value_idx, 0]) ||
             (range.end <= 0.5 && range.start <= values[range.value_idx, 0]))
         {
             action = apply_action(action, range.acition);
         }
     }
     return(action);
 }
Beispiel #4
0
        protected void Start_Maneuver(ref EnvCarRace.Action action)
        {
            float direction = Get_Direction(this.currentGoalV);

            if (direction < -0.01)
            {
                action.accelerate = 0.8f;
                action.steer      = -1f;
            }
            else if (direction > 0.01)
            {
                action.accelerate = 0.8f;
                action.steer      = 1f;
            }
            else
            {
                //this.carState = CarState.Collecting;
            }
        }
Beispiel #5
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            action.accelerate = 1f;

            switch (carState)
            {
            case CarState.Started:
                Start_Maneuver(ref action);
                break;

            case CarState.Collecting:
                Collect(ref action);
                break;
            }

            return(action);
        }
 private void humanIntervene(EnvCarRace.Action action)
 {
     if (Input.up.down)
     {
         action.accelerate = 1f;
     }
     if (Input.down.down)
     {
         action.brake = 1f;
     }
     if (Input.left.down)
     {
         action.steer--;
     }
     if (Input.right.down)
     {
         action.steer++;
     }
 }
Beispiel #7
0
        private Tuple <EnvCarRace.Action, float> vec_to_steer()
        {
            float steer_const = 1.0f;

            Vector2 goal_vec = new Vector2(this.currentGoalV.X - this.positionV.X, this.currentGoalV.Y - this.positionV.Y);

            float bot_dir             = (float)Math.Atan2(this.velocityV.Y, this.velocityV.X);
            float goal_dir_rel_to_bot = (float)Math.Atan2(goal_vec.Y, goal_vec.X);

            // Fix over 0 rads
            if (bot_dir < goal_dir_rel_to_bot)
            {
                float dist        = Math.Abs(goal_dir_rel_to_bot - bot_dir);
                float dist_padded = Math.Abs((bot_dir + (float)(Math.PI * 2)) - goal_dir_rel_to_bot);

                bot_dir = dist < dist_padded ? bot_dir : bot_dir + (float)(Math.PI * 2.0f);
            }
            else
            {
                float dist        = Math.Abs(bot_dir - goal_dir_rel_to_bot);
                float dist_padded = Math.Abs((goal_dir_rel_to_bot + (float)(Math.PI * 2)) - bot_dir);

                goal_dir_rel_to_bot = dist < dist_padded ? goal_dir_rel_to_bot : goal_dir_rel_to_bot + (float)(Math.PI * 2.0f);
            }

            EnvCarRace.Action act = new EnvCarRace.Action();

            float dist_out = Math.Abs(goal_dir_rel_to_bot - bot_dir) / (float)(Math.PI * 2.0f);

            // Right
            if (goal_dir_rel_to_bot - bot_dir > 0)
            {
                act.steer = 1.0f;
            }
            // Left
            else if (bot_dir - goal_dir_rel_to_bot > 0)
            {
                act.steer = -1.0f;
            }

            return(new Tuple <EnvCarRace.Action, float>(act, dist_out));
        }
        private EnvCarRace.Action apply_action(EnvCarRace.Action action, int action_int)
        {
            // Steer
            if (action_int == 0)
            {
                action.steer = -1;
            }
            else if (action_int == 1)
            {
                action.steer = 0;
            }
            else if (action_int == 2)
            {
                action.steer = 1;
            }
            else if (action_int == 3)
            {
                action.accelerate = 0;
            }
            else if (action_int == 4)
            {
                action.accelerate = 1;
            }
            else if (action_int == 5)
            {
                action.brake = 0;
            }
            else if (action_int == 6)
            {
                action.brake = 1;
            }

            values[6, 0] = action.steer;
            values[7, 0] = action.accelerate;
            values[8, 0] = action.brake;

            return(action);
        }
Beispiel #9
0
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            if (Input.w.down)
            {
                action.accelerate = 1f;
            }
            if (Input.s.down)
            {
                action.brake = 1f;
            }
            if (Input.a.down)
            {
                action.steer--;
            }
            if (Input.d.down)
            {
                action.steer++;
            }

            return(action);
        }
Beispiel #10
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            float current_dist = (this.currentGoalV - this.positionV).Length();
            float direction    = Get_Direction(this.currentGoalV);
            int   next_dest    = this.goalIndex + 1;

            if (next_dest >= this.goals.Count)
            {
                next_dest = this.goalIndex;
            }
            float next_direction = Get_Direction(this.goals[next_dest]);

            action.brake = 0f;

            if (direction < -0.01)
            {
                action.steer = -1;
            }
            else if (direction > 0.01)
            {
                action.steer = 1;
            }
            else
            {
                action.steer = 0;
            }

            action.accelerate = 1f;

            if (this.velocityV.Length() > 30)
            {
                float drift_dist = current_dist / currentGoalV.Length();
                if (drift_dist > 10)
                {
                    drift_dist = 10;
                }


                //if(Math.Abs(direction) - Math.Abs(next_direction))


                if ((current_dist / currentGoalV.Length()) < (this.velocityV.Length() * 0.004))
                {
                    if (next_direction < -0.01)
                    {
                        action.steer = -1;
                    }
                    else if (next_direction > 0.01)
                    {
                        action.steer = 1;
                    }
                    else
                    {
                        action.steer = 0;
                    }

                    action.accelerate = 0;
                }
            }

            return(action);
        }
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            currentGoal     = this.currentGoalV;
            currentPosition = this.positionV;
            meToGoal        = currentGoal - currentPosition;
            meToGoal.Normalize();

            if (this.goalIndex < this.goals.Count - 1)
            {
                nextGoal = this.goals[goalIndex + 1];
            }
            else
            {
                nextGoal = null;
            }

            if (nextGoal != null)
            {
                meToNextGoal = nextGoal.Value - currentPosition;
                meToNextGoal = Vector2.Normalize(meToNextGoal.Value);

                currentGoalToNextGoal = nextGoal - currentGoal;
                currentGoalToNextGoal = Vector2.Normalize(currentGoalToNextGoal.Value);
            }
            else
            {
                meToNextGoal = null;
            }

            if (this.goalIndex > 0)
            {
                lastGoal = this.goals[goalIndex - 1];
            }
            else
            {
                lastGoal = null;
            }

            if (lastGoal != null)
            {
                meToLastGoal = lastGoal - currentPosition;
                meToLastGoal = Vector2.Normalize(meToLastGoal.Value);
            }
            else
            {
                meToLastGoal = null;
            }

            lineOfView = new Vector2 {
                X = this.directionRightV.Y,
                Y = -this.directionRightV.X
            };
            lineOfView.Normalize();

            lineOfViewOrthogonalRight = directionRightV;
            lineOfViewOrthogonalRight.Normalize();

            lineOfViewOrthogonalLeft = Vector2.Negate(lineOfViewOrthogonalRight);
            lineOfViewOrthogonalLeft.Normalize();


            distanceToCurrentGoal = Vector2.Distance(currentGoal, currentPosition);

            directionToCurrentGoal = Vector2.Dot(directionRightV, meToGoal);

            if (nextGoal != null)
            {
                directionToNextGoal = Vector2.Dot(directionRightV, meToNextGoal.Value);

                angleCurrentGoalToNextGoal = (Vector2.Dot(meToGoal, currentGoalToNextGoal.Value) / (meToGoal.Length() * currentGoalToNextGoal.Value.Length()));


                //Console.WriteLine(directionToCurrentGoal + " " + directionToNextGoal + " " + angleCurrentGoalToNextGoal + "\n");

                nextGoalInCurrentDirection = angleCurrentGoalToNextGoal > .75;
            }


            angleCurrentDirectionToCurrentGoal = (Vector2.Dot(meToGoal, lineOfView) / (meToGoal.Length() * lineOfView.Length()));

            currentGoalInCurrentDirection = angleCurrentDirectionToCurrentGoal > .75;


            if (distanceToLastGoal == null)
            {
                distanceToLastGoal = -100000;
            }

            if (directionToCurrentGoal > 0)
            {
                if ((distanceToCurrentGoal < TRIGGER_DISTANCE_TO_GOAL && !nextGoalInCurrentDirection.Value) || (distanceToLastGoal.Value < TRIGGER_DISTANCE_TO_GOAL && !currentGoalInCurrentDirection))
                {
                    action.accelerate = .1f;
                    action.brake      = 1f;
                }
                else
                {
                    action.accelerate = 1f;
                    action.brake      = 0f;
                }
            }
            else
            {
                action.accelerate = .2f;
            }


            float toGoalDir = Vector2.Dot(directionRightV, meToGoal);

            if (distanceToCurrentGoal > TRIGGER_DISTANCE_TO_GOAL)
            {
                if (toGoalDir < 0)
                {
                    action.steer = -1f;
                }
                else
                {
                    action.steer = 1f;
                }
            }
            else
            {
                if (nextGoal != null)
                {
                    float toNextGoal = Vector2.Dot((Vector2)meToNextGoal, directionRightV);
                    if (toNextGoal < 0)
                    {
                        action.steer = -.5f;
                    }
                    else
                    {
                        action.steer = .5f;
                    }
                }
            }

            return(action);
        }
Beispiel #12
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // create rotation matrix (0, 2pi) gegen uhrzeigersinn
            Vector2 rotateV(Vector2 vec, double alpha)
            {
                Vector2 vec_rot = new Vector2();

                vec_rot.X = (float)Math.Cos(alpha) * vec.X + (float)Math.Sin(alpha) * vec.Y;
                vec_rot.Y = -(float)Math.Sin(alpha) * vec.X + (float)Math.Cos(alpha) * vec.Y;
                return(vec_rot);
            }

            Vector2 goal_orientation      = this.currentGoalV - this.positionV;
            Vector2 orientation_rot_left  = rotateV(this.orientationV, 0.5f * Math.PI);
            Vector2 orientation_rot_right = rotateV(this.orientationV, 1.5f * Math.PI);
            float   orient_angle          = (float)Math.Acos(Vector2.Dot(this.orientationV, goal_orientation) / (this.orientationV.Length() * goal_orientation.Length()));
            float   angle_left            = (float)Math.Acos(Vector2.Dot(orientation_rot_left, goal_orientation) / (orientation_rot_left.Length() * goal_orientation.Length()));
            float   angle_right           = (float)Math.Acos(Vector2.Dot(orientation_rot_right, goal_orientation) / (orientation_rot_left.Length() * goal_orientation.Length()));

            bool next_target_bad_angle;

            if (this.goalIndex < this.goals.Count - 1)
            {
                Vector2 next_goal_orientation = this.goals[this.goalIndex + 1] - this.positionV;

                float next_orient_angle = (float)Math.Acos(Vector2.Dot(this.orientationV, next_goal_orientation) / (this.orientationV.Length() * next_goal_orientation.Length()));

                float next_goal_brake_th = 2 * MathHelper.Pi / 4; // angle
                next_target_bad_angle = next_orient_angle > next_goal_brake_th;

                // target the next goal if next to current goal
                float target_change_th = 5.0f;
                if (goal_orientation.Length() < target_change_th)
                {
                    float next_angle_left  = (float)Math.Acos(Vector2.Dot(orientation_rot_left, next_goal_orientation) / (orientation_rot_left.Length() * next_goal_orientation.Length()));
                    float next_angle_right = (float)Math.Acos(Vector2.Dot(orientation_rot_right, next_goal_orientation) / (orientation_rot_left.Length() * next_goal_orientation.Length()));

                    angle_left  = next_angle_left;
                    angle_right = next_angle_right;
                }
            }
            else
            {
                next_target_bad_angle = false;
            }



            // insanely awesome bot code goes here
            float max_acc         = 1.0f;  // maximum acceleration
            float brake_threshold = 30.0f; // 30 is a reasonable value for the distance to goal

            action.brake = 0.0f;           // -> normally dont brake


            // break if near goal and next goal is not directly assessed

            if (this.goalIndex != this.goals.Count - 1)
            {
                if (goal_orientation.Length() < brake_threshold && next_target_bad_angle) // brake if near to goal
                {
                    action.accelerate = 0.0f;
                    action.brake      = 1.0f;
                }
                else
                {
                    action.accelerate = max_acc; // in [0, 1]
                }
            }
            else
            {
                action.accelerate = max_acc; // in [0, 1]
            }



            // best steering
            float angle_brake_th = 4 * MathHelper.Pi / 8;
            float acc_cap        = 0.0f;
            float steering_brake = 1.0f;

            if (angle_left < angle_right) // steer left
            {
                action.steer = -1.0f;
                if (orient_angle > angle_brake_th)
                {
                    action.accelerate = 0.0f;
                    action.brake      = steering_brake;
                }
            }
            else if (angle_left > angle_right) // steer right
            {
                action.steer = 1.0f;
                if (orient_angle > angle_brake_th)
                {
                    action.accelerate = 0.0f;
                    action.brake      = steering_brake;
                }
            }
            else // driving away or towards goal
            {
                if (Vector2.Dot(this.orientationV, this.currentGoalV - this.positionV) > 0) // driving towards the goal
                {
                    //Console.WriteLine("driving towards the goal");
                    action.steer = 0.0f;
                }
                else  // driving away from the goal
                {
                    action.steer = 1.0f;  // choose either direction
                }
            }


            // correct if too slow
            float min_speed_fast = 22.5f; //
            float min_speed_slow = 10.0f; // if orient_angle is larger than pi/2

            if (orient_angle > MathHelper.Pi / 2 && this.velocityV.Length() < min_speed_slow)
            {
                action.accelerate = 1.0f;
                action.brake      = 0.0f;
            }
            else if (orient_angle <= MathHelper.Pi / 2 && this.velocityV.Length() < min_speed_fast)
            {
                action.accelerate = 1.0f;
                action.brake      = 0.0f;
            }

            return(action);
        }
Beispiel #13
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            // brake if prediction aims wrong
            prediction = positionV + velocityV * 0.7f;
            predictionForCheckpoint = positionV + velocityV * 0.75f;

            for (int i = 0; i < predictions.Length; i++)
            {
                Vector2 vN = Vector2.Normalize(new Vector2(-velocityV.Y, velocityV.X));
                float   currentSteeringInComparisonToVelocity = Vector2.Dot(vN, orientationV);

                float ijau = ((float)i / predictions.Length);
                predictions[i] = positionV + velocityV * ijau;// + directionRightV * currentSteeringInComparisonToVelocity * ((float)Math.Pow(ijau, 1.2f)) * 10f;
            }

            EnvCarRace.Action action = new EnvCarRace.Action();

            // insanely awesome bot code goes here

            // set action.<> values to steer your bot
            //action.accelerate = rand.NextFloat();
            //action.steer = 1f;
            //action.brake = (float)Math.Round(0.2f); // use Math. for math calculations


            if (goalIndex > 0)
            {
                reachedFirst = true;
            }

            if (goalIndex + 1 > trackIndex)
            {
                reachedTrack = false;
            }

            if (goalIndex == goals.Count - 1)
            {
                reachedTrack = true;
            }


            Vector2 goal;

            if (!reachedTrack)
            {
                float scurvejau = sCurve[goalIndex] ? 0.75f : 0.5f;
                tInArray = trackIndex * trackPointsPerGoal + (int)(trackPointsPerGoal * scurvejau);

                Vector2 trackTangent = Vector2.Normalize(track[tInArray + 1] - track[tInArray]);
                Vector2 distToPlayer = predictionForCheckpoint - track[tInArray];

                if ((Vector2.Dot(distToPlayer, trackTangent) > 0) ||
                    ((currentGoalV - positionV).Length() < (currentGoalV - track[tInArray]).Length()))
                {
                    //if (trackIndex / trackPointsPerGoal < goalIndex + 1)
                    //if (trackIndex < goalIndex + 2)
                    //{
                    trackIndex++;
                    //trackTangent = Vector2.Normalize(track[tInArray + 1] - track[tInArray]);
                    //distToPlayer = positionV - track[tInArray];
                    reachedTrack = true;
                    //}
                    //else
                    //    break;
                }

                //nextIndex = trackIndex;// (int)(trackIndex + velocityV.Length() / ((currentGoalV - (goalIndex > 0 ? goals[goalIndex - 1] : startPos)).Length() / 40f));

                //if ((nextIndex / trackPointsPerGoal) > goalIndex)
                //{
                //    nextIndex = (goalIndex + 1) * trackPointsPerGoal;
                //    tryCoolCurveOverGoal = true;
                //}
                //else
                //    tryCoolCurveOverGoal = false;

                Console.WriteLine(velocityV.Length());
                if (tInArray > track.Count - 1)
                {
                    tInArray = track.Count - 1;
                }

                goal = track[tInArray];// this.currentGoalV;
            }
            else
            {
                goal = currentGoalV;
            }
            Vector2 dist = goal - positionV;

            //float distAngle = (float)Math.Atan2(dist.Y, dist.X);

            //float angle = distAngle - orientation;
            //if (Math.Abs(angle) > MathHelper.Pi)
            //    angle -= MathHelper.TwoPi;


            Vector2 distNormalized = Vector2.Normalize(dist);

            float toRight = (Vector2.Dot(directionRightV, distNormalized));


            //Vector2 x1 = track[nextIndex] - track[nextIndex - 1];
            //Vector2 x2 = orientationV;//positionV - track[nextIndex - 1];
            //Vector2 n1 = new Vector2(-x1.Y, x1.X);

            //float toRight2 = Vector2.Dot(n1, x2);

            //if (toRight + toRight2 * 100 < 0)

            if (toRight >= 0)
            {
                action.steer = 1f;
            }
            else
            {
                action.steer = -1f;
            }



            bool    overGoal         = false;
            Vector2 goalToPos        = positionV - currentGoalV;
            Vector2 goalToPrediction = prediction - currentGoalV;

            if (Vector2.Dot(goalToPos, goalToPrediction) < 0)
            {
                overGoal = true;
            }

            bool predictCol = false;

            foreach (var p in predictions)
            {
                if (Vector2.Distance(currentGoalV, p) < (env.goalRadius + width))
                {
                    predictCol = true;
                    break;
                }
            }

            if (overGoal && !predictCol)
            {
                action.brake = 1f;
            }



            float forward = (Vector2.Dot(orientationV, distNormalized));

            float d = dist.Length();

            if (goalIndex < goals.Count - 1)
            {
                //float curve = -Vector2.Dot(distNormalized, Vector2.Normalize(goals[goalIndex + 1] - goal));
                //if (curve > 0)
                //{
                //    float end = 10 + curve * 15;
                //    if (d < end)
                //    {
                //        d = 0;
                //        //d -= end * 2f / 3f;
                //    }
                //}
            }

            float targetVelocity = (float)(Math.Sign(forward) * Math.Abs(Math.Pow(forward, 10f))) * d * 7f;

            if (action.brake == 1f)
            {
                targetVelocity = 0f;
            }

            if (targetVelocity < 10f)
            {
                targetVelocity = 10f;
            }

            if (velocityV.Length() < targetVelocity)
            {
                action.accelerate = 1f;
            }
            else
            {
                action.brake = 1f;
            }

            // type this. to see what you have access to
            //this.<>
            //this.currentGoalV f.ex.

            return(action);
        }
Beispiel #14
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // goal after current goal:
            Vector2 nextGoalV = (this.goalIndex + 1 < this.goals.Count) ? this.goals[this.goalIndex + 1] : this.currentGoalV;

            // set action.<> values to steer your bot
            action.accelerate = 1f; // immer vollgas, keine rücksicht auf verluste
            // action.steer = 1f;
            // action.brake = (float)Math.Round(0.2f); // use Math. for math calculations

            // maybe add a steer penalty somewhere

            /*
             * Vector2 scalarV = Vector2.Multiply(this.directionRightV, Vector2.Subtract(this.currentGoalV, this.positionV));
             * float scalar = scalarV.X + scalarV.Y;
             * if (scalar > 0)
             * {
             *  action.steer = 1f;
             * }
             * else if (scalar < 0)
             * {
             *  action.steer = -1f;
             * }*/

            // secret approach: randomly try to get a nice curve, else straight at point
            if (this.rand.Next() % 10 < 4 && this.goalIndex != 0)
            {
                float steerPenalty = 0.5f;

                double currentAngle = getAngle(this.positionV, this.currentGoalV, nextGoalV);
                // angle in deg = currentAngle * 180 / MathHelper.Pi
                if (currentAngle < targetAngle - borderAngle)
                {
                    // steer away from current goal
                    action.steer = -steerPenalty;
                }
                else if (currentAngle < targetAngle)
                {
                    // steer towards current goal
                    action.steer = steerPenalty;
                }
                else if (currentAngle < targetAngle + borderAngle)
                {
                    // steer away from current goal (other direciton)
                    action.steer = steerPenalty;
                }
                else
                {
                    // steer towards current goal (other direciton)
                    action.steer = -steerPenalty;
                }
            }
            else
            {
                Vector2 scalarV = Vector2.Multiply(this.directionRightV, Vector2.Subtract(this.currentGoalV, this.positionV));
                float   scalar  = scalarV.X + scalarV.Y;
                if (scalar > 0)
                {
                    action.steer = 1f;
                }
                else if (scalar < 0)
                {
                    action.steer = -1f;
                }
            }



            // type this. to see what you have access to
            //this.<>

            return(action);
        }
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // insanely awesome bot code goes here

            // set action.<> values to steer your bot
            //action.accelerate = rand.NextFloat();
            //action.steer = 1f;
            //action.brake = (float)Math.Round(0.2f); // use Math. for math calculations


            Vector2 goal = this.currentGoalV;

            Vector2 dist = goal - positionV;

            //float distAngle = (float)Math.Atan2(dist.Y, dist.X);

            //float angle = distAngle - orientation;
            //if (Math.Abs(angle) > MathHelper.Pi)
            //    angle -= MathHelper.TwoPi;


            Vector2 distNormalized = Vector2.Normalize(dist);

            float toRight = (Vector2.Dot(directionRightV, distNormalized));

            if (toRight >= 0)
            {
                action.steer = 1f;
            }
            else
            {
                action.steer = -1f;
            }

            float forward = (Vector2.Dot(orientationV, distNormalized));

            float d = dist.Length();

            if (goalIndex < goals.Count - 1)
            {
                float curve = -Vector2.Dot(distNormalized, Vector2.Normalize(goals[goalIndex + 1] - currentGoalV));
                float end   = 15 + curve * 30;
                if (d < end)
                {
                    d -= end * 2f / 3f;
                }
            }

            float targetVelocity = (float)(Math.Sign(forward) * Math.Abs(Math.Pow(forward, 10f))) * d * 5f;

            if (targetVelocity < 10f)
            {
                targetVelocity = 10f;
            }

            if (velocityV.Length() < targetVelocity)
            {
                action.accelerate = 1f;
            }
            else
            {
                action.brake = 1f;
            }

            // type this. to see what you have access to
            //this.<>
            //this.currentGoalV f.ex.

            return(action);
        }
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // create rotation matrix (0, 2pi) gegen uhrzeigersinn
            Vector2 rotateV(Vector2 vec, double alpha)
            {
                Vector2 vec_rot = new Vector2();

                vec_rot.X = (float)Math.Cos(alpha) * vec.X + (float)Math.Sin(alpha) * vec.Y;
                vec_rot.Y = -(float)Math.Sin(alpha) * vec.X + (float)Math.Cos(alpha) * vec.Y;
                return(vec_rot);
            }

            Vector2 goal_orientation      = this.currentGoalV - this.positionV;
            Vector2 orientation_rot_left  = rotateV(this.orientationV, 0.5f * Math.PI);
            Vector2 orientation_rot_right = rotateV(this.orientationV, 1.5f * Math.PI);

            // insanely awesome bot code goes here
            float max_speed       = 1.0f; // speed=acceleration
            float brake_threshold = 10.0f;

            action.brake = 0.0f; // in [0, 1] -> normally dont brake

            // accelerate only if far away from the goal
            if (this.goalIndex != this.goals.Count)
            {
                if (goal_orientation.Length() < brake_threshold) // brake if near to goal
                {
                    action.accelerate = 0.0f;
                    action.brake      = 1.0f;
                }
                else
                {
                    action.accelerate = max_speed; // in [0, 1]
                }
            }
            else
            {
                action.accelerate = max_speed; // in [0, 1]
            }



            /*
             * Console.WriteLine(this.orientationV);
             * Console.WriteLine(orientation_rot_left);
             * Console.WriteLine(orientation_rot_right);
             * Console.WriteLine();*/

            float angle_left  = (float)Math.Acos(Vector2.Dot(orientation_rot_left, goal_orientation) / (orientation_rot_left.Length() * goal_orientation.Length()));  // norm ignored
            float angle_right = (float)Math.Acos(Vector2.Dot(orientation_rot_right, goal_orientation) / (orientation_rot_left.Length() * goal_orientation.Length())); // norm ignored

            //Console.WriteLine(angle_left);
            //Console.WriteLine(angle_right);

            if (angle_left < angle_right)
            {
                action.steer = -1.0f;
            }
            else if (angle_left > angle_right)
            {
                action.steer = 1.0f;
            }
            else
            {
                if (Vector2.Dot(this.orientationV, this.currentGoalV - this.positionV) > 0) // driving towards the goal
                {
                    //Console.WriteLine("driving towards the goal");
                    action.steer = 0.0f;
                }
                else  // driving away from the goal
                {
                    action.steer = 1.0f;  // choose either direction
                }
            }


            return(action);
        }
Beispiel #17
0
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // insanely awesome bot code goes here


            Vector2 goalDirection = currentGoalV - positionV;

            goalDirection.Normalize();
            float forward = Vector2.Dot(goalDirection, orientationV);

            Vector2 goaldirectionTotal = currentGoalV - positionV;

            float goalposition = goaldirectionTotal.Length();

            if (goalIndex > 0)
            {
                Vector2 lastgoaldirection      = goals[goalIndex - 1];
                Vector2 lastgoaldirectionTotal = lastgoaldirection - positionV;
                float   lastgoal = lastgoaldirectionTotal.Length();


                if ((goalposition > 20 && lastgoal > 5 && forward > 0.8f || velocityV.Length() < 10))
                {
                    action.accelerate = 1;
                }
                else
                {
                    action.brake = 1f;
                }


                float toRight = Vector2.Dot(goalDirection, directionRightV);

                if (toRight < 0)
                {
                    action.steer--;
                }
                else
                {
                    action.steer++;
                }
            }
            else
            {
                if ((goalposition > 20 && forward > 0.8f || velocityV.Length() < 10))
                {
                    action.accelerate = 1;
                }
                else
                {
                    action.brake = 1f;
                }


                float toRight = Vector2.Dot(goalDirection, directionRightV);

                if (toRight < 0)
                {
                    action.steer--;
                }
                else
                {
                    action.steer++;
                }
            }

            // set action.<> values to steer your bot
            // action.accelerate = rand.NextFloat();
            // action.steer = 0f;        // steer >0 right; steer <0 left
            // action.brake = (float)Math.Round(0.2f); // use Math. for math calculations

            // type this. to see what you have access to
            //this.<>

            //this.currentGoalV f.ex.

            return(action);
        }
        // this function is called every frame
        protected override EnvCarRace.Action GetAction()
        {
            EnvCarRace.Action action = new EnvCarRace.Action();

            // insanely awesome bot code goes here

            // set action.<> values to steer your bot
            //action.accelerate = rand.NextFloat();
            //action.steer = 1f;
            //action.brake = (float)Math.Round(0.2f); // use Math. for math calculations

            // type this. to see what you have access to
            //this.<>
            //this.currentGoalV f.ex.

            int nextGoalIndex = this.goalIndex + 1;

            Vector2?nextGoal;

            if (this.goalIndex < this.goals.Count - 1)
            {
                nextGoal = this.goals[nextGoalIndex];
            }
            else
            {
                nextGoal = null;
            }

            Vector2 currGoal     = this.currentGoalV;
            Vector2 currPos      = this.positionV;
            Vector2 vectorToGoal = currGoal - currPos;

            vectorToGoal.Normalize();

            Vector2 lineOfView = new Vector2
            {
                X = this.directionRightV.Y,
                Y = -this.directionRightV.X
            };

            lineOfView.Normalize();

            Vector2?currentGoalToNextGoal = nextGoal - currGoal;


            Vector2 vectorToGoalOrthogonal = new Vector2
            {
                X = -vectorToGoal.Y,
                Y = vectorToGoal.X
            };



            float distanceToCurrGoal = Vector2.Distance(currGoal, currPos);

            float toGoal = Vector2.Dot(lineOfView, vectorToGoal);

            if (toGoal > 0)
            {
                if (distanceToCurrGoal > 20)
                {
                    action.accelerate = 1f;
                    action.brake      = 0f;
                }
                else
                {
                    action.accelerate = .7f;
                    action.brake      = 1f;
                }
            }
            else
            {
                action.accelerate = .1f;
            }


            float toGoalDir = Vector2.Dot(directionRightV, vectorToGoal);

            //Console.WriteLine(toGoalDir);


            if (distanceToCurrGoal > 10)
            {
                action.steer = toGoalDir;
            }
            else
            {
                if (nextGoal != null)
                {
                    Vector2 meToNextGoal = currPos - (Vector2)nextGoal;
                    meToNextGoal.Normalize();
                    bool nextGoalRight = true;

                    float toNextGoal = Vector2.Dot((Vector2)meToNextGoal, directionRightV);
                    action.steer = -toNextGoal;
                }
            }



            return(action);
        }