annotate tests/test_collision.nim @ 749:0b5566fc214d

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