├── .gitignore ├── SciTEDirectory.properties ├── dub.json ├── examples ├── DroidSans.ttf ├── SciTEDirectory.properties ├── demo │ ├── SciTEDirectory.properties │ ├── demo.d │ ├── dub.json │ ├── framework │ │ ├── debug_draw.d │ │ ├── main.d │ │ ├── test.d │ │ └── window.d │ └── tests │ │ ├── addpair.d │ │ ├── applyforce.d │ │ ├── basicslidercrank.d │ │ ├── bodytypes.d │ │ ├── breakable.d │ │ ├── bridge.d │ │ ├── bullettest.d │ │ ├── cantilever.d │ │ ├── car.d │ │ ├── chain.d │ │ ├── charactercollision.d │ │ ├── collisionfiltering.d │ │ ├── collisionprocessing.d │ │ ├── compoundshapes.d │ │ ├── confined.d │ │ ├── continuoustest.d │ │ ├── convexhull.d │ │ ├── conveyorbelt.d │ │ ├── distancetest.d │ │ ├── dominos.d │ │ ├── dumpshell.d │ │ ├── dynamictreetest.d │ │ ├── edgeshapes.d │ │ ├── edgetest.d │ │ ├── gears.d │ │ ├── heavyonlight.d │ │ ├── heavyonlighttwo.d │ │ ├── mobile.d │ │ ├── mobilebalanced.d │ │ ├── motorjoint.d │ │ ├── onesidedplatform.d │ │ ├── pinball.d │ │ ├── polycollision.d │ │ ├── polyshapes.d │ │ ├── prismatic.d │ │ ├── pulleys.d │ │ ├── pyramid.d │ │ ├── raycast.d │ │ ├── revolute.d │ │ ├── ropejoint.d │ │ ├── sensortest.d │ │ ├── shapeediting.d │ │ ├── slidercrank.d │ │ ├── spherestack.d │ │ ├── test_entries.d │ │ ├── theojansen.d │ │ ├── tiles.d │ │ ├── timeofimpact.d │ │ ├── tumbler.d │ │ ├── varyingfriction.d │ │ ├── varyingrestitution.d │ │ ├── verticalstack.d │ │ └── web.d └── hello_world │ ├── dub.json │ └── hello_world.d ├── license.txt ├── readme.md ├── screenshot └── dbox.png └── src └── dbox ├── collision ├── b2broadphase.d ├── b2collidecircle.d ├── b2collideedge.d ├── b2collidepolygon.d ├── b2collision.d ├── b2distance.d ├── b2dynamictree.d ├── b2timeofimpact.d ├── package.d └── shapes │ ├── b2chainshape.d │ ├── b2circleshape.d │ ├── b2edgeshape.d │ ├── b2polygonshape.d │ ├── b2shape.d │ └── package.d ├── common ├── b2blockallocator.d ├── b2draw.d ├── b2growablestack.d ├── b2math.d ├── b2settings.d ├── b2stackallocator.d ├── b2timer.d ├── b2util.d └── package.d ├── dynamics ├── b2body.d ├── b2contactmanager.d ├── b2fixture.d ├── b2island.d ├── b2timestep.d ├── b2world.d ├── b2worldcallbacks.d ├── contacts │ ├── b2chainandcirclecontact.d │ ├── b2chainandpolygoncontact.d │ ├── b2circlecontact.d │ ├── b2contact.d │ ├── b2contactsolver.d │ ├── b2edgeandcirclecontact.d │ ├── b2edgeandpolygoncontact.d │ ├── b2polygonandcirclecontact.d │ ├── b2polygoncontact.d │ └── package.d ├── joints │ ├── b2distancejoint.d │ ├── b2frictionjoint.d │ ├── b2gearjoint.d │ ├── b2joint.d │ ├── b2motorjoint.d │ ├── b2mousejoint.d │ ├── b2prismaticjoint.d │ ├── b2pulleyjoint.d │ ├── b2revolutejoint.d │ ├── b2ropejoint.d │ ├── b2weldjoint.d │ ├── b2wheeljoint.d │ └── package.d └── package.d ├── package.d └── rope ├── b2rope.d └── package.d /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /bin/* 3 | /examples/bin/* 4 | /data 5 | /todo/* 6 | *.a 7 | *.c 8 | *.dat 9 | *.h 10 | *.o 11 | *.7z 12 | *.di 13 | *.cpp 14 | *.dat 15 | *.log 16 | *.lib 17 | !implib/*.lib 18 | *.dll 19 | *.exe 20 | *.map 21 | *.obj 22 | *.rar 23 | *.xml 24 | *.deps 25 | *.modTime 26 | .sandbox 27 | .dub 28 | src/translate.d 29 | *.visualdproj 30 | *.sln 31 | *.suo 32 | todo.md 33 | *.pdb 34 | *.exe_cv 35 | -------------------------------------------------------------------------------- /SciTEDirectory.properties: -------------------------------------------------------------------------------- 1 | # This is a SciTE[1] configuration file. You can ignore this file if you're not using SciTE. 2 | # [1] : http://www.scintilla.org/SciTE.html 3 | command.go.subsystem.*.d=0 4 | command.go.*.d=dub -q --root=$(SciteDirectoryHome) 5 | -------------------------------------------------------------------------------- /dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dbox", 3 | 4 | "description": "dbox is a D port of the Box2D game physics library", 5 | 6 | "authors": [ 7 | "Erin Catto", 8 | "Andrej Mitrovic" 9 | ], 10 | 11 | "homepage": "https://github.com/d-gamedev-team/dbox", 12 | 13 | "copyright": "Copyright (c) 2006-2013 Erin Catto http://www.gphysics.com", 14 | 15 | "license": "zlib", 16 | 17 | "targetName": "dbox", 18 | 19 | "targetType": "staticLibrary", 20 | 21 | "targetPath" : "bin", 22 | 23 | "sourcePaths": [ 24 | "src" 25 | ], 26 | 27 | "dependencies": { 28 | "glad-drey": ">=0.0.2", 29 | "dimgui": ">=0.1.7-alpha", 30 | }, 31 | 32 | "subPackages": [ 33 | "examples/hello_world", 34 | "examples/demo", 35 | ], 36 | } 37 | -------------------------------------------------------------------------------- /examples/DroidSans.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d-gamedev-team/dbox/6f81fe065abec1e7def44fc777c5d8e9da936104/examples/DroidSans.ttf -------------------------------------------------------------------------------- /examples/SciTEDirectory.properties: -------------------------------------------------------------------------------- 1 | # This is a SciTE[1] configuration file. You can ignore this file if you're not using SciTE. 2 | # [1] : http://www.scintilla.org/SciTE.html 3 | command.go.subsystem.*.d=0 4 | command.go.*.d=dub -q --root=$(FileDir) 5 | -------------------------------------------------------------------------------- /examples/demo/SciTEDirectory.properties: -------------------------------------------------------------------------------- 1 | # This is a SciTE[1] configuration file. You can ignore this file if you're not using SciTE. 2 | # [1] : http://www.scintilla.org/SciTE.html 3 | command.go.subsystem.*.json=0 4 | command.go.subsystem.*.d=0 5 | command.go.*.d=dub -q --root=$(SciteDirectoryHome) 6 | command.go.*.json=dub -q --root=$(SciteDirectoryHome) 7 | -------------------------------------------------------------------------------- /examples/demo/demo.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module demo; 19 | 20 | import framework.main; 21 | 22 | void main() 23 | { 24 | runTests(); 25 | } 26 | -------------------------------------------------------------------------------- /examples/demo/dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demo", 3 | 4 | "description": "demo", 5 | 6 | "authors": [ 7 | "Erin Catto", 8 | "Andrej Mitrovic" 9 | ], 10 | 11 | "homepage": "https://github.com/d-gamedev-team/dbox", 12 | 13 | "copyright": "Copyright (c) 2006-2009 Erin Catto http://www.box2d.org", 14 | 15 | "license": "zlib", 16 | 17 | "buildOptions": ["debugInfo", "warningsAsErrors"], 18 | 19 | "targetName" : "demo", 20 | 21 | "targetPath" : "../../bin", 22 | 23 | "targetType": "executable", 24 | 25 | "sourceFiles": ["demo.d"], 26 | 27 | "sourcePaths": [ 28 | ".", "framework", "tests" 29 | ], 30 | 31 | "mainSourceFile": "demo.d", 32 | 33 | "libs-linux": [ 34 | "dl", 35 | "glfw", 36 | ], 37 | 38 | "libs-osx": [ 39 | "glfw3", 40 | ], 41 | 42 | "dependencies": { 43 | "dbox": {"path": "../../", "version": "~master"}, 44 | "glfw-drey": ">=0.0.2", 45 | "glwtf-drey": ">=0.0.1", 46 | "dimgui": ">=0.1.4-alpha", 47 | }, 48 | } 49 | -------------------------------------------------------------------------------- /examples/demo/framework/window.d: -------------------------------------------------------------------------------- 1 | module framework.window; 2 | 3 | /** 4 | Contains various helpers, common code, and initialization routines. 5 | */ 6 | 7 | import std.algorithm : min; 8 | import std.exception : enforce; 9 | import std.functional : toDelegate; 10 | import std.stdio : stderr; 11 | import std.string : format; 12 | 13 | import deimos.glfw.glfw3; 14 | 15 | import glad.gl.enums; 16 | import glad.gl.ext; 17 | import glad.gl.funcs; 18 | import glad.gl.loader; 19 | import glad.gl.types; 20 | 21 | import glwtf.input; 22 | import glwtf.window; 23 | 24 | /// init 25 | shared static this() 26 | { 27 | enforce(glfwInit()); 28 | } 29 | 30 | /// uninit 31 | shared static ~this() 32 | { 33 | glfwTerminate(); 34 | } 35 | 36 | /// 37 | enum WindowMode 38 | { 39 | fullscreen, 40 | windowed, 41 | } 42 | 43 | /** 44 | Create a window, an OpenGL 3.x context, and set up some other 45 | common routines for error handling, window resizing, etc. 46 | */ 47 | Window createWindow(string windowName, WindowMode windowMode = WindowMode.windowed, int width = 1024, int height = 768) 48 | { 49 | auto vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 50 | 51 | // constrain the window size so it isn't larger than the desktop size. 52 | width = min(width, vidMode.width); 53 | height = min(height, vidMode.height); 54 | 55 | // set the window to be initially inivisible since we're repositioning it. 56 | glfwWindowHint(GLFW_VISIBLE, 0); 57 | 58 | // enable debugging 59 | glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); 60 | 61 | Window window = createWindowContext(windowName, WindowMode.windowed, width, height); 62 | 63 | // center the window on the screen 64 | glfwSetWindowPos(window.window, (vidMode.width - width) / 2, (vidMode.height - height) / 2); 65 | 66 | // glfw-specific error routine (not a generic GL error handler) 67 | register_glfw_error_callback(&glfwErrorCallback); 68 | 69 | // anti-aliasing number of samples. 70 | window.samples = 4; 71 | 72 | // activate an opengl context. 73 | window.make_context_current(); 74 | 75 | // load all OpenGL function pointers via glad. 76 | enforce(gladLoadGL()); 77 | 78 | enforce(glGenBuffers !is null); 79 | 80 | // only interested in GL 3.x 81 | enforce(GLVersion.major >= 3); 82 | 83 | // turn v-sync off. 84 | glfwSwapInterval(1); 85 | 86 | version (OSX) 87 | { 88 | // GL_ARM_debug_output and GL_KHR_debug are not supported under OS X 10.9.3 89 | } 90 | else 91 | { 92 | // check if the debug output extension is supported 93 | if (GL_ARB_debug_output || GL_KHR_debug) 94 | { 95 | 96 | // cast: workaround for 'nothrow' propagation bug (haven't been able to reduce it) 97 | auto hookDebugCallback = GL_ARB_debug_output 98 | ? glDebugMessageCallbackARB 99 | : cast(typeof(glDebugMessageCallbackARB)) 100 | glDebugMessageCallback; 101 | 102 | 103 | // hook the debug callback 104 | // cast: when using derelict it assumes its nothrow 105 | hookDebugCallback(cast(GLDEBUGPROCARB)&glErrorCallback, null); 106 | 107 | // enable proper stack tracing support (otherwise we'd get random failures at runtime) 108 | glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); 109 | } 110 | } 111 | 112 | // finally show the window 113 | glfwShowWindow(window.window); 114 | 115 | return window; 116 | } 117 | 118 | /** Create a window and an OpenGL context. */ 119 | Window createWindowContext(string windowName, WindowMode windowMode, int width, int height) 120 | { 121 | auto window = new Window(); 122 | auto monitor = windowMode == WindowMode.fullscreen ? glfwGetPrimaryMonitor() : null; 123 | auto context = window.create_highest_available_context(width, height, windowName, monitor, null, GLFW_OPENGL_CORE_PROFILE); 124 | 125 | // ensure we've loaded a proper context 126 | enforce(context.major >= 3); 127 | 128 | return window; 129 | } 130 | 131 | /** Just emit errors to stderr on GLFW errors. */ 132 | void glfwErrorCallback(int code, string msg) 133 | { 134 | stderr.writefln("Error (%s): %s", code, msg); 135 | } 136 | 137 | /// 138 | class GLException : Exception 139 | { 140 | @safe pure nothrow this(string msg = "", string file = __FILE__, size_t line = __LINE__, Throwable next = null) 141 | { 142 | super(msg, file, line, next); 143 | } 144 | 145 | @safe pure nothrow this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) 146 | { 147 | super(msg, file, line, next); 148 | } 149 | } 150 | 151 | /** 152 | GL_ARB_debug_output or GL_KHR_debug callback. 153 | 154 | Throwing exceptions across language boundaries is ok as 155 | long as $(B GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB) is enabled. 156 | */ 157 | extern (Windows) 158 | private void glErrorCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, in GLchar* message, GLvoid* userParam) 159 | { 160 | string msg = format("source: %s, type: %s, id: %s, severity: %s, length: %s, message: %s, userParam: %s", 161 | source, type, id, severity, length, message.to!string, userParam); 162 | throw new GLException(msg); 163 | } 164 | -------------------------------------------------------------------------------- /examples/demo/tests/addpair.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.addpair; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class AddPair : Test 33 | { 34 | this() 35 | { 36 | m_world.SetGravity(b2Vec2(0.0f, 0.0f)); 37 | { 38 | auto shape = new b2CircleShape(); 39 | shape.m_p.SetZero(); 40 | shape.m_radius = 0.1f; 41 | 42 | float minX = -6.0f; 43 | float maxX = 0.0f; 44 | float minY = 4.0f; 45 | float maxY = 6.0f; 46 | 47 | for (int32 i = 0; i < 400; ++i) 48 | { 49 | b2BodyDef bd; 50 | bd.type = b2_dynamicBody; 51 | bd.position = b2Vec2(RandomFloat(minX, maxX), RandomFloat(minY, maxY)); 52 | b2Body* body_ = m_world.CreateBody(&bd); 53 | body_.CreateFixture(shape, 0.01f); 54 | } 55 | } 56 | 57 | { 58 | auto shape = new b2PolygonShape(); 59 | shape.SetAsBox(1.5f, 1.5f); 60 | b2BodyDef bd; 61 | bd.type = b2_dynamicBody; 62 | bd.position.Set(-40.0f, 5.0f); 63 | bd.bullet = true; 64 | b2Body* body_ = m_world.CreateBody(&bd); 65 | body_.CreateFixture(shape, 1.0f); 66 | body_.SetLinearVelocity(b2Vec2(150.0f, 0.0f)); 67 | } 68 | } 69 | 70 | static Test Create() 71 | { 72 | return new typeof(this); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /examples/demo/tests/basicslidercrank.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.basicslidercrank; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class BasicSliderCrank : Test 33 | { 34 | this() 35 | { 36 | b2Body* ground = null; 37 | { 38 | b2BodyDef bd; 39 | bd.position.Set(0.0f, 17.0f); 40 | ground = m_world.CreateBody(&bd); 41 | } 42 | 43 | { 44 | b2Body* prevBody = ground; 45 | 46 | // Define crank. 47 | { 48 | auto shape = new b2PolygonShape(); 49 | shape.SetAsBox(4.0f, 1.0f); 50 | 51 | b2BodyDef bd; 52 | bd.type = b2_dynamicBody; 53 | bd.position.Set(-8.0f, 20.0f); 54 | b2Body* body_ = m_world.CreateBody(&bd); 55 | body_.CreateFixture(shape, 2.0f); 56 | 57 | b2RevoluteJointDef rjd = new b2RevoluteJointDef(); 58 | rjd.Initialize(prevBody, body_, b2Vec2(-12.0f, 20.0f)); 59 | m_world.CreateJoint(rjd); 60 | 61 | prevBody = body_; 62 | } 63 | 64 | // Define connecting rod 65 | { 66 | auto shape = new b2PolygonShape(); 67 | shape.SetAsBox(8.0f, 1.0f); 68 | 69 | b2BodyDef bd; 70 | bd.type = b2_dynamicBody; 71 | bd.position.Set(4.0f, 20.0f); 72 | b2Body* body_ = m_world.CreateBody(&bd); 73 | body_.CreateFixture(shape, 2.0f); 74 | 75 | b2RevoluteJointDef rjd = new b2RevoluteJointDef(); 76 | rjd.Initialize(prevBody, body_, b2Vec2(-4.0f, 20.0f)); 77 | m_world.CreateJoint(rjd); 78 | 79 | prevBody = body_; 80 | } 81 | 82 | // Define piston 83 | { 84 | auto shape = new b2PolygonShape(); 85 | shape.SetAsBox(3.0f, 3.0f); 86 | 87 | b2BodyDef bd; 88 | bd.type = b2_dynamicBody; 89 | bd.fixedRotation = true; 90 | bd.position.Set(12.0f, 20.0f); 91 | b2Body* body_ = m_world.CreateBody(&bd); 92 | body_.CreateFixture(shape, 2.0f); 93 | 94 | b2RevoluteJointDef rjd = new b2RevoluteJointDef(); 95 | rjd.Initialize(prevBody, body_, b2Vec2(12.0f, 20.0f)); 96 | m_world.CreateJoint(rjd); 97 | 98 | b2PrismaticJointDef pjd = new b2PrismaticJointDef(); 99 | pjd.Initialize(ground, body_, b2Vec2(12.0f, 17.0f), b2Vec2(1.0f, 0.0f)); 100 | m_world.CreateJoint(pjd); 101 | } 102 | } 103 | } 104 | 105 | static Test Create() 106 | { 107 | return new typeof(this); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /examples/demo/tests/bodytypes.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.bodytypes; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class BodyTypes : Test 33 | { 34 | this() 35 | { 36 | b2Body* ground = null; 37 | { 38 | b2BodyDef bd; 39 | ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f)); 43 | 44 | b2FixtureDef fd; 45 | fd.shape = shape; 46 | 47 | ground.CreateFixture(&fd); 48 | } 49 | 50 | // Define attachment 51 | { 52 | b2BodyDef bd; 53 | bd.type = b2_dynamicBody; 54 | bd.position.Set(0.0f, 3.0f); 55 | m_attachment = m_world.CreateBody(&bd); 56 | 57 | auto shape = new b2PolygonShape(); 58 | shape.SetAsBox(0.5f, 2.0f); 59 | m_attachment.CreateFixture(shape, 2.0f); 60 | } 61 | 62 | // Define platform 63 | { 64 | b2BodyDef bd; 65 | bd.type = b2_dynamicBody; 66 | bd.position.Set(-4.0f, 5.0f); 67 | m_platform = m_world.CreateBody(&bd); 68 | 69 | auto shape = new b2PolygonShape(); 70 | shape.SetAsBox(0.5f, 4.0f, b2Vec2(4.0f, 0.0f), 0.5f * b2_pi); 71 | 72 | b2FixtureDef fd; 73 | fd.shape = shape; 74 | fd.friction = 0.6f; 75 | fd.density = 2.0f; 76 | m_platform.CreateFixture(&fd); 77 | 78 | b2RevoluteJointDef rjd = new b2RevoluteJointDef(); 79 | rjd.Initialize(m_attachment, m_platform, b2Vec2(0.0f, 5.0f)); 80 | rjd.maxMotorTorque = 50.0f; 81 | rjd.enableMotor = true; 82 | m_world.CreateJoint(rjd); 83 | 84 | b2PrismaticJointDef pjd = new b2PrismaticJointDef(); 85 | pjd.Initialize(ground, m_platform, b2Vec2(0.0f, 5.0f), b2Vec2(1.0f, 0.0f)); 86 | 87 | pjd.maxMotorForce = 1000.0f; 88 | pjd.enableMotor = true; 89 | pjd.lowerTranslation = -10.0f; 90 | pjd.upperTranslation = 10.0f; 91 | pjd.enableLimit = true; 92 | 93 | m_world.CreateJoint(pjd); 94 | 95 | m_speed = 3.0f; 96 | } 97 | 98 | // Create a payload 99 | { 100 | b2BodyDef bd; 101 | bd.type = b2_dynamicBody; 102 | bd.position.Set(0.0f, 8.0f); 103 | b2Body* body_ = m_world.CreateBody(&bd); 104 | 105 | auto shape = new b2PolygonShape(); 106 | shape.SetAsBox(0.75f, 0.75f); 107 | 108 | b2FixtureDef fd; 109 | fd.shape = shape; 110 | fd.friction = 0.6f; 111 | fd.density = 2.0f; 112 | 113 | body_.CreateFixture(&fd); 114 | } 115 | } 116 | 117 | override void Keyboard(int key) 118 | { 119 | switch (key) 120 | { 121 | case GLFW_KEY_D: 122 | m_platform.SetType(b2_dynamicBody); 123 | break; 124 | 125 | case GLFW_KEY_S: 126 | m_platform.SetType(b2_staticBody); 127 | break; 128 | 129 | case GLFW_KEY_K: 130 | m_platform.SetType(b2_kinematicBody); 131 | m_platform.SetLinearVelocity(b2Vec2(-m_speed, 0.0f)); 132 | m_platform.SetAngularVelocity(0.0f); 133 | break; 134 | 135 | default: 136 | break; 137 | } 138 | } 139 | 140 | override void Step(Settings* settings) 141 | { 142 | super.Step(settings); 143 | 144 | // Drive the kinematic body_. 145 | if (m_platform.GetType() == b2_kinematicBody) 146 | { 147 | b2Vec2 p = m_platform.GetTransform().p; 148 | b2Vec2 v = m_platform.GetLinearVelocity(); 149 | 150 | if ((p.x < -10.0f && v.x < 0.0f) || 151 | (p.x > 10.0f && v.x > 0.0f)) 152 | { 153 | v.x = -v.x; 154 | m_platform.SetLinearVelocity(v); 155 | } 156 | } 157 | 158 | g_debugDraw.DrawString(5, m_textLine, "Keys: (d) dynamic, (s) static, (k) kinematic"); 159 | m_textLine += DRAW_STRING_NEW_LINE; 160 | } 161 | 162 | static Test Create() 163 | { 164 | return new typeof(this); 165 | } 166 | 167 | b2Body* m_attachment; 168 | b2Body* m_platform; 169 | float32 m_speed = 0; 170 | } 171 | -------------------------------------------------------------------------------- /examples/demo/tests/breakable.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.breakable; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | // This is used to test sensor shapes. 33 | class Breakable : Test 34 | { 35 | this() 36 | { 37 | // Ground body_ 38 | { 39 | b2BodyDef bd; 40 | b2Body* ground = m_world.CreateBody(&bd); 41 | 42 | auto shape = new b2EdgeShape(); 43 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 44 | ground.CreateFixture(shape, 0.0f); 45 | } 46 | 47 | // Breakable dynamic body_ 48 | { 49 | b2BodyDef bd; 50 | bd.type = b2_dynamicBody; 51 | bd.position.Set(0.0f, 40.0f); 52 | bd.angle = 0.25f * b2_pi; 53 | m_body1 = m_world.CreateBody(&bd); 54 | 55 | m_shape1 = new typeof(m_shape1); 56 | m_shape1.SetAsBox(0.5f, 0.5f, b2Vec2(-0.5f, 0.0f), 0.0f); 57 | m_piece1 = m_body1.CreateFixture(m_shape1, 1.0f); 58 | 59 | m_shape2 = new typeof(m_shape2); 60 | m_shape2.SetAsBox(0.5f, 0.5f, b2Vec2(0.5f, 0.0f), 0.0f); 61 | m_piece2 = m_body1.CreateFixture(m_shape2, 1.0f); 62 | } 63 | 64 | m_break = false; 65 | m_broke = false; 66 | } 67 | 68 | override void PostSolve(b2Contact contact, const(b2ContactImpulse)* impulse) 69 | { 70 | if (m_broke) 71 | { 72 | // The body_ already broke. 73 | return; 74 | } 75 | 76 | // Should the body_ break? 77 | int32 count = contact.GetManifold().pointCount; 78 | 79 | float32 maxImpulse = 0.0f; 80 | 81 | for (int32 i = 0; i < count; ++i) 82 | { 83 | maxImpulse = b2Max(maxImpulse, impulse.normalImpulses[i]); 84 | } 85 | 86 | if (maxImpulse > 40.0f) 87 | { 88 | // Flag the body_ for breaking. 89 | m_break = true; 90 | } 91 | } 92 | 93 | void Break() 94 | { 95 | // Create two bodies from one. 96 | b2Body* body1 = m_piece1.GetBody(); 97 | b2Vec2 center = body1.GetWorldCenter(); 98 | 99 | body1.DestroyFixture(m_piece2); 100 | m_piece2 = null; 101 | 102 | b2BodyDef bd; 103 | bd.type = b2_dynamicBody; 104 | bd.position = body1.GetPosition(); 105 | bd.angle = body1.GetAngle(); 106 | 107 | b2Body* body2 = m_world.CreateBody(&bd); 108 | m_piece2 = body2.CreateFixture(m_shape2, 1.0f); 109 | 110 | // Compute consistent velocities for new bodies based on 111 | // cached velocity. 112 | b2Vec2 center1 = body1.GetWorldCenter(); 113 | b2Vec2 center2 = body2.GetWorldCenter(); 114 | 115 | b2Vec2 velocity1 = m_velocity + b2Cross(m_angularVelocity, center1 - center); 116 | b2Vec2 velocity2 = m_velocity + b2Cross(m_angularVelocity, center2 - center); 117 | 118 | body1.SetAngularVelocity(m_angularVelocity); 119 | body1.SetLinearVelocity(velocity1); 120 | 121 | body2.SetAngularVelocity(m_angularVelocity); 122 | body2.SetLinearVelocity(velocity2); 123 | } 124 | 125 | override void Step(Settings* settings) 126 | { 127 | super.Step(settings); 128 | 129 | if (m_break) 130 | { 131 | Break(); 132 | m_broke = true; 133 | m_break = false; 134 | } 135 | 136 | // Cache velocities to improve movement on breakage. 137 | if (m_broke == false) 138 | { 139 | m_velocity = m_body1.GetLinearVelocity(); 140 | m_angularVelocity = m_body1.GetAngularVelocity(); 141 | } 142 | } 143 | 144 | b2Body* m_body1; 145 | b2Vec2 m_velocity; 146 | float32 m_angularVelocity = 0; 147 | b2PolygonShape m_shape1; 148 | b2PolygonShape m_shape2; 149 | b2Fixture* m_piece1; 150 | b2Fixture* m_piece2; 151 | 152 | bool m_broke; 153 | bool m_break; 154 | 155 | static Test Create() 156 | { 157 | return new typeof(this); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /examples/demo/tests/bridge.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.bridge; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Bridge : Test 33 | { 34 | enum 35 | { 36 | e_count = 30, 37 | } 38 | 39 | 40 | this() 41 | { 42 | b2Body* ground = null; 43 | { 44 | b2BodyDef bd; 45 | ground = m_world.CreateBody(&bd); 46 | 47 | auto shape = new b2EdgeShape(); 48 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 49 | ground.CreateFixture(shape, 0.0f); 50 | } 51 | 52 | { 53 | auto shape = new b2PolygonShape(); 54 | shape.SetAsBox(0.5f, 0.125f); 55 | 56 | b2FixtureDef fd; 57 | fd.shape = shape; 58 | fd.density = 20.0f; 59 | fd.friction = 0.2f; 60 | 61 | b2RevoluteJointDef jd = new b2RevoluteJointDef(); 62 | 63 | b2Body* prevBody = ground; 64 | 65 | for (int32 i = 0; i < e_count; ++i) 66 | { 67 | b2BodyDef bd; 68 | bd.type = b2_dynamicBody; 69 | bd.position.Set(-14.5f + 1.0f * i, 5.0f); 70 | b2Body* body_ = m_world.CreateBody(&bd); 71 | body_.CreateFixture(&fd); 72 | 73 | b2Vec2 anchor = b2Vec2(-15.0f + 1.0f * i, 5.0f); 74 | jd.Initialize(prevBody, body_, anchor); 75 | m_world.CreateJoint(jd); 76 | 77 | if (i == (e_count >> 1)) 78 | { 79 | m_middle = body_; 80 | } 81 | prevBody = body_; 82 | } 83 | 84 | b2Vec2 anchor = b2Vec2(-15.0f + 1.0f * e_count, 5.0f); 85 | jd.Initialize(prevBody, ground, anchor); 86 | m_world.CreateJoint(jd); 87 | } 88 | 89 | for (int32 i = 0; i < 2; ++i) 90 | { 91 | b2Vec2 vertices[3]; 92 | vertices[0].Set(-0.5f, 0.0f); 93 | vertices[1].Set(0.5f, 0.0f); 94 | vertices[2].Set(0.0f, 1.5f); 95 | 96 | auto shape = new b2PolygonShape(); 97 | shape.Set(vertices.ptr, 3); 98 | 99 | b2FixtureDef fd; 100 | fd.shape = shape; 101 | fd.density = 1.0f; 102 | 103 | b2BodyDef bd; 104 | bd.type = b2_dynamicBody; 105 | bd.position.Set(-8.0f + 8.0f * i, 12.0f); 106 | b2Body* body_ = m_world.CreateBody(&bd); 107 | body_.CreateFixture(&fd); 108 | } 109 | 110 | for (int32 i = 0; i < 3; ++i) 111 | { 112 | b2CircleShape shape = new b2CircleShape(); 113 | shape.m_radius = 0.5f; 114 | 115 | b2FixtureDef fd; 116 | fd.shape = shape; 117 | fd.density = 1.0f; 118 | 119 | b2BodyDef bd; 120 | bd.type = b2_dynamicBody; 121 | bd.position.Set(-6.0f + 6.0f * i, 10.0f); 122 | b2Body* body_ = m_world.CreateBody(&bd); 123 | body_.CreateFixture(&fd); 124 | } 125 | } 126 | 127 | static Test Create() 128 | { 129 | return new typeof(this); 130 | } 131 | 132 | b2Body* m_middle; 133 | } 134 | -------------------------------------------------------------------------------- /examples/demo/tests/bullettest.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.bullettest; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class BulletTest : Test 33 | { 34 | this() 35 | { 36 | { 37 | b2BodyDef bd; 38 | bd.position.Set(0.0f, 0.0f); 39 | b2Body* body_ = m_world.CreateBody(&bd); 40 | 41 | b2EdgeShape edge = new b2EdgeShape(); 42 | 43 | edge.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f)); 44 | body_.CreateFixture(edge, 0.0f); 45 | 46 | auto shape = new b2PolygonShape(); 47 | shape.SetAsBox(0.2f, 1.0f, b2Vec2(0.5f, 1.0f), 0.0f); 48 | body_.CreateFixture(shape, 0.0f); 49 | } 50 | 51 | { 52 | b2BodyDef bd; 53 | bd.type = b2_dynamicBody; 54 | bd.position.Set(0.0f, 4.0f); 55 | 56 | b2PolygonShape box = new b2PolygonShape(); 57 | box.SetAsBox(2.0f, 0.1f); 58 | 59 | m_body = m_world.CreateBody(&bd); 60 | m_body.CreateFixture(box, 1.0f); 61 | 62 | box.SetAsBox(0.25f, 0.25f); 63 | 64 | // m_x = RandomFloat(-1.0f, 1.0f); 65 | m_x = 0.20352793f; 66 | bd.position.Set(m_x, 10.0f); 67 | bd.bullet = true; 68 | 69 | m_bullet = m_world.CreateBody(&bd); 70 | m_bullet.CreateFixture(box, 100.0f); 71 | 72 | m_bullet.SetLinearVelocity(b2Vec2(0.0f, -50.0f)); 73 | } 74 | } 75 | 76 | void Launch() 77 | { 78 | m_body.SetTransform(b2Vec2(0.0f, 4.0f), 0.0f); 79 | m_body.SetLinearVelocity(b2Vec2_zero); 80 | m_body.SetAngularVelocity(0.0f); 81 | 82 | m_x = RandomFloat(-1.0f, 1.0f); 83 | m_bullet.SetTransform(b2Vec2(m_x, 10.0f), 0.0f); 84 | m_bullet.SetLinearVelocity(b2Vec2(0.0f, -50.0f)); 85 | m_bullet.SetAngularVelocity(0.0f); 86 | 87 | b2_gjkCalls = 0; 88 | b2_gjkIters = 0; 89 | b2_gjkMaxIters = 0; 90 | 91 | b2_toiCalls = 0; 92 | b2_toiIters = 0; 93 | b2_toiMaxIters = 0; 94 | b2_toiRootIters = 0; 95 | b2_toiMaxRootIters = 0; 96 | } 97 | 98 | override void Step(Settings* settings) 99 | { 100 | super.Step(settings); 101 | 102 | if (b2_gjkCalls > 0) 103 | { 104 | g_debugDraw.DrawString(5, m_textLine, format("gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d", 105 | b2_gjkCalls, b2_gjkIters / cast(float32)b2_gjkCalls, b2_gjkMaxIters)); 106 | m_textLine += DRAW_STRING_NEW_LINE; 107 | } 108 | 109 | if (b2_toiCalls > 0) 110 | { 111 | g_debugDraw.DrawString(5, m_textLine, format("toi calls = %d, ave toi iters = %3.1f, max toi iters = %d", 112 | b2_toiCalls, b2_toiIters / cast(float32)b2_toiCalls, b2_toiMaxRootIters)); 113 | m_textLine += DRAW_STRING_NEW_LINE; 114 | 115 | g_debugDraw.DrawString(5, m_textLine, format("ave toi root iters = %3.1f, max toi root iters = %d", 116 | b2_toiRootIters / cast(float32)b2_toiCalls, b2_toiMaxRootIters)); 117 | m_textLine += DRAW_STRING_NEW_LINE; 118 | } 119 | 120 | if (m_stepCount % 60 == 0) 121 | { 122 | Launch(); 123 | } 124 | } 125 | 126 | static Test Create() 127 | { 128 | return new typeof(this); 129 | } 130 | 131 | b2Body* m_body; 132 | b2Body* m_bullet; 133 | float32 m_x = 0; 134 | } 135 | -------------------------------------------------------------------------------- /examples/demo/tests/chain.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.chain; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Chain : Test 33 | { 34 | this() 35 | { 36 | b2Body* ground = null; 37 | { 38 | b2BodyDef bd; 39 | ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 43 | ground.CreateFixture(shape, 0.0f); 44 | } 45 | 46 | { 47 | auto shape = new b2PolygonShape(); 48 | shape.SetAsBox(0.6f, 0.125f); 49 | 50 | b2FixtureDef fd; 51 | fd.shape = shape; 52 | fd.density = 20.0f; 53 | fd.friction = 0.2f; 54 | 55 | b2RevoluteJointDef jd = new b2RevoluteJointDef; 56 | jd.collideConnected = false; 57 | 58 | const float32 y = 25.0f; 59 | b2Body* prevBody = ground; 60 | 61 | for (int32 i = 0; i < 30; ++i) 62 | { 63 | b2BodyDef bd; 64 | bd.type = b2_dynamicBody; 65 | bd.position.Set(0.5f + i, y); 66 | b2Body* body_ = m_world.CreateBody(&bd); 67 | body_.CreateFixture(&fd); 68 | 69 | b2Vec2 anchor = b2Vec2(cast(float32)i, y); 70 | jd.Initialize(prevBody, body_, anchor); 71 | m_world.CreateJoint(jd); 72 | 73 | prevBody = body_; 74 | } 75 | } 76 | } 77 | 78 | static Test Create() 79 | { 80 | return new typeof(this); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /examples/demo/tests/compoundshapes.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.compoundshapes; 19 | 20 | import core.stdc.math; 21 | 22 | import std.algorithm; 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class CompoundShapes : Test 34 | { 35 | this() 36 | { 37 | { 38 | b2BodyDef bd; 39 | bd.position.Set(0.0f, 0.0f); 40 | b2Body* body_ = m_world.CreateBody(&bd); 41 | 42 | auto shape = new b2EdgeShape(); 43 | shape.Set(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f)); 44 | 45 | body_.CreateFixture(shape, 0.0f); 46 | } 47 | 48 | { 49 | b2CircleShape circle1 = new b2CircleShape(); 50 | circle1.m_radius = 0.5f; 51 | circle1.m_p.Set(-0.5f, 0.5f); 52 | 53 | b2CircleShape circle2 = new b2CircleShape(); 54 | circle2.m_radius = 0.5f; 55 | circle2.m_p.Set(0.5f, 0.5f); 56 | 57 | for (int i = 0; i < 10; ++i) 58 | { 59 | float32 x = RandomFloat(-0.1f, 0.1f); 60 | b2BodyDef bd; 61 | bd.type = b2_dynamicBody; 62 | bd.position.Set(x + 5.0f, 1.05f + 2.5f * i); 63 | bd.angle = RandomFloat(-b2_pi, b2_pi); 64 | b2Body* body_ = m_world.CreateBody(&bd); 65 | body_.CreateFixture(circle1, 2.0f); 66 | body_.CreateFixture(circle2, 0.0f); 67 | } 68 | } 69 | 70 | { 71 | b2PolygonShape polygon1 = new b2PolygonShape(); 72 | polygon1.SetAsBox(0.25f, 0.5f); 73 | 74 | b2PolygonShape polygon2 = new b2PolygonShape(); 75 | polygon2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi); 76 | 77 | for (int i = 0; i < 10; ++i) 78 | { 79 | float32 x = RandomFloat(-0.1f, 0.1f); 80 | b2BodyDef bd; 81 | bd.type = b2_dynamicBody; 82 | bd.position.Set(x - 5.0f, 1.05f + 2.5f * i); 83 | bd.angle = RandomFloat(-b2_pi, b2_pi); 84 | b2Body* body_ = m_world.CreateBody(&bd); 85 | body_.CreateFixture(polygon1, 2.0f); 86 | body_.CreateFixture(polygon2, 2.0f); 87 | } 88 | } 89 | 90 | { 91 | b2Transform xf1; 92 | xf1.q.Set(0.3524f * b2_pi); 93 | xf1.p = xf1.q.GetXAxis(); 94 | 95 | b2Vec2 vertices[3]; 96 | 97 | b2PolygonShape triangle1 = new b2PolygonShape(); 98 | vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f)); 99 | vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f)); 100 | vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f)); 101 | triangle1.Set(vertices); 102 | 103 | b2Transform xf2; 104 | xf2.q.Set(-0.3524f * b2_pi); 105 | xf2.p = -xf2.q.GetXAxis(); 106 | 107 | b2PolygonShape triangle2 = new b2PolygonShape(); 108 | vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f)); 109 | vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f)); 110 | vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f)); 111 | triangle2.Set(vertices); 112 | 113 | for (int32 i = 0; i < 10; ++i) 114 | { 115 | float32 x = RandomFloat(-0.1f, 0.1f); 116 | b2BodyDef bd; 117 | bd.type = b2_dynamicBody; 118 | bd.position.Set(x, 2.05f + 2.5f * i); 119 | bd.angle = 0.0f; 120 | b2Body* body_ = m_world.CreateBody(&bd); 121 | body_.CreateFixture(triangle1, 2.0f); 122 | body_.CreateFixture(triangle2, 2.0f); 123 | } 124 | } 125 | 126 | { 127 | b2PolygonShape bottom = new b2PolygonShape(); 128 | bottom.SetAsBox(1.5f, 0.15f); 129 | 130 | b2PolygonShape left = new b2PolygonShape(); 131 | left.SetAsBox(0.15f, 2.7f, b2Vec2(-1.45f, 2.35f), 0.2f); 132 | 133 | b2PolygonShape right = new b2PolygonShape(); 134 | right.SetAsBox(0.15f, 2.7f, b2Vec2(1.45f, 2.35f), -0.2f); 135 | 136 | b2BodyDef bd; 137 | bd.type = b2_dynamicBody; 138 | bd.position.Set(0.0f, 2.0f); 139 | b2Body* body_ = m_world.CreateBody(&bd); 140 | body_.CreateFixture(bottom, 4.0f); 141 | body_.CreateFixture(left, 4.0f); 142 | body_.CreateFixture(right, 4.0f); 143 | } 144 | } 145 | 146 | static Test Create() 147 | { 148 | return new typeof(this); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /examples/demo/tests/confined.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.confined; 19 | 20 | import core.stdc.math; 21 | 22 | import std.algorithm; 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class Confined : Test 34 | { 35 | enum 36 | { 37 | e_columnCount = 0, 38 | e_rowCount = 0 39 | } 40 | 41 | 42 | this() 43 | { 44 | { 45 | b2BodyDef bd; 46 | b2Body* ground = m_world.CreateBody(&bd); 47 | 48 | auto shape = new b2EdgeShape(); 49 | 50 | // Floor 51 | shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f)); 52 | ground.CreateFixture(shape, 0.0f); 53 | 54 | // Left wall 55 | shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(-10.0f, 20.0f)); 56 | ground.CreateFixture(shape, 0.0f); 57 | 58 | // Right wall 59 | shape.Set(b2Vec2(10.0f, 0.0f), b2Vec2(10.0f, 20.0f)); 60 | ground.CreateFixture(shape, 0.0f); 61 | 62 | // Roof 63 | shape.Set(b2Vec2(-10.0f, 20.0f), b2Vec2(10.0f, 20.0f)); 64 | ground.CreateFixture(shape, 0.0f); 65 | } 66 | 67 | float32 radius = 0.5f; 68 | b2CircleShape shape = new b2CircleShape(); 69 | shape.m_p.SetZero(); 70 | shape.m_radius = radius; 71 | 72 | b2FixtureDef fd; 73 | fd.shape = shape; 74 | fd.density = 1.0f; 75 | fd.friction = 0.1f; 76 | 77 | for (int32 j = 0; j < e_columnCount; ++j) 78 | { 79 | for (int i = 0; i < e_rowCount; ++i) 80 | { 81 | b2BodyDef bd; 82 | bd.type = b2_dynamicBody; 83 | bd.position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius); 84 | b2Body* body_ = m_world.CreateBody(&bd); 85 | 86 | body_.CreateFixture(&fd); 87 | } 88 | } 89 | 90 | m_world.SetGravity(b2Vec2(0.0f, 0.0f)); 91 | } 92 | 93 | void CreateCircle() 94 | { 95 | float32 radius = 2.0f; 96 | b2CircleShape shape = new b2CircleShape(); 97 | shape.m_p.SetZero(); 98 | shape.m_radius = radius; 99 | 100 | b2FixtureDef fd; 101 | fd.shape = shape; 102 | fd.density = 1.0f; 103 | fd.friction = 0.0f; 104 | 105 | b2Vec2 p = b2Vec2(RandomFloat(), 3.0f + RandomFloat()); 106 | b2BodyDef bd; 107 | bd.type = b2_dynamicBody; 108 | bd.position = p; 109 | 110 | // bd.allowSleep = false; 111 | b2Body* body_ = m_world.CreateBody(&bd); 112 | 113 | body_.CreateFixture(&fd); 114 | } 115 | 116 | override void Keyboard(int key) 117 | { 118 | switch (key) 119 | { 120 | case GLFW_KEY_C: 121 | CreateCircle(); 122 | break; 123 | 124 | default: 125 | break; 126 | } 127 | } 128 | 129 | override void Step(Settings* settings) 130 | { 131 | super.Step(settings); 132 | 133 | bool sleeping = true; 134 | 135 | for (b2Body* b = m_world.GetBodyList(); b; b = b.GetNext()) 136 | { 137 | if (b.GetType() != b2_dynamicBody) 138 | { 139 | continue; 140 | } 141 | 142 | if (b.IsAwake()) 143 | { 144 | sleeping = false; 145 | } 146 | } 147 | 148 | if (m_stepCount == 180) 149 | { 150 | m_stepCount += 0; 151 | } 152 | 153 | // if (sleeping) 154 | // { 155 | // CreateCircle(); 156 | // } 157 | 158 | for (b2Body* b = m_world.GetBodyList(); b; b = b.GetNext()) 159 | { 160 | if (b.GetType() != b2_dynamicBody) 161 | { 162 | continue; 163 | } 164 | 165 | b2Vec2 p = b.GetPosition(); 166 | 167 | if (p.x <= -10.0f || 10.0f <= p.x || p.y <= 0.0f || 20.0f <= p.y) 168 | { 169 | p.x += 0.0f; 170 | } 171 | } 172 | 173 | g_debugDraw.DrawString(5, m_textLine, "Press 'c' to create a circle."); 174 | m_textLine += DRAW_STRING_NEW_LINE; 175 | } 176 | 177 | static Test Create() 178 | { 179 | return new typeof(this); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /examples/demo/tests/convexhull.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.convexhull; 19 | 20 | import core.stdc.math; 21 | 22 | import std.algorithm; 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class ConvexHull : Test 34 | { 35 | enum 36 | { 37 | e_count = b2_maxPolygonVertices 38 | } 39 | 40 | 41 | this() 42 | { 43 | Generate(); 44 | m_auto = false; 45 | } 46 | 47 | void Generate() 48 | { 49 | b2Vec2 lowerBound = b2Vec2(-8.0f, -8.0f); 50 | b2Vec2 upperBound = b2Vec2(8.0f, 8.0f); 51 | 52 | for (int32 i = 0; i < e_count; ++i) 53 | { 54 | float32 x = 10.0f * RandomFloat(); 55 | float32 y = 10.0f * RandomFloat(); 56 | 57 | // Clamp onto a square to help create collinearities. 58 | // This will stress the convex hull algorithm. 59 | b2Vec2 v = b2Vec2(x, y); 60 | v = b2Clamp(v, lowerBound, upperBound); 61 | m_points[i] = v; 62 | } 63 | 64 | m_count = e_count; 65 | } 66 | 67 | override void Keyboard(int key) 68 | { 69 | switch (key) 70 | { 71 | case GLFW_KEY_A: 72 | m_auto = !m_auto; 73 | break; 74 | 75 | case GLFW_KEY_G: 76 | Generate(); 77 | break; 78 | 79 | default: 80 | break; 81 | } 82 | } 83 | 84 | override void Step(Settings* settings) 85 | { 86 | super.Step(settings); 87 | 88 | auto shape = new b2PolygonShape(); 89 | shape.Set(m_points[0 .. m_count]); 90 | 91 | g_debugDraw.DrawString(5, m_textLine, "Press g to generate a new random convex hull"); 92 | m_textLine += DRAW_STRING_NEW_LINE; 93 | 94 | g_debugDraw.DrawPolygon(shape.m_vertices.ptr, shape.m_count, b2Color(0.9f, 0.9f, 0.9f)); 95 | 96 | for (int32 i = 0; i < m_count; ++i) 97 | { 98 | g_debugDraw.DrawPoint(m_points[i], 3.0f, b2Color(0.3f, 0.9f, 0.3f)); 99 | g_debugDraw.DrawString(m_points[i] + b2Vec2(0.05f, 0.05f), format("%d", i)); 100 | } 101 | 102 | if (shape.Validate() == false) 103 | { 104 | m_textLine += 0; 105 | } 106 | 107 | if (m_auto) 108 | { 109 | Generate(); 110 | } 111 | } 112 | 113 | static Test Create() 114 | { 115 | return new typeof(this); 116 | } 117 | 118 | b2Vec2 m_points[b2_maxPolygonVertices]; 119 | int32 m_count; 120 | bool m_auto; 121 | } 122 | -------------------------------------------------------------------------------- /examples/demo/tests/conveyorbelt.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.conveyorbelt; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class ConveyorBelt : Test 33 | { 34 | b2EdgeShape shape1; 35 | b2PolygonShape shape2; 36 | b2PolygonShape shape3; 37 | 38 | this() 39 | { 40 | // Ground 41 | { 42 | b2BodyDef bd; 43 | b2Body* ground = m_world.CreateBody(&bd); 44 | 45 | shape1 = new b2EdgeShape(); 46 | alias shape = shape1; 47 | shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f)); 48 | ground.CreateFixture(shape, 0.0f); 49 | } 50 | 51 | // Platform 52 | { 53 | b2BodyDef bd; 54 | bd.position.Set(-5.0f, 5.0f); 55 | b2Body* body_ = m_world.CreateBody(&bd); 56 | 57 | shape2 = new b2PolygonShape(); 58 | alias shape = shape2; 59 | shape.SetAsBox(10.0f, 0.5f); 60 | 61 | b2FixtureDef fd; 62 | fd.shape = shape; 63 | fd.friction = 0.8f; 64 | m_platform = body_.CreateFixture(&fd); 65 | } 66 | 67 | // Boxes 68 | for (int32 i = 0; i < 5; ++i) 69 | { 70 | b2BodyDef bd; 71 | bd.type = b2_dynamicBody; 72 | bd.position.Set(-10.0f + 2.0f * i, 7.0f); 73 | b2Body* body_ = m_world.CreateBody(&bd); 74 | 75 | shape3 = new b2PolygonShape(); 76 | alias shape = shape3; 77 | shape.SetAsBox(0.5f, 0.5f); 78 | body_.CreateFixture(shape, 20.0f); 79 | } 80 | } 81 | 82 | override void PreSolve(b2Contact contact, const(b2Manifold)* oldManifold) 83 | { 84 | super.PreSolve(contact, oldManifold); 85 | 86 | b2Fixture* fixtureA = contact.GetFixtureA(); 87 | b2Fixture* fixtureB = contact.GetFixtureB(); 88 | 89 | if (fixtureA == m_platform) 90 | { 91 | contact.SetTangentSpeed(5.0f); 92 | } 93 | 94 | if (fixtureB == m_platform) 95 | { 96 | contact.SetTangentSpeed(-5.0f); 97 | } 98 | } 99 | 100 | override void Step(Settings* settings) 101 | { 102 | super.Step(settings); 103 | } 104 | 105 | b2Fixture* m_platform; 106 | 107 | static Test Create() 108 | { 109 | return new typeof(this); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /examples/demo/tests/distancetest.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.distancetest; 19 | 20 | import core.stdc.math; 21 | 22 | import std.algorithm; 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class DistanceTest : Test 34 | { 35 | this() 36 | { 37 | m_polygonA = new b2PolygonShape(); 38 | m_polygonB = new b2PolygonShape(); 39 | 40 | { 41 | m_transformA.SetIdentity(); 42 | m_transformA.p.Set(0.0f, -0.2f); 43 | m_polygonA.SetAsBox(10.0f, 0.2f); 44 | } 45 | 46 | { 47 | m_positionB.Set(12.017401f, 0.13678508f); 48 | m_angleB = -0.0109265f; 49 | m_transformB.Set(m_positionB, m_angleB); 50 | 51 | m_polygonB.SetAsBox(2.0f, 0.1f); 52 | } 53 | } 54 | 55 | override void Step(Settings* settings) 56 | { 57 | super.Step(settings); 58 | 59 | b2DistanceInput input; 60 | input.proxyA.Set(m_polygonA, 0); 61 | input.proxyB.Set(m_polygonB, 0); 62 | input.transformA = m_transformA; 63 | input.transformB = m_transformB; 64 | input.useRadii = true; 65 | b2SimplexCache cache; 66 | cache.count = 0; 67 | b2DistanceOutput output; 68 | b2Distance(&output, &cache, &input); 69 | 70 | g_debugDraw.DrawString(5, m_textLine, format("distance = %g", output.distance)); 71 | m_textLine += DRAW_STRING_NEW_LINE; 72 | 73 | g_debugDraw.DrawString(5, m_textLine, format("iterations = %d", output.iterations)); 74 | m_textLine += DRAW_STRING_NEW_LINE; 75 | 76 | { 77 | b2Color color = b2Color(0.9f, 0.9f, 0.9f); 78 | b2Vec2 v[b2_maxPolygonVertices]; 79 | 80 | for (int32 i = 0; i < m_polygonA.m_count; ++i) 81 | { 82 | v[i] = b2Mul(m_transformA, m_polygonA.m_vertices[i]); 83 | } 84 | 85 | g_debugDraw.DrawPolygon(v.ptr, m_polygonA.m_count, color); 86 | 87 | for (int32 i = 0; i < m_polygonB.m_count; ++i) 88 | { 89 | v[i] = b2Mul(m_transformB, m_polygonB.m_vertices[i]); 90 | } 91 | 92 | g_debugDraw.DrawPolygon(v.ptr, m_polygonB.m_count, color); 93 | } 94 | 95 | b2Vec2 x1 = output.pointA; 96 | b2Vec2 x2 = output.pointB; 97 | 98 | b2Color c1 = b2Color(1.0f, 0.0f, 0.0f); 99 | g_debugDraw.DrawPoint(x1, 4.0f, c1); 100 | 101 | b2Color c2 = b2Color(1.0f, 1.0f, 0.0f); 102 | g_debugDraw.DrawPoint(x2, 4.0f, c2); 103 | } 104 | 105 | override void Keyboard(int key) 106 | { 107 | switch (key) 108 | { 109 | case GLFW_KEY_A: 110 | m_positionB.x -= 0.1f; 111 | break; 112 | 113 | case GLFW_KEY_D: 114 | m_positionB.x += 0.1f; 115 | break; 116 | 117 | case GLFW_KEY_S: 118 | m_positionB.y -= 0.1f; 119 | break; 120 | 121 | case GLFW_KEY_W: 122 | m_positionB.y += 0.1f; 123 | break; 124 | 125 | case GLFW_KEY_Q: 126 | m_angleB += 0.1f * b2_pi; 127 | break; 128 | 129 | case GLFW_KEY_E: 130 | m_angleB -= 0.1f * b2_pi; 131 | break; 132 | 133 | default: 134 | break; 135 | } 136 | 137 | m_transformB.Set(m_positionB, m_angleB); 138 | } 139 | 140 | static Test Create() 141 | { 142 | return new typeof(this); 143 | } 144 | 145 | b2Vec2 m_positionB; 146 | float32 m_angleB; 147 | 148 | b2Transform m_transformA; 149 | b2Transform m_transformB; 150 | b2PolygonShape m_polygonA; 151 | b2PolygonShape m_polygonB; 152 | } 153 | -------------------------------------------------------------------------------- /examples/demo/tests/edgetest.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.edgetest; 19 | 20 | import core.stdc.math; 21 | import core.stdc.stdlib; 22 | 23 | import std.algorithm; 24 | import std.string; 25 | import std.typecons; 26 | 27 | import deimos.glfw.glfw3; 28 | 29 | import dbox; 30 | 31 | import framework.debug_draw; 32 | import framework.test; 33 | 34 | class EdgeTest : Test 35 | { 36 | this() 37 | { 38 | { 39 | b2BodyDef bd; 40 | b2Body* ground = m_world.CreateBody(&bd); 41 | 42 | b2Vec2 v1 = b2Vec2(-10.0f, 0.0f), 43 | v2 = b2Vec2(-7.0f, -2.0f), 44 | v3 = b2Vec2(-4.0f, 0.0f), 45 | v4 = b2Vec2(0.0f, 0.0f), 46 | v5 = b2Vec2(4.0f, 0.0f), 47 | v6 = b2Vec2(7.0f, 2.0f), 48 | v7 = b2Vec2(10.0f, 0.0f); 49 | 50 | auto shape = new b2EdgeShape(); 51 | 52 | shape.Set(v1, v2); 53 | shape.m_hasVertex3 = true; 54 | shape.m_vertex3 = v3; 55 | ground.CreateFixture(shape, 0.0f); 56 | 57 | shape.Set(v2, v3); 58 | shape.m_hasVertex0 = true; 59 | shape.m_hasVertex3 = true; 60 | shape.m_vertex0 = v1; 61 | shape.m_vertex3 = v4; 62 | ground.CreateFixture(shape, 0.0f); 63 | 64 | shape.Set(v3, v4); 65 | shape.m_hasVertex0 = true; 66 | shape.m_hasVertex3 = true; 67 | shape.m_vertex0 = v2; 68 | shape.m_vertex3 = v5; 69 | ground.CreateFixture(shape, 0.0f); 70 | 71 | shape.Set(v4, v5); 72 | shape.m_hasVertex0 = true; 73 | shape.m_hasVertex3 = true; 74 | shape.m_vertex0 = v3; 75 | shape.m_vertex3 = v6; 76 | ground.CreateFixture(shape, 0.0f); 77 | 78 | shape.Set(v5, v6); 79 | shape.m_hasVertex0 = true; 80 | shape.m_hasVertex3 = true; 81 | shape.m_vertex0 = v4; 82 | shape.m_vertex3 = v7; 83 | ground.CreateFixture(shape, 0.0f); 84 | 85 | shape.Set(v6, v7); 86 | shape.m_hasVertex0 = true; 87 | shape.m_vertex0 = v5; 88 | ground.CreateFixture(shape, 0.0f); 89 | } 90 | 91 | { 92 | b2BodyDef bd; 93 | bd.type = b2_dynamicBody; 94 | bd.position.Set(-0.5f, 0.6f); 95 | bd.allowSleep = false; 96 | b2Body* body_ = m_world.CreateBody(&bd); 97 | 98 | b2CircleShape shape = new b2CircleShape(); 99 | shape.m_radius = 0.5f; 100 | 101 | body_.CreateFixture(shape, 1.0f); 102 | } 103 | 104 | { 105 | b2BodyDef bd; 106 | bd.type = b2_dynamicBody; 107 | bd.position.Set(1.0f, 0.6f); 108 | bd.allowSleep = false; 109 | b2Body* body_ = m_world.CreateBody(&bd); 110 | 111 | auto shape = new b2PolygonShape(); 112 | shape.SetAsBox(0.5f, 0.5f); 113 | 114 | body_.CreateFixture(shape, 1.0f); 115 | } 116 | } 117 | 118 | static Test Create() 119 | { 120 | return new typeof(this); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /examples/demo/tests/heavyonlight.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.heavyonlight; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import dbox; 26 | 27 | import framework.debug_draw; 28 | import framework.test; 29 | 30 | /// This stress tests the dynamic tree broad-phase. This also shows that tile 31 | /// based collision is _not_ smooth due to Box2D not knowing about adjacency. 32 | class HeavyOnLight : Test 33 | { 34 | this() 35 | { 36 | { 37 | b2BodyDef bd; 38 | b2Body* ground = m_world.CreateBody(&bd); 39 | 40 | auto shape = new b2EdgeShape(); 41 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 42 | ground.CreateFixture(shape, 0.0f); 43 | } 44 | 45 | b2BodyDef bd; 46 | bd.type = b2_dynamicBody; 47 | bd.position.Set(0.0f, 0.5f); 48 | b2Body* body_ = m_world.CreateBody(&bd); 49 | 50 | auto shape = new b2CircleShape(); 51 | shape.m_radius = 0.5f; 52 | body_.CreateFixture(shape, 10.0f); 53 | 54 | bd.position.Set(0.0f, 6.0f); 55 | body_ = m_world.CreateBody(&bd); 56 | shape.m_radius = 5.0f; 57 | body_.CreateFixture(shape, 10.0f); 58 | } 59 | 60 | static Test Create() 61 | { 62 | return new typeof(this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /examples/demo/tests/heavyonlighttwo.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.heavyonlighttwo; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class HeavyOnLightTwo : Test 33 | { 34 | this() 35 | { 36 | { 37 | b2BodyDef bd; 38 | b2Body* ground = m_world.CreateBody(&bd); 39 | 40 | auto shape = new b2EdgeShape(); 41 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 42 | ground.CreateFixture(shape, 0.0f); 43 | } 44 | 45 | b2BodyDef bd; 46 | bd.type = b2_dynamicBody; 47 | bd.position.Set(0.0f, 2.5f); 48 | b2Body* body_ = m_world.CreateBody(&bd); 49 | 50 | b2CircleShape shape = new b2CircleShape(); 51 | shape.m_radius = 0.5f; 52 | body_.CreateFixture(shape, 10.0f); 53 | 54 | bd.position.Set(0.0f, 3.5f); 55 | body_ = m_world.CreateBody(&bd); 56 | body_.CreateFixture(shape, 10.0f); 57 | 58 | m_heavy = null; 59 | } 60 | 61 | void ToggleHeavy() 62 | { 63 | if (m_heavy) 64 | { 65 | m_world.DestroyBody(m_heavy); 66 | m_heavy = null; 67 | } 68 | else 69 | { 70 | b2BodyDef bd; 71 | bd.type = b2_dynamicBody; 72 | bd.position.Set(0.0f, 9.0f); 73 | m_heavy = m_world.CreateBody(&bd); 74 | 75 | b2CircleShape shape = new b2CircleShape(); 76 | shape.m_radius = 5.0f; 77 | m_heavy.CreateFixture(shape, 10.0f); 78 | } 79 | } 80 | 81 | override void Keyboard(int key) 82 | { 83 | switch (key) 84 | { 85 | case GLFW_KEY_H: 86 | ToggleHeavy(); 87 | break; 88 | 89 | default: 90 | break; 91 | } 92 | } 93 | 94 | static Test Create() 95 | { 96 | return new typeof(this); 97 | } 98 | 99 | b2Body* m_heavy; 100 | } 101 | -------------------------------------------------------------------------------- /examples/demo/tests/mobile.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.mobile; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Mobile : Test 33 | { 34 | enum 35 | { 36 | e_depth = 4 37 | } 38 | 39 | 40 | this() 41 | { 42 | b2Body* ground; 43 | 44 | // Create ground body_. 45 | { 46 | b2BodyDef bodyDef; 47 | bodyDef.position.Set(0.0f, 20.0f); 48 | ground = m_world.CreateBody(&bodyDef); 49 | } 50 | 51 | float32 a = 0.5f; 52 | b2Vec2 h = b2Vec2(0.0f, a); 53 | 54 | b2Body* root = AddNode(ground, b2Vec2_zero, 0, 3.0f, a); 55 | 56 | b2RevoluteJointDef jointDef = new b2RevoluteJointDef; 57 | jointDef.bodyA = ground; 58 | jointDef.bodyB = root; 59 | jointDef.localAnchorA.SetZero(); 60 | jointDef.localAnchorB = h; 61 | m_world.CreateJoint(jointDef); 62 | } 63 | 64 | b2Body* AddNode(b2Body* parent, b2Vec2 localAnchor, int32 depth, float32 offset, float32 a) 65 | { 66 | float32 density = 20.0f; 67 | b2Vec2 h = b2Vec2(0.0f, a); 68 | 69 | b2Vec2 p = parent.GetPosition() + localAnchor - h; 70 | 71 | b2BodyDef bodyDef; 72 | bodyDef.type = b2_dynamicBody; 73 | bodyDef.position = p; 74 | b2Body* body_ = m_world.CreateBody(&bodyDef); 75 | 76 | auto shape = new b2PolygonShape(); 77 | shape.SetAsBox(0.25f * a, a); 78 | body_.CreateFixture(shape, density); 79 | 80 | if (depth == e_depth) 81 | { 82 | return body_; 83 | } 84 | 85 | b2Vec2 a1 = b2Vec2(offset, -a); 86 | b2Vec2 a2 = b2Vec2(-offset, -a); 87 | b2Body* body1 = AddNode(body_, a1, depth + 1, 0.5f * offset, a); 88 | b2Body* body2 = AddNode(body_, a2, depth + 1, 0.5f * offset, a); 89 | 90 | b2RevoluteJointDef jointDef = new b2RevoluteJointDef(); 91 | jointDef.bodyA = body_; 92 | jointDef.localAnchorB = h; 93 | 94 | jointDef.localAnchorA = a1; 95 | jointDef.bodyB = body1; 96 | m_world.CreateJoint(jointDef); 97 | 98 | jointDef.localAnchorA = a2; 99 | jointDef.bodyB = body2; 100 | m_world.CreateJoint(jointDef); 101 | 102 | return body_; 103 | } 104 | 105 | static Test Create() 106 | { 107 | return new typeof(this); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /examples/demo/tests/mobilebalanced.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.mobilebalanced; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class MobileBalanced : Test 33 | { 34 | enum 35 | { 36 | e_depth = 4 37 | } 38 | 39 | this() 40 | { 41 | b2Body* ground; 42 | 43 | // Create ground body_. 44 | { 45 | b2BodyDef bodyDef; 46 | bodyDef.position.Set(0.0f, 20.0f); 47 | ground = m_world.CreateBody(&bodyDef); 48 | } 49 | 50 | float32 a = 0.5f; 51 | b2Vec2 h = b2Vec2(0.0f, a); 52 | 53 | b2Body* root = AddNode(ground, b2Vec2_zero, 0, 3.0f, a); 54 | 55 | b2RevoluteJointDef jointDef = new b2RevoluteJointDef; 56 | jointDef.bodyA = ground; 57 | jointDef.bodyB = root; 58 | jointDef.localAnchorA.SetZero(); 59 | jointDef.localAnchorB = h; 60 | m_world.CreateJoint(jointDef); 61 | } 62 | 63 | 64 | b2Body* AddNode(b2Body* parent, b2Vec2 localAnchor, int32 depth, float32 offset, float32 a) 65 | { 66 | float32 density = 20.0f; 67 | b2Vec2 h = b2Vec2(0.0f, a); 68 | 69 | b2Vec2 p = parent.GetPosition() + localAnchor - h; 70 | 71 | b2BodyDef bodyDef; 72 | bodyDef.type = b2_dynamicBody; 73 | bodyDef.position = p; 74 | b2Body* body_ = m_world.CreateBody(&bodyDef); 75 | 76 | auto shape = new b2PolygonShape(); 77 | shape.SetAsBox(0.25f * a, a); 78 | body_.CreateFixture(shape, density); 79 | 80 | if (depth == e_depth) 81 | { 82 | return body_; 83 | } 84 | 85 | shape.SetAsBox(offset, 0.25f * a, b2Vec2(0, -a), 0.0f); 86 | body_.CreateFixture(shape, density); 87 | 88 | b2Vec2 a1 = b2Vec2(offset, -a); 89 | b2Vec2 a2 = b2Vec2(-offset, -a); 90 | b2Body* body1 = AddNode(body_, a1, depth + 1, 0.5f * offset, a); 91 | b2Body* body2 = AddNode(body_, a2, depth + 1, 0.5f * offset, a); 92 | 93 | b2RevoluteJointDef jointDef = new b2RevoluteJointDef(); 94 | jointDef.bodyA = body_; 95 | jointDef.localAnchorB = h; 96 | 97 | jointDef.localAnchorA = a1; 98 | jointDef.bodyB = body1; 99 | m_world.CreateJoint(jointDef); 100 | 101 | jointDef.localAnchorA = a2; 102 | jointDef.bodyB = body2; 103 | m_world.CreateJoint(jointDef); 104 | 105 | return body_; 106 | } 107 | 108 | 109 | static Test Create() 110 | { 111 | return new typeof(this); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /examples/demo/tests/motorjoint.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.motorjoint; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class MotorJoint : Test 33 | { 34 | this() 35 | { 36 | b2Body* ground = null; 37 | { 38 | b2BodyDef bd; 39 | ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f)); 43 | 44 | b2FixtureDef fd; 45 | fd.shape = shape; 46 | 47 | ground.CreateFixture(&fd); 48 | } 49 | 50 | // Define motorized body_ 51 | { 52 | b2BodyDef bd; 53 | bd.type = b2_dynamicBody; 54 | bd.position.Set(0.0f, 8.0f); 55 | b2Body* body_ = m_world.CreateBody(&bd); 56 | 57 | auto shape = new b2PolygonShape(); 58 | shape.SetAsBox(2.0f, 0.5f); 59 | 60 | b2FixtureDef fd; 61 | fd.shape = shape; 62 | fd.friction = 0.6f; 63 | fd.density = 2.0f; 64 | body_.CreateFixture(&fd); 65 | 66 | b2MotorJointDef mjd = new b2MotorJointDef(); 67 | mjd.Initialize(ground, body_); 68 | mjd.maxForce = 1000.0f; 69 | mjd.maxTorque = 1000.0f; 70 | m_joint = cast(b2MotorJoint)m_world.CreateJoint(mjd); 71 | } 72 | 73 | m_go = false; 74 | m_time = 0.0f; 75 | } 76 | 77 | override void Keyboard(int key) 78 | { 79 | switch (key) 80 | { 81 | case GLFW_KEY_S: 82 | m_go = !m_go; 83 | break; 84 | 85 | default: 86 | break; 87 | } 88 | } 89 | 90 | override void Step(Settings* settings) 91 | { 92 | super.Step(settings); 93 | 94 | if (m_go && settings.hz > 0.0f) 95 | { 96 | m_time += 1.0f / settings.hz; 97 | } 98 | 99 | b2Vec2 linearOffset; 100 | linearOffset.x = 6.0f * sinf(2.0f * m_time); 101 | linearOffset.y = 8.0f + 4.0f * sinf(1.0f * m_time); 102 | 103 | float32 angularOffset = 4.0f * m_time; 104 | 105 | m_joint.SetLinearOffset(linearOffset); 106 | m_joint.SetAngularOffset(angularOffset); 107 | 108 | g_debugDraw.DrawPoint(linearOffset, 4.0f, b2Color(0.9f, 0.9f, 0.9f)); 109 | 110 | g_debugDraw.DrawString(5, m_textLine, "Keys: (s) pause"); 111 | m_textLine += 15; 112 | } 113 | 114 | static Test Create() 115 | { 116 | return new typeof(this); 117 | } 118 | 119 | b2MotorJoint m_joint; 120 | float32 m_time; 121 | bool m_go; 122 | } 123 | -------------------------------------------------------------------------------- /examples/demo/tests/onesidedplatform.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.onesidedplatform; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class OneSidedPlatform : Test 33 | { 34 | alias State = int; 35 | enum : State 36 | { 37 | e_unknown, 38 | e_above, 39 | e_below 40 | } 41 | 42 | this() 43 | { 44 | // Ground 45 | { 46 | b2BodyDef bd; 47 | b2Body* ground = m_world.CreateBody(&bd); 48 | 49 | auto shape = new b2EdgeShape(); 50 | shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f)); 51 | ground.CreateFixture(shape, 0.0f); 52 | } 53 | 54 | // Platform 55 | { 56 | b2BodyDef bd; 57 | bd.position.Set(0.0f, 10.0f); 58 | b2Body* body_ = m_world.CreateBody(&bd); 59 | 60 | auto shape = new b2PolygonShape(); 61 | shape.SetAsBox(3.0f, 0.5f); 62 | m_platform = body_.CreateFixture(shape, 0.0f); 63 | 64 | m_bottom = 10.0f - 0.5f; 65 | m_top = 10.0f + 0.5f; 66 | } 67 | 68 | // Actor 69 | { 70 | b2BodyDef bd; 71 | bd.type = b2_dynamicBody; 72 | bd.position.Set(0.0f, 12.0f); 73 | b2Body* body_ = m_world.CreateBody(&bd); 74 | 75 | m_radius = 0.5f; 76 | b2CircleShape shape = new b2CircleShape(); 77 | shape.m_radius = m_radius; 78 | m_character = body_.CreateFixture(shape, 20.0f); 79 | 80 | body_.SetLinearVelocity(b2Vec2(0.0f, -50.0f)); 81 | 82 | m_state = e_unknown; 83 | } 84 | } 85 | 86 | override void PreSolve(b2Contact contact, const(b2Manifold)* oldManifold) 87 | { 88 | super.PreSolve(contact, oldManifold); 89 | 90 | b2Fixture* fixtureA = contact.GetFixtureA(); 91 | b2Fixture* fixtureB = contact.GetFixtureB(); 92 | 93 | if (fixtureA != m_platform && fixtureA != m_character) 94 | { 95 | return; 96 | } 97 | 98 | if (fixtureB != m_platform && fixtureB != m_character) 99 | { 100 | return; 101 | } 102 | 103 | b2Vec2 position = m_character.GetBody().GetPosition(); 104 | 105 | if (position.y < m_top + m_radius - 3.0f * b2_linearSlop) 106 | { 107 | contact.SetEnabled(false); 108 | } 109 | } 110 | 111 | override void Step(Settings* settings) 112 | { 113 | super.Step(settings); 114 | 115 | g_debugDraw.DrawString(5, m_textLine, "Press: (c) create a shape, (d) destroy a shape."); 116 | m_textLine += DRAW_STRING_NEW_LINE; 117 | 118 | b2Vec2 v = m_character.GetBody().GetLinearVelocity(); 119 | g_debugDraw.DrawString(5, m_textLine, format("Character Linear Velocity: %f", v.y)); 120 | m_textLine += DRAW_STRING_NEW_LINE; 121 | } 122 | 123 | static Test Create() 124 | { 125 | return new typeof(this); 126 | } 127 | 128 | float32 m_radius = 0, m_top = 0, m_bottom = 0; 129 | State m_state; 130 | b2Fixture* m_platform; 131 | b2Fixture* m_character; 132 | } 133 | -------------------------------------------------------------------------------- /examples/demo/tests/pinball.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.pinball; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | /// This tests bullet collision and provides an example of a gameplay scenario. 33 | /// This also uses a loop shape. 34 | class Pinball : Test 35 | { 36 | this() 37 | { 38 | // Ground body_ 39 | b2Body* ground = null; 40 | { 41 | b2BodyDef bd; 42 | ground = m_world.CreateBody(&bd); 43 | 44 | b2Vec2 vs[5]; 45 | vs[0].Set(0.0f, -2.0f); 46 | vs[1].Set(8.0f, 6.0f); 47 | vs[2].Set(8.0f, 20.0f); 48 | vs[3].Set(-8.0f, 20.0f); 49 | vs[4].Set(-8.0f, 6.0f); 50 | 51 | b2ChainShape loop = new b2ChainShape(); 52 | loop.CreateLoop(vs); 53 | b2FixtureDef fd; 54 | fd.shape = loop; 55 | fd.density = 0.0f; 56 | ground.CreateFixture(&fd); 57 | } 58 | 59 | // Flippers 60 | { 61 | b2Vec2 p1 = b2Vec2(-2.0f, 0.0f), p2 = b2Vec2(2.0f, 0.0f); 62 | 63 | b2BodyDef bd; 64 | bd.type = b2_dynamicBody; 65 | 66 | bd.position = p1; 67 | b2Body* leftFlipper = m_world.CreateBody(&bd); 68 | 69 | bd.position = p2; 70 | b2Body* rightFlipper = m_world.CreateBody(&bd); 71 | 72 | b2PolygonShape box = new b2PolygonShape(); 73 | box.SetAsBox(1.75f, 0.1f); 74 | 75 | b2FixtureDef fd; 76 | fd.shape = box; 77 | fd.density = 1.0f; 78 | 79 | leftFlipper.CreateFixture(&fd); 80 | rightFlipper.CreateFixture(&fd); 81 | 82 | b2RevoluteJointDef jd = new b2RevoluteJointDef(); 83 | jd.bodyA = ground; 84 | jd.localAnchorB.SetZero(); 85 | jd.enableMotor = true; 86 | jd.maxMotorTorque = 1000.0f; 87 | jd.enableLimit = true; 88 | 89 | jd.motorSpeed = 0.0f; 90 | jd.localAnchorA = p1; 91 | jd.bodyB = leftFlipper; 92 | jd.lowerAngle = -30.0f * b2_pi / 180.0f; 93 | jd.upperAngle = 5.0f * b2_pi / 180.0f; 94 | m_leftJoint = cast(b2RevoluteJoint)m_world.CreateJoint(jd); 95 | 96 | jd.motorSpeed = 0.0f; 97 | jd.localAnchorA = p2; 98 | jd.bodyB = rightFlipper; 99 | jd.lowerAngle = -5.0f * b2_pi / 180.0f; 100 | jd.upperAngle = 30.0f * b2_pi / 180.0f; 101 | m_rightJoint = cast(b2RevoluteJoint)m_world.CreateJoint(jd); 102 | } 103 | 104 | // Circle character 105 | { 106 | b2BodyDef bd; 107 | bd.position.Set(1.0f, 15.0f); 108 | bd.type = b2_dynamicBody; 109 | bd.bullet = true; 110 | 111 | m_ball = m_world.CreateBody(&bd); 112 | 113 | b2CircleShape shape = new b2CircleShape(); 114 | shape.m_radius = 0.2f; 115 | 116 | b2FixtureDef fd; 117 | fd.shape = shape; 118 | fd.density = 1.0f; 119 | m_ball.CreateFixture(&fd); 120 | } 121 | 122 | m_button = false; 123 | } 124 | 125 | override void Step(Settings* settings) 126 | { 127 | super.Step(settings); 128 | 129 | if (m_button) 130 | { 131 | m_leftJoint.SetMotorSpeed(20.0f); 132 | m_rightJoint.SetMotorSpeed(-20.0f); 133 | } 134 | else 135 | { 136 | m_leftJoint.SetMotorSpeed(-10.0f); 137 | m_rightJoint.SetMotorSpeed(10.0f); 138 | } 139 | 140 | g_debugDraw.DrawString(5, m_textLine, "Press 'a' to control the flippers"); 141 | m_textLine += DRAW_STRING_NEW_LINE; 142 | } 143 | 144 | override void Keyboard(int key) 145 | { 146 | switch (key) 147 | { 148 | case GLFW_KEY_A: 149 | m_button = true; 150 | break; 151 | 152 | default: 153 | break; 154 | } 155 | } 156 | 157 | override void KeyboardUp(int key) 158 | { 159 | switch (key) 160 | { 161 | case GLFW_KEY_A: 162 | m_button = false; 163 | break; 164 | 165 | default: 166 | break; 167 | } 168 | } 169 | 170 | static Test Create() 171 | { 172 | return new typeof(this); 173 | } 174 | 175 | b2RevoluteJoint m_leftJoint; 176 | b2RevoluteJoint m_rightJoint; 177 | b2Body* m_ball; 178 | bool m_button; 179 | } 180 | -------------------------------------------------------------------------------- /examples/demo/tests/polycollision.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.polycollision; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class PolyCollision : Test 33 | { 34 | this() 35 | { 36 | m_polygonA = new b2PolygonShape(); 37 | m_polygonB = new b2PolygonShape(); 38 | 39 | { 40 | m_polygonA.SetAsBox(0.2f, 0.4f); 41 | m_transformA.Set(b2Vec2(0.0f, 0.0f), 0.0f); 42 | } 43 | 44 | { 45 | m_polygonB.SetAsBox(0.5f, 0.5f); 46 | m_positionB.Set(19.345284f, 1.5632932f); 47 | m_angleB = 1.9160721f; 48 | m_transformB.Set(m_positionB, m_angleB); 49 | } 50 | } 51 | 52 | override void Step(Settings* settings) 53 | { 54 | super.Step(settings); 55 | 56 | b2Manifold manifold; 57 | b2CollidePolygons(&manifold, m_polygonA, m_transformA, m_polygonB, m_transformB); 58 | 59 | b2WorldManifold worldManifold; 60 | worldManifold.Initialize(&manifold, m_transformA, m_polygonA.m_radius, m_transformB, m_polygonB.m_radius); 61 | 62 | g_debugDraw.DrawString(5, m_textLine, format("point count = %d", manifold.pointCount)); 63 | m_textLine += DRAW_STRING_NEW_LINE; 64 | 65 | { 66 | b2Color color = b2Color(0.9f, 0.9f, 0.9f); 67 | b2Vec2 v[b2_maxPolygonVertices]; 68 | 69 | for (int32 i = 0; i < m_polygonA.m_count; ++i) 70 | { 71 | v[i] = b2Mul(m_transformA, m_polygonA.m_vertices[i]); 72 | } 73 | 74 | g_debugDraw.DrawPolygon(v.ptr, m_polygonA.m_count, color); 75 | 76 | for (int32 i = 0; i < m_polygonB.m_count; ++i) 77 | { 78 | v[i] = b2Mul(m_transformB, m_polygonB.m_vertices[i]); 79 | } 80 | 81 | g_debugDraw.DrawPolygon(v.ptr, m_polygonB.m_count, color); 82 | } 83 | 84 | for (int32 i = 0; i < manifold.pointCount; ++i) 85 | { 86 | g_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, b2Color(0.9f, 0.3f, 0.3f)); 87 | } 88 | } 89 | 90 | override void Keyboard(int key) 91 | { 92 | switch (key) 93 | { 94 | case GLFW_KEY_A: 95 | m_positionB.x -= 0.1f; 96 | break; 97 | 98 | case GLFW_KEY_D: 99 | m_positionB.x += 0.1f; 100 | break; 101 | 102 | case GLFW_KEY_S: 103 | m_positionB.y -= 0.1f; 104 | break; 105 | 106 | case GLFW_KEY_W: 107 | m_positionB.y += 0.1f; 108 | break; 109 | 110 | case GLFW_KEY_Q: 111 | m_angleB += 0.1f * b2_pi; 112 | break; 113 | 114 | case GLFW_KEY_E: 115 | m_angleB -= 0.1f * b2_pi; 116 | break; 117 | 118 | default: 119 | break; 120 | } 121 | 122 | m_transformB.Set(m_positionB, m_angleB); 123 | } 124 | 125 | b2PolygonShape m_polygonA; 126 | b2PolygonShape m_polygonB; 127 | 128 | b2Transform m_transformA; 129 | b2Transform m_transformB; 130 | 131 | b2Vec2 m_positionB; 132 | float32 m_angleB = 0; 133 | 134 | static Test Create() 135 | { 136 | return new typeof(this); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /examples/demo/tests/prismatic.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.prismatic; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Prismatic : Test 33 | { 34 | this() 35 | { 36 | b2Body* ground = null; 37 | { 38 | b2BodyDef bd; 39 | ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 43 | ground.CreateFixture(shape, 0.0f); 44 | } 45 | 46 | { 47 | auto shape = new b2PolygonShape(); 48 | shape.SetAsBox(2.0f, 0.5f); 49 | 50 | b2BodyDef bd; 51 | bd.type = b2_dynamicBody; 52 | bd.position.Set(-10.0f, 10.0f); 53 | bd.angle = 0.5f * b2_pi; 54 | bd.allowSleep = false; 55 | b2Body* body_ = m_world.CreateBody(&bd); 56 | body_.CreateFixture(shape, 5.0f); 57 | 58 | b2PrismaticJointDef pjd = new b2PrismaticJointDef(); 59 | 60 | // Bouncy limit 61 | b2Vec2 axis = b2Vec2(2.0f, 1.0f); 62 | axis.Normalize(); 63 | pjd.Initialize(ground, body_, b2Vec2(0.0f, 0.0f), axis); 64 | 65 | // Non-bouncy limit 66 | // pjd.Initialize(ground, body_, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f)); 67 | 68 | pjd.motorSpeed = 10.0f; 69 | pjd.maxMotorForce = 10000.0f; 70 | pjd.enableMotor = true; 71 | pjd.lowerTranslation = 0.0f; 72 | pjd.upperTranslation = 20.0f; 73 | pjd.enableLimit = true; 74 | 75 | m_joint = cast(b2PrismaticJoint)m_world.CreateJoint(pjd); 76 | } 77 | } 78 | 79 | override void Keyboard(int key) 80 | { 81 | switch (key) 82 | { 83 | case GLFW_KEY_L: 84 | m_joint.EnableLimit(!m_joint.IsLimitEnabled()); 85 | break; 86 | 87 | case GLFW_KEY_M: 88 | m_joint.EnableMotor(!m_joint.IsMotorEnabled()); 89 | break; 90 | 91 | case GLFW_KEY_S: 92 | m_joint.SetMotorSpeed(-m_joint.GetMotorSpeed()); 93 | break; 94 | 95 | default: 96 | break; 97 | } 98 | } 99 | 100 | override void Step(Settings* settings) 101 | { 102 | super.Step(settings); 103 | 104 | g_debugDraw.DrawString(5, m_textLine, "Keys: (l) limits, (m) motors, (s) speed"); 105 | m_textLine += DRAW_STRING_NEW_LINE; 106 | float32 force = m_joint.GetMotorForce(settings.hz); 107 | g_debugDraw.DrawString(5, m_textLine, format("Motor Force = %4.0f", cast(float)force)); 108 | m_textLine += DRAW_STRING_NEW_LINE; 109 | } 110 | 111 | static Test Create() 112 | { 113 | return new typeof(this); 114 | } 115 | 116 | b2PrismaticJoint m_joint; 117 | } 118 | -------------------------------------------------------------------------------- /examples/demo/tests/pulleys.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.pulleys; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Pulleys : Test 33 | { 34 | this() 35 | { 36 | float32 y = 16.0f; 37 | float32 L = 12.0f; 38 | float32 a = 1.0f; 39 | float32 b = 2.0f; 40 | 41 | b2Body* ground = null; 42 | { 43 | b2BodyDef bd; 44 | ground = m_world.CreateBody(&bd); 45 | 46 | b2EdgeShape edge = new b2EdgeShape(); 47 | edge.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 48 | //ground.CreateFixture(&shape, 0.0f); 49 | 50 | b2CircleShape circle = new b2CircleShape(); 51 | circle.m_radius = 2.0f; 52 | 53 | circle.m_p.Set(-10.0f, y + b + L); 54 | ground.CreateFixture(circle, 0.0f); 55 | 56 | circle.m_p.Set(10.0f, y + b + L); 57 | ground.CreateFixture(circle, 0.0f); 58 | } 59 | 60 | { 61 | b2PolygonShape shape = new b2PolygonShape(); 62 | shape.SetAsBox(a, b); 63 | 64 | b2BodyDef bd; 65 | bd.type = b2_dynamicBody; 66 | 67 | //bd.fixedRotation = true; 68 | bd.position.Set(-10.0f, y); 69 | b2Body* body1 = m_world.CreateBody(&bd); 70 | body1.CreateFixture(shape, 5.0f); 71 | 72 | bd.position.Set(10.0f, y); 73 | b2Body* body2 = m_world.CreateBody(&bd); 74 | body2.CreateFixture(shape, 5.0f); 75 | 76 | b2PulleyJointDef pulleyDef = new b2PulleyJointDef(); 77 | b2Vec2 anchor1 = b2Vec2(-10.0f, y + b); 78 | b2Vec2 anchor2 = b2Vec2(10.0f, y + b); 79 | b2Vec2 groundAnchor1 = b2Vec2(-10.0f, y + b + L); 80 | b2Vec2 groundAnchor2 = b2Vec2(10.0f, y + b + L); 81 | pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f); 82 | 83 | m_joint1 = cast(b2PulleyJoint)m_world.CreateJoint(pulleyDef); 84 | } 85 | } 86 | 87 | override void Step(Settings* settings) 88 | { 89 | super.Step(settings); 90 | 91 | float32 ratio = m_joint1.GetRatio(); 92 | float32 L = m_joint1.GetCurrentLengthA() + ratio * m_joint1.GetCurrentLengthB(); 93 | g_debugDraw.DrawString(5, m_textLine, format("L1 + %4.2f * L2 = %4.2f", cast(float)ratio, cast(float)L)); 94 | m_textLine += DRAW_STRING_NEW_LINE; 95 | } 96 | 97 | static Test Create() 98 | { 99 | return new typeof(this); 100 | } 101 | 102 | b2PulleyJoint m_joint1; 103 | } 104 | -------------------------------------------------------------------------------- /examples/demo/tests/pyramid.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.pyramid; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class Pyramid : Test 33 | { 34 | enum 35 | { 36 | e_count = 20 37 | } 38 | 39 | this() 40 | { 41 | { 42 | b2BodyDef bd; 43 | b2Body* ground = m_world.CreateBody(&bd); 44 | 45 | auto shape = new b2EdgeShape(); 46 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 47 | ground.CreateFixture(shape, 0.0f); 48 | } 49 | 50 | { 51 | float32 a = 0.5f; 52 | auto shape = new b2PolygonShape(); 53 | shape.SetAsBox(a, a); 54 | 55 | b2Vec2 x = b2Vec2(-7.0f, 0.75f); 56 | b2Vec2 y; 57 | b2Vec2 deltaX = b2Vec2(0.5625f, 1.25f); 58 | b2Vec2 deltaY = b2Vec2(1.125f, 0.0f); 59 | 60 | for (int32 i = 0; i < e_count; ++i) 61 | { 62 | y = x; 63 | 64 | for (int32 j = i; j < e_count; ++j) 65 | { 66 | b2BodyDef bd; 67 | bd.type = b2_dynamicBody; 68 | bd.position = y; 69 | b2Body* body_ = m_world.CreateBody(&bd); 70 | body_.CreateFixture(shape, 5.0f); 71 | 72 | y += deltaY; 73 | } 74 | 75 | x += deltaX; 76 | } 77 | } 78 | } 79 | 80 | static Test Create() 81 | { 82 | return new typeof(this); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /examples/demo/tests/ropejoint.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.ropejoint; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | /// This test shows how a rope joint can be used to stabilize a chain of 33 | /// bodies with a heavy payload. Notice that the rope joint just prevents 34 | /// excessive stretching and has no other effect. 35 | /// By disabling the rope joint you can see that the Box2D solver has trouble 36 | /// supporting heavy bodies with light bodies. Try playing around with the 37 | /// densities, time step, and iterations to see how they affect stability. 38 | /// This test also shows how to use contact filtering. Filtering is configured 39 | /// so that the payload does not collide with the chain. 40 | class RopeJoint : Test 41 | { 42 | this() 43 | { 44 | b2Body* ground = null; 45 | { 46 | b2BodyDef bd; 47 | ground = m_world.CreateBody(&bd); 48 | 49 | auto shape = new b2EdgeShape(); 50 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 51 | ground.CreateFixture(shape, 0.0f); 52 | } 53 | 54 | { 55 | auto shape = new b2PolygonShape(); 56 | shape.SetAsBox(0.5f, 0.125f); 57 | 58 | b2FixtureDef fd; 59 | fd.shape = shape; 60 | fd.density = 20.0f; 61 | fd.friction = 0.2f; 62 | fd.filter.categoryBits = 0x0001; 63 | fd.filter.maskBits = 0xFFFF & ~0x0002; 64 | 65 | b2RevoluteJointDef jd = new b2RevoluteJointDef(); 66 | jd.collideConnected = false; 67 | 68 | const int32 N = 10; 69 | const float32 y = 15.0f; 70 | m_ropeDef = new b2RopeJointDef(); 71 | m_ropeDef.localAnchorA.Set(0.0f, y); 72 | 73 | b2Body* prevBody = ground; 74 | 75 | for (int32 i = 0; i < N; ++i) 76 | { 77 | b2BodyDef bd; 78 | bd.type = b2_dynamicBody; 79 | bd.position.Set(0.5f + 1.0f * i, y); 80 | 81 | if (i == N - 1) 82 | { 83 | shape.SetAsBox(1.5f, 1.5f); 84 | fd.density = 100.0f; 85 | fd.filter.categoryBits = 0x0002; 86 | bd.position.Set(1.0f * i, y); 87 | bd.angularDamping = 0.4f; 88 | } 89 | 90 | b2Body* body_ = m_world.CreateBody(&bd); 91 | 92 | body_.CreateFixture(&fd); 93 | 94 | b2Vec2 anchor = b2Vec2(cast(float32)i, y); 95 | jd.Initialize(prevBody, body_, anchor); 96 | m_world.CreateJoint(jd); 97 | 98 | prevBody = body_; 99 | } 100 | 101 | m_ropeDef.localAnchorB.SetZero(); 102 | 103 | float32 extraLength = 0.01f; 104 | m_ropeDef.maxLength = N - 1.0f + extraLength; 105 | m_ropeDef.bodyB = prevBody; 106 | } 107 | 108 | { 109 | m_ropeDef.bodyA = ground; 110 | m_rope = m_world.CreateJoint(m_ropeDef); 111 | } 112 | } 113 | 114 | override void Keyboard(int key) 115 | { 116 | switch (key) 117 | { 118 | case GLFW_KEY_J: 119 | 120 | if (m_rope) 121 | { 122 | m_world.DestroyJoint(m_rope); 123 | m_rope = null; 124 | } 125 | else 126 | { 127 | m_rope = m_world.CreateJoint(m_ropeDef); 128 | } 129 | break; 130 | 131 | default: 132 | break; 133 | } 134 | } 135 | 136 | override void Step(Settings* settings) 137 | { 138 | super.Step(settings); 139 | g_debugDraw.DrawString(5, m_textLine, "Press (j) to toggle the rope joint."); 140 | m_textLine += DRAW_STRING_NEW_LINE; 141 | 142 | if (m_rope) 143 | { 144 | g_debugDraw.DrawString(5, m_textLine, "Rope ON"); 145 | } 146 | else 147 | { 148 | g_debugDraw.DrawString(5, m_textLine, "Rope OFF"); 149 | } 150 | m_textLine += DRAW_STRING_NEW_LINE; 151 | } 152 | 153 | static Test Create() 154 | { 155 | return new typeof(this); 156 | } 157 | 158 | b2RopeJointDef m_ropeDef; 159 | b2Joint m_rope; 160 | } 161 | -------------------------------------------------------------------------------- /examples/demo/tests/sensortest.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.sensortest; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | // This is used to test sensor shapes. 34 | class SensorTest : Test 35 | { 36 | enum 37 | { 38 | e_count = 7 39 | } 40 | 41 | this() 42 | { 43 | { 44 | b2BodyDef bd; 45 | b2Body* ground = m_world.CreateBody(&bd); 46 | 47 | { 48 | auto shape = new b2EdgeShape(); 49 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 50 | ground.CreateFixture(shape, 0.0f); 51 | } 52 | 53 | { 54 | b2CircleShape shape = new b2CircleShape(); 55 | shape.m_radius = 5.0f; 56 | shape.m_p.Set(0.0f, 10.0f); 57 | 58 | b2FixtureDef fd; 59 | fd.shape = shape; 60 | fd.isSensor = true; 61 | m_sensor = ground.CreateFixture(&fd); 62 | } 63 | } 64 | 65 | { 66 | b2CircleShape shape = new b2CircleShape(); 67 | shape.m_radius = 1.0f; 68 | 69 | for (int32 i = 0; i < e_count; ++i) 70 | { 71 | b2BodyDef bd; 72 | bd.type = b2_dynamicBody; 73 | bd.position.Set(-10.0f + 3.0f * i, 20.0f); 74 | bd.userData = &m_touching[i]; 75 | 76 | m_touching[i] = false; 77 | m_bodies[i] = m_world.CreateBody(&bd); 78 | 79 | m_bodies[i].CreateFixture(shape, 1.0f); 80 | } 81 | } 82 | } 83 | 84 | // Implement contact listener. 85 | override void BeginContact(b2Contact contact) 86 | { 87 | b2Fixture* fixtureA = contact.GetFixtureA(); 88 | b2Fixture* fixtureB = contact.GetFixtureB(); 89 | 90 | if (fixtureA == m_sensor) 91 | { 92 | void* userData = fixtureB.GetBody().GetUserData(); 93 | 94 | if (userData) 95 | { 96 | bool* touching = cast(bool*)userData; 97 | *touching = true; 98 | } 99 | } 100 | 101 | if (fixtureB == m_sensor) 102 | { 103 | void* userData = fixtureA.GetBody().GetUserData(); 104 | 105 | if (userData) 106 | { 107 | bool* touching = cast(bool*)userData; 108 | *touching = true; 109 | } 110 | } 111 | } 112 | 113 | // Implement contact listener. 114 | override void EndContact(b2Contact contact) 115 | { 116 | b2Fixture* fixtureA = contact.GetFixtureA(); 117 | b2Fixture* fixtureB = contact.GetFixtureB(); 118 | 119 | if (fixtureA == m_sensor) 120 | { 121 | void* userData = fixtureB.GetBody().GetUserData(); 122 | 123 | if (userData) 124 | { 125 | bool* touching = cast(bool*)userData; 126 | *touching = false; 127 | } 128 | } 129 | 130 | if (fixtureB == m_sensor) 131 | { 132 | void* userData = fixtureA.GetBody().GetUserData(); 133 | 134 | if (userData) 135 | { 136 | bool* touching = cast(bool*)userData; 137 | *touching = false; 138 | } 139 | } 140 | } 141 | 142 | override void Step(Settings* settings) 143 | { 144 | super.Step(settings); 145 | 146 | // Traverse the contact results. Apply a force on shapes 147 | // that overlap the sensor. 148 | for (int32 i = 0; i < e_count; ++i) 149 | { 150 | if (m_touching[i] == false) 151 | { 152 | continue; 153 | } 154 | 155 | b2Body* body_ = m_bodies[i]; 156 | b2Body* ground = m_sensor.GetBody(); 157 | 158 | b2CircleShape circle = cast(b2CircleShape)m_sensor.GetShape(); 159 | b2Vec2 center = ground.GetWorldPoint(circle.m_p); 160 | 161 | b2Vec2 position = body_.GetPosition(); 162 | 163 | b2Vec2 d = center - position; 164 | 165 | if (d.LengthSquared() < FLT_EPSILON * FLT_EPSILON) 166 | { 167 | continue; 168 | } 169 | 170 | d.Normalize(); 171 | b2Vec2 F = 100.0f * d; 172 | body_.ApplyForce(F, position, false); 173 | } 174 | } 175 | 176 | static Test Create() 177 | { 178 | return new typeof(this); 179 | } 180 | 181 | b2Fixture* m_sensor; 182 | b2Body* m_bodies[e_count]; 183 | bool m_touching[e_count]; 184 | } 185 | -------------------------------------------------------------------------------- /examples/demo/tests/shapeediting.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.shapeediting; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | // This is used to test sensor shapes. 34 | class ShapeEditing : Test 35 | { 36 | this() 37 | { 38 | { 39 | b2BodyDef bd; 40 | b2Body* ground = m_world.CreateBody(&bd); 41 | 42 | auto shape = new b2EdgeShape(); 43 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 44 | ground.CreateFixture(shape, 0.0f); 45 | } 46 | 47 | b2BodyDef bd; 48 | bd.type = b2_dynamicBody; 49 | bd.position.Set(0.0f, 10.0f); 50 | m_body = m_world.CreateBody(&bd); 51 | 52 | auto shape = new b2PolygonShape(); 53 | shape.SetAsBox(4.0f, 4.0f, b2Vec2(0.0f, 0.0f), 0.0f); 54 | m_fixture1 = m_body.CreateFixture(shape, 10.0f); 55 | 56 | m_fixture2 = null; 57 | 58 | m_sensor = false; 59 | } 60 | 61 | override void Keyboard(int key) 62 | { 63 | switch (key) 64 | { 65 | case GLFW_KEY_C: 66 | 67 | if (m_fixture2 == null) 68 | { 69 | b2CircleShape shape = new b2CircleShape(); 70 | shape.m_radius = 3.0f; 71 | shape.m_p.Set(0.5f, -4.0f); 72 | m_fixture2 = m_body.CreateFixture(shape, 10.0f); 73 | m_body.SetAwake(true); 74 | } 75 | break; 76 | 77 | case GLFW_KEY_D: 78 | 79 | if (m_fixture2 != null) 80 | { 81 | m_body.DestroyFixture(m_fixture2); 82 | m_fixture2 = null; 83 | m_body.SetAwake(true); 84 | } 85 | break; 86 | 87 | case GLFW_KEY_S: 88 | 89 | if (m_fixture2 != null) 90 | { 91 | m_sensor = !m_sensor; 92 | m_fixture2.SetSensor(m_sensor); 93 | } 94 | break; 95 | 96 | default: 97 | break; 98 | } 99 | } 100 | 101 | override void Step(Settings* settings) 102 | { 103 | super.Step(settings); 104 | 105 | g_debugDraw.DrawString(5, m_textLine, "Press: (c) create a shape, (d) destroy a shape."); 106 | m_textLine += DRAW_STRING_NEW_LINE; 107 | g_debugDraw.DrawString(5, m_textLine, format("sensor = %d", m_sensor)); 108 | m_textLine += DRAW_STRING_NEW_LINE; 109 | } 110 | 111 | static Test Create() 112 | { 113 | return new typeof(this); 114 | } 115 | 116 | b2Body* m_body; 117 | b2Fixture* m_fixture1; 118 | b2Fixture* m_fixture2; 119 | bool m_sensor; 120 | } 121 | -------------------------------------------------------------------------------- /examples/demo/tests/spherestack.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.spherestack; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class SphereStack : Test 34 | { 35 | enum 36 | { 37 | e_count = 10 38 | } 39 | 40 | this() 41 | { 42 | { 43 | b2BodyDef bd; 44 | b2Body* ground = m_world.CreateBody(&bd); 45 | 46 | auto shape = new b2EdgeShape(); 47 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 48 | ground.CreateFixture(shape, 0.0f); 49 | } 50 | 51 | { 52 | b2CircleShape shape = new b2CircleShape(); 53 | shape.m_radius = 1.0f; 54 | 55 | for (int32 i = 0; i < e_count; ++i) 56 | { 57 | b2BodyDef bd; 58 | bd.type = b2_dynamicBody; 59 | bd.position.Set(0.0, 4.0f + 3.0f * i); 60 | 61 | m_bodies[i] = m_world.CreateBody(&bd); 62 | 63 | m_bodies[i].CreateFixture(shape, 1.0f); 64 | 65 | m_bodies[i].SetLinearVelocity(b2Vec2(0.0f, -50.0f)); 66 | } 67 | } 68 | } 69 | 70 | static Test Create() 71 | { 72 | return new typeof(this); 73 | } 74 | 75 | b2Body* m_bodies[e_count]; 76 | } 77 | -------------------------------------------------------------------------------- /examples/demo/tests/test_entries.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.test_entries; 19 | 20 | import framework.test; 21 | 22 | import tests.addpair; 23 | import tests.applyforce; 24 | import tests.basicslidercrank; 25 | import tests.bodytypes; 26 | import tests.breakable; 27 | import tests.bridge; 28 | import tests.bullettest; 29 | import tests.cantilever; 30 | import tests.car; 31 | import tests.continuoustest; 32 | import tests.chain; 33 | import tests.charactercollision; 34 | import tests.collisionfiltering; 35 | import tests.collisionprocessing; 36 | import tests.compoundshapes; 37 | import tests.confined; 38 | import tests.convexhull; 39 | import tests.conveyorbelt; 40 | import tests.distancetest; 41 | import tests.dominos; 42 | import tests.dumpshell; 43 | import tests.dynamictreetest; 44 | import tests.edgeshapes; 45 | import tests.edgetest; 46 | import tests.gears; 47 | import tests.heavyonlight; 48 | import tests.heavyonlighttwo; 49 | import tests.mobile; 50 | import tests.mobilebalanced; 51 | import tests.motorjoint; 52 | import tests.onesidedplatform; 53 | import tests.pinball; 54 | import tests.polycollision; 55 | import tests.polyshapes; 56 | import tests.prismatic; 57 | import tests.pulleys; 58 | import tests.pyramid; 59 | import tests.raycast; 60 | import tests.revolute; 61 | import tests.ropejoint; 62 | import tests.sensortest; 63 | import tests.shapeediting; 64 | import tests.slidercrank; 65 | import tests.spherestack; 66 | import tests.theojansen; 67 | import tests.tiles; 68 | import tests.timeofimpact; 69 | import tests.tumbler; 70 | import tests.varyingfriction; 71 | import tests.varyingrestitution; 72 | import tests.verticalstack; 73 | import tests.web; 74 | 75 | TestEntry[] g_testEntries; 76 | 77 | shared static this() 78 | { 79 | g_testEntries = 80 | [ 81 | TestEntry("Tiles", &Tiles.Create), 82 | TestEntry("Heavy on Light", &HeavyOnLight.Create), 83 | TestEntry("Heavy on Light Two", &HeavyOnLightTwo.Create), 84 | TestEntry("Vertical Stack", &VerticalStack.Create), 85 | TestEntry("Basic Slider Crank", &BasicSliderCrank.Create), 86 | TestEntry("Slider Crank", &SliderCrank.Create), 87 | TestEntry("Sphere Stack", &SphereStack.Create), 88 | TestEntry("Convex Hull", &ConvexHull.Create), 89 | TestEntry("Tumbler", &Tumbler.Create), 90 | TestEntry("Ray-Cast", &RayCast.Create), 91 | TestEntry("Dump Shell", &DumpShell.Create), 92 | TestEntry("Apply Force", &ApplyForce.Create), 93 | TestEntry("Continuous Test", &ContinuousTest.Create), 94 | TestEntry("Time of Impact", &TimeOfImpact.Create), 95 | TestEntry("Motor Joint", &MotorJoint.Create), 96 | TestEntry("One-Sided Platform", &OneSidedPlatform.Create), 97 | TestEntry("Mobile", &Mobile.Create), 98 | TestEntry("MobileBalanced", &MobileBalanced.Create), 99 | TestEntry("Conveyor Belt", &ConveyorBelt.Create), 100 | TestEntry("Gears", &Gears.Create), 101 | TestEntry("Varying Restitution", &VaryingRestitution.Create), 102 | TestEntry("Cantilever", &Cantilever.Create), 103 | TestEntry("Character Collision", &CharacterCollision.Create), 104 | TestEntry("Edge Test", &EdgeTest.Create), 105 | TestEntry("Body Types", &BodyTypes.Create), 106 | TestEntry("Shape Editing", &ShapeEditing.Create), 107 | TestEntry("Car", &Car.Create), // broken 108 | TestEntry("Prismatic", &Prismatic.Create), 109 | TestEntry("Revolute", &Revolute.Create), 110 | TestEntry("Pulleys", &Pulleys.Create), 111 | TestEntry("Polygon Shapes", &PolyShapes.Create), 112 | TestEntry("Web", &Web.Create), 113 | TestEntry("RopeJoint", &RopeJoint.Create), 114 | TestEntry("Pinball", &Pinball.Create), 115 | TestEntry("Bullet Test", &BulletTest.Create), 116 | TestEntry("Confined", &Confined.Create), // broken 117 | TestEntry("Pyramid", &Pyramid.Create), 118 | TestEntry("Theo Jansen's Walker", &TheoJansen.Create), 119 | TestEntry("Edge Shapes", &EdgeShapes.Create), 120 | TestEntry("PolyCollision", &PolyCollision.Create), 121 | TestEntry("Bridge", &Bridge.Create), 122 | TestEntry("Breakable", &Breakable.Create), 123 | TestEntry("Chain", &Chain.Create), 124 | TestEntry("Collision Filtering", &CollisionFiltering.Create), 125 | TestEntry("Collision Processing", &CollisionProcessing.Create), 126 | TestEntry("Compound Shapes", &CompoundShapes.Create), 127 | TestEntry("Distance Test", &DistanceTest.Create), 128 | TestEntry("Dominos", &Dominos.Create), 129 | TestEntry("Dynamic Tree", &DynamicTreeTest.Create), 130 | TestEntry("Sensor Test", &SensorTest.Create), 131 | TestEntry("Varying Friction", &VaryingFriction.Create), 132 | TestEntry("Performance: Add Pair Stress Test", &AddPair.Create), 133 | ]; 134 | 135 | import std.algorithm; 136 | sort!((a, b) => a.name < b.name)(g_testEntries); 137 | } 138 | -------------------------------------------------------------------------------- /examples/demo/tests/tiles.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.tiles; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import dbox; 26 | 27 | import framework.debug_draw; 28 | import framework.test; 29 | 30 | /// This stress tests the dynamic tree broad-phase. This also shows that tile 31 | /// based collision is _not_ smooth due to Box2D not knowing about adjacency. 32 | class Tiles : Test 33 | { 34 | enum 35 | { 36 | e_count = 20 37 | }; 38 | 39 | this() 40 | { 41 | m_fixtureCount = 0; 42 | auto timer = b2Timer(); 43 | 44 | { 45 | float32 a = 0.5f; 46 | b2BodyDef bd; 47 | bd.position.y = -a; 48 | b2Body* ground = m_world.CreateBody(&bd); 49 | 50 | int32 N = 200; 51 | int32 M = 10; 52 | b2Vec2 position; 53 | position.y = 0.0f; 54 | 55 | for (int32 j = 0; j < M; ++j) 56 | { 57 | position.x = -N * a; 58 | 59 | for (int32 i = 0; i < N; ++i) 60 | { 61 | auto shape = new b2PolygonShape(); 62 | shape.SetAsBox(a, a, position, 0.0f); 63 | ground.CreateFixture(shape, 0.0f); 64 | ++m_fixtureCount; 65 | position.x += 2.0f * a; 66 | } 67 | 68 | position.y -= 2.0f * a; 69 | } 70 | } 71 | 72 | { 73 | float32 a = 0.5f; 74 | auto shape = new b2PolygonShape(); 75 | shape.SetAsBox(a, a); 76 | 77 | b2Vec2 x = b2Vec2(-7.0f, 0.75f); 78 | b2Vec2 y; 79 | b2Vec2 deltaX = b2Vec2(0.5625f, 1.25f); 80 | b2Vec2 deltaY = b2Vec2(1.125f, 0.0f); 81 | 82 | for (int32 i = 0; i < e_count; ++i) 83 | { 84 | y = x; 85 | 86 | for (int32 j = i; j < e_count; ++j) 87 | { 88 | b2BodyDef bd; 89 | bd.type = b2_dynamicBody; 90 | bd.position = y; 91 | 92 | // if (i == 0 && j == 0) 93 | // { 94 | // bd.allowSleep = false; 95 | // } 96 | // else 97 | // { 98 | // bd.allowSleep = true; 99 | // } 100 | 101 | b2Body* body_ = m_world.CreateBody(&bd); 102 | body_.CreateFixture(shape, 5.0f); 103 | ++m_fixtureCount; 104 | y += deltaY; 105 | } 106 | 107 | x += deltaX; 108 | } 109 | } 110 | 111 | m_createTime = timer.GetMilliseconds(); 112 | } 113 | 114 | override void Step(Settings* settings) 115 | { 116 | super.Step(settings); 117 | 118 | auto cm = m_world.GetContactManager(); 119 | int32 height = cm.m_broadPhase.GetTreeHeight(); 120 | int32 leafCount = cm.m_broadPhase.GetProxyCount(); 121 | int32 minimumNodeCount = 2 * leafCount - 1; 122 | float32 minimumHeight = ceilf(logf(cast(float32)minimumNodeCount) / logf(2.0f)); 123 | g_debugDraw.DrawString(5, m_textLine, format("dynamic tree height = %d, min = %d", height, cast(int32)minimumHeight)); 124 | m_textLine += DRAW_STRING_NEW_LINE; 125 | 126 | g_debugDraw.DrawString(5, m_textLine, format("create time = %6.2f ms, fixture count = %d", 127 | m_createTime, m_fixtureCount)); 128 | m_textLine += DRAW_STRING_NEW_LINE; 129 | 130 | // b2DynamicTree* tree = &m_world.m_contactManager.m_broadPhase.m_tree; 131 | 132 | // if (m_stepCount == 400) 133 | // { 134 | // tree.RebuildBottomUp(); 135 | // } 136 | } 137 | 138 | static Test Create() 139 | { 140 | return new typeof(this); 141 | } 142 | 143 | int32 m_fixtureCount; 144 | float32 m_createTime = 0; 145 | } 146 | -------------------------------------------------------------------------------- /examples/demo/tests/timeofimpact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.timeofimpact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class TimeOfImpact : Test 34 | { 35 | this() 36 | { 37 | m_shapeA = new b2PolygonShape(); 38 | m_shapeB = new b2PolygonShape(); 39 | 40 | m_shapeA.SetAsBox(25.0f, 5.0f); 41 | m_shapeB.SetAsBox(2.5f, 2.5f); 42 | } 43 | 44 | override void Step(Settings* settings) 45 | { 46 | super.Step(settings); 47 | 48 | b2Sweep sweepA; 49 | sweepA.c0.Set(24.0f, -60.0f); 50 | sweepA.a0 = 2.95f; 51 | sweepA.c = sweepA.c0; 52 | sweepA.a = sweepA.a0; 53 | sweepA.localCenter.SetZero(); 54 | 55 | b2Sweep sweepB; 56 | sweepB.c0.Set(53.474274f, -50.252514f); 57 | sweepB.a0 = 513.36676f; // - 162.0f * b2_pi; 58 | sweepB.c.Set(54.595478f, -51.083473f); 59 | sweepB.a = 513.62781f; // - 162.0f * b2_pi; 60 | sweepB.localCenter.SetZero(); 61 | 62 | // sweepB.a0 -= 300.0f * b2_pi; 63 | // sweepB.a -= 300.0f * b2_pi; 64 | 65 | b2TOIInput input; 66 | input.proxyA.Set(m_shapeA, 0); 67 | input.proxyB.Set(m_shapeB, 0); 68 | input.sweepA = sweepA; 69 | input.sweepB = sweepB; 70 | input.tMax = 1.0f; 71 | 72 | b2TOIOutput output; 73 | 74 | b2TimeOfImpact(&output, &input); 75 | 76 | g_debugDraw.DrawString(5, m_textLine, format("toi = %g", output.t)); 77 | m_textLine += DRAW_STRING_NEW_LINE; 78 | 79 | g_debugDraw.DrawString(5, m_textLine, format("max toi iters = %d, max root iters = %d", b2_toiMaxIters, b2_toiMaxRootIters)); 80 | m_textLine += DRAW_STRING_NEW_LINE; 81 | 82 | b2Vec2 vertices[b2_maxPolygonVertices]; 83 | 84 | b2Transform transformA; 85 | sweepA.GetTransform(&transformA, 0.0f); 86 | 87 | for (int32 i = 0; i < m_shapeA.m_count; ++i) 88 | { 89 | vertices[i] = b2Mul(transformA, m_shapeA.m_vertices[i]); 90 | } 91 | 92 | g_debugDraw.DrawPolygon(vertices.ptr, m_shapeA.m_count, b2Color(0.9f, 0.9f, 0.9f)); 93 | 94 | b2Transform transformB; 95 | sweepB.GetTransform(&transformB, 0.0f); 96 | 97 | // b2Vec2 localPoint(2.0f, -0.1f); 98 | 99 | for (int32 i = 0; i < m_shapeB.m_count; ++i) 100 | { 101 | vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]); 102 | } 103 | 104 | g_debugDraw.DrawPolygon(vertices.ptr, m_shapeB.m_count, b2Color(0.5f, 0.9f, 0.5f)); 105 | 106 | sweepB.GetTransform(&transformB, output.t); 107 | 108 | for (int32 i = 0; i < m_shapeB.m_count; ++i) 109 | { 110 | vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]); 111 | } 112 | 113 | g_debugDraw.DrawPolygon(vertices.ptr, m_shapeB.m_count, b2Color(0.5f, 0.7f, 0.9f)); 114 | 115 | sweepB.GetTransform(&transformB, 1.0f); 116 | 117 | for (int32 i = 0; i < m_shapeB.m_count; ++i) 118 | { 119 | vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]); 120 | } 121 | 122 | g_debugDraw.DrawPolygon(vertices.ptr, m_shapeB.m_count, b2Color(0.9f, 0.5f, 0.5f)); 123 | } 124 | 125 | b2PolygonShape m_shapeA; 126 | b2PolygonShape m_shapeB; 127 | 128 | static Test Create() 129 | { 130 | return new typeof(this); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /examples/demo/tests/tumbler.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.tumbler; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class Tumbler : Test 34 | { 35 | enum 36 | { 37 | e_count = 800 38 | } 39 | 40 | this() 41 | { 42 | b2Body* ground = null; 43 | { 44 | b2BodyDef bd; 45 | ground = m_world.CreateBody(&bd); 46 | } 47 | 48 | { 49 | b2BodyDef bd; 50 | bd.type = b2_dynamicBody; 51 | bd.allowSleep = false; 52 | bd.position.Set(0.0f, 10.0f); 53 | b2Body* body_ = m_world.CreateBody(&bd); 54 | 55 | auto shape = new b2PolygonShape(); 56 | shape.SetAsBox(0.5f, 10.0f, b2Vec2(10.0f, 0.0f), 0.0); 57 | body_.CreateFixture(shape, 5.0f); 58 | shape.SetAsBox(0.5f, 10.0f, b2Vec2(-10.0f, 0.0f), 0.0); 59 | body_.CreateFixture(shape, 5.0f); 60 | shape.SetAsBox(10.0f, 0.5f, b2Vec2(0.0f, 10.0f), 0.0); 61 | body_.CreateFixture(shape, 5.0f); 62 | shape.SetAsBox(10.0f, 0.5f, b2Vec2(0.0f, -10.0f), 0.0); 63 | body_.CreateFixture(shape, 5.0f); 64 | 65 | b2RevoluteJointDef jd = new b2RevoluteJointDef(); 66 | jd.bodyA = ground; 67 | jd.bodyB = body_; 68 | jd.localAnchorA.Set(0.0f, 10.0f); 69 | jd.localAnchorB.Set(0.0f, 0.0f); 70 | jd.referenceAngle = 0.0f; 71 | jd.motorSpeed = 0.05f * b2_pi; 72 | jd.maxMotorTorque = 1e8f; 73 | jd.enableMotor = true; 74 | m_joint = cast(b2RevoluteJoint)m_world.CreateJoint(jd); 75 | } 76 | 77 | m_count = 0; 78 | } 79 | 80 | override void Step(Settings* settings) 81 | { 82 | super.Step(settings); 83 | 84 | if (m_count < e_count) 85 | { 86 | b2BodyDef bd; 87 | bd.type = b2_dynamicBody; 88 | bd.position.Set(0.0f, 10.0f); 89 | b2Body* body_ = m_world.CreateBody(&bd); 90 | 91 | auto shape = new b2PolygonShape(); 92 | shape.SetAsBox(0.125f, 0.125f); 93 | body_.CreateFixture(shape, 1.0f); 94 | 95 | ++m_count; 96 | } 97 | } 98 | 99 | static Test Create() 100 | { 101 | return new typeof(this); 102 | } 103 | 104 | b2RevoluteJoint m_joint; 105 | int32 m_count; 106 | } 107 | -------------------------------------------------------------------------------- /examples/demo/tests/varyingfriction.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.varyingfriction; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class VaryingFriction : Test 34 | { 35 | this() 36 | { 37 | { 38 | b2BodyDef bd; 39 | b2Body* ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 43 | ground.CreateFixture(shape, 0.0f); 44 | } 45 | 46 | { 47 | auto shape = new b2PolygonShape(); 48 | shape.SetAsBox(13.0f, 0.25f); 49 | 50 | b2BodyDef bd; 51 | bd.position.Set(-4.0f, 22.0f); 52 | bd.angle = -0.25f; 53 | 54 | b2Body* ground = m_world.CreateBody(&bd); 55 | ground.CreateFixture(shape, 0.0f); 56 | } 57 | 58 | { 59 | auto shape = new b2PolygonShape(); 60 | shape.SetAsBox(0.25f, 1.0f); 61 | 62 | b2BodyDef bd; 63 | bd.position.Set(10.5f, 19.0f); 64 | 65 | b2Body* ground = m_world.CreateBody(&bd); 66 | ground.CreateFixture(shape, 0.0f); 67 | } 68 | 69 | { 70 | auto shape = new b2PolygonShape(); 71 | shape.SetAsBox(13.0f, 0.25f); 72 | 73 | b2BodyDef bd; 74 | bd.position.Set(4.0f, 14.0f); 75 | bd.angle = 0.25f; 76 | 77 | b2Body* ground = m_world.CreateBody(&bd); 78 | ground.CreateFixture(shape, 0.0f); 79 | } 80 | 81 | { 82 | auto shape = new b2PolygonShape(); 83 | shape.SetAsBox(0.25f, 1.0f); 84 | 85 | b2BodyDef bd; 86 | bd.position.Set(-10.5f, 11.0f); 87 | 88 | b2Body* ground = m_world.CreateBody(&bd); 89 | ground.CreateFixture(shape, 0.0f); 90 | } 91 | 92 | { 93 | auto shape = new b2PolygonShape(); 94 | shape.SetAsBox(13.0f, 0.25f); 95 | 96 | b2BodyDef bd; 97 | bd.position.Set(-4.0f, 6.0f); 98 | bd.angle = -0.25f; 99 | 100 | b2Body* ground = m_world.CreateBody(&bd); 101 | ground.CreateFixture(shape, 0.0f); 102 | } 103 | 104 | { 105 | auto shape = new b2PolygonShape(); 106 | shape.SetAsBox(0.5f, 0.5f); 107 | 108 | b2FixtureDef fd; 109 | fd.shape = shape; 110 | fd.density = 25.0f; 111 | 112 | float friction[5] = [0.75f, 0.5f, 0.35f, 0.1f, 0.0f]; 113 | 114 | for (int i = 0; i < 5; ++i) 115 | { 116 | b2BodyDef bd; 117 | bd.type = b2_dynamicBody; 118 | bd.position.Set(-15.0f + 4.0f * i, 28.0f); 119 | b2Body* body_ = m_world.CreateBody(&bd); 120 | 121 | fd.friction = friction[i]; 122 | body_.CreateFixture(&fd); 123 | } 124 | } 125 | } 126 | 127 | static Test Create() 128 | { 129 | return new typeof(this); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /examples/demo/tests/varyingrestitution.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.varyingrestitution; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.math; 22 | 23 | import std.string; 24 | import std.typecons; 25 | 26 | import deimos.glfw.glfw3; 27 | 28 | import dbox; 29 | 30 | import framework.debug_draw; 31 | import framework.test; 32 | 33 | class VaryingRestitution : Test 34 | { 35 | this() 36 | { 37 | { 38 | b2BodyDef bd; 39 | b2Body* ground = m_world.CreateBody(&bd); 40 | 41 | auto shape = new b2EdgeShape(); 42 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 43 | ground.CreateFixture(shape, 0.0f); 44 | } 45 | 46 | { 47 | b2CircleShape shape = new b2CircleShape(); 48 | shape.m_radius = 1.0f; 49 | 50 | b2FixtureDef fd; 51 | fd.shape = shape; 52 | fd.density = 1.0f; 53 | 54 | float32 restitution[7] = [0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f]; 55 | 56 | for (int32 i = 0; i < 7; ++i) 57 | { 58 | b2BodyDef bd; 59 | bd.type = b2_dynamicBody; 60 | bd.position.Set(-10.0f + 3.0f * i, 20.0f); 61 | 62 | b2Body* body_ = m_world.CreateBody(&bd); 63 | 64 | fd.restitution = restitution[i]; 65 | body_.CreateFixture(&fd); 66 | } 67 | } 68 | } 69 | 70 | static Test Create() 71 | { 72 | return new typeof(this); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /examples/demo/tests/verticalstack.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module tests.verticalstack; 19 | 20 | import core.stdc.math; 21 | 22 | import std.string; 23 | import std.typecons; 24 | 25 | import deimos.glfw.glfw3; 26 | 27 | import dbox; 28 | 29 | import framework.debug_draw; 30 | import framework.test; 31 | 32 | class VerticalStack : Test 33 | { 34 | enum 35 | { 36 | e_columnCount = 1, 37 | e_rowCount = 15 38 | 39 | // e_columnCount = 1, 40 | // e_rowCount = 1 41 | } 42 | 43 | 44 | this() 45 | { 46 | { 47 | b2BodyDef bd; 48 | b2Body* ground = m_world.CreateBody(&bd); 49 | 50 | auto shape = new b2EdgeShape(); 51 | shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); 52 | ground.CreateFixture(shape, 0.0f); 53 | 54 | shape.Set(b2Vec2(20.0f, 0.0f), b2Vec2(20.0f, 20.0f)); 55 | ground.CreateFixture(shape, 0.0f); 56 | } 57 | 58 | float32 xs[5] = [0.0f, -10.0f, -5.0f, 5.0f, 10.0f]; 59 | 60 | for (int32 j = 0; j < e_columnCount; ++j) 61 | { 62 | auto shape = new b2PolygonShape(); 63 | shape.SetAsBox(0.5f, 0.5f); 64 | 65 | b2FixtureDef fd; 66 | fd.shape = shape; 67 | fd.density = 1.0f; 68 | fd.friction = 0.3f; 69 | 70 | for (int i = 0; i < e_rowCount; ++i) 71 | { 72 | b2BodyDef bd; 73 | bd.type = b2_dynamicBody; 74 | 75 | int32 n = j * e_rowCount + i; 76 | assert(n < e_rowCount * e_columnCount); 77 | m_indices[n] = n; 78 | bd.userData = m_indices.ptr + n; 79 | 80 | float32 x = 0.0f; 81 | 82 | // float32 x = RandomFloat(-0.02f, 0.02f); 83 | // float32 x = i % 2 == 0 ? -0.01f : 0.01f; 84 | bd.position.Set(xs[j] + x, 0.55f + 1.1f * i); 85 | b2Body* body_ = m_world.CreateBody(&bd); 86 | 87 | m_bodies[n] = body_; 88 | 89 | body_.CreateFixture(&fd); 90 | } 91 | } 92 | 93 | m_bullet = null; 94 | } 95 | 96 | override void Keyboard(int key) 97 | { 98 | switch (key) 99 | { 100 | case GLFW_KEY_COMMA: 101 | 102 | if (m_bullet != null) 103 | { 104 | m_world.DestroyBody(m_bullet); 105 | m_bullet = null; 106 | } 107 | 108 | { 109 | auto shape = new b2CircleShape(); 110 | shape.m_radius = 0.25f; 111 | 112 | b2FixtureDef fd; 113 | fd.shape = shape; 114 | fd.density = 20.0f; 115 | fd.restitution = 0.05f; 116 | 117 | b2BodyDef bd; 118 | bd.type = b2_dynamicBody; 119 | bd.bullet = true; 120 | bd.position.Set(-31.0f, 5.0f); 121 | 122 | m_bullet = m_world.CreateBody(&bd); 123 | m_bullet.CreateFixture(&fd); 124 | 125 | m_bullet.SetLinearVelocity(b2Vec2(400.0f, 0.0f)); 126 | } 127 | break; 128 | 129 | case GLFW_KEY_B: 130 | g_blockSolve = !g_blockSolve; 131 | break; 132 | 133 | default: 134 | } 135 | } 136 | 137 | override void Step(Settings* settings) 138 | { 139 | super.Step(settings); 140 | 141 | g_debugDraw.DrawString(5, m_textLine, "Press: (,) to launch a bullet."); 142 | m_textLine += DRAW_STRING_NEW_LINE; 143 | g_debugDraw.DrawString(5, m_textLine, format("Blocksolve = %d", g_blockSolve)); 144 | 145 | // if (m_stepCount == 300) 146 | // { 147 | // if (m_bullet != null) 148 | // { 149 | // m_world.DestroyBody(m_bullet); 150 | // m_bullet = null; 151 | // } 152 | 153 | // { 154 | // b2CircleShape shape; 155 | // shape.m_radius = 0.25f; 156 | 157 | // b2FixtureDef fd; 158 | // fd.shape = shape; 159 | // fd.density = 20.0f; 160 | // fd.restitution = 0.05f; 161 | 162 | // b2BodyDef bd; 163 | // bd.type = b2_dynamicBody; 164 | // bd.bullet = true; 165 | // bd.position.Set(-31.0f, 5.0f); 166 | 167 | // m_bullet = m_world.CreateBody(&bd); 168 | // m_bullet.CreateFixture(&fd); 169 | 170 | // m_bullet.SetLinearVelocity(b2Vec2(400.0f, 0.0f)); 171 | // } 172 | // } 173 | } 174 | 175 | b2Body* m_bullet; 176 | b2Body*[e_rowCount * e_columnCount] m_bodies; 177 | int32[e_rowCount * e_columnCount] m_indices; 178 | 179 | static Test Create() 180 | { 181 | return new typeof(this); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /examples/hello_world/dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello_world", 3 | 4 | "description": "hello_world", 5 | 6 | "authors": [ 7 | "Erin Catto", 8 | "Andrej Mitrovic" 9 | ], 10 | 11 | "homepage": "https://github.com/d-gamedev-team/dbox", 12 | 13 | "copyright": "Copyright (c) 2006-2009 Erin Catto http://www.box2d.org", 14 | 15 | "license": "zlib", 16 | 17 | "buildOptions": ["debugInfo"], 18 | 19 | "buildRequirements": ["silenceDeprecations"], 20 | 21 | "targetName" : "hello_world", 22 | 23 | "targetPath" : "../../bin", 24 | 25 | "targetType": "executable", 26 | 27 | "sourceFiles": ["hello_world.d"], 28 | 29 | "mainSourceFile": "hello_world.d", 30 | 31 | "dependencies": { 32 | "dbox": {"path": "../../", "version": "~master"}, 33 | "glfw-drey": ">=0.0.2", 34 | "glwtf-drey": ">=0.0.1", 35 | }, 36 | } 37 | -------------------------------------------------------------------------------- /examples/hello_world/hello_world.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | 19 | module hello_world; 20 | 21 | import std.exception; 22 | import std.stdio; 23 | 24 | import dbox; 25 | 26 | // This is a simple example of building and running a simulation 27 | // using Box2D. Here we create a large ground box and a small dynamic 28 | // box. 29 | // There are no graphics for this example. Box2D is meant to be used 30 | // with your rendering engine in your game engine. 31 | void main() 32 | { 33 | // Define the gravity vector. 34 | b2Vec2 gravity = b2Vec2(0.0f, -10.0f); 35 | 36 | // Construct a world object, which will hold and simulate the rigid bodies. 37 | b2World world = b2World(gravity); 38 | 39 | // Define the ground body. 40 | b2BodyDef groundBodyDef; 41 | groundBodyDef.position.Set(0.0f, -10.0f); 42 | 43 | // Call the body factory which allocates memory for the ground body 44 | // from a pool and creates the ground box shape (also from a pool). 45 | // The body is also added to the world. 46 | b2Body* groundBody = enforce(world.CreateBody(&groundBodyDef)); 47 | 48 | // Define the ground box shape. 49 | b2PolygonShape groundBox = new b2PolygonShape; 50 | 51 | // The extents are the half-widths of the box. 52 | groundBox.SetAsBox(50.0f, 10.0f); 53 | 54 | // Add the ground fixture to the ground body. 55 | groundBody.CreateFixture(groundBox, 0.0f); 56 | 57 | // Define the dynamic body. We set its position and call the body factory. 58 | b2BodyDef bodyDef; 59 | bodyDef.type = b2_dynamicBody; 60 | bodyDef.position.Set(0.0f, 4.0f); 61 | b2Body* worldBody = world.CreateBody(&bodyDef); 62 | 63 | // Define another box shape for our dynamic body. 64 | b2PolygonShape dynamicBox = new b2PolygonShape; 65 | dynamicBox.SetAsBox(1.0f, 1.0f); 66 | 67 | // Define the dynamic body fixture. 68 | b2FixtureDef fixtureDef; 69 | fixtureDef.shape = dynamicBox; 70 | 71 | // Set the box density to be non-zero, so it will be dynamic. 72 | fixtureDef.density = 1.0f; 73 | 74 | // Override the default friction. 75 | fixtureDef.friction = 0.3f; 76 | 77 | // Add the shape to the body. 78 | worldBody.CreateFixture(&fixtureDef); 79 | 80 | // Prepare for simulation. Typically we use a time step of 1/60 of a 81 | // second (60Hz) and 10 iterations. This provides a high quality simulation 82 | // in most game scenarios. 83 | float32 timeStep = 1.0f / 60.0f; 84 | int32 velocityIterations = 6; 85 | int32 positionIterations = 2; 86 | 87 | // This is our little game loop. 88 | for (int32 i = 0; i < 60; ++i) 89 | { 90 | // Instruct the world to perform a single step of simulation. 91 | // It is generally best to keep the time step and iterations fixed. 92 | world.Step(timeStep, velocityIterations, positionIterations); 93 | 94 | // Now print the position and angle of the body. 95 | b2Vec2 position = worldBody.GetPosition(); 96 | float32 angle = worldBody.GetAngle(); 97 | 98 | printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle); 99 | } 100 | 101 | // When the world destructor is called, all bodies and joints are freed. This can 102 | // create orphaned pointers, so be careful about your world management. 103 | } 104 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006-2013 Erin Catto http://www.gphysics.com 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and must not be 16 | misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source distribution. 18 | 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # dbox 2 | 3 | ![dbox](https://raw.github.com/d-gamedev-team/dbox/master/screenshot/dbox.png) 4 | 5 | This is a D port of the [Box2D] game physics library. 6 | 7 | Currently **dbox** targets Box2D version **2.3.1**. 8 | 9 | [Box2D] was created by [Erin Catto]. 10 | 11 | Homepage: https://github.com/d-gamedev-team/dbox 12 | 13 | ## Supported compiler versions 14 | 15 | Compilers based on the **v2.066** front-end can be used to build and run dbox. 16 | 17 | ## Examples 18 | 19 | Use [dub] to build and run the examples: 20 | 21 | ``` 22 | # A console example of the physics engine being run. 23 | $ dub run dbox:hello_world 24 | 25 | # An interactive GUI test-suite containing dozens of examples. 26 | $ dub run dbox:demo 27 | ``` 28 | 29 | ## Runtime Requirements 30 | 31 | ### Windows and Linux 32 | 33 | You will need to install the [glfw] shared library in order to run the demo. 34 | 35 | ### OSX 36 | 37 | You can try installing the [glfw] v3 library via the brew package manager: 38 | 39 | ``` 40 | brew tap homebrew/versions 41 | brew install glfw3 42 | ``` 43 | 44 | ## Documentation 45 | 46 | Documentation is coming soon. 47 | 48 | ## Building dbox as a static library 49 | 50 | Run [dub] alone in the root project directory to build **dbox** as a static library: 51 | 52 | ``` 53 | $ dub 54 | ``` 55 | 56 | ## Links 57 | 58 | - The [Box2D homepage][Box2D]. 59 | - The [Box2D repository][Box2D_Repo]. 60 | 61 | ## License 62 | 63 | Distributed under the [zlib] license. 64 | 65 | See the accompanying file [license.txt][zlib]. 66 | 67 | [Erin Catto]: http://www.gphysics.com 68 | [dub]: http://code.dlang.org/ 69 | [Box2D]: http://box2d.org/ 70 | [Box2D_Repo]: http://code.google.com/p/box2d/ 71 | [zlib]: https://raw.github.com/d-gamedev-team/dbox/master/license.txt 72 | [glfw]: http://www.glfw.org/ 73 | -------------------------------------------------------------------------------- /screenshot/dbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d-gamedev-team/dbox/6f81fe065abec1e7def44fc777c5d8e9da936104/screenshot/dbox.png -------------------------------------------------------------------------------- /src/dbox/collision/b2collidecircle.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.collision.b2collidecircle; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.common; 25 | import dbox.collision; 26 | import dbox.collision.shapes; 27 | 28 | /// Compute the collision manifold between two circles. 29 | void b2CollideCircles( 30 | b2Manifold* manifold, 31 | const(b2CircleShape) circleA, b2Transform xfA, 32 | const(b2CircleShape) circleB, b2Transform xfB) 33 | { 34 | manifold.pointCount = 0; 35 | 36 | b2Vec2 pA = b2Mul(xfA, circleA.m_p); 37 | b2Vec2 pB = b2Mul(xfB, circleB.m_p); 38 | 39 | b2Vec2 d = pB - pA; 40 | float32 distSqr = b2Dot(d, d); 41 | float32 rA = circleA.m_radius, rB = circleB.m_radius; 42 | float32 radius = rA + rB; 43 | 44 | if (distSqr > radius * radius) 45 | { 46 | return; 47 | } 48 | 49 | manifold.type = b2Manifold.Type.e_circles; 50 | manifold.localPoint = circleA.m_p; 51 | manifold.localNormal.SetZero(); 52 | manifold.pointCount = 1; 53 | 54 | manifold.points[0].localPoint = circleB.m_p; 55 | manifold.points[0].id.key = 0; 56 | } 57 | 58 | /// Compute the collision manifold between a polygon and a circle. 59 | void b2CollidePolygonAndCircle( 60 | b2Manifold* manifold, 61 | const(b2PolygonShape) polygonA, b2Transform xfA, 62 | const(b2CircleShape) circleB, b2Transform xfB) 63 | { 64 | manifold.pointCount = 0; 65 | 66 | // Compute circle position in the frame of the polygon. 67 | b2Vec2 c = b2Mul(xfB, circleB.m_p); 68 | b2Vec2 cLocal = b2MulT(xfA, c); 69 | 70 | // Find the min separating edge. 71 | int32 normalIndex = 0; 72 | float32 separation = -b2_maxFloat; 73 | float32 radius = polygonA.m_radius + circleB.m_radius; 74 | int32 vertexCount = polygonA.m_count; 75 | const(b2Vec2)* vertices = polygonA.m_vertices.ptr; 76 | const(b2Vec2)* normals = polygonA.m_normals.ptr; 77 | 78 | for (int32 i = 0; i < vertexCount; ++i) 79 | { 80 | float32 s = b2Dot(normals[i], cLocal - vertices[i]); 81 | 82 | if (s > radius) 83 | { 84 | // Early out. 85 | return; 86 | } 87 | 88 | if (s > separation) 89 | { 90 | separation = s; 91 | normalIndex = i; 92 | } 93 | } 94 | 95 | // Vertices that subtend the incident face. 96 | int32 vertIndex1 = normalIndex; 97 | int32 vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0; 98 | b2Vec2 v1 = vertices[vertIndex1]; 99 | b2Vec2 v2 = vertices[vertIndex2]; 100 | 101 | // If the center is inside the polygon ... 102 | if (separation < b2_epsilon) 103 | { 104 | manifold.pointCount = 1; 105 | manifold.type = b2Manifold.Type.e_faceA; 106 | manifold.localNormal = normals[normalIndex]; 107 | manifold.localPoint = 0.5f * (v1 + v2); 108 | manifold.points[0].localPoint = circleB.m_p; 109 | manifold.points[0].id.key = 0; 110 | return; 111 | } 112 | 113 | // Compute barycentric coordinates 114 | float32 u1 = b2Dot(cLocal - v1, v2 - v1); 115 | float32 u2 = b2Dot(cLocal - v2, v1 - v2); 116 | 117 | if (u1 <= 0.0f) 118 | { 119 | if (b2DistanceSquared(cLocal, v1) > radius * radius) 120 | { 121 | return; 122 | } 123 | 124 | manifold.pointCount = 1; 125 | manifold.type = b2Manifold.Type.e_faceA; 126 | manifold.localNormal = cLocal - v1; 127 | manifold.localNormal.Normalize(); 128 | manifold.localPoint = v1; 129 | manifold.points[0].localPoint = circleB.m_p; 130 | manifold.points[0].id.key = 0; 131 | } 132 | else if (u2 <= 0.0f) 133 | { 134 | if (b2DistanceSquared(cLocal, v2) > radius * radius) 135 | { 136 | return; 137 | } 138 | 139 | manifold.pointCount = 1; 140 | manifold.type = b2Manifold.Type.e_faceA; 141 | manifold.localNormal = cLocal - v2; 142 | manifold.localNormal.Normalize(); 143 | manifold.localPoint = v2; 144 | manifold.points[0].localPoint = circleB.m_p; 145 | manifold.points[0].id.key = 0; 146 | } 147 | else 148 | { 149 | b2Vec2 faceCenter = 0.5f * (v1 + v2); 150 | float32 separation2 = b2Dot(cLocal - faceCenter, normals[vertIndex1]); 151 | 152 | if (separation2 > radius) 153 | { 154 | return; 155 | } 156 | 157 | manifold.pointCount = 1; 158 | manifold.type = b2Manifold.Type.e_faceA; 159 | manifold.localNormal = normals[vertIndex1]; 160 | manifold.localPoint = faceCenter; 161 | manifold.points[0].localPoint = circleB.m_p; 162 | manifold.points[0].id.key = 0; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/dbox/collision/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.collision; 19 | 20 | public 21 | { 22 | import dbox.collision.b2broadphase; 23 | import dbox.collision.b2collidecircle; 24 | import dbox.collision.b2collideedge; 25 | import dbox.collision.b2collidepolygon; 26 | import dbox.collision.b2collision; 27 | import dbox.collision.b2distance; 28 | import dbox.collision.b2dynamictree; 29 | import dbox.collision.b2timeofimpact; 30 | } 31 | -------------------------------------------------------------------------------- /src/dbox/collision/shapes/b2circleshape.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.collision.shapes.b2circleshape; 19 | 20 | import dbox.common; 21 | 22 | import dbox.collision; 23 | import dbox.collision.shapes; 24 | 25 | /// A circle shape. 26 | class b2CircleShape : b2Shape 27 | { 28 | /// 29 | this() 30 | { 31 | m_type = e_circle; 32 | m_radius = 0.0f; 33 | m_p.SetZero(); 34 | } 35 | 36 | /// Implement b2Shape. 37 | override b2CircleShape Clone(b2BlockAllocator* allocator) const 38 | { 39 | void* mem = allocator.Allocate(b2memSizeOf!b2CircleShape); 40 | b2CircleShape clone = b2emplace!b2CircleShape(mem); 41 | 42 | // clone.tupleof = this.tupleof; 43 | // Note: see Issue 12791: .tupleof does not take base class fields into account 44 | // https://issues.dlang.org/show_bug.cgi?id=12791 45 | clone.m_type = this.m_type; 46 | clone.m_radius = this.m_radius; 47 | 48 | clone.m_p = this.m_p; 49 | 50 | return clone; 51 | } 52 | 53 | /// @see b2Shape.GetChildCount 54 | override int32 GetChildCount() const 55 | { 56 | return 1; 57 | } 58 | 59 | /// Implement b2Shape. 60 | override bool TestPoint(b2Transform transform, b2Vec2 p) const 61 | { 62 | b2Vec2 center = transform.p + b2Mul(transform.q, m_p); 63 | b2Vec2 d = p - center; 64 | return b2Dot(d, d) <= m_radius * m_radius; 65 | } 66 | 67 | // Collision Detection in Interactive 3D Environments by Gino van den Bergen 68 | // From Section 3.1.2 69 | // x = s + a * r 70 | // norm(x) = radius 71 | /// Implement b2Shape. 72 | override bool RayCast(b2RayCastOutput* output, b2RayCastInput input, 73 | b2Transform transform, int32 childIndex) const 74 | { 75 | B2_NOT_USED(childIndex); 76 | 77 | b2Vec2 position = transform.p + b2Mul(transform.q, m_p); 78 | b2Vec2 s = input.p1 - position; 79 | float32 b = b2Dot(s, s) - m_radius * m_radius; 80 | 81 | // Solve quadratic equation. 82 | b2Vec2 r = input.p2 - input.p1; 83 | float32 c = b2Dot(s, r); 84 | float32 rr = b2Dot(r, r); 85 | float32 sigma = c * c - rr * b; 86 | 87 | // Check for negative discriminant and short segment. 88 | if (sigma < 0.0f || rr < b2_epsilon) 89 | { 90 | return false; 91 | } 92 | 93 | // Find the point of intersection of the line with the circle. 94 | float32 a = -(c + b2Sqrt(sigma)); 95 | 96 | // Is the intersection point on the segment? 97 | if (0.0f <= a && a <= input.maxFraction * rr) 98 | { 99 | a /= rr; 100 | output.fraction = a; 101 | output.normal = s + a * r; 102 | output.normal.Normalize(); 103 | return true; 104 | } 105 | 106 | return false; 107 | } 108 | 109 | /// @see b2Shape.ComputeAABB 110 | override void ComputeAABB(b2AABB* aabb, b2Transform transform, int32 childIndex) const 111 | { 112 | B2_NOT_USED(childIndex); 113 | 114 | b2Vec2 p = transform.p + b2Mul(transform.q, m_p); 115 | aabb.lowerBound.Set(p.x - m_radius, p.y - m_radius); 116 | aabb.upperBound.Set(p.x + m_radius, p.y + m_radius); 117 | } 118 | 119 | /// @see b2Shape.ComputeMass 120 | override void ComputeMass(b2MassData* massData, float32 density) const 121 | { 122 | massData.mass = density * b2_pi * m_radius * m_radius; 123 | massData.center = m_p; 124 | 125 | // inertia about the local origin 126 | massData.I = massData.mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p)); 127 | } 128 | 129 | /// Get the supporting vertex index in the given direction. 130 | int32 GetSupport(b2Vec2 d) const 131 | { 132 | B2_NOT_USED(d); 133 | return 0; 134 | } 135 | 136 | /// Get the supporting vertex in the given direction. 137 | b2Vec2 GetSupportVertex(b2Vec2 d) const 138 | { 139 | B2_NOT_USED(d); 140 | return m_p; 141 | } 142 | 143 | /// Get the vertex count. 144 | int32 GetVertexCount() const 145 | { 146 | return 1; 147 | } 148 | 149 | /// Get a vertex by index. Used by b2Distance. 150 | b2Vec2 GetVertex(int32 index) const 151 | { 152 | B2_NOT_USED(index); 153 | assert(index == 0); 154 | return m_p; 155 | } 156 | 157 | /// Position 158 | b2Vec2 m_p; 159 | } 160 | -------------------------------------------------------------------------------- /src/dbox/collision/shapes/b2shape.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.collision.shapes.b2shape; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.common; 25 | import dbox.collision; 26 | 27 | /// This holds the mass data computed for a shape. 28 | struct b2MassData 29 | { 30 | /// The mass of the shape, usually in kilograms. 31 | float32 mass = 0; 32 | 33 | /// The position of the shape's centroid relative to the shape's origin. 34 | b2Vec2 center; 35 | 36 | /// The rotational inertia of the shape about the local origin. 37 | float32 I = 0; 38 | } 39 | 40 | /// A shape is used for collision detection. You can create a shape however you like. 41 | /// Shapes used for simulation in b2World* are created automatically when a b2Fixture* 42 | /// is created. Shapes may encapsulate a one or more child shapes. 43 | class b2Shape 44 | { 45 | enum Type 46 | { 47 | e_circle = 0, 48 | e_edge = 1, 49 | e_polygon = 2, 50 | e_chain = 3, 51 | e_typeCount = 4 52 | } 53 | 54 | alias e_circle = Type.e_circle; 55 | alias e_edge = Type.e_edge; 56 | alias e_polygon = Type.e_polygon; 57 | alias e_chain = Type.e_chain; 58 | alias e_typeCount = Type.e_typeCount; 59 | 60 | /// Clone the concrete shape using the provided allocator. 61 | abstract b2Shape Clone(b2BlockAllocator* allocator) const; 62 | 63 | /// Get the type of this shape. You can use this to down cast to the concrete shape. 64 | /// @return the shape type. 65 | Type GetType() const 66 | { 67 | return m_type; 68 | } 69 | 70 | /// Get the number of child primitives. 71 | abstract int32 GetChildCount() const; 72 | 73 | /// Test a point for containment in this shape. This only works for convex shapes. 74 | /// @param xf the shape world transform. 75 | /// @param p a point in world coordinates. 76 | abstract bool TestPoint(b2Transform xf, b2Vec2 p) const; 77 | 78 | /// Cast a ray against a child shape. 79 | /// @param output the ray-cast results. 80 | /// @param input the ray-cast input parameters. 81 | /// @param transform the transform to be applied to the shape. 82 | /// @param childIndex the child shape index 83 | abstract bool RayCast(b2RayCastOutput* output, b2RayCastInput input, 84 | b2Transform transform, int32 childIndex) const; 85 | 86 | /// Given a transform, compute the associated axis aligned bounding box for a child shape. 87 | /// @param aabb returns the axis aligned box. 88 | /// @param xf the world transform of the shape. 89 | /// @param childIndex the child shape 90 | abstract void ComputeAABB(b2AABB* aabb, b2Transform xf, int32 childIndex) const; 91 | 92 | /// Compute the mass properties of this shape using its dimensions and density. 93 | /// The inertia tensor is computed about the local origin. 94 | /// @param massData returns the mass data for this shape. 95 | /// @param density the density in kilograms per meter squared. 96 | abstract void ComputeMass(b2MassData* massData, float32 density) const; 97 | 98 | Type m_type; 99 | float32 m_radius = 0; 100 | } 101 | -------------------------------------------------------------------------------- /src/dbox/collision/shapes/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.collision.shapes; 19 | 20 | public 21 | { 22 | import dbox.collision.shapes.b2chainshape; 23 | import dbox.collision.shapes.b2circleshape; 24 | import dbox.collision.shapes.b2edgeshape; 25 | import dbox.collision.shapes.b2polygonshape; 26 | import dbox.collision.shapes.b2shape; 27 | } 28 | -------------------------------------------------------------------------------- /src/dbox/common/b2draw.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Erin Catto http://box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.common.b2draw; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.common; 25 | import dbox.common.b2math; 26 | 27 | /// Color for debug drawing. Each value has the range [0,1]. 28 | struct b2Color 29 | { 30 | void Set(float32 ri, float32 gi, float32 bi, float32 ai = 1.0f) 31 | { 32 | r = ri; 33 | g = gi; 34 | b = bi; 35 | a = ai; 36 | } 37 | 38 | float32 r = 0, g = 0, b = 0, a = 1.0f; 39 | } 40 | 41 | /// Implement and register this class with a b2World* to provide debug drawing of physics 42 | /// entities in your game. 43 | class b2Draw 44 | { 45 | enum 46 | { 47 | e_shapeBit = 0x0001, ///< draw shapes 48 | e_jointBit = 0x0002, ///< draw joint connections 49 | e_aabbBit = 0x0004, ///< draw axis aligned bounding boxes 50 | e_pairBit = 0x0008, ///< draw broad-phase pairs 51 | e_centerOfMassBit = 0x0010 ///< draw center of mass frame 52 | } 53 | 54 | /// Set the drawing flags. 55 | void SetFlags(uint32 flags) 56 | { 57 | m_drawFlags = flags; 58 | } 59 | 60 | /// Get the drawing flags. 61 | uint32 GetFlags() const 62 | { 63 | return m_drawFlags; 64 | } 65 | 66 | /// Append flags to the current flags. 67 | void AppendFlags(uint32 flags) 68 | { 69 | m_drawFlags |= flags; 70 | } 71 | 72 | /// Clear flags from the current flags. 73 | void ClearFlags(uint32 flags) 74 | { 75 | m_drawFlags &= ~flags; 76 | } 77 | 78 | /// Draw a closed polygon provided in CCW order. 79 | abstract void DrawPolygon(const(b2Vec2)* vertices, int32 vertexCount, b2Color color); 80 | 81 | /// Draw a solid closed polygon provided in CCW order. 82 | abstract void DrawSolidPolygon(const(b2Vec2)* vertices, int32 vertexCount, b2Color color); 83 | 84 | /// Draw a circle. 85 | abstract void DrawCircle(b2Vec2 center, float32 radius, b2Color color); 86 | 87 | /// Draw a solid circle. 88 | abstract void DrawSolidCircle(b2Vec2 center, float32 radius, b2Vec2 axis, b2Color color); 89 | 90 | /// Draw a line segment. 91 | abstract void DrawSegment(b2Vec2 p1, b2Vec2 p2, b2Color color); 92 | 93 | /// Draw a transform. Choose your own length scale. 94 | /// @param xf a transform. 95 | abstract void DrawTransform(b2Transform xf); 96 | 97 | protected: 98 | uint32 m_drawFlags; 99 | } 100 | -------------------------------------------------------------------------------- /src/dbox/common/b2growablestack.d: -------------------------------------------------------------------------------- 1 | module dbox.common.b2growablestack; 2 | 3 | import core.stdc.float_; 4 | import core.stdc.stdlib; 5 | import core.stdc.string; 6 | 7 | import dbox.common; 8 | import dbox.common.b2math; 9 | 10 | /* 11 | * Copyright (c) 2010 Erin Catto http://www.box2d.org 12 | * 13 | * This software is provided 'as-is', without any express or implied 14 | * warranty. In no event will the authors be held liable for any damages 15 | * arising from the use of this software. 16 | * Permission is granted to anyone to use this software for any purpose, 17 | * including commercial applications, and to alter it and redistribute it 18 | * freely, subject to the following restrictions: 19 | * 1. The origin of this software must not be misrepresented; you must not 20 | * claim that you wrote the original software. If you use this software 21 | * in a product, an acknowledgment in the product documentation would be 22 | * appreciated but is not required. 23 | * 2. Altered source versions must be plainly marked as such, and must not be 24 | * misrepresented as being the original software. 25 | * 3. This notice may not be removed or altered from any source distribution. 26 | */ 27 | 28 | // #ifndef B2_GROWABLE_STACK_H 29 | // #define B2_GROWABLE_STACK_H 30 | import dbox.common; 31 | 32 | /// This is a growable LIFO stack with an initial capacity of N. 33 | /// If the stack size exceeds the initial capacity, the heap is used 34 | /// to increase the size of the stack. 35 | struct b2GrowableStack(T, int32 N) 36 | { 37 | @disable this(); 38 | @disable this(this); 39 | 40 | this(int) 41 | { 42 | m_stack = m_array.ptr; 43 | } 44 | 45 | T* m_stack; 46 | 47 | ~this() 48 | { 49 | if (m_stack != m_array.ptr) 50 | { 51 | b2Free(m_stack); 52 | m_stack = null; 53 | } 54 | } 55 | 56 | void Push(T element) 57 | { 58 | if (m_count == m_capacity) 59 | { 60 | T* old = m_stack; 61 | m_capacity *= 2; 62 | 63 | static if (is(T == class)) 64 | enum size = b2memSizeOf!T; 65 | else 66 | enum size = b2memSizeOf!T; 67 | 68 | m_stack = cast(T*)b2Alloc(m_capacity * size); 69 | memcpy(m_stack, old, m_count * size); 70 | 71 | if (old != m_array.ptr) 72 | { 73 | b2Free(old); 74 | } 75 | } 76 | 77 | m_stack[m_count] = element; 78 | ++m_count; 79 | } 80 | 81 | T Pop() 82 | { 83 | assert(m_count > 0); 84 | --m_count; 85 | return m_stack[m_count]; 86 | } 87 | 88 | int32 GetCount() 89 | { 90 | return m_count; 91 | } 92 | 93 | /* private */ 94 | T[N] m_array; 95 | int32 m_count; 96 | int32 m_capacity = N; 97 | } 98 | -------------------------------------------------------------------------------- /src/dbox/common/b2stackallocator.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.common.b2stackallocator; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.common; 25 | 26 | const int32 b2_stackSize = 100 * 1024; // 100k 27 | const int32 b2_maxStackEntries = 32; 28 | 29 | struct b2StackEntry 30 | { 31 | byte* data; 32 | size_t size; 33 | bool usedMalloc; 34 | } 35 | 36 | // This is a stack allocator used for fast per step allocations. 37 | // You must nest allocate/free pairs. The code will assert 38 | // if you try to interleave multiple allocate/free pairs. 39 | struct b2StackAllocator 40 | { 41 | /// 42 | ~this() 43 | { 44 | assert(m_index == 0); 45 | assert(m_entryCount == 0); 46 | } 47 | 48 | /// 49 | void* Allocate(size_t size) 50 | { 51 | assert(m_entryCount < b2_maxStackEntries); 52 | 53 | b2StackEntry* entry = m_entries.ptr + m_entryCount; 54 | entry.size = size; 55 | 56 | if (m_index + size > b2_stackSize) 57 | { 58 | entry.data = cast(byte*)b2Alloc(size); 59 | entry.usedMalloc = true; 60 | } 61 | else 62 | { 63 | entry.data = m_data.ptr + m_index; 64 | entry.usedMalloc = false; 65 | m_index += size; 66 | } 67 | 68 | m_allocation += size; 69 | m_maxAllocation = b2Max(m_maxAllocation, m_allocation); 70 | ++m_entryCount; 71 | 72 | return entry.data; 73 | } 74 | 75 | /// 76 | void Free(void* p) 77 | { 78 | assert(m_entryCount > 0); 79 | b2StackEntry* entry = m_entries.ptr + m_entryCount - 1; 80 | assert(p == entry.data); 81 | 82 | if (entry.usedMalloc) 83 | { 84 | b2Free(p); 85 | } 86 | else 87 | { 88 | m_index -= entry.size; 89 | } 90 | m_allocation -= entry.size; 91 | --m_entryCount; 92 | 93 | p = null; 94 | } 95 | 96 | /// 97 | int32 GetMaxAllocation() const 98 | { 99 | return m_maxAllocation; 100 | } 101 | 102 | private: 103 | 104 | byte[b2_stackSize] m_data; 105 | int32 m_index; 106 | 107 | int32 m_allocation; 108 | int32 m_maxAllocation; 109 | 110 | b2StackEntry[b2_maxStackEntries] m_entries; 111 | int32 m_entryCount; 112 | } 113 | -------------------------------------------------------------------------------- /src/dbox/common/b2timer.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Erin Catto http://box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.common.b2timer; 19 | 20 | import dbox.common; 21 | 22 | import std.datetime; 23 | 24 | /// Workaround for missing default struct ctors in D. 25 | @property B2Timer b2Timer() 26 | { 27 | return B2Timer(1); 28 | } 29 | 30 | /// Timer for profiling. This has platform specific code and may 31 | /// not work on every platform. 32 | struct B2Timer 33 | { 34 | /// This struct must be properly initialized with an explicit constructor. 35 | @disable this(); 36 | 37 | /// This struct cannot be copied. 38 | @disable this(this); 39 | 40 | /// Explicit constructor. 41 | /// Start the timer. 42 | this(int) 43 | { 44 | sw.start(); 45 | } 46 | 47 | /// Reset the timer. 48 | void Reset() 49 | { 50 | sw.reset(); 51 | } 52 | 53 | /// Get the time since construction or the last reset. 54 | float32 GetMilliseconds() const 55 | { 56 | return sw.peek.to!("msecs", float); 57 | } 58 | 59 | private: 60 | 61 | StopWatch sw; 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/common/b2util.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.common.b2util; 19 | 20 | import std.conv; 21 | 22 | /// Generic .sizeof alternative which works with both class and non-class types, 23 | /// which makes allocating the proper amount of memory for type instances safer. 24 | template b2memSizeOf(T) if (is(T == class)) 25 | { 26 | enum b2memSizeOf = __traits(classInstanceSize, T); 27 | } 28 | 29 | /// ditto. 30 | template b2memSizeOf(T) if (!is(T == class)) 31 | { 32 | enum b2memSizeOf = T.sizeof; 33 | } 34 | 35 | /// Emplace alternative which works with void* rather than void[]. 36 | T b2emplace(T)(void* chunk) if (is(T == class)) 37 | { 38 | return emplace!T(chunk[0 .. b2memSizeOf!T]); 39 | } 40 | 41 | /// ditto 42 | T* b2emplace(T)(void* chunk) if (!is(T == class)) 43 | { 44 | return emplace!T(chunk[0 .. b2memSizeOf!T]); 45 | } 46 | 47 | /// ditto 48 | T b2emplace(T, Args...)(void* chunk, Args args) if (is(T == class)) 49 | { 50 | return emplace!T(chunk[0 .. b2memSizeOf!T], args); 51 | } 52 | 53 | /// ditto 54 | T* b2emplace(T, Args...)(void* chunk, Args args) if (!is(T == class)) 55 | { 56 | return emplace!T(chunk[0 .. b2memSizeOf!T], args); 57 | } 58 | 59 | /** 60 | Export all enum members as aliases. This allows enums to be used as types 61 | and allows its members to be used as if they're defined in module scope. 62 | */ 63 | mixin template _ExportEnumMembers(E) if (is(E == enum)) 64 | { 65 | mixin(_makeEnumAliases!(E)()); 66 | } 67 | 68 | /// ditto 69 | string _makeEnumAliases(E)() if (is(E == enum)) 70 | { 71 | import std.array; 72 | import std.string; 73 | 74 | enum enumName = __traits(identifier, E); 75 | Appender!(string[]) result; 76 | 77 | foreach (string member; __traits(allMembers, E)) 78 | result ~= format("alias %s = %s.%s;", member, enumName, member); 79 | 80 | return result.data.join("\n"); 81 | } 82 | -------------------------------------------------------------------------------- /src/dbox/common/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.common; 19 | 20 | public 21 | { 22 | import dbox.common.b2blockallocator; 23 | import dbox.common.b2draw; 24 | import dbox.common.b2growablestack; 25 | import dbox.common.b2math; 26 | import dbox.common.b2settings; 27 | import dbox.common.b2stackallocator; 28 | import dbox.common.b2timer; 29 | import dbox.common.b2util; 30 | } 31 | -------------------------------------------------------------------------------- /src/dbox/dynamics/b2timestep.d: -------------------------------------------------------------------------------- 1 | module dbox.dynamics.b2timestep; 2 | 3 | import core.stdc.stdlib; 4 | import core.stdc.float_; 5 | import core.stdc.string; 6 | 7 | import dbox.common; 8 | import dbox.collision; 9 | import dbox.dynamics; 10 | import dbox.dynamics.contacts; 11 | import dbox.dynamics.joints; 12 | 13 | /* 14 | * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org 15 | * 16 | * This software is provided 'as-is', without any express or implied 17 | * warranty. In no event will the authors be held liable for any damages 18 | * arising from the use of this software. 19 | * Permission is granted to anyone to use this software for any purpose, 20 | * including commercial applications, and to alter it and redistribute it 21 | * freely, subject to the following restrictions: 22 | * 1. The origin of this software must not be misrepresented; you must not 23 | * claim that you wrote the original software. If you use this software 24 | * in a product, an acknowledgment in the product documentation would be 25 | * appreciated but is not required. 26 | * 2. Altered source versions must be plainly marked as such, and must not be 27 | * misrepresented as being the original software. 28 | * 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | // #ifndef B2_TIME_STEP_H 32 | // #define B2_TIME_STEP_H 33 | 34 | import dbox.common.b2math; 35 | 36 | /// Profiling data. Times are in milliseconds. 37 | struct b2Profile 38 | { 39 | float32 step = 0; 40 | float32 collide = 0; 41 | float32 solve = 0; 42 | float32 solveInit = 0; 43 | float32 solveVelocity = 0; 44 | float32 solvePosition = 0; 45 | float32 broadphase = 0; 46 | float32 solveTOI = 0; 47 | } 48 | 49 | /// This is an internal structure. 50 | struct b2TimeStep 51 | { 52 | float32 dt = 0; // time step 53 | float32 inv_dt = 0; // inverse time step (0 if dt == 0). 54 | float32 dtRatio = 0; // dt * inv_dt0 55 | int32 velocityIterations; 56 | int32 positionIterations; 57 | bool warmStarting; 58 | } 59 | 60 | /// This is an internal structure. 61 | struct b2Position 62 | { 63 | b2Vec2 c; 64 | float32 a = 0; 65 | } 66 | 67 | /// This is an internal structure. 68 | struct b2Velocity 69 | { 70 | b2Vec2 v; 71 | float32 w = 0; 72 | } 73 | 74 | /// Solver Data 75 | struct b2SolverData 76 | { 77 | b2TimeStep step; 78 | b2Position* positions; 79 | b2Velocity* velocities; 80 | } 81 | 82 | // #endif 83 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2chainandcirclecontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2chainandcirclecontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2ChainAndCircleContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2ChainAndCircleContact); 37 | return b2emplace!b2ChainAndCircleContact(mem, fixtureA, indexA, fixtureB, indexB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2ChainAndCircleContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB) 49 | { 50 | super(fixtureA, indexA, fixtureB, indexB); 51 | assert(m_fixtureA.GetType() == b2Shape.e_chain); 52 | assert(m_fixtureB.GetType() == b2Shape.e_circle); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2ChainShape chain = cast(b2ChainShape)m_fixtureA.GetShape(); 59 | 60 | import std.typecons; 61 | auto edge = scoped!b2EdgeShape(); 62 | 63 | chain.GetChildEdge(edge, m_indexA); 64 | b2CollideEdgeAndCircle(manifold, edge, xfA, 65 | cast(b2CircleShape)m_fixtureB.GetShape(), xfB); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2chainandpolygoncontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2chainandpolygoncontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2ChainAndPolygonContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2ChainAndPolygonContact); 37 | return b2emplace!b2ChainAndPolygonContact(mem, fixtureA, indexA, fixtureB, indexB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2ChainAndPolygonContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB) 49 | { 50 | super(fixtureA, indexA, fixtureB, indexB); 51 | assert(m_fixtureA.GetType() == b2Shape.e_chain); 52 | assert(m_fixtureB.GetType() == b2Shape.e_polygon); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2ChainShape chain = cast(b2ChainShape)m_fixtureA.GetShape(); 59 | 60 | import std.typecons; 61 | auto edge = scoped!b2EdgeShape(); 62 | 63 | chain.GetChildEdge(edge, m_indexA); 64 | b2CollideEdgeAndPolygon(manifold, edge, xfA, 65 | cast(b2PolygonShape)m_fixtureB.GetShape(), xfB); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2circlecontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2circlecontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2CircleContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2CircleContact); 37 | return b2emplace!b2CircleContact(mem, fixtureA, fixtureB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2CircleContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, b2Fixture* fixtureB) 49 | { 50 | super(fixtureA, 0, fixtureB, 0); 51 | assert(m_fixtureA.GetType() == b2Shape.e_circle); 52 | assert(m_fixtureB.GetType() == b2Shape.e_circle); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2CollideCircles(manifold, 59 | cast(b2CircleShape)m_fixtureA.GetShape(), xfA, 60 | cast(b2CircleShape)m_fixtureB.GetShape(), xfB); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2edgeandcirclecontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2edgeandcirclecontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2EdgeAndCircleContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2EdgeAndCircleContact); 37 | return b2emplace!b2EdgeAndCircleContact(mem, fixtureA, fixtureB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2EdgeAndCircleContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, b2Fixture* fixtureB) 49 | { 50 | super(fixtureA, 0, fixtureB, 0); 51 | assert(m_fixtureA.GetType() == b2Shape.e_edge); 52 | assert(m_fixtureB.GetType() == b2Shape.e_circle); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2CollideEdgeAndCircle(manifold, 59 | cast(b2EdgeShape)m_fixtureA.GetShape(), xfA, 60 | cast(b2CircleShape)m_fixtureB.GetShape(), xfB); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2edgeandpolygoncontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2edgeandpolygoncontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2EdgeAndPolygonContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2EdgeAndPolygonContact); 37 | return b2emplace!b2EdgeAndPolygonContact(mem, fixtureA, fixtureB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2EdgeAndPolygonContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, b2Fixture* fixtureB) 49 | { 50 | super(fixtureA, 0, fixtureB, 0); 51 | assert(m_fixtureA.GetType() == b2Shape.e_edge); 52 | assert(m_fixtureB.GetType() == b2Shape.e_polygon); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2CollideEdgeAndPolygon(manifold, 59 | cast(b2EdgeShape)m_fixtureA.GetShape(), xfA, 60 | cast(b2PolygonShape)m_fixtureB.GetShape(), xfB); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2polygonandcirclecontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2polygonandcirclecontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2PolygonAndCircleContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2PolygonAndCircleContact); 37 | return b2emplace!b2PolygonAndCircleContact(mem, fixtureA, fixtureB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2PolygonAndCircleContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, b2Fixture* fixtureB) 49 | { 50 | super(fixtureA, 0, fixtureB, 0); 51 | assert(m_fixtureA.GetType() == b2Shape.e_polygon); 52 | assert(m_fixtureB.GetType() == b2Shape.e_circle); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2CollidePolygonAndCircle(manifold, 59 | cast(b2PolygonShape)m_fixtureA.GetShape(), xfA, 60 | cast(b2CircleShape)m_fixtureB.GetShape(), xfB); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/b2polygoncontact.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts.b2polygoncontact; 19 | 20 | import core.stdc.float_; 21 | import core.stdc.stdlib; 22 | import core.stdc.string; 23 | 24 | import dbox.collision; 25 | import dbox.collision.shapes; 26 | import dbox.common; 27 | import dbox.dynamics; 28 | import dbox.dynamics.contacts; 29 | 30 | /// 31 | class b2PolygonContact : b2Contact 32 | { 33 | /// 34 | static b2Contact Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 35 | { 36 | void* mem = allocator.Allocate(b2memSizeOf!b2PolygonContact); 37 | return b2emplace!b2PolygonContact(mem, fixtureA, fixtureB); 38 | } 39 | 40 | /// 41 | static void Destroy(b2Contact contact, b2BlockAllocator* allocator) 42 | { 43 | destroy(contact); 44 | allocator.Free(cast(void*)contact, b2memSizeOf!b2PolygonContact); 45 | } 46 | 47 | /// 48 | this(b2Fixture* fixtureA, b2Fixture* fixtureB) 49 | { 50 | super(fixtureA, 0, fixtureB, 0); 51 | assert(m_fixtureA.GetType() == b2Shape.e_polygon); 52 | assert(m_fixtureB.GetType() == b2Shape.e_polygon); 53 | } 54 | 55 | /// 56 | override void Evaluate(b2Manifold* manifold, b2Transform xfA, b2Transform xfB) 57 | { 58 | b2CollidePolygons(manifold, 59 | cast(b2PolygonShape)m_fixtureA.GetShape(), xfA, 60 | cast(b2PolygonShape)m_fixtureB.GetShape(), xfB); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/dbox/dynamics/contacts/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.contacts; 19 | 20 | public 21 | { 22 | import dbox.dynamics.contacts.b2chainandcirclecontact; 23 | import dbox.dynamics.contacts.b2chainandpolygoncontact; 24 | import dbox.dynamics.contacts.b2circlecontact; 25 | import dbox.dynamics.contacts.b2contact; 26 | import dbox.dynamics.contacts.b2contactsolver; 27 | import dbox.dynamics.contacts.b2edgeandcirclecontact; 28 | import dbox.dynamics.contacts.b2edgeandpolygoncontact; 29 | import dbox.dynamics.contacts.b2polygonandcirclecontact; 30 | import dbox.dynamics.contacts.b2polygoncontact; 31 | } 32 | -------------------------------------------------------------------------------- /src/dbox/dynamics/joints/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics.joints; 19 | 20 | public 21 | { 22 | import dbox.dynamics.joints.b2distancejoint; 23 | import dbox.dynamics.joints.b2frictionjoint; 24 | import dbox.dynamics.joints.b2gearjoint; 25 | import dbox.dynamics.joints.b2joint; 26 | import dbox.dynamics.joints.b2motorjoint; 27 | import dbox.dynamics.joints.b2mousejoint; 28 | import dbox.dynamics.joints.b2prismaticjoint; 29 | import dbox.dynamics.joints.b2pulleyjoint; 30 | import dbox.dynamics.joints.b2revolutejoint; 31 | import dbox.dynamics.joints.b2ropejoint; 32 | import dbox.dynamics.joints.b2weldjoint; 33 | import dbox.dynamics.joints.b2wheeljoint; 34 | } 35 | -------------------------------------------------------------------------------- /src/dbox/dynamics/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.dynamics; 19 | 20 | public 21 | { 22 | import dbox.dynamics.b2body; 23 | import dbox.dynamics.b2contactmanager; 24 | import dbox.dynamics.b2fixture; 25 | import dbox.dynamics.b2island; 26 | import dbox.dynamics.b2timestep; 27 | import dbox.dynamics.b2world; 28 | import dbox.dynamics.b2worldcallbacks; 29 | } 30 | -------------------------------------------------------------------------------- /src/dbox/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2012 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox; 19 | 20 | public 21 | { 22 | import dbox.collision; 23 | import dbox.collision.shapes; 24 | import dbox.common; 25 | import dbox.dynamics; 26 | import dbox.dynamics.contacts; 27 | import dbox.dynamics.joints; 28 | import dbox.rope; 29 | } 30 | -------------------------------------------------------------------------------- /src/dbox/rope/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2012 Erin Catto http://www.box2d.org 3 | * 4 | * This software is provided 'as-is', without any express or implied 5 | * warranty. In no event will the authors be held liable for any damages 6 | * arising from the use of this software. 7 | * Permission is granted to anyone to use this software for any purpose, 8 | * including commercial applications, and to alter it and redistribute it 9 | * freely, subject to the following restrictions: 10 | * 1. The origin of this software must not be misrepresented; you must not 11 | * claim that you wrote the original software. If you use this software 12 | * in a product, an acknowledgment in the product documentation would be 13 | * appreciated but is not required. 14 | * 2. Altered source versions must be plainly marked as such, and must not be 15 | * misrepresented as being the original software. 16 | * 3. This notice may not be removed or altered from any source distribution. 17 | */ 18 | module dbox.rope; 19 | 20 | public 21 | { 22 | import dbox.rope.b2rope; 23 | } 24 | --------------------------------------------------------------------------------