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