1. Xathos
  2. cantankerous_physics

Commits

Xathos  committed 3adf132

Prototype2 update - minor fixes

  • Participants
  • Parent commits 441a092
  • Branches default

Comments (0)

Files changed (8)

File collision.py

View file
     #Note: a is already taken care of by the first if block
     if b == 0 and d == 0:
         return None
+    elif c == 0 and d == 0:
+        return None
     elif c == 0:
         fx = x / a - b / a * y / d
         fy = y / d

File collision.pyc

Binary file modified.

File prototype2/prototype2.py

View file
 # r: Raycast from the ray's origin to cursor position
 # b: Display bounding boxes
 # n: Disable display of bounding boxes
-#
-#
+# v: Turn visualization on
+# o: Turn visualization off (for stress testing)
 
 window = pyglet.window.Window()
 keys = key.KeyStateHandler()
 cursor_pos = Vector2()
 ray = LineSegment(Vector2(), Vector2())
 ray_hit = None
+all_ray_hits = []
 
 #Options:
 draw_bounds = True #Show bounding boxes? (toggleable: b/n)
+viz_on = True #Show anything at all? (toggleable: v/o)
 debug_raycasting = True #Print debug info from raycasting?
 
 @window.event
     pass
 
 def redraw ():
-    global draw_bounds
+    global draw_bounds, viz_on
     window.clear()
+    if not viz_on:
+        return
     world.debug_draw(draw_bounds)
     #Manually debug draw the ray (not included in world)
     if draw_bounds:
     glColor4f(1.0, 0.0, 0.0, 1.0)
     pyglet.graphics.draw(2, GL_LINES,
             ('v2f', (ray.v1.x, ray.v1.y, ray.v2.x, ray.v2.y)))
+    #Draw other hits
+    glColor4f(1.0, 0.0, 1.0, 1.0)
+    for point in all_ray_hits:
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (point.x - 5, point.y, point.x + 5, point.y)))
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (point.x, point.y + 5, point.x, point.y - 5)))
+        
     #Draw hit point
     if ray_hit is not None:
         glColor4f(0.0, 1.0, 0.0, 1.0)
                 ('v2f', (ray_hit.x, ray_hit.y - 5, ray_hit.x, ray_hit.y + 5)))
             
 def update (dt):
-    global ray, draw_bounds, ray_hit
+    global ray, draw_bounds, ray_hit, all_ray_hits, viz_on, world, obj
 
     if keys[key.D]:
         if len(obj.geometry.points) > 0:
         obj.geometry.points = []
         obj.geometry.rebuild()
         redraw()
+    if keys[key.P]:
+        del obj
+        obj = GameObject(Vector2())
+        obj.geometry.points = []
+        obj.geometry.rebuild()
+        world.add(obj)
+    if keys[key.X]:
+        world.objs = []
+        redraw()
     if keys[key.S]:
         ray = LineSegment(cursor_pos, cursor_pos)
         ray_hit = cursor_pos
+        all_ray_hits = []
         redraw()
     if keys[key.R]:
         ray = LineSegment(ray.v1, cursor_pos)
+        all_ray_hits = world.raycast_return_all(ray, debug_raycasting)
+        ray_hit = world.raycast_cull(ray, all_ray_hits)
         ray_hit = world.raycast(ray, debug_raycasting)
         #print(ray_hit)
         redraw()
     if keys[key.N]:
         draw_bounds = False
         redraw()
+    if keys[key.V]:
+        viz_on = True
+        redraw()
+    if keys[key.O]:
+        viz_on = False
+        redraw()
 
 window.clear()
 pyglet.clock.schedule_interval(update, 1.0 / 24.0)

File prototype2/stress_test.py

View file
+import sys
+sys.path.append("..")
+from vector2 import Vector2
+from math import sqrt
+from temp import Polygon, GameObject, PhysicsWorld
+from collision import LineSegment, detect_line_collision
+import pyglet
+from pyglet.gl import *
+from pyglet.window import key
+import random
+from time import clock
+
+# INPUT:
+# d: Add a point to the polygon at cursor position (mouse position)
+# c: Clear all vertex data from the polygon
+# s: Reset the ray's origin to cursor position
+# r: Raycast from the ray's origin to cursor position
+# b: Display bounding boxes
+# n: Disable display of bounding boxes
+# v: Turn visualization on
+# o: Turn visualization off (for stress testing)
+
+window = pyglet.window.Window()
+keys = key.KeyStateHandler()
+window.push_handlers(keys)
+world = PhysicsWorld()
+
+obj = GameObject(Vector2(200, 200))
+world.add(obj)
+
+cursor_pos = Vector2()
+ray = LineSegment(Vector2(), Vector2())
+ray_hit = None
+
+#Options:
+draw_bounds = False #Show bounding boxes? (toggleable: b/n)
+viz_on = False #Show anything at all? (toggleable: v/o)
+debug_raycasting = False #Print debug info from raycasting?
+benchmark_mode = True #Run at maximum possible speed with minimal debugging?
+calculations_per_frame = 5000 #Calculations to run per frame
+# (number of randomly generated line segments within ray bounds)
+timer = 1.5 #Time that benchmark runs
+record_timer = 0.2 #Time before recording starts (first frame runs quickly and would skew the results)
+
+@window.event
+def on_mouse_motion (x, y, dx, dy):
+    global cursor_pos
+    cursor_pos = Vector2(x, y)
+
+@window.event
+def on_draw ():
+    pass
+
+def redraw ():
+    global draw_bounds, viz_on
+    window.clear()
+    if not viz_on:
+        return
+    world.debug_draw(draw_bounds)
+    #Manually debug draw the ray (not included in world)
+    if draw_bounds:
+        #Draw bounds
+        glColor4f(0.3, 0.0, 0.3, 1.0)
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray.xbounds[0], ray.ybounds[0], ray.xbounds[1], ray.ybounds[0])))
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray.xbounds[1], ray.ybounds[0], ray.xbounds[1], ray.ybounds[1])))
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray.xbounds[1], ray.ybounds[1], ray.xbounds[0], ray.ybounds[1])))
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray.xbounds[0], ray.ybounds[1], ray.xbounds[0], ray.ybounds[0])))
+    #Draw ray
+    glColor4f(1.0, 0.0, 0.0, 1.0)
+    pyglet.graphics.draw(2, GL_LINES,
+            ('v2f', (ray.v1.x, ray.v1.y, ray.v2.x, ray.v2.y)))
+    #Draw hit point
+    if ray_hit is not None:
+        glColor4f(0.0, 1.0, 0.0, 1.0)
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray_hit.x - 5, ray_hit.y, ray_hit.x + 5, ray_hit.y)))
+        pyglet.graphics.draw(2, GL_LINES,
+                ('v2f', (ray_hit.x, ray_hit.y - 5, ray_hit.x, ray_hit.y + 5)))
+            
+def update (dt):
+    global ray, draw_bounds, ray_hit, viz_on
+
+    if keys[key.D]:
+        if len(obj.geometry.points) > 0:
+            lastpoint = obj.geometry.points[-1]
+            d = sqrt((lastpoint.x - cursor_pos.x) ** 2 + (lastpoint.y - cursor_pos.y) ** 2)
+        else:
+            d = 10.0
+        if d > 5.0:
+            obj.geometry.points += [cursor_pos]
+            obj.geometry.rebuild()
+        redraw()
+    if keys[key.C]:
+        obj.geometry.points = []
+        obj.geometry.rebuild()
+        redraw()
+    if keys[key.S]:
+        ray = LineSegment(cursor_pos, cursor_pos)
+        ray_hit = cursor_pos
+        redraw()
+    if keys[key.R]:
+        ray = LineSegment(ray.v1, cursor_pos)
+        ray_hit = world.raycast(ray, debug_raycasting)
+        redraw()
+    if keys[key.B]:
+        draw_bounds = True
+        redraw()
+    if keys[key.N]:
+        draw_bounds = False
+        redraw()
+    if keys[key.V]:
+        viz_on = True
+        redraw()
+    if keys[key.O]:
+        viz_on = False
+        redraw()
+
+runs = 0
+total_time = 0.0
+total_fps = 0.0
+def benchmark (dt):
+    global timer, record_timer, runs, total_time, total_fps
+    print("%0.2f s: %0.2f fps"%(dt, 1.0 / dt))
+    ray_hit = world.raycast(ray, debug_raycasting)
+    record_timer -= dt
+    if record_timer < 0:
+        total_time += dt
+        total_fps += 1.0 / dt
+        runs += 1
+    timer -= dt
+    if timer < 0:
+        print("Test completed:")
+        print("Average time: %0.2f sec per frame (%0.2f fps)"%(total_time / runs, total_fps / runs))
+        print("Ran for %0.2f sec at %i calculations per frame."%(total_time, calculations_per_frame))
+        print("Average of %0.2f calculations per second."%(calculations_per_frame / total_time * runs))
+        quit()
+
+for i in range(calculations_per_frame):
+    obj.geometry.points += [Vector2(random.random() * 600, random.random() * 400)]
+obj.geometry.rebuild()
+ray = LineSegment(Vector2(0, 0), Vector2(600, 400))
+
+redraw()
+if benchmark_mode:
+    pyglet.clock.schedule_interval(benchmark, 1.0 / 50.0)
+else:
+    pyglet.clock.schedule_interval(update, 1.0 / 24.0)
+pyglet.app.run()

File prototype2/temp.py

View file
                 if hit is not None:
                     hits += [hit]
         if debug:
-            print("Found %i points of intersection after %i calculations:\n%s"%(len(hits), calcs, hits))
+           # print("Found %i points of intersection after %i calculations:\n%s"%(len(hits), calcs, hits))
+           print("Found %i points of intersection after %i calculations:\n"%(len(hits), calcs))
         return hits
     
     def raycast (self, ray, debug = False):
-        """Return the """
-        hits = self.raycast_return_all(ray, debug)
+        return self.raycast_cull(ray, self.raycast_return_all(ray, debug))
+
+    def raycast_cull (self, ray, hits):
         closest = None
         dist = 100000.0
         for point in hits:

File prototype2/temp.pyc

Binary file added.

File vector2.py

View file
         self.y = float(y)
 
     def __str__ (self):
-        return "(%s, %s)"%(self.x, self.y)
+        return "(%0.2f, %0.2f)"%(self.x, self.y)
 
     def __repr__ (self):
-        return "Vector2(%s, %s)"%(self.x, self.y)
+        return "Vector2(%0.2f, %0.2f)"%(self.x, self.y)
 
     def __add__ (self, vector):
         return Vector2(self.x + vector.x, self.y + vector.y)

File vector2.pyc

Binary file modified.