class SteeringObject { public: //! Current Position Vector2D position; //! Velocity Vector2D velocity; //! Maximum Speed per frame double maxSpeed; public: //! Seek behaviour /** * The seek behaviour will return a desired * velocity below the max speed, that should * move twards the desiredPosition * * Example usage: velocity = behaviourSeek(badGuy.getPosition()); */ Vector2D behaviourSeek(Vector2D desiredPosition) { // desiredVelocity is the distance between here and // where we want to be. if we acheived this, we would // 'zap' there Vector2D desiredVelocity = position - desiredPosition; // turns 5,5 into, say, 0.5, 0.5, so we can max-speed-alise desiredVelocity.Normalize(); // Now multiply 0.5,0.5 by maxspeed, which we pretend for this // example is 3, so we're 1.5,1.5 . desiredVelocity *= maxSpeed; // Now return the desired velocity return desiredVelocity; } //! Flee Behaviour /** * This behaviour will generate a velocity * to move *away* from the provided vector2d * position. */ Vector2D behaviourFlee(Vector2D desiredPosition) { // desiredVelocity is the distance between here and // where we want to be. if we acheived this, we would // 'zap' there Vector2D desiredVelocity = desiredPosition - position; // turns 5,5 into, say, 0.5, 0.5, so we can max-speed-alise desiredVelocity.Normalize(); // Now multiply 0.5,0.5 by maxspeed, which we pretend for this // example is 3, so we're 1.5,1.5 . desiredVelocity *= maxSpeed; // Now return the desired velocity return desiredVelocity; } };