Skip to content

Instantly share code, notes, and snippets.

@UtMan88 UtMan88/Steering.cpp
Created Sep 26, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.