def _vertices(self, N, length, pos): self.length = length alpha = pi / N theta = 2 * alpha b = length / (2 * sin(alpha)) P0 = Vector(b, 0) pos = Vector(*pos) return [(P0.rotated(n * theta)) + pos for n in range(N)]
# -*- coding: utf8 -*- from FGAme.mathutils import Vector, dot, area, center_of_mass, clip, pi from FGAme.mathutils import shadow_x, shadow_y from FGAme.physics.collision import Collision from FGAme.physics import Circle, AABB, Poly, Rectangle from FGAme.util import multifunction u_x = Vector(1, 0) DEFAULT_DIRECTIONS = [u_x.rotated(n * pi / 12) for n in [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11]] class CollisionError(Exception): '''Declara que não existem colisões disponíveis para os dois tipos de objetos''' pass def get_collision_generic(A, B): '''Retorna uma colisão genérica definido pela CBB dos objetos A e B''' rA = A.cbb_radius rB = B.cbb_radius delta = B._pos - A._pos if delta.norm() < rA + rB: n = delta.normalized() D = rA + rB - delta.norm() pos = A._pos + (rA - D / 2) * n
from FGAme.mathutils import Vector, dot, area, center_of_mass, clip, pi from FGAme.physics.collision import (Collision, get_collision, get_collision_aabb) from FGAme.physics.elements import (Circle, AABB, Poly, Rectangle) u_x = Vector(1, 0) DEFAULT_DIRECTIONS = [ u_x.rotated(n * pi / 12) for n in [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11] ] ############################################################################### # Colisões entre figuras primitivas simples ############################################################################### get_collision[AABB, AABB] = get_collision_aabb @get_collision.dispatch(Circle, Circle) def circle_collision(A, B): '''Testa a colisão pela distância dos centros''' delta = B.pos - A.pos if delta.norm() < A.radius + B.radius: n = delta.normalized() D = A.radius + B.radius - delta.norm() pos = A.pos + (A.radius - D / 2) * n return Collision(A, B, pos=pos, n=n) else: return None
def test_vector_rotation(): v = Vector(1, 0) assert (v.rotated(pi / 2) - Vector(0, 1)).norm() < 1e-6, v
def test_vector_norm(): v = Vector(1, 2) u = v.normalized() r = v.rotated(pi / 3) assert abs(u.norm() - 1) < 1e-6, u.norm() assert abs(v.norm() - r.norm()) < 1e-6, (v.norm(), r.norm())