Beispiel #1
0
    def apply_force(self, force, point=(0, 0)):
        point = vec2d.Vec2D(point[0], point[1])
        point -= self._cog

        force = vec2d.Vec2D(force[0], force[1])

        self._forces += force
        self._torque += point.cross(force)
Beispiel #2
0
    def apply_impulse(self, impulse, point=(0, 0)):
        point = vec2d.Vec2D(point[0], point[1])
        point -= self._cog

        impulse = vec2d.Vec2D(impulse[0], impulse[1])
        impulse /= self._mass

        self._vel += impulse
        self._av += point.cross(impulse) / self._inertia
Beispiel #3
0
    def __init__(self, *args, **kwargs):
        super().__init__(img=resources.player_image, *args, **kwargs)

        self.vel = vec2d.Vec2D()
        self.acc = vec2d.Vec2D()
        self.speed = 35
        self.max_speed = 50
        self.mass = 3
        self.scale = 0.5
        self.remove = False
        self.children = []
        self.key_handler = key.KeyStateHandler()
Beispiel #4
0
    def __init__(self, x, y):
        self._pos = vec2d.Vec2D(x, y)

        self._vel = vec2d.Vec2D()
        self._av = 0.0

        self._forces = vec2d.Vec2D()
        self._torque = 0.0
        self._cog = vec2d.Vec2D()

        self._rot = 0
        self._sx = 1
        self._sy = 1

        self._mass = 1
        self._inertia = 100

        self._speed = 10
        self._max_vel = self._speed
Beispiel #5
0
    def clamp_vec2d(self, v):
        """ Returns copy of Vec2D clamped to the AABB.

        Args:
            v(Vec2D): vector

        Returns:
            Vec2D
        """

        return vec2d.Vec2D(
            util.clamp(v.x, self._left, self._right),
            util.clamp(v.y, self._bottom, self._top)
        )
Beispiel #6
0
    def compute(vertices, transform):
        xmin = float('inf')
        ymin = float('inf')
        xmax = 0
        ymax = 0

        for x, y in vertices:
            vertex = transform * vec2d.Vec2D(x, y)

            xmin = min(xmin, vertex.x)
            xmax = max(xmax, vertex.x)
            ymin = min(ymin, vertex.y)
            ymax = max(ymax, vertex.y)

        return AABB(xmin, ymin, xmax, ymax)
Beispiel #7
0
    def mul(a, b):
        """Matrix multiplication.
        Matrices can be multiplied by:
          - scalar value (int or float)
          - Vec2D
          - another Matrix

        Raises:
            AttributeError: Attribute `b` either must be a scalar value, Vec2D or another Matrix.
            ArithmeticError: First Matrix column count doesn't match second Matrix row count.

        Args:
            a(Matrix): Matrix to update
            b(mixed): scaling value

        Return mixed
        """

        if type(b) == vec2d.Vec2D:
            result = vec2d.Vec2D(util.dot(a.getRow(0), [b.x, b.y, 1, 1]),
                                 util.dot(a.getRow(1), [b.x, b.y, 1, 1]))

        elif type(b) == Matrix:
            if a._cols != b._rows:
                raise ArithmeticError(
                    "First Matrix column count doesn't match second Matrix row count."
                )

            result = Matrix.new(a._rows, b._cols)

            for r in range(a._rows):
                rowValues = a.getRow(r)

                for c in range(b._cols):
                    result[r][c] = util.dot(rowValues, b.getColumn(c))

        elif type(b) == int or type(b) == float:
            result = Matrix.new(a._rows, b._cols)

            for r in range(a._rows):
                for c in range(a._cols):
                    result[r][c] = a._matx[r][c] * b
        else:
            raise AttributeError(
                "Attribute either must be a scalar value, Vec2D or another Matrix."
            )

        return result
Beispiel #8
0
 def rotation_vector(self):
     return vec2d.Vec2D(1, 0).rotate(self._rot)
    def applyForce(self, dx, dy):
        force = vec2d.Vec2D(dx, dy)
        force.scale(1 / self.mass)

        self.acc += force