Physics module

Physics contains the objects relating to mathematical calculations, such as Vectors, Forces, and PhysicsObjects. There are no UI components in this module.

All displacements are calculated on an abstract plane - Ui.PhysicsCanvas is responsible for translating to canvas coordinates.

One semi-exception - PhysicsObjects handle their own collision detection, and they do that by referencing the collection of PhysicsObjects in PhysicsCanvas.

All the physics logic and calculation should be handled here.

class Physics.Force(angle, magnitude, duration=1.0, constant=False)[source]

Bases: Physics.Vector

A force which operates over time, executing a single force on the object once per sec.

If constant is true, the force does not deplete and will continue acting on the object.

PhysicsObjects have lists of forces currently operating on them. As the PhysicsObject updates, it gets a Vector representing a force for each force acting on it. That force is scaled based on the time of the update interval. It also reduces the ‘remaining’ attribute of the Force until the Force is depleted.

Parameters
  • angle (number) – Angle in radians

  • magnitude (number) – Magnitude in Newtons (per second)

  • duration (number) – Seconds force is exerted for

  • constant (bool) – Whether force depletes or not

static make_directional_force(direction, magnitude, duration=1.0, constant=False)[source]

Similar to Vector.make_directional_vector, creates a force in a cardinal direction.

Parameters
  • direction (str) – N,S,E,W,NE,SE,NW,SW

  • magnitude (number) – In Newtons

  • duration (number) – In seconds

  • constant (boolean) – Whether force depletes

Returns

A new force

Return type

Force

update(interval)[source]

Scales the current vector magnitude to the interval, so that self.force_magnitude is delivered each second; if updates occur more frequently than once a second, this causes this forces temporary magnitude to be lower to account for that.

Parameters

interval (number) – Time since last update, in seconds.

class Physics.GravitationalForceGenerator(planet, moon)[source]

Bases: object

remove()[source]
update(interval)[source]

Gets the location of self.planet and self.moon, figures out F_G between them, then adds opposite forces to each one reduced by the interval.

\(F_G = \frac{Gm_1m_2}{r^2}\)

Parameters

interval – Update time, seconds :type interval: number

class Physics.PhysicsObject(material, mass)[source]

Bases: object

An object which has vectors for acceleration, velocity, and displacement.

Abstracts the physics calculations - Ui.PhysicsCanvas is responsible for the rendering, and translating the displacement of the PhysicsObject into the Tkinter Canvas coordinate space.

Each update, it determines how much force should be applied based on the interval and the list of forces currently affecting this object.

From the net force and mass, it calculates acceleration. From the acceleration, it calculates velocity. From velocity, displacement is calculated.

\(F_{net} = \sum{F}\text{ Newtons}\)

\(a = \frac{F_\text{net}}{m}\text{ m/s}^2\)

\(v = at + v_0 \text{ m/s}\)

\(s = vt + s_0 = \frac{1}{2}at^2+v_0t+s_0 \text{ m}\)

Parameters
  • material (Substance.Material) – The material from Substance.Material used in this object. Determines its color and size based on the material density.

  • mass (Number) – The mass of the object. (kg)

acceleration

A vector of acceleration magnitude and angle

canvas_id

Used by the tkinter canvas to reference the shape linked to this object

check_collision(interval)[source]

The way collision is checked is that the next displacement from the velocity is calculated.

For each other extant physics object on the canvas, the next displacement from velocity is calculated.

If the lines between the displacements cross, the vectors have ‘collided’.

This function returns True or False and is used in the update function. If collision doesn’t happen, the update function handles simple movement.

Parameters

interval

Returns

Whether collision happened

Return type

bool

clear_forces()[source]

Clears forces from self.dependent_force_generators by calling remove() on each

collide(other_object, my_next_displacement, other_next_displacement, interval)[source]

Called by the check_collision function. Next displacements calculated there are passed as parameters to avoid redundant calculations.

The elastic collision formulas:

link - https://imada.sdu.dk/~rolf/Edu/DM815/E10/2dcollisions.pdf

