Add reflection demo
This commit is contained in:
		
							
								
								
									
										52
									
								
								geom/demos/reflection.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								geom/demos/reflection.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
import pygame
 | 
			
		||||
from ..geom import Polygon, generate_polygon
 | 
			
		||||
from .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs
 | 
			
		||||
from ..colors import *
 | 
			
		||||
from ..math import *
 | 
			
		||||
from ..draw import *
 | 
			
		||||
from math import pi, sin, cos
 | 
			
		||||
 | 
			
		||||
class Reflection:
 | 
			
		||||
 | 
			
		||||
    title = "Reflection"
 | 
			
		||||
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.s_angle = -pi/4
 | 
			
		||||
 | 
			
		||||
    def handle_key_down(self, key):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def handle_input(self):
 | 
			
		||||
        inputs = get_inputs()
 | 
			
		||||
        if inputs[pygame.K_q]:
 | 
			
		||||
            self.s_angle -= (1/60) * pi/3
 | 
			
		||||
        if inputs[pygame.K_e]:
 | 
			
		||||
            self.s_angle += (1/60) * pi/3
 | 
			
		||||
 | 
			
		||||
    def render(self, surface):
 | 
			
		||||
        self.handle_input()
 | 
			
		||||
 | 
			
		||||
        s_end = scale(cos(self.s_angle),
 | 
			
		||||
                      sin(self.s_angle),
 | 
			
		||||
                      WIDTH)
 | 
			
		||||
        s_start = reverse(*s_end)
 | 
			
		||||
 | 
			
		||||
        # the reflecting surface
 | 
			
		||||
        s = sub(*s_end, *s_start)
 | 
			
		||||
 | 
			
		||||
        # the incoming ray, towards reflection surface
 | 
			
		||||
        incoming = sub(0, 0, *get_cursor_pos())
 | 
			
		||||
 | 
			
		||||
        # surface normal
 | 
			
		||||
        normal = normalize(*lnormal(*s))
 | 
			
		||||
 | 
			
		||||
        # projection of incoming ray onto normal, multiplied by 2
 | 
			
		||||
        projection_2 = dot(*incoming, *normal) * 2
 | 
			
		||||
 | 
			
		||||
        # outgoing ray, away from reflection surface
 | 
			
		||||
        outgoing = sub(*incoming, *scale(*normal, projection_2))
 | 
			
		||||
 | 
			
		||||
        line(surface, WHITE, s_start, s_end)
 | 
			
		||||
        line(surface, CYAN, (0, 0), scale(*normal, 1000))
 | 
			
		||||
        line(surface, YELLOW, get_cursor_pos(), (0, 0))
 | 
			
		||||
        line(surface, RED, (0, 0), outgoing)
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
import pygame
 | 
			
		||||
from .demos import aabb, vec_proj, sat
 | 
			
		||||
from .demos import aabb, vec_proj, sat, reflection
 | 
			
		||||
from . import HEIGHT
 | 
			
		||||
from .colors import *
 | 
			
		||||
from .draw import text_screen
 | 
			
		||||
@ -9,6 +9,7 @@ class Screen:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.demos = [
 | 
			
		||||
            vec_proj.VecProj(),
 | 
			
		||||
            reflection.Reflection(),
 | 
			
		||||
            aabb.AABBDistance(),
 | 
			
		||||
            aabb.PointAABBDistance(),
 | 
			
		||||
            sat.SeparatingAxisTheorem(),
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user