comparison 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
comparison
equal deleted inserted replaced
319:83954ffb238b 320:89800309ab09
2 2
3 3
4 proc main() = 4 proc main() =
5 var scene = newScene("main", root=newEntity("rect")) 5 var scene = newScene("main", root=newEntity("rect"))
6 6
7 var obj1 = newEntity("Obj1", rect(color="f00f")) 7 var obj1 = newEntity("Obj1", {
8 var obj2 = newEntity("Obj2", rect()) 8 "mesh": Component(rect(color="f00f")),
9 var obj3 = newEntity("Obj3", circle(color="0f0f")) 9 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
10 })
11 var obj2 = newEntity("Obj2", {
12 "mesh": Component(rect()),
13 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
14 })
15 var obj3 = newEntity("Obj3", {
16 "mesh": Component(circle(color="0f0f")),
17 "hitbox": Component(HitSphere(radius: 0.5))
18 })
10 19
11 scene.root.add obj2 20 scene.root.add obj2
12 scene.root.add obj1 21 scene.root.add obj1
13 scene.root.add obj3 22 scene.root.add obj3
14 obj1.transform = scale3d(0.8, 0.8) 23 obj1.transform = scale3d(0.8, 0.8)
15 obj3.transform = scale3d(0.1, 0.1) 24 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 25
20 const 26 const
21 vertexInput = @[ 27 vertexInput = @[
22 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), 28 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
23 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 29 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
35 fragmentCode="""fragcolor = colorout;""", 41 fragmentCode="""fragcolor = colorout;""",
36 ) 42 )
37 43
38 var engine = initEngine("Test collisions") 44 var engine = initEngine("Test collisions")
39 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 45 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
40 engine.addScene(scene, vertexInput, @[]) 46 engine.addScene(scene, vertexInput, @[], materialIndexAttribute="")
41 scene.addShaderGlobal("perspective", Unit4F32) 47 scene.addShaderGlobal("perspective", Unit4F32)
42 48
43 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 49 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
44 if engine.windowWasResized(): 50 if engine.windowWasResized():
45 var winSize = engine.getWindow().size 51 var winSize = engine.getWindow().size
53 59
54 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z) 60 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) 61 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) 62 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) 63 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0)
58 var hb1 = HitBox(obj1.components[1]) 64 echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()])
59 var hb2 = HitBox(obj2.components[1])
60 var hb3 = HitSphere(obj3.components[1])
61 echo overlaps(hb1, hb3)
62 engine.renderScene(scene) 65 engine.renderScene(scene)
63 engine.destroy() 66 engine.destroy()
64 67
65 68
66 when isMainModule: 69 when isMainModule: