class Main(game.Game): def __init__(self): game.Game.__init__(self) self.DEBUG = True #self.world.addObject(self.circle()) self.world.addObject(self.testCircle((255, 255), 10)) self.world.addObject(self.testRect((500, 500), (100, 100))) self.world.addObject(self.testRect((255, 500), (100, 100))) self.world.addObject(self.light()) self.addTest2Objects((0, 0), 50, (50, 50)) #self.world.setLight( (255,255,255,255)) def addTest2Objects(self, (x, y), r, (w, h)): parent = self.world.createCircGameObject(x, y, r, True, False) parent.name = "parent" parent.addComponent(Sample.Controller) rot = parent.addComponent(Sample.Rotate) rot.speed = 20 self.world.addObject(parent) child = self.world.createRectGameObject(0, 0, w, h, True, False) child.transform.parent = parent.transform child.transform.localPosition = Vector2.Vector2(4 * r, 0) child.name = "child" rot = child.addComponent(Sample.Rotate) rot.speed = 200 self.world.addObject(child) child2 = self.world.createRectGameObject(0, 0, w, h, True, False) child2.transform.parent = child.transform child2.transform.localPosition = Vector2.Vector2(2 * r, 0) child2.name = "child2" rot = child2.addComponent(Sample.Rotate) rot.speed = 50 self.world.addObject(child2) return parent
def getPolygonFromPoints(pts): vectors = [] if len(pts) == 0: return vectors (x, y) = pts[0] vectors.append(Vector2.Vector2(0, 0)) for i in range(1, len(pts)): vectors.append(Vector2.Vector2(pts[i][0] - x, pts[i][1] - y)) return ((x, y), vectors)
def testCirclePolygonSat(circle, polygon, flip): dist = INF shorterDist = INF closestPoint = Vector2.Vector2() result = CollisionInfo() if flip: result.collider1 = polygon result.collider2 = circle else: result.collider1 = circle result.collider2 = polygon p1 = polygon.getPoints() # get offset # vOffset = Vector2.Vector2()#circle.position.sub(polygon.position) # find closes point for p in p1: currentDist = p.distance(circle.position) if currentDist < dist: dist = currentDist closestPoint.x = p.x closestPoint.y = p.y vAxis = closestPoint.sub(circle.position).normalize() # project polygon (min0, max0) = getMinMax(vAxis, p1) # project circle min1 = vAxis.dot(circle.position) max1 = min1 + circle.radius min1 = min1 - circle.radius (dist, new) = makeResult(result, min0, max0, min1, max1, vAxis, flip) if dist < shorterDist: shorterDist = dist if new == None: return None result = new for i in range(0, len(p1)): vAxis = getAxisNormal(p1, i) (min0, max0) = getMinMax(vAxis, p1) # project circle min1 = vAxis.dot(circle.position) max1 = min1 + circle.radius min1 = min1 - circle.radius # sOffset = vAxis.dot( vOffset) # min0 = min0 + sOffset # max0 = max0 + sOffset (dist, new) = makeResult(result, min0, max0, min1, max1, vAxis, flip) if dist < shorterDist: shorterDist = dist if new == None: return None result = new return result
def __init__(self, world, x, y, r, color): GameObject.GameObject.__init__(self, world) self.transform.position = Vector2.Vector2(x, y) Collider.CircleCollider(self, 0, 0, r) render = self.addComponent(CircleRender) render.color = color render.radius = r
def __init__(self): self.collider1 = gameEngine.components.Collider.Collider(None) self.collider1 = None self.collider2 = gameEngine.components.Collider.Collider(None) self.collider2 = None self.distance = 0 self.direction = Vector2.Vector2() # normal vector
def points(self): points = [] start = Vector2.Vector2(self.radius,0) points.append(start) angle = 0 while angle < 360: points.append(start.rotate(angle) ) angle += 30 return points
def circle(self): circle = self.world.createCircle(500, 500, 50, (255, 255, 255), True) circle.name = 'player' circle.rigid.velocity = Vector2.Vector2(0, 100) circle.addComponent(Sample.Controller) #flashLight = circle.addComponent(LightSource.FlashLight) #flashLight.setVals(350,250,180,(255,0,0),20, (0,0)) #circle.addComponent(Sample.FlashLightController) return circle
def HandleCollision(obj1, obj2, info): # only riged component respond to collision rig1 = obj1.rigid rig2 = obj2.rigid if (rig1 == None or rig1.kinematic) and ( rig2 == None or rig2.kinematic ): # why try to handle collision they don't respond to collision return normal = info.direction.normalize() v1 = rig1.velocity if rig1 != None else Vector2.Vector2() ns1 = normal.dot(v1) v1x = normal.scale(ns1) v1y = v1.sub(v1x) normal.scale(-1) v2 = rig2.velocity if rig2 != None else Vector2.Vector2() ns2 = normal.dot(v2) v2x = normal.scale(ns2) v2y = v2.sub(v2x) if (rig2 == None): rig1.velocity = v1x.scale(-1).add(v1y) elif (rig1 == None): rig2.velocity = v2x.scale(-1).add(v2y) else: m1 = rig1.mass if rig1 != None else INF m2 = rig2.mass if rig2 != None else INF cm = m1 + m2 if not rig1.kinematic: rig1.velocity = v1x.scale( (m1 - m2) / cm).add(v2x.scale((2 * m2) / cm)).add(v2y) if not rig2.kinematic: rig2.velocity = v1x.scale( (2 * m1) / cm).add(v2x.scale((m2 - m1) / cm)).add(v2y)
def center(self): return self.transform.position.add(Vector2.Vector2(*self.offset))
def endCorner(self): return Vector2.Vector2(self.right, self.bottom)
def startCorner(self): return Vector2.Vector2(self.left, self.top)
def parentPosition(self): if self._parent: return self._parent.position return Vector2.Vector2()
def __init__(self, gameObject): Render.Render.__init__(self, gameObject) self.color = (0, 0, 0, 0) self.radius = 0 self.offset = Vector2.Vector2(0, 0)
def getPath(self, start, goal): if self.navMesh.data: return self.navMesh.GetPath(Vector2.Vector2(*start), Vector2.Vector2(*goal)) return None
def draw(self, screen): pos = self.gameObject.shape.StartCorner() if self.gameObject != None and self.gameObject.shape != None else Vector2.Vector2() area = self.sprite_data[self.current] if self.sprite_data.has_key(self.current) else None pos = pos.xy() pos = (pos[0]+self.offset[0], pos[1]+self.offset[1]) screen.blit(self.image, pos, area)
def validPosition(self, pos): if self.navMesh.data: return self.navMesh.data.get_node_from_point( Vector2.Vector2(*pos)) != None return None
@author: otrebor ''' ''' points are not world points is a set of vectors using x,y as reference point, to simplify movement ''' import Shape import gameEngine.util.Vector2 as Vector2 import pygame class Polygon(Shape.Shape): def __init__(self, transform, (x, y)=(0, 0), points=[], angle=0): Shape.Shape.__init__(self, transform) self._angle = angle self._offset = (x, y) self._corners = [Vector2.Vector2(pt.x, pt.y) for pt in points] self._cache = None self._cachePos = Vector2.Zero self._cacheAngle = 0 self._cachePAngle = 0 self._cacheRadius = None self.calAABB() def calAABB(self): self.aabb = (min([pt.x for pt in self.corners]), min([pt.y for pt in self.corners]), max([pt.x for pt in self.corners]), max([pt.y for pt in self.corners])) return self.aabb @property
''' Created on Jan 28, 2014 @author: otrebor ''' ''' Rectangle position is the left top corner ''' import Polygon import gameEngine.util.Vector2 as Vector2 class Rectangle(Polygon.Polygon): def __init__(self, transform, (x, y)=(0, 0), (w, h)=(0, 0), angle=0): self.offset = (x, y) points = [ Vector2.Vector2(-w / 2, -h / 2), Vector2.Vector2(w / 2, -h / 2), Vector2.Vector2(w / 2, h / 2), Vector2.Vector2(-w / 2, h / 2) ] Polygon.Polygon.__init__(self, transform, (x, y), points, angle=0)
import pygame import GameObject import gameEngine.components.Collider as Collider import gameEngine.components.Render as Render import gameEngine.util.Vector2 as Vector2 #creates game object of rectangle type class RectObject(GameObject.GameObject): def __init__(self, world, (x, y)=(0, 0), (w, h)=(0, 0), color=None): GameObject.GameObject.__init__(self, world) Collider.RectCollider(self, (0, 0), w, h) render = self.addComponent(RectRender) render.color = color self.transform.position = Vector2.Vector2(x, y) class RectRender(Render.Render): def __init__(self, gameObject): Render.Render.__init__(self, gameObject) self.color = (0, 0, 0, 0) self.offset = (0, 0) def rect(self): x = self.shape.left + self.offset[0] y = self.shape.top + self.offset[1] w = self.shape.width + self.offset[0] h = self.shape.height + self.offset[1] return pygame.rect.Rect(x, y, w, h)
def toVector2(self, poly): _list = [] for i in range(0, len(poly)): _list.append(Vector2.Vector2(poly[i][0], poly[i][1])) return _list