Kinematic

Kinematic movement

This module implements a series of classes and methods that emulate the behavior of objects moving in a 2D space in a kinematic way (involving acceleration)

Notes

This might need a slightly better explaination

SteeringOutput

class steering.kinematic.SteeringOutput(linear=None, angular=None)[source]

Container for Steering data

This class is used as a container for the output of the KinematicSteeringBehavior algorithms.

These objects can be added, multiplied, and compared to eachother. Each of these operations will be executed element-wise

Parameters:
  • linear (pygame.math.Vector2, optional) – Linear acceleration, defaults to (0, 0)
  • angular (int, optional) – Angular acceleration, defaults to 0
linear

Linear acceleration

Type:pygame.math.Vector2
angular

Angular acceleration

Type:int
update(gameobject, tick)[source]

Update a GameObject’s velocity and rotation

This method should be called once per loop, it updates the given GameObject’s velocity and rotation based on this SteeringOutput’s acceleration request

Parameters:
  • gameobject (GameObject) – The Game Objectthat will be updated
  • tick (int) – Time transcurred since last loop
kinematic.negative_steering(angular)

Returns a steering request opposite to the linear and angular accelerations provided.

Parameters:
Returns:

Return type:

SteeringOutput

steering.kinematic.null_steering

Constant with 0 linear acceleration and 0 angular acceleration

Type:SteeringOutput

KinematicSteeringBehavior

class steering.kinematic.KinematicSteeringBehavior[source]

Template KinematicSteeringBehavior class

This class is a template to supply base methods for KinematicSteeringBehaviors. This class is meant to be subclassed since the methods here are just placeholders

draw_indicators(screen, offset=<function KinematicSteeringBehavior.<lambda>>)[source]

Draws appropiate indicators for each KinematicSteeringBehavior

Parameters:
  • screen (pygame.Surface) – Surface in which to draw indicators, normally this would be the screen Surface
  • offset (function, optional) –

    Function that applies an offset to the object’s position

    This is meant to be used together with scrolling cameras, leave empty if your game doesn’t implement one,it defaults to a linear function f(pos) -> pos

get_steering()[source]

Returns a steering request

Returns:Requested steering
Return type:SteeringOutput
class steering.kinematic.Align(character, target, target_radius=1, slow_radius=20, time_to_target=0.1)[source]

KinematicSteeringBehavior that makes the character Align with the target’s orientation

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Align with it’s orientation at
  • target_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will stop rotation
  • slow_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will start to slow rotation
  • time_to_target (float, optional) – Estimated time, in seconds, to Align with the target’s orientation
class steering.kinematic.Arrive(character, target, target_radius=None, slow_radius=None, time_to_target=0.2)[source]

KinematicSteeringBehavior that makes the character Arrive at a target

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Arrive at
  • target_radius (int, optional) – Distance from the center of the target at which the character will stop
  • slow_radius (int, optional) – Distance from the center of the target at which the character will start to slow down
  • time_to_target (float, optional) – Estimated time, in seconds, to Arrive at the target
class steering.kinematic.CollisionAvoidance(character, targets, radius=None)[source]

KinematicSteeringBehavior that makes the character Avoid Collision with a list of targets

This behavior looks at the velocities of the character and the targets to determine if they will collide in the next few loops, and if they will, it accelerates away from the collision point

This behavior is meant to be used in combination with other behaviors, see steering.blended.BlendedSteering .

Parameters:
  • character (GameObject) – Character with this behavior
  • targets (list(GameObject)) – Targets to avoid collision with
  • radius (int, optional) – Distance at which the future positions of the character and any target are are considered as colliding
class steering.kinematic.Drag(linear_strength=10, angular_strength=1)[source]

KinematicSteeringBehavior that applies a Drag to the character

This behavior should be applied to every GameObject in every loop (unless it’s meant to be permanently stationary). It applies an acceleration contrary to it’s current linear and angular velocity.

Parameters:strenght (float, optional) – The strength of the drag to apply, should be a number in the range (0, 1], any number outside of that range will have unexpected behavior.
class steering.kinematic.Evade(character, target, max_prediction_time=0.2)[source]

KinematicSteeringBehavior that makes the character Evade the target

This behavior tries to predict the target’s future position based on the direction it is currently moving, and then Flee s from that

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Evade
  • max_prediction_time (float, optional) – Maximum time, in seconds, to look ahead while predicting future position
class steering.kinematic.Face(character, target, target_radius=1, slow_radius=10, time_to_target=0.1)[source]

KinematicSteeringBehavior that makes the character Face the target

This behavior creates a DummyGameObject that is looking in the direction of the target and then Align s with that dummy’s orientation

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Face
  • target_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will stop rotation
  • slow_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will start to slow rotation
  • time_to_target (float, optional) – Estimated time, in seconds, to Face the target
class steering.kinematic.Flee(character, target)[source]

KinematicSteeringBehavior that makes the character Flee from a target

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Flee from
class steering.kinematic.FollowPath(character, path)[source]

KinematicSteeringBehavior that makes the character Follow a Path

This behavior makes the character follow a particular Path. It will do so until the character has traversed all points in it.

Parameters:
class steering.kinematic.LookWhereYoureGoing(character, target_radius=1, slow_radius=20, time_to_target=0.1)[source]

KinematicSteeringBehavior that makes the character Look Where He’s Going

This behavior makes the character face in the direction it’s moving by creating a DummyGameObject that is looking in the direction of the character’s velocity and then it Align s with that.

This behavior is meant to be used in combination with other behaviors, see steering.blended.BlendedSteering .

Parameters:
  • character (GameObject) – Character with this behavior
  • target_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will stop rotation
  • slow_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will start to slow rotation
  • time_to_target (float, optional) – Estimated time, in seconds, to LookWhereYoureGoing
class steering.kinematic.NullSteering[source]

KinematicSteeringBehavior that makes the character Stay Still

class steering.kinematic.ObstacleAvoidance(character, obstacles, avoid_distance=None, lookahead=None)[source]

KinematicSteeringBehavior that makes the character Avoid Obstacles

This behavior looks ahead in the current direction the character is moving to see if it will collide with any obstacle, and if it does, creates a target away from the collision point and Seek s that.

The difference between this and CollisionAvoidance is that the Obstacles are considered to be a rectangular shape of a any size, while the targets are normally almost-square-sized.

This behavior is meant to be used in combination with other behaviors, see steering.blended.BlendedSteering .

Parameters:
  • character (GameObject) – Character with this behavior
  • obstacles (list(GameObject)) – Obstacles to avoid collision with
  • avoid_distance (int, optional) – Distance from the collision point at which the target that the algorithm uses to avoid collision will be generated
  • lookahead (int, optional) – Distance to look ahead in the direction of the player’s velocity
class steering.kinematic.Pursue(character, target, max_prediction_time=0.2)[source]

KinematicSteeringBehavior that makes the character Purse the target

This behavior tries to predict the target’s future position based on the direction it is currently moving, and then Seek s that

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to Pursue
  • max_prediction_time (float, optional) – Maximum time, in seconds, to look ahead while predicting future position
class steering.kinematic.Seek(character, target)[source]

KinematicSteeringBehavior that makes the character Seek a target

Parameters:
class steering.kinematic.Separation(character, targets, treshold=None)[source]

KinematicSteeringBehavior that makes the character Separate itself from a list of targets

Parameters:
  • character (GameObject) – Character with this behavior
  • targets (list(GameObject)) – Targets to stay separated from
  • treshold (int, optional) – Distance from any of the targets at which the character will start separate from them
class steering.kinematic.VelocityMatch(character, target, time_to_target=0.1)[source]

KinematicSteeringBehavior that makes the character match the velocity of the target

Parameters:
  • character (GameObject) – Character with this behavior
  • target (GameObject) – Target to match it’s velocity
  • time_to_target (float, optional) – Estimated time, in seconds, to reach the target’s velocity
class steering.kinematic.Wander(character, wander_offset=50, wander_radius=15, wander_rate=20)[source]

KinematicSteeringBehavior that makes the character Wander

This behavior makes the character move with it’s maximum speed in a random direction that feels smooth, meaning that it does not rotate too abruptly. This generates a target in front of the character and Seek s it while applying :py:class:`LookWhereYoureGoing, you can use the KinematicSteeringBehavior.draw_indicators() to see how the target is generated. This Behavior also uses LookWhereYoureGoing.

Parameters:
  • character (GameObject) – Character with this behavior
  • wander_offset (int, optional) – Distance in front of the character to generate target to Seek
  • wander_radius (int, optional) – Radius of the circumference in front of the character in which the target will generated
  • wander_rate (int, optional) – Angles, in degrees, that the target is allowed to move along the circumference
  • align_target_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will stop rotation
  • slow_radius (int, optional) – Distance, in degrees, from the target orientation at which the character will start to slow rotation
  • align_time (float, optional) – Estimated time, in seconds, to LookWhereYoureGoing