annotate tests/test_collision.nim @ 277:1fbef3a2d769

add: collision tests
author Sam <sam@basx.dev>
date Sat, 03 Jun 2023 13:32:15 +0700
parents
children 89800309ab09
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
2
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
3
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
4 proc main() =
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
5 var scene = newScene("main", root=newEntity("rect"))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
6
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
7 var obj1 = newEntity("Obj1", rect(color="f00f"))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
8 var obj2 = newEntity("Obj2", rect())
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
9 var obj3 = newEntity("Obj3", circle(color="0f0f"))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
10
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
11 scene.root.add obj2
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
12 scene.root.add obj1
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
13 scene.root.add obj3
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
14 obj1.transform = scale3d(0.8, 0.8)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
15 obj3.transform = scale3d(0.1, 0.1)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
16 obj1.add HitBox(transform: translate3d(-0.5, -0.5, -0.5))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
17 obj2.add HitBox(transform: translate3d(-0.5, -0.5, -0.5))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
18 obj3.add HitSphere(radius: 0.5)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
19
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
20 const
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
21 vertexInput = @[
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
22 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
23 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
24 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
25 ]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
26 intermediate = @[attr[Vec4f]("colorout"),]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
27 uniforms = @[attr[Mat4]("perspective")]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
28 fragOutput = @[attr[Vec4f]("fragcolor")]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
29 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
30 inputs=vertexInput,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
31 intermediate=intermediate,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
32 outputs=fragOutput,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
33 uniforms=uniforms,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
34 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35 fragmentCode="""fragcolor = colorout;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
36 )
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
37
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
38 var engine = initEngine("Test collisions")
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
39 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
40 engine.addScene(scene, vertexInput, @[])
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
41 scene.addShaderGlobal("perspective", Unit4F32)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
42
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
43 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
44 if engine.windowWasResized():
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
45 var winSize = engine.getWindow().size
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
46 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
47 if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001, 0, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
48 if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001, 0, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
49 if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d( 0, -0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
50 if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d( 0, 0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
51 if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
52 if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
53
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55 if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
56 if engine.keyIsDown(Key.C): obj2.transform = obj2.transform * translate3d(0, -0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
57 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
58 var hb1 = HitBox(obj1.components[1])
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
59 var hb2 = HitBox(obj2.components[1])
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
60 var hb3 = HitSphere(obj3.components[1])
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
61 echo overlaps(hb1, hb3)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
62 engine.renderScene(scene)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
63 engine.destroy()
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
64
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
65
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
66 when isMainModule:
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
67 main()