Skip to content

Instantly share code, notes, and snippets.

@UtMan88
Created September 26, 2017 00:42
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 UtMan88/5df7edc7e5cd4d58b5b0254968c7070d to your computer and use it in GitHub Desktop.
Save UtMan88/5df7edc7e5cd4d58b5b0254968c7070d to your computer and use it in GitHub Desktop.
ArduBoy Mock-Steering (Seek) Algorithm
struct FPoint {
float x; /**< The X coordinate of the point */
float y; /**< The Y coordinate of the point */
};
FPoint steer(const FPoint& target, const FPoint& pos, const FPoint& curForward) {
// Direction to target
FPoint dir;
dir.x = target.x - pos.x;
dir.y = target.y - pos.y;
// Normalize direction
float mag;
mag = sqrt(sq(dir.x) + sq(dir.y));
dir.x = dir.x/mag;
dir.y = dir.y/mag;
// Steer
return turnDirection(dir, curForward);
}
FPoint turnDirection(const FPoint& dir, const FPoint& curForward) {
float mag;
FPoint fwdSum;
// Faking it, we find the vector between curForward & dir
fwdSum.x = dir.x + curForward.x;
fwdSum.y = dir.y + curForward.y;
// Normalize
mag = sqrt(sq(fwdSum.x) + sq(fwdSum.y));
FPoint steerVel;
steerVel.x = fwdSum.x/mag;
steerVel.y = fwdSum.y/mag;
return steerVel;
}
void Example() {
//AI Update
// Villain AI
if(g_villSize >= 1.0f && arduboy.everyXFrames(VILLAIN_UPDATE_DELAY)) {
FPoint steering = steer(g_objects[PLAYER_INDX].position, g_objects[VILLAIN_INDX].position, g_objects[VILLAIN_INDX].forward);
g_objects[VILLAIN_INDX].forward = steering;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment