annotate tests/test_collision.nim @ 320:89800309ab09

fix: collision tests
author Sam <sam@basx.dev>
date Sun, 13 Aug 2023 19:00:11 +0700
parents 1fbef3a2d769
children 69e18f69713b
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
320
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
7 var obj1 = newEntity("Obj1", {
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
8 "mesh": Component(rect(color="f00f")),
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
9 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
10 })
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
11 var obj2 = newEntity("Obj2", {
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
12 "mesh": Component(rect()),
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
13 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
14 })
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
15 var obj3 = newEntity("Obj3", {
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
16 "mesh": Component(circle(color="0f0f")),
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
17 "hitbox": Component(HitSphere(radius: 0.5))
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
18 })
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
19
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
20 scene.root.add obj2
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
21 scene.root.add obj1
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
22 scene.root.add obj3
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
23 obj1.transform = scale3d(0.8, 0.8)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
24 obj3.transform = scale3d(0.1, 0.1)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
25
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
26 const
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
27 vertexInput = @[
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
28 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
29 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
30 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
31 ]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
32 intermediate = @[attr[Vec4f]("colorout"),]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
33 uniforms = @[attr[Mat4]("perspective")]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
34 fragOutput = @[attr[Vec4f]("fragcolor")]
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
36 inputs=vertexInput,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
37 intermediate=intermediate,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
38 outputs=fragOutput,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
39 uniforms=uniforms,
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
40 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
41 fragmentCode="""fragcolor = colorout;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
42 )
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
43
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
44 var engine = initEngine("Test collisions")
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
45 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
320
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
46 engine.addScene(scene, vertexInput, @[], materialIndexAttribute="")
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
47 scene.addShaderGlobal("perspective", Unit4F32)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
48
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
49 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
50 if engine.windowWasResized():
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
51 var winSize = engine.getWindow().size
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
52 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
53 if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001, 0, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54 if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001, 0, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55 if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d( 0, -0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
56 if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d( 0, 0.001, 0)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
57 if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
58 if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
59
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
60 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
61 if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
62 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
63 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0)
320
89800309ab09 fix: collision tests
Sam <sam@basx.dev>
parents: 277
diff changeset
64 echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()])
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
65 engine.renderScene(scene)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
66 engine.destroy()
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
67
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
68
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
69 when isMainModule:
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
70 main()