~nch/gamelog

ref: 2860fd647c20bdd08c60e5362e2ecffd9a4c1961 gamelog/test_collisions.nim -rw-r--r-- 1.7 KiB View raw
2860fd64 — nc minor reorg 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import unittest
import glm
import game
import options
import glfw
import opengl
import debug_draw

let unit_sphere = (vec3f(0, 0, 0), vec3f(1, 1, 1)).Ellipse

proc display_scene(triangle: array[3, Vec3f]) =
    glfw.initialize()
    load_extensions()

    let width = 800
    let height = 600


    let proj: Mat4[float32] = perspective[float32](Pi / 4, width.float32 / height.float32, 0.1, 100)

    var c = DefaultOpenglWindowConfig
    c.title = "game"
    var w = new_window(c)

    glfw.make_context_current(w)

    var debug = init_debug()

    while true:
        let cam_pos = vec3f(sin(glfw.get_time()) * 5, 2, cos(glfw.get_time()) * 5)
        let view: Mat4[float32] = look_at(cam_pos, vec3f(0, 0, 0), vec3f(0, 1, 0))
        var mvp = proj * view

        gl_clear(GL_COLOR_BUFFER_BIT)
        debug.sphere(unit_sphere.pos, unit_sphere.radius.x)
        debug.edge(triangle[0], triangle[1], triangle[2], triangle[0])
        debug.draw(mvp)

        w.swap_buffers
        poll_events()
        if w.should_close == true or w.is_key_down(keyEscape):
            break

display_scene([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)])

suite "collision":
    test "no collision":
        require not check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()
        require not check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()

    test "collisions while still":
        require check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(0, 0, 0), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()
        require check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()