def _ball(self, pos, radius, dynamic=True, density=1.0, restitution=0.16, friction=0.5): # Add a ball without correcting any settings # meaning, pos and vertices are in meters # Define the body x, y = pos bodyDef = box2d.b2BodyDef() bodyDef.position.Set(x, y) bodyDef.sleepFlag = True # bodyDef.allowSleep(True) userData = { 'color' : self.parent.get_color() } bodyDef.userData = userData # Create the Body if not dynamic: density = 0 body = self.parent.world.CreateBody(bodyDef) self.parent.element_count += 1 # Add a shape to the Body circleDef = box2d.b2CircleDef() circleDef.density = density circleDef.radius = radius circleDef.restitution = restitution circleDef.friction = friction body.CreateShape(circleDef) body.SetMassFromShapes(); return body
def _ball(self, pos, radius, dynamic=True, density=1.0, restitution=0.16, friction=0.5): # Add a ball without correcting any settings # meaning, pos and vertices are in meters # Define the body x, y = pos bodyDef = box2d.b2BodyDef() bodyDef.position.Set(x, y) bodyDef.sleepFlag = True # bodyDef.allowSleep(True) userData = {'color': self.parent.get_color()} bodyDef.userData = userData # Create the Body if not dynamic: density = 0 body = self.parent.world.CreateBody(bodyDef) self.parent.element_count += 1 # Add a shape to the Body circleDef = box2d.b2CircleDef() circleDef.density = density circleDef.radius = radius circleDef.restitution = restitution circleDef.friction = friction body.CreateShape(circleDef) body.SetMassFromShapes() return body
def concavePoly(self, vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=True): # 1. Step: Reduce # Detect if the polygon is closed or open if vertices[0] != vertices[-1]: is_closed = False else: is_closed = True # Continue reducing the vertecs x, y = c = tools_poly.calc_center(vertices) vertices = tools_poly.poly_center_vertices(vertices) # Bring coordinates into the world coordinate system (flip, camera offset, ...) if screenCoord: x, y = self.parent.to_world(c) else: x, y = c # If required, translate pixel -> meters if self.parent.input == INPUT_PIXELS: # translate pixel -> meters x /= self.parent.ppm y /= self.parent.ppm # Let's add the body bodyDef = box2d.b2BodyDef() bodyDef.position = (x, y) userData = {'color': self.parent.get_color()} bodyDef.userData = userData # Create the Body if not dynamic: density = 0 body = self.parent.world.CreateBody(bodyDef) self.parent.element_count += 1 # Create the reusable Box2D polygon and circle definitions polyDef = box2d.b2PolygonDef() polyDef.vertexCount = 4 # rectangle polyDef.density = density polyDef.restitution = restitution polyDef.friction = friction circleDef = box2d.b2CircleDef() circleDef.density = density circleDef.radius = 0.086 circleDef.restitution = restitution circleDef.friction = friction # Set the scale factor factor = 8.0 v2 = box2d.b2Vec2(*vertices[0]) for v in vertices[1:]: v1 = v2.copy() v2 = box2d.b2Vec2(*v) vdir = v2 - v1 # (v2x-v1x, v2y-v1y) vdir.Normalize() # we need a little size for the end part vn = box2d.b2Vec2(-vdir.y * factor, vdir.x * factor) v = [v1 + vn, v1 - vn, v2 - vn, v2 + vn] # Create a line (rect) for each part of the polygon, # and attach it to the body polyDef.setVertices([vi / self.parent.ppm for vi in v]) try: polyDef.checkValues() except ValueError: print "concavePoly: Created an invalid polygon!" return None body.CreateShape(polyDef) # Now add a circle to the points between the rects # to avoid sharp edges and gaps if not is_closed and v2.tuple() == vertices[-1]: # Don't add a circle at the end break circleDef.localPosition = v2 / self.parent.ppm body.CreateShape(circleDef) # Now, all shapes have been attached body.SetMassFromShapes() # Return hard and soft reduced vertices return body
def concavePoly(self, vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=True): # 1. Step: Reduce # Detect if the polygon is closed or open if vertices[0] != vertices[-1]: is_closed = False else: is_closed = True # Continue reducing the vertecs x, y = c = tools_poly.calc_center(vertices) vertices = tools_poly.poly_center_vertices(vertices) # Bring coordinates into the world coordinate system (flip, camera offset, ...) if screenCoord: x, y = self.parent.to_world(c) else: x, y = c # If required, translate pixel -> meters if self.parent.input == INPUT_PIXELS: # translate pixel -> meters x /= self.parent.ppm y /= self.parent.ppm # Let's add the body bodyDef = box2d.b2BodyDef() bodyDef.position.Set(x, y) bodyDef.sleepFlag = True userData = { 'color' : self.parent.get_color() } bodyDef.userData = userData # Create the Body if not dynamic: density = 0 body = self.parent.world.CreateBody(bodyDef) self.parent.element_count += 1 # Create the reusable Box2D polygon and circle definitions polyDef = box2d.b2PolygonDef() polyDef.vertexCount = 4 # rectangle polyDef.density = density polyDef.restitution = restitution polyDef.friction = friction circleDef = box2d.b2CircleDef() circleDef.density = density circleDef.radius = 0.086 circleDef.restitution = restitution circleDef.friction = friction # Set the scale factor factor = 8.0 v2 = box2d.b2Vec2().fromTuple(vertices[0]) for v in vertices[1:]: v1 = v2.copy() v2 = box2d.b2Vec2().fromTuple(v) vdir = v2-v1 # (v2x-v1x, v2y-v1y) vdir.Normalize() # we need a little size for the end part vn = box2d.b2Vec2(-vdir.y*factor, vdir.x*factor) v = [ v1+vn, v1-vn, v2-vn, v2+vn ] # Create a line (rect) for each part of the polygon, # and attach it to the body for i in range(len(v)): polyDef.setVertex(i, v[i] / self.parent.ppm) if not tools_poly.checkDef(polyDef): print "concavePoly: Created an invalid polygon!" return [], 0 body.CreateShape(polyDef) # Now add a circle to the points between the rects # to avoid sharp edges and gaps if not is_closed and v2.tuple() == vertices[-1]: # Don't add a circle at the end break circleDef.localPosition = v2 / self.parent.ppm body.CreateShape(circleDef) # Now, all shapes have been attached body.SetMassFromShapes() # Return hard and soft reduced vertices return body