Checks to see if the player at the given `position` and `height` is colliding with any blocks in the world. Parameters ---------- position : tuple of len 3 The (x, y, z) position to check for collisions at. height : int or float The h
(self, position, height)
| 610 | self.position = (x, y, z) |
| 611 | |
| 612 | def collide(self, position, height): |
| 613 | """ Checks to see if the player at the given `position` and `height` |
| 614 | is colliding with any blocks in the world. |
| 615 | |
| 616 | Parameters |
| 617 | ---------- |
| 618 | position : tuple of len 3 |
| 619 | The (x, y, z) position to check for collisions at. |
| 620 | height : int or float |
| 621 | The height of the player. |
| 622 | |
| 623 | Returns |
| 624 | ------- |
| 625 | position : tuple of len 3 |
| 626 | The new position of the player taking into account collisions. |
| 627 | |
| 628 | """ |
| 629 | # How much overlap with a dimension of a surrounding block you need to |
| 630 | # have to count as a collision. If 0, touching terrain at all counts as |
| 631 | # a collision. If .49, you sink into the ground, as if walking through |
| 632 | # tall grass. If >= .5, you'll fall through the ground. |
| 633 | pad = 0.25 |
| 634 | p = list(position) |
| 635 | np = normalize(position) |
| 636 | for face in FACES: # check all surrounding blocks |
| 637 | for i in xrange(3): # check each dimension independently |
| 638 | if not face[i]: |
| 639 | continue |
| 640 | # How much overlap you have with this dimension. |
| 641 | d = (p[i] - np[i]) * face[i] |
| 642 | if d < pad: |
| 643 | continue |
| 644 | for dy in xrange(height): # check each height |
| 645 | op = list(np) |
| 646 | op[1] -= dy |
| 647 | op[i] += face[i] |
| 648 | if tuple(op) not in self.model.world: |
| 649 | continue |
| 650 | p[i] -= (d - pad) * face[i] |
| 651 | if face == (0, -1, 0) or face == (0, 1, 0): |
| 652 | # You are colliding with the ground or ceiling, so stop |
| 653 | # falling / rising. |
| 654 | self.dy = 0 |
| 655 | break |
| 656 | return tuple(p) |
| 657 | |
| 658 | def on_mouse_press(self, x, y, button, modifiers): |
| 659 | """ Called when a mouse button is pressed. See pyglet docs for button |