Game AI & Unity/Java Steering game

[Game AI][Steering Behavior] Task_Arrive (controllers.ArriveController)

bay07 2024. 3. 21. 06:28

 

얘는 파일 자체가 없기 때문에, 새로 만들어주어야한다. 

앞에 만들었던 Controller 코드를 참고해서 만들면 된다. 

 

package controllers;

import engine.Car;
import engine.Game;
import engine.GameObject;
import java.awt.Color;

 

먼저, 필요한 패키지와 클래스들을 가져온다

 

public class ArriveController extends Controller {
    private GameObject target;

    public ArriveController(GameObject target) {
        this.target = target;
   
    }
}

 

Controller에 있는 내용들을 상속을 해서 그대로 받아주고, 

그 안에 ArriveController를 만들어준다. 그리고 매개변수로는 GameObject target을 넣어준다. 

 

    @Override
    public void update(Car subject, Game game, double delta_t, double[] controlVariables) {
        // Calculate angle to seek the target
        double targetAngle = Math.atan2(target.getY() - subject.getY(), target.getX() - subject.getX());

        // Normalize angle to be between -π and π
        targetAngle = (targetAngle + Math.PI) % (2 * Math.PI) - Math.PI;

        // Calculate the difference between current and target angle
        double angleDiff = targetAngle - subject.getAngle();

        // Normalize angle difference to be between -π and π
        angleDiff = (angleDiff + Math.PI) % (2 * Math.PI) - Math.PI;

        // Map angle difference to steering control
        double steering = angleDiff / Math.PI;

        // Set steering control
        controlVariables[VARIABLE_STEERING] = steering;

        // Apply throttle to move forward
        controlVariables[VARIABLE_THROTTLE] = 1;

        // Apply brake if the car is close to the target
        double distanceToTarget = Math.sqrt(Math.pow(target.getX() - subject.getX(), 2) + Math.pow(target.getY() - subject.getY(), 2));
        if (distanceToTarget < 50) { // Adjust the threshold as needed
            controlVariables[VARIABLE_THROTTLE] = 0; // Stop moving forward
            controlVariables[VARIABLE_BRAKE] = 1; // Apply brake
        } else {
            controlVariables[VARIABLE_BRAKE] = 0; // Release brake
        }

 

그리고 그 안에 경로계산에 필요한 수학적 연산들을 적어준다. 

이렇게 하면 대략적으로 ArriveController가 완성된다.

 

▷ 전체 코드 

더보기
package controllers;

import engine.Car;
import engine.Game;
import engine.GameObject;
import java.awt.Color;

public class ArriveController extends Controller {
    private GameObject target;

    public ArriveController(GameObject target) {
        this.target = target;
    }

    @Override
    public void update(Car subject, Game game, double delta_t, double[] controlVariables) {
        // Calculate angle to seek the target
        double targetAngle = Math.atan2(target.getY() - subject.getY(), target.getX() - subject.getX());

        // Normalize angle to be between -π and π
        targetAngle = (targetAngle + Math.PI) % (2 * Math.PI) - Math.PI;

        // Calculate the difference between current and target angle
        double angleDiff = targetAngle - subject.getAngle();

        // Normalize angle difference to be between -π and π
        angleDiff = (angleDiff + Math.PI) % (2 * Math.PI) - Math.PI;

        // Map angle difference to steering control
        double steering = angleDiff / Math.PI;

        // Set steering control
        controlVariables[VARIABLE_STEERING] = steering;

        // Apply throttle to move forward
        controlVariables[VARIABLE_THROTTLE] = 1;

        // Apply brake if the car is close to the target
        double distanceToTarget = Math.sqrt(Math.pow(target.getX() - subject.getX(), 2) + Math.pow(target.getY() - subject.getY(), 2));
        if (distanceToTarget < 50) { // Adjust the threshold as needed
            controlVariables[VARIABLE_THROTTLE] = 0; // Stop moving forward
            controlVariables[VARIABLE_BRAKE] = 1; // Apply brake
        } else {
            controlVariables[VARIABLE_BRAKE] = 0; // Release brake
        }
    }
}

 

Steering_Task_Arrive1.zip
0.04MB