Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testComparison(self):
int_vec = Vec2d(3, -2)
flt_vec = Vec2d(3.0, -2.0)
zero_vec = Vec2d(0, 0)
self.assertTrue(int_vec == flt_vec)
self.assertTrue(int_vec != zero_vec)
self.assertTrue((flt_vec == zero_vec) == False)
self.assertTrue((flt_vec != int_vec) == False)
self.assertTrue(int_vec == (3, -2))
self.assertTrue(int_vec != [0, 0])
self.assertTrue(int_vec != 5)
self.assertTrue(int_vec != [3, -2, -5])
def test_collect_segment(self):
pset = a.PolylineSet()
pset.collect_segment((0,0),(5,5))
pset.collect_segment((5,5),(10,10))
pset.collect_segment((2,5),(2,10))
expected = [
[Vec2d(0.0, 0.0), Vec2d(5.0, 5.0), Vec2d(10.0, 10.0)],
[Vec2d(2.0, 5.0), Vec2d(2.0, 10.0)]]
self.assertEqual(len(pset), 2)
self.assertEqual(list(pset), expected)
def testConversion(self):
b = p.Body(1,1)
b.position = 10,20
self.assertEqual(b.local_to_world((1,1)), Vec2d(11,21))
self.assertEqual(b.world_to_local((1,1)), Vec2d(-9,-19))
min_x = 1000
max_y = 0
min_y = 1000
for l in line:
max_x = max(max_x, l.x)
min_x = min(min_x, l.x)
max_y = max(max_y, l.y)
min_y = min(min_y, l.y)
w,h = max_x - min_x, max_y - min_y
# we skip the line which has less than 35 height, since its the "hole" in
# the p in pymunk, and we dont need it.
if h < 35:
continue
center = Vec2d(min_x + w/2., min_y + h/2.)
t = pymunk.Transform(a=1.0, d=1.0, tx=-center.x, ty=-center.y)
r += 30
if r > 255:
r = 0
line = [Vec2d(l.x, 300-l.y) for l in line]
lines.append(line)
return lines
from ctypes import *
from .vec2d import Vec2d
from ._base import uintptr_t, cpGroup, cpBitmask, cpFloat
from ._chipmunk_manual import ShapeFilter, Transform
from ._contact_point_set import ContactPoint, ContactPointSet
cpVect = Vec2d
STRING = c_char_p
from .libload import load_library, platform_specific_functions
try:
import pymunkoptions
_lib_debug = pymunkoptions.options["debug"]
except:
_lib_debug = True #Set to True to print the Chipmunk path.
chipmunk_lib = load_library("chipmunk", debug_lib=_lib_debug)
function_pointer = platform_specific_functions()['function_pointer']
STRING = c_char_p
# cpfsqrt = sqrt # alias
def _update_vertices(self, offset=(0, 0), scaling_factor=1.0,
rotation_angle=0):
# FIXME: review vertices update algorithm
for i in xrange(len(self.vertices)):
self.vertices[i] += Vec2d(offset)
self.vertices[i].length *= scaling_factor
cannon_body.position += Vec2d(-1,0) * speed
if (keys[K_RIGHT]):
cannon_body.position += Vec2d(1,0) * speed
mouse_position = pymunk.pygame_util.from_pygame( Vec2d(pygame.mouse.get_pos()), screen )
cannon_body.angle = (mouse_position - cannon_body.position).angle
# move the unfired arrow together with the cannon
arrow_body.position = cannon_body.position + Vec2d(cannon_shape.radius + 40, 0).rotated(cannon_body.angle)
arrow_body.angle = cannon_body.angle
for flying_arrow in flying_arrows:
drag_constant = 0.0002
pointing_direction = Vec2d(1,0).rotated(flying_arrow.angle)
flight_direction = Vec2d(flying_arrow.velocity)
flight_speed = flight_direction.normalize_return_length()
dot = flight_direction.dot(pointing_direction)
# (1-abs(dot)) can be replaced with (1-dot) to make arrows turn
# around even when fired straight up. Might not be as accurate, but
# maybe look better.
drag_force_magnitude = (1-abs(dot)) * flight_speed **2 * drag_constant * flying_arrow.mass
arrow_tail_position = Vec2d(-50, 0).rotated(flying_arrow.angle)
flying_arrow.apply_impulse_at_world_point(drag_force_magnitude * -flight_direction, arrow_tail_position)
flying_arrow.angular_velocity *= 0.5
### Clear screen
screen.fill(pygame.color.THECOLORS["black"])
### Draw stuff
space.debug_draw(draw_options)
self.space.gravity = (0.0, -900.0)
self.space.threads = threads
### ground
shape = pymunk.Segment(self.space.static_body, (5, 100), (595,100), 1.0)
shape.friction = 1.0
self.space.add(shape)
### pyramid
x=Vec2d(-270, 7.5) + (300,100)
y=Vec2d(0,0)
deltaX=Vec2d(0.5625, 1.1)*20
deltaY=Vec2d(1.125, 0.0)*20
for i in range(25):
y = Vec2d(x)
for j in range(i, 25):
size = 10
points = [(-size, -size), (-size, size), (size,size), (size, -size)]
mass = 1.0
moment = pymunk.moment_for_poly(mass, points, (0,0))
body = pymunk.Body(mass, moment)
body.position = y
shape = pymunk.Poly(body, points)
shape.friction = 1
self.space.add(body,shape)
y += deltaY
x += deltaX
def _from_cp(cls, _points):
normal = Vec2d(_points.normal)
points = []
for i in range(_points.count):
_p = _points.points[i]
p = ContactPoint(
Vec2d._fromcffi(_p.pointA),
Vec2d._fromcffi(_p.pointB),
_p.distance)
points.append(p)
return cls(normal, points)
impulse = power * Vec2d(1,0)
arrow_body.apply_impulse(impulse.rotated(arrow_body.angle))
space.add(arrow_body)
flying_arrows.append(arrow_body)
arrow_body, arrow_shape = create_arrow()
space.add(arrow_shape)
keys = pygame.key.get_pressed()
speed = 2.5
if (keys[K_UP]):
cannon_body.position += Vec2d(0,1) * speed
if (keys[K_DOWN]):
cannon_body.position += Vec2d(0,-1) * speed
if (keys[K_LEFT]):
cannon_body.position += Vec2d(-1,0) * speed
if (keys[K_RIGHT]):
cannon_body.position += Vec2d(1,0) * speed
mouse_position = from_pygame( Vec2d(pygame.mouse.get_pos()), screen )
cannon_body.angle = (mouse_position - cannon_body.position).angle
# move the unfired arrow together with the cannon
arrow_body.position = cannon_body.position + Vec2d(cannon_shape.radius + 40, 0).rotated(cannon_body.angle)
arrow_body.angle = cannon_body.angle
for flying_arrow in flying_arrows:
drag_constant = 0.0002
pointing_direction = Vec2d(1,0).rotated(flying_arrow.angle)
flight_direction = Vec2d(flying_arrow.velocity)
flight_speed = flight_direction.normalize_return_length()