Parameters
  • other_object (ForceObject) – The colliding object

  • my_next_displacement (Vector) – Displacement vector that would realize if no collision

  • other_next_displacement (Vector) – Displacement vector for other object that would realize if no collision

  • interval (number) – Interval of distance move, second(s)

dependent_force_generators

Force generators like Physics.GravitationalForceGenerator

displacement

A vector of positional offset from the 0,0 of world origin

forces

Currently active forces affecting this object

get_energy_vector()[source]
height

height in m

mass

mass in kg

material

The material the PhysicsObject is made of

physics_canvas

Reference to canvas added when object rendered on canvas

side

Length of a side in m

update(interval)[source]

Gets a new vector equal to multiplying acceleration by Velocity and adds it to the velocity, mutating the velocity vector. \(v = at + v_0\) Does the same for displacement. \(s = vt + s_0\) Tells physicsCanvas to move the rendering of the oval.

Parameters

interval (number) – The time since last update.

velocity

A vector of velocity magnitude and angle

width

width in m

class Physics.Vector(angle, magnitude)[source]

Bases: object

Represents direction and magnitude. Vectors can be broken into x and y components and reassembled from those components.

Parameters
  • angle (number) – The angle, in radians

  • magnitude (number) – The magnitude of the Vector

add(other_vector)[source]

Use this to MUTATE this Vector by adding another Vector to it.

The other Vector will be unchanged.

Parameters

other_vector (Vector) – A vector to add to this one.

add_make(other_vector)[source]

Use this to have a new vector return from the addition and the input vectors to be unchanged.

No existing Vector will mutate.

Parameters

other_vector (Vector) – A vector to add to this one.

Returns

A brand new Vector

Return type

Vector

calculate_angles()[source]

Calculates angle and mag from components.

\(\theta= \arctan{(\frac{y}{x})}\)

\(\text{mag} = \sqrt{x^2+y^2}\)

calculate_components()[source]

Calculates components from angle and magnitude.

\(y = \text{vector}_\text{mag} * \sin{\theta}\)

\(x = \text{vector}_\text{mag} * \cos{\theta}\)

dot_product(other_vector)[source]

Gets the scalar dot product of this and another vector Non mutating

Parameters

other_vector – A vector to multiply with this one

Returns

A scalar

Return type

number

static make_directional_vector(direction='S', magnitude=1)[source]

Static method which creates a directional Vector from compass coordinates passed as a character.

Direction string controls vector angle. I.e.: ‘E’: 0 radians (0 degrees) ‘W’: 3.1415 radians (180 degrees)

Valid directions are e,ne,n,nw,w,sw,s,se

Parameters
  • direction (str) – character for direction

  • magnitude (number) – vector magnitude

static make_vector_from_components(x, y)[source]

Static: Create a new Vector from x and y values. :param x: The x component :type x: number :param y: The y component :type y: number :returns: A brand new Vector :rtype: Vector

normal_make()[source]

Returns a new magnitude 1 vector in the same angle.

Returns

new Vector

Return type

Vector

rotate(radians)[source]

Mutates this Vector by adding radians to its angle and recalculating its components.

Parameters

radians (number) – Radians to add to this Vector’s angle.

scale(scalar)[source]

Mutates this Vector by multiplying its magnitude by the scalar.

Parameters

scalar (number) – Scalar to multiply this Vector’s angle by.

scale_make(scalar)[source]

Creates a new Vector of a magnitude equal to this Vector’s magnitude multiplied by the scalar, and returns the new Vector..

Parameters

scalar (number) – Scalar to multiply this Vector’s angle by.

Returns

A brand new Vector

Return type

Vector

subtract(other_vector)[source]

Use this to MUTATE this Vector by subtracting another Vector from it.

The other Vector will be unchanged.

Parameters

other_vector (Vector) – A vector to subtract from this one.

subtract_make(other_vector)[source]

Use this to have a new vector return from the subtraction and the input vectors to be unchanged.

No existing Vector will mutate.

Parameters

other_vector (Vector) – A vector to subtract FROM this one.

Returns

A brand new Vector

Return type

Vector