Skip to content
Snippets Groups Projects
Commit 3e3333e0 authored by Memo Akten's avatar Memo Akten
Browse files

MemoControllerUtils.thrustTo

parent 007f57b5
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@ public class MemoControllerRandom implements BattleController {
public double ATTACK_PROB = 0.1;
public double ATTACK_SHOOT_PROB = 1;
public double ATTACK_THRUST_PROB = 0.01;
public double ATTACK_ROT_THRESH = 0.1;
public double ATTACK_ROT_THRESH = 0.01;
public double FLEE_ROT_CHANGE_PROB = 0.3;
public double FLEE_SHOOT_PROB = 0.1;
public double FLEE_THRUST_PROB = 0.8;
......@@ -38,6 +38,8 @@ public class MemoControllerRandom implements BattleController {
if(Math.random() < ATTACK_PROB) {
action.thrust = Math.random() < ATTACK_THRUST_PROB ? 1 : 0;
action.shoot = Math.random() < ATTACK_SHOOT_PROB;
// TODO, include velocity vector in target pos
action.turn = MemoControllerUtils.lookAt(thisShip.s, thisShip.d, otherShip.s, ATTACK_ROT_THRESH);
} else{
action.thrust = Math.random() < FLEE_THRUST_PROB ? 1 : 0;
......
package battle.controllers.Memo;
import math.Vector2d;
import asteroids.Action;
/**
* Created by Memo Akten on 11/06/2015.
*/
public class MemoControllerUtils {
static double lookAt(Vector2d s, Vector2d d, Vector2d lookat, double threshold) {
// returns turn value (-1, 0, 1)
static double lookAt(Vector2d s, Vector2d d, Vector2d lookat, double rot_threshold) {
Vector2d desired_rot_vec = lookat.copy();
desired_rot_vec.add(s, -1);
double current_rot = Math.atan2(d.y, d.x);
double target_rot = Math.atan2(desired_rot_vec.y, desired_rot_vec.x);
if(Math.abs(current_rot - target_rot) < threshold) {
if(Math.abs(current_rot - target_rot) < rot_threshold) {
return 0;
} else {
if(current_rot > target_rot) return -1;
......@@ -21,5 +23,16 @@ public class MemoControllerUtils {
}
}
// fills in Action with turn and thrust
static double thrustTo(Vector2d s, Vector2d d, Vector2d desired_pos, double dist_threshold, double rot_threshold, Action action) {
double dist_to_desired_pos = s.dist(desired_pos);
if(dist_to_desired_pos < dist_threshold) {
action.thrust = 0;
} else {
action.thrust = 1;
action.turn = lookAt(s, d, desired_pos, rot_threshold);
}
return action.thrust;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment