Skip to content

Commit

Permalink
Merge pull request kivy#5 from Kovak/fix_quadtree_item_removal
Browse files Browse the repository at this point in the history
Fix quadtree item removal
  • Loading branch information
Kovak committed Sep 18, 2013
2 parents 0899a33 + 18c1a9b commit e4d81bb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
44 changes: 42 additions & 2 deletions kivent_cython/kivent_cython/quadtree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ cdef class QuadTree(object):
self.se = QuadTree(gameworld, position_system, size_system, se_items, depth, (center_x, center_y, r, b))
self.sw = QuadTree(gameworld, position_system, size_system, sw_items, depth, (l, center_y, center_x, b))

def print_trees(self):
print 'tree: ', self.depth, self.items
trees = [self.nw, self.ne, self.se, self.sw]
for tree in trees:
if tree:
tree.print_trees()

def remove_item(self, int item):
cdef object position_system = self.position_system
cdef object size_system = self.size_system
cdef list entities = self.gameworld.entities
cdef float center_x = self.center_x
cdef float center_y = self.center_y
cdef QuadTree nw = self.nw
cdef QuadTree sw = self.sw
cdef QuadTree ne = self.ne
cdef QuadTree se = self.se
cdef dict entity = entities[item]
cdef tuple pos = entity[position_system]['position']
cdef tuple size = entity[size_system]['size']
cdef float item_left = pos[0] - size[0]
cdef float item_top = pos[1] + size[1]
cdef float item_right = pos[0] + size[0]
cdef float item_bottom = pos[1] - size[1]

if item in self.items:
print 'removing item', item, 'from', self.depth
self.items.remove(item)

if nw and item_left <= center_x and item_top >= center_y:
nw.remove_item(item)
if sw and item_left <= center_x and item_bottom <= center_y:
sw.remove_item(item)
if ne and item_right >= center_x and item_top >= center_y:
ne.remove_item(item)
if se and item_right >= center_x and item_bottom <= center_y:
se.remove_item(item)

def remove_items(self, list list_of_items):
for item in list_of_items:
self.remove_item(item)

def add_items(self, list list_of_items, int depth):
depth -= 1
if depth == 0:
Expand Down Expand Up @@ -131,7 +173,6 @@ cdef class QuadTree(object):
items_to_add = self.check_items()
self.add_items(items_to_add, self.depth)


def check_items(self):
cdef QuadTree sw = self.sw
cdef QuadTree nw = self.nw
Expand Down Expand Up @@ -177,7 +218,6 @@ cdef class QuadTree(object):
cdef object size_system = self.size_system
cdef list entities = self.gameworld.entities


def overlaps(int entity_id):
cdef dict entity = entities[entity_id]
cdef tuple pos = entity[position_system]['position']
Expand Down
25 changes: 10 additions & 15 deletions kivent_cython/kivent_cython/renderers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ class QuadTreeQuadRenderer(QuadRenderer):
Clock.schedule_once(self.setup_quadtree)

def remove_entity(self, int entity_id):
self.quadtree.remove_item(entity_id)
super(QuadTreeQuadRenderer, self).remove_entity(entity_id)

def create_component(self, entity_id, entity_component_dict):
Expand Down Expand Up @@ -494,7 +495,7 @@ class QuadTreePointRenderer(PointRenderer):
Clock.schedule_once(self.setup_quadtree)

def remove_entity(self, int entity_id):
print 'removing entity,', entity_id
self.quadtree.remove_item(entity_id)
super(QuadTreePointRenderer, self).remove_entity(entity_id)

def create_component(self, entity_id, entity_component_dict):
Expand All @@ -504,11 +505,6 @@ class QuadTreePointRenderer(PointRenderer):
def setup_quadtree(self, dt):
self.quadtree = QuadTree(self.gameworld, self.render_information_from, self.system_id,
self.entity_ids, depth=7, bounding_rect=(0, 0, self.quadtree_size[0], self.quadtree_size[1]))
print 'quadtree setup', self.quadtree

def enter_delete_mode(self):
self.paused = True
self.quadtree = None

def update_render_state(self):
cdef object parent = self.gameworld
Expand All @@ -535,12 +531,11 @@ class QuadTreePointRenderer(PointRenderer):
cdef object viewport
cdef list bb_list
cdef set current_on_screen
if not self.quadtree == None:
viewport = self.gameworld.systems[self.viewport]
camera_pos = viewport.camera_pos
size = viewport.size
bb_list = [-camera_pos[0] + size[0], -camera_pos[0], -camera_pos[1] + size[1], -camera_pos[1]]
current_on_screen = self.quadtree.bb_hit(bb_list[0], bb_list[1], bb_list[2], bb_list[3])
return current_on_screen
else:
return set([])

viewport = self.gameworld.systems[self.viewport]
camera_pos = viewport.camera_pos
size = viewport.size
bb_list = [-camera_pos[0] + size[0], -camera_pos[0], -camera_pos[1] + size[1], -camera_pos[1]]
current_on_screen = self.quadtree.bb_hit(bb_list[0], bb_list[1], bb_list[2], bb_list[3])
return current_on_screen

23 changes: 3 additions & 20 deletions sample_application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,10 @@ def init_game(self, dt):

def on_state(self, instance, value):
if value == 'choose_character':
self.gameworld.systems['quadtree_renderer'].enter_delete_mode()
self.gameworld.systems['asteroids_level'].clear_level()
self.clear_gameworld_objects()
self.cleared = False
Clock.schedule_once(self.check_clear)

def check_quadtree_created(self, dt):
systems = self.gameworld.systems
quadtree_renderer = systems['quadtree_renderer']
if quadtree_renderer.quadtree:
Clock.schedule_once(self.setup_new_level)
self.cleared = True
else:
Clock.schedule_once(self.check_quadtree_created)

def check_clear(self, dt):
systems = self.gameworld.systems
Expand All @@ -72,14 +62,11 @@ def check_clear(self, dt):
self.clear_gameworld_objects()
Clock.schedule_once(self.check_clear, .01)
else:
Clock.schedule_once(self.setup_new_quadtree)
Clock.schedule_once(self.check_quadtree_created)
self.cleared = True
self.setup_new_level()

def setup_new_quadtree(self, dt):
Clock.schedule_once(
self.gameworld.systems['quadtree_renderer'].setup_quadtree)

def setup_new_level(self, dt):
def setup_new_level(self):
Clock.schedule_once(
self.gameworld.systems['asteroids_level'].generate_new_level)

Expand Down Expand Up @@ -216,10 +203,6 @@ def setup_collision_callbacks(self):
physics.add_collision_handler(2, 4,
begin_func=ship_system.collision_begin_ship_probe)


def test_remove_entity(self, dt):
self.gameworld.remove_entity(0)

def set_choose_character_state(self, dt):
self.gameworld.state = 'choose_character'

Expand Down

0 comments on commit e4d81bb

Please sign in to comment.