Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually finish flipping the Y axis #275

Merged
merged 17 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions ppb/camera.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import Sequence
from typing import Union
from numbers import Number

from ppb import Vector
from ppb.sprites import BaseSprite
from ppb.flags import DoNotRender


class Camera(BaseSprite):

image = DoNotRender

def __init__(self, viewport: Sequence[int]=(0, 0, 800, 600),
pixel_ratio: float=64):
def __init__(self, viewport: Sequence[int] = (0, 0, 800, 600),
pixel_ratio: float = 64):
"""

viewport: A container of origin x, origin y, width, and
Expand All @@ -35,11 +35,11 @@ def __init__(self, viewport: Sequence[int]=(0, 0, 800, 600),

@property
def frame_top(self) -> Number:
return self.position.y - self.half_height
return self.position.y + self.half_height

@property
def frame_bottom(self) -> Number:
return self.position.y + self.half_height
return self.position.y - self.half_height

@property
def frame_left(self) -> Number:
Expand All @@ -64,6 +64,7 @@ def half_height(self) -> float:
@property
def half_width(self) -> float:
return self.frame_width / 2

@property
def viewport_width(self) -> int:
return self._viewport_width
Expand All @@ -82,7 +83,7 @@ def viewport_height(self, value: int):
self._viewport_height = value
self.viewport_offset = Vector(self.viewport_width / 2, value / 2)

def point_in_viewport(self, point:Vector) -> bool:
def point_in_viewport(self, point: Vector) -> bool:
px, py = point
vpx, vpy = self.viewport_origin
vpw = self.viewport_width
Expand All @@ -92,22 +93,23 @@ def point_in_viewport(self, point:Vector) -> bool:
def in_frame(self, sprite: BaseSprite) -> bool:
return (self.frame_left <= sprite.right and
self.frame_right >= sprite.left and
self.frame_top <= sprite.bottom and
self.frame_bottom >= sprite.top
self.frame_top >= sprite.bottom and
self.frame_bottom <= sprite.top
)

def translate_to_frame(self, point: Vector) -> Vector:
"""
Converts a vector from pixel-based window to in-game coordinate space
"""
offset = (point - self.viewport_offset) * (1/self.pixel_ratio)
loc = self.position + offset
return loc.update(y=-loc.y)
# 1. Scale from pixels to game unites
scaled = point / self.pixel_ratio
# 2. Reposition relative to frame edges
return Vector(self.frame_left + scaled.x, self.frame_top - scaled.y)

def translate_to_viewport(self, point: Vector) -> Vector:
"""
Converts a vector from in-game to pixel-based window coordinate space
"""
point = point.update(y=-point.y)
offset = (point - self.position) * self.pixel_ratio
return self.viewport_offset + offset
# 1. Reposition based on frame edges
# 2. Scale from game units to pixels
return Vector(point.x - self.frame_left, self.frame_top - point.y) * self.pixel_ratio
8 changes: 4 additions & 4 deletions ppb/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Side(FauxFloat):
sides = {
LEFT: ('x', -1),
RIGHT: ('x', 1),
TOP: ('y', -1),
BOTTOM: ('y', 1)
TOP: ('y', 1),
BOTTOM: ('y', -1)
}

def __init__(self, parent: 'BaseSprite', side: str):
Expand Down Expand Up @@ -242,15 +242,15 @@ def top(self):

@top.setter
def top(self, value):
self.position = Vector(self.position.x, value + self._offset_value)
self.position = Vector(self.position.x, value - self._offset_value)

@property
def bottom(self):
return Side(self, BOTTOM)

@bottom.setter
def bottom(self, value):
self.position = Vector(self.position.x, value - self._offset_value)
self.position = Vector(self.position.x, value + self._offset_value)

@property
def _offset_value(self):
Expand Down
95 changes: 91 additions & 4 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from math import isclose

from ppb import BaseSprite
from ppb import Vector
from ppb.camera import Camera

from hypothesis import given, assume, note, example
import hypothesis.strategies as st
from .utils import vectors


def test_camera_move():
cam = Camera()
Expand Down Expand Up @@ -29,11 +35,15 @@ def test_camera_point_in_viewport_not_at_origin():
def test_camera_translate_to_frame():
cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80)
assert cam.position == Vector(0, 0)
assert cam.frame_top == 3.75
assert cam.frame_bottom == -3.75
assert cam.frame_left == -5
assert cam.frame_right == 5
assert cam.translate_to_frame(Vector(400, 300)) == Vector(0, 0)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(2, 1)
cam.position = Vector(5, 5)
assert cam.translate_to_frame(Vector(400, 300)) == Vector(5, -5)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(7, -4)
assert cam.translate_to_frame(Vector(400, 300)) == Vector(5, 5)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(7, 6)


def test_camera_translate_to_viewport():
Expand All @@ -42,8 +52,16 @@ def test_camera_translate_to_viewport():
assert cam.translate_to_viewport(Vector(0, 0)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(2, 1)) == Vector(560, 220)
cam.position = Vector(5, 5)
assert cam.translate_to_viewport(Vector(5, -5)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(7, -4)) == Vector(560, 220)
assert cam.translate_to_viewport(Vector(5, 5)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(7, 4)) == Vector(560, 380)


def test_camera_translate_to_viewport_2():
cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=1)
assert cam.position == Vector(0, 0)
assert cam.translate_to_viewport(Vector(0, 0)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(100, 100)) == Vector(500, 200)
assert cam.translate_to_viewport(Vector(-150, -500)) == Vector(250, 800)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to parameterize these tests? It seems like we have a lot of tests that are the same basic format?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that when I find a spare moment.



def test_sprite_in_viewport():
Expand Down Expand Up @@ -71,3 +89,72 @@ def test_viewport_change_affects_frame_height():
assert cam.frame_left == -5
cam.viewport_width = 400
assert cam.frame_left == -2.5


@given(
vp_width=st.integers(min_value=1),
vp_height=st.integers(min_value=1),
pixel_ratio=st.floats(min_value=1, max_value=1e5, allow_nan=False, allow_infinity=False),
cam_pos=vectors(1e15), # Set low to prevent loss-of-precision problems about frame size
point=vectors(),
)
@example(vp_width=2, vp_height=2, pixel_ratio=1.0, cam_pos=Vector(0.0, 0.0), point=Vector(0.0, 0.0))
def test_transfromation_roundtrip(vp_width, vp_height, pixel_ratio, cam_pos, point):
cam = Camera(
viewport=(0, 0, vp_width, vp_height),
pixel_ratio=pixel_ratio,
)
cam.position = cam_pos

note(f"frame: ({cam.frame_left}, {cam.frame_bottom}) -> ({cam.frame_right}, {cam.frame_top})")

# Some underflow/loss of precision problems
assume(cam.frame_left != cam.frame_right)
assume(cam.frame_top != cam.frame_bottom)

note(f"point: {point}")

point_frame = cam.translate_to_frame(point)
note(f"point->frame: {point_frame}")
point_viewport = cam.translate_to_viewport(point_frame)
note(f"point->frame->viewport: {point_viewport}")
assert point_viewport.isclose(point, rel_tol=1e-5, rel_to=[cam.position])

point_viewport = cam.translate_to_viewport(point)
note(f"point->viewport: {point_viewport}")
point_frame = cam.translate_to_frame(point_viewport)
note(f"point->viewport->frame: {point_frame}")
assert point_frame.isclose(point, rel_tol=1e-5, rel_to=[cam.position])


@given(
vp_width=st.integers(min_value=1),
vp_height=st.integers(min_value=1),
pixel_ratio=st.floats(min_value=1, max_value=1e5, allow_nan=False, allow_infinity=False),
cam_pos=vectors(),
point=vectors(),
delta=vectors(),
)
def test_transfromation_movement(
vp_width, vp_height, pixel_ratio, cam_pos, point, delta,
):
cam = Camera(
viewport=(0, 0, vp_width, vp_height),
pixel_ratio=pixel_ratio,
)
cam.position = cam_pos

assume(delta.length != 0)

point_moved = point + delta

assume(point_moved != point) # This will happen when delta is too small to make an effect

note(f"point moved: {point_moved}")

diff = cam.translate_to_frame(point_moved) - cam.translate_to_frame(point)
note(f"frame diff: {diff}")
assert isclose(delta.length, pixel_ratio * diff.length, rel_tol=1e-5)
diff = cam.translate_to_viewport(point_moved) - cam.translate_to_viewport(point)
note(f"viewport diff: {diff}")
assert isclose(diff.length, pixel_ratio * delta.length, rel_tol=1e-5)
Loading