Module pygravity.twod
This is the 2D implementation of pygravity
Classes
Vector2 GravityContainer GravityCaster GravityAcceptor PhysicsManager
Packages
util Utility classes pygame_simulation Rendering tools that use pygravity.twod.util and pygame
Expand source code
"""This is the 2D implementation of pygravity
Classes
-------
Vector2
GravityContainer
GravityCaster
GravityAcceptor
PhysicsManager
Packages
--------
util
Utility classes
pygame_simulation
Rendering tools that use pygravity.twod.util and pygame
"""
from pygravity.twod.gravity import (GravityAcceptor, GravityCaster,
GravityContainer)
from pygravity.twod.physics import PhysicsManager
from pygravity.twod.vector import Vector2
__all__ = ['Vector2',
'GravityContainer', 'GravityCaster', 'GravityAcceptor',
'PhysicsManager']
Sub-modules
pygravity.twod.gravity-
Generic 2D gravity related classes for pygravity
pygravity.twod.physics-
An object to hold the current position and velocity of physics-enabled objects (2D pygravity)
pygravity.twod.pygame_simulationpygravity.twod.utilpygravity.twod.vector-
Vector2 implementation used by pygravity
Classes
class GravityAcceptor (position: Vector2, container: GravityContainer, physics_manager: PhysicsManager)-
Represents a body that can get pulled on by other spatial bodies
Attributes
position:Vector2- The current position of the body (in meters)
container:GravityContainer- The container with GravityCasters that can pull on this body
physics_manager:PhysicsManager- The manager to which to add calculated velocity to
Instance variables
var container-
Return an attribute of instance, which is of type owner.
var physics_manager-
Return an attribute of instance, which is of type owner.
var position-
Return an attribute of instance, which is of type owner.
Methods
def calculate(self, time_passed: float) ‑> Vector2-
Calculates the things that happened to this body within time_passed The unit for time_passed is seconds This function returns the velocity that was applied to this body
class GravityCaster (position: Vector2, mass: float)-
Represents a spatial body that can pull on other spatial bodies
Attributes
position:Vector2- The current position of the body (in meters)
mass:float- The mass of the body (in kilograms)
Instance variables
var mass-
Return an attribute of instance, which is of type owner.
var position-
Return an attribute of instance, which is of type owner.
class GravityContainer (*casters: GravityCaster)-
List optimized for GravityCaster objects
Methods
def add_caster(self, caster: GravityCaster)-
Adds a caster to this containter
def add_casters(self, *casters: GravityCaster)-
Adds multiple casters to this container (potentially slower than add_caster)
def remove_caster(self, caster: GravityCaster) ‑> GravityCaster-
Removes the specified caster from this container and returns it
class PhysicsManager (position: Vector2, velocity: Vector2 = None)-
An object to hold the current position and velocity of physics-enabled objects
Attributes
Instance variables
var position-
Return an attribute of instance, which is of type owner.
var velocity-
Return an attribute of instance, which is of type owner.
Methods
def add_velocity(self, x: float, y: float)-
Adds the specified velocity to the object
def add_velocity_vector(self, velocity: Vector2)-
Same as add_velocity but uses a Vector2 instead of x and y amounts
def calculate(self, time_passed: float) ‑> Vector2-
Updates this object's position based on current velocity The unit for time used anywhere in pygravity is seconds This function returns the amount that the body moved (equal to self.velocity * time_passed)
class Vector2 (x: float = 0, y: float = 0)-
Attributes
x:float- The X coordinate.
y:float- The Y coordinate.
Instance variables
var x-
Return an attribute of instance, which is of type owner.
var y-
Return an attribute of instance, which is of type owner.
Methods
def as_direction_magnitude(self) ‑> Tuple[float, float]-
Return (self.direction(), self.magnitude())
def as_tuple(self) ‑> Tuple[float, float]-
Return (self.x, self.y)
def copy(self) ‑> Vector2-
Create a copy of this vector and return it
def direction(self) ‑> float-
Return the direction this vector is facing (in degrees)
def from_direction_magnitude(cls, angle: float, distance: float) ‑> Vector2-
Create a vector from an angle and length. Angle is in degrees.
def magnitude(self) ‑> float-
Return this vector's magnitude
def normalize(self)-
Mutably normalize this vector so it has a length of 0, while keeping the same direction
def set_to(self, x: float, y: float)-
Mutably update this vector's x and y coordinates to x and y, respectively
def sqr_magnitude(self) ‑> float-
Return the square of this vector's magnitude. This is faster to compute because the square root doesn't have to calculated.