Skip to content

Instantly share code, notes, and snippets.

@Chase-san
Last active December 13, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chase-san/4132e0696156d64ca7c5 to your computer and use it in GitHub Desktop.
Save Chase-san/4132e0696156d64ca7c5 to your computer and use it in GitHub Desktop.
Not a whiff of rotations.
package sample;
import roboflight.Robot;
import roboflight.RobotPeer;
import roboflight.UpdateEvent;
import roboflight.util.Vector;
public class Edge implements Robot {
public RobotPeer peer;
@Override
public void setRobotPeer(RobotPeer peer) {
this.peer = peer;
}
@Override
public void onTurnStarted() {}
@Override
public void onUpdate(UpdateEvent e) {
//fire at the enemy
peer.setFireBullet(e.getPosition().sub(peer.getPosition()));
}
Vector thrust = new Vector(1,0,0);
@Override
public void onTurnEnded() {
//set in some direction
peer.setThrust(thrust);
Vector p = peer.getPosition();
if(p.length() > 900) {
//basically we need a perpendicular tangent space vector,
//this sounds complicated but is actually rather simple
//We need an outward vector from the origin to our position,
//which we can get by normalizing our position
p.normalize();
//Now we need a sample perpendicular tangent space vector,
//in this case, up
Vector up = new Vector(0,1,0);
//now we just take the cross product and use that as our thrust
thrust = Vector.cross(p, up);
//WHOOPS, now we have a problem, when we run into the walls we
//stop, so we have to point inward a bit.
//So lets just add the inverse of the outward vector to our
//thrust, that will make us thrust more inward.
//the inverse of our outward from origin vector
p.scale(-1);
//But if we scale down the inward to origin vector, we can
//make a smoother turning curve
p.scale(0.25);
//Add it
thrust.add(p);
//This step isn't required as the game normalizes the thrust
//if it is over MAX_ROBOT_THRUST.
thrust.normalize();
peer.setThrust(thrust);
//Now you're thinking with vectors!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment