controllers.SeekController
먼저 빈 파일 하나 만들어줬다. 이름은 SeekController.java 로 만들어주면 된다
그리고 안에 Controller.java에 있던 내용을 가져올 것이다.
- Controller에 있던 코드
package controllers;
import engine.Car;
import engine.Game;
/**
*
* @author santi
*/
public abstract class Controller {
/*
commands is an array with three components:
- the desired "STEER" (-1 to +1)
- the desired "THROTTLE" (0 to +1)
- the deired "BRAKE" (0 to +1)
*/
public static final int VARIABLE_STEERING = 0;
public static final int VARIABLE_THROTTLE = 1;
public static final int VARIABLE_BRAKE = 2;
public abstract void update(Car subject, Game game, double delta_t, double controlVariables[]);
}
이 코드들을 가져오올 수 있는 가장 좋은 방법이 상속을 하는 것이다.
package controllers;
import engine.Car;
import engine.Game;
import engine.GameObject;
public class SeekController extends Controller {
private GameObject target;
public SeekController(GameObject target) {
this.target = target;
}
}
GameObject target에 Controller에 있는 내용을 상속해준다.
그리고 SeekController에도 지정을 해준다.
package controllers;
import engine.Car;
import engine.Game;
import engine.GameObject;
public class SeekController extends Controller {
private GameObject target;
public SeekController(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;
}
}
각 과정에 필요한 수학적인 연산을 해준다.
▷ 작성한 전체 코드
controllers.SeekController
더보기
package controllers;
import engine.Car;
import engine.Game;
import engine.GameObject;
public class SeekController extends Controller {
private GameObject target;
public SeekController(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;
}
}
'Game AI & Unity > Java Steering game' 카테고리의 다른 글
[Game AI][Steering Behavior] Task_Arrive (Problem) (0) | 2024.03.21 |
---|---|
[Game AI][Steering Behavior] 19. Task_Seek (0) | 2024.03.21 |
[Game AI][Steering Behavior] 17. Task_Seek (test.SeekScenario ) (0) | 2024.03.20 |
[Game AI][Steering Behavior] 16. Task _Seek (Problem) (0) | 2024.03.20 |
[Game AI][Steering Behavior] 15 KeyboardExample.java (0) | 2024.03.20 |