├── .gitignore ├── CITATION.cff ├── CMakeLists.txt ├── LICENSE.txt ├── README.textile ├── TODO.txt ├── VERSION.txt ├── android └── Android.mk ├── codeblocks └── Chipmunk.cbp ├── demo ├── Bench.c ├── Buoyancy.c ├── CMakeLists.txt ├── Chains.c ├── ChipmunkDebugDraw.c ├── ChipmunkDebugDraw.h ├── ChipmunkDemo.c ├── ChipmunkDemo.h ├── ChipmunkDemoTextSupport.c ├── ChipmunkDemoTextSupport.h ├── ContactGraph.c ├── ContactPoints.c_ ├── Convex.c ├── Crane.c ├── GJK.c_ ├── GLEWConfig.cmake ├── GLFWConfig.cmake ├── Joints.c ├── LogoSmash.c ├── OneWay.c ├── Planet.c ├── Player.c ├── Plink.c ├── Pump.c ├── PyramidStack.c ├── PyramidTopple.c ├── Query.c ├── Shatter.c ├── Slice.c ├── Smooth.c_ ├── Springies.c ├── Sticky.c ├── Tank.c ├── TheoJansen.c ├── Tumble.c ├── Unicycle.c ├── VeraMoBI.ttf_sdf.h ├── VeraMoBd.ttf_sdf.h └── sokol │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── fips.yml │ ├── sokol.c │ ├── sokol.h │ ├── sokol.m │ ├── sokol_app.h │ ├── sokol_gfx.h │ └── sokol_time.h ├── doc-src ├── Doxyfile ├── MakeDocs.rb ├── chipmunk-docs.textile ├── doxygen_generator.rb ├── doxygen_main ├── extract_protos.rb └── upload_docs.sh ├── doc ├── examples.html ├── examples │ ├── BreakableJoint.html │ ├── CollisionCallback.html │ ├── Crushing.html │ ├── DynamicStatic.html │ ├── Hello Chipmunk.html │ ├── JointRecipies.html │ ├── Moments.html │ ├── PlaySoundOnCollision.html │ ├── Sleeping.html │ ├── cpConvexHull.html │ └── cpSpaceEachBody.html ├── images │ ├── hash_just_right.png │ ├── hash_too_big.png │ ├── hash_too_small.png │ ├── hms_logo.png │ └── logo1_med.png └── stylesheet.css ├── include └── chipmunk │ ├── chipmunk.h │ ├── chipmunk_ffi.h │ ├── chipmunk_private.h │ ├── chipmunk_structs.h │ ├── chipmunk_types.h │ ├── chipmunk_unsafe.h │ ├── cpArbiter.h │ ├── cpBB.h │ ├── cpBody.h │ ├── cpConstraint.h │ ├── cpDampedRotarySpring.h │ ├── cpDampedSpring.h │ ├── cpGearJoint.h │ ├── cpGrooveJoint.h │ ├── cpHastySpace.h │ ├── cpMarch.h │ ├── cpPinJoint.h │ ├── cpPivotJoint.h │ ├── cpPolyShape.h │ ├── cpPolyline.h │ ├── cpRatchetJoint.h │ ├── cpRobust.h │ ├── cpRotaryLimitJoint.h │ ├── cpShape.h │ ├── cpSimpleMotor.h │ ├── cpSlideJoint.h │ ├── cpSpace.h │ ├── cpSpatialIndex.h │ ├── cpTransform.h │ └── cpVect.h ├── msvc ├── README └── VS2015 │ ├── chipmunk │ ├── chipmunk.vcxproj │ └── chipmunk.vcxproj.filters │ └── demo │ ├── Chipmunk.sln │ ├── demo.vcxproj │ └── demo.vcxproj.filters ├── objectivec ├── include │ └── ObjectiveChipmunk │ │ ├── ChipmunkAutoGeometry.h │ │ ├── ChipmunkBody.h │ │ ├── ChipmunkConstraint.h │ │ ├── ChipmunkImageSampler.h │ │ ├── ChipmunkMultiGrab.h │ │ ├── ChipmunkPointCloudSampler.h │ │ ├── ChipmunkShape.h │ │ ├── ChipmunkSpace.h │ │ ├── ChipmunkTileCache.h │ │ └── ObjectiveChipmunk.h └── src │ ├── ChipmunkAutoGeometry.m │ ├── ChipmunkBody.m │ ├── ChipmunkConstraint.m │ ├── ChipmunkImageSampler.m │ ├── ChipmunkMultiGrab.m │ ├── ChipmunkPointCloudSampler.m │ ├── ChipmunkShape.m │ ├── ChipmunkSpace.m │ └── ChipmunkTileCache.m ├── scripts ├── BuildReleaseArchive.rb └── UploadArchive.rb ├── src ├── CMakeLists.txt ├── chipmunk.c ├── cpArbiter.c ├── cpArray.c ├── cpBBTree.c ├── cpBody.c ├── cpCollision.c ├── cpConstraint.c ├── cpDampedRotarySpring.c ├── cpDampedSpring.c ├── cpGearJoint.c ├── cpGrooveJoint.c ├── cpHashSet.c ├── cpHastySpace.c ├── cpMarch.c ├── cpPinJoint.c ├── cpPivotJoint.c ├── cpPolyShape.c ├── cpPolyline.c ├── cpRatchetJoint.c ├── cpRobust.c ├── cpRotaryLimitJoint.c ├── cpShape.c ├── cpSimpleMotor.c ├── cpSlideJoint.c ├── cpSpace.c ├── cpSpaceComponent.c ├── cpSpaceDebug.c ├── cpSpaceHash.c ├── cpSpaceQuery.c ├── cpSpaceStep.c ├── cpSpatialIndex.c ├── cpSweep1D.c └── prime.h └── xcode ├── Benchmark ├── Benchmark-Info.plist └── iPhoneBenchmarkMain.m ├── Chipmunk7.xcodeproj └── project.pbxproj ├── ObjectiveChipmunkTests ├── BodyTest.m ├── CallbacksTest.m ├── ConvexTest.m ├── MemoryTest.m ├── MiscTest.m ├── ObjectiveChipmunkTests-Info.plist ├── ShapeTest.m ├── SpaceTest.m ├── TestImageLA.png ├── TestImageRGB.png └── en.lproj │ └── InfoPlist.strings ├── bench.rb ├── bench2.rb ├── iphonestatic.command ├── macstatic.command └── main-Info.plist /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | doc/index.html 3 | 4 | *.pbxuser 5 | *.perspectivev3 6 | xcshareddata 7 | xcuserdata 8 | project.xcworkspace 9 | DerivedData 10 | build 11 | Chipmunk-Mac 12 | Chipmunk-iOS 13 | ObjectiveChipmunk-Mac 14 | ObjectiveChipmunk-iOS 15 | generated_docs 16 | 17 | CMakeCache.txt 18 | CMakeFiles 19 | Makefile 20 | cmake_install.cmake 21 | install_manifest.txt 22 | 23 | *.o 24 | *.obj 25 | *.dll 26 | *.so 27 | *.so.* 28 | *.dylib 29 | *.a 30 | *.exe 31 | *.lib 32 | chipmunk_demos 33 | 34 | # Visual studio 35 | *.opensdf 36 | *.opendb 37 | *.sdf 38 | *.suo 39 | *.pdb 40 | *.vcxproj.user 41 | msvc/vc10/demo/ipch/ 42 | msvc/vc10/chipmunk/Win32/ 43 | msvc/vc10/chipmunk/x64/ 44 | msvc/vc10/demo/Win32/ 45 | msvc/vc10/demo/x64/ 46 | msvc/vc12/chipmunk/Win32/ 47 | msvc/vc12/chipmunk/x64/ 48 | msvc/vc12/demo/Win32/ 49 | msvc/vc12/demo/x64/ 50 | msvc/vc13/chipmunk/Win32/ 51 | msvc/vc13/chipmunk/x64/ 52 | msvc/vc13/demo/Win32/ 53 | msvc/vc13/demo/x64/ 54 | msvc/vc14/demo/Win32/ 55 | msvc/vc14/demo/x64/ 56 | *.log 57 | *.tlog 58 | *.db 59 | 60 | .vscode 61 | -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | # This CITATION.cff file was generated with cffinit. 2 | # Visit https://bit.ly/cffinit to generate yours today! 3 | 4 | cff-version: 1.2.0 5 | title: Chipmunk2D 6 | message: >- 7 | If you use this software, please cite it using the 8 | metadata from this file. 9 | type: software 10 | authors: 11 | - given-names: Scott 12 | family-names: Lembcke 13 | repository-code: 'https://github.com/slembcke/Chipmunk2D' 14 | url: 'https://chipmunk-physics.net/' 15 | abstract: >- 16 | Chipmunk2D is a simple, lightweight, fast and portable 2D 17 | rigid body physics library written in C. 18 | keywords: 19 | - 2d physics simulation 20 | - rigidbody physics 21 | license: MIT 22 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | 3 | project(chipmunk) 4 | 5 | # to change the prefix, run cmake with the parameter: 6 | # -D CMAKE_INSTALL_PREFIX=/my/prefix 7 | 8 | # to change the build type, run cmake with the parameter: 9 | # -D CMAKE_BUILD_TYPE= 10 | # run "cmake --help-variable CMAKE_BUILD_TYPE" for details 11 | if(NOT CMAKE_BUILD_TYPE) 12 | SET(CMAKE_BUILD_TYPE Release CACHE STRING 13 | "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." 14 | FORCE) 15 | endif() 16 | 17 | # to manually select install locations of libraries and executables 18 | # -D LIB_INSTALL_DIR mylib 19 | # -D BIN_INSTALL_DIR newbin 20 | set(LIB_INSTALL_DIR lib CACHE STRING "Install location of libraries") 21 | set(BIN_INSTALL_DIR bin CACHE STRING "Install location of executables") 22 | 23 | # other options for the build, you can i.e. activate the shared library by passing 24 | # -D BUILD_SHARED=ON 25 | # to cmake. Other options analog 26 | if(ANDROID) 27 | option(BUILD_DEMOS "Build the demo applications" OFF) 28 | option(INSTALL_DEMOS "Install the demo applications" OFF) 29 | option(BUILD_SHARED "Build and install the shared library" ON) 30 | option(BUILD_STATIC "Build as static library" ON) 31 | option(INSTALL_STATIC "Install the static library" OFF) 32 | else() 33 | option(BUILD_DEMOS "Build the demo applications" ON) 34 | option(INSTALL_DEMOS "Install the demo applications" OFF) 35 | option(BUILD_SHARED "Build and install the shared library" ON) 36 | option(BUILD_STATIC "Build as static library" ON) 37 | option(INSTALL_STATIC "Install the static library" ON) 38 | endif() 39 | 40 | if(CMAKE_C_COMPILER_ID STREQUAL "Clang") 41 | option(FORCE_CLANG_BLOCKS "Force enable Clang blocks" YES) 42 | endif() 43 | 44 | # sanity checks... 45 | if(INSTALL_DEMOS) 46 | set(BUILD_DEMOS ON FORCE) 47 | endif() 48 | 49 | # these need the static lib too 50 | if(BUILD_DEMOS OR INSTALL_STATIC) 51 | set(BUILD_STATIC ON FORCE) 52 | endif() 53 | 54 | if(NOT MSVC) 55 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") # always use gnu99 56 | if(FORCE_CLANG_BLOCKS) 57 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fblocks") 58 | endif() 59 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ffast-math") # extend release-profile with fast-math 60 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall") # extend debug-profile with -Wall 61 | endif() 62 | 63 | add_subdirectory(src) 64 | 65 | if(BUILD_DEMOS) 66 | add_subdirectory(demo) 67 | endif() 68 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007-2015 Scott Lembcke and Howling Moon Software 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /TODO.txt: -------------------------------------------------------------------------------- 1 | Priorities: 2 | Basics tutorial. 3 | Simple top down player controls. 4 | * test tiny omega bug in cpvslerp(). 5 | User definable default winding. 6 | User definable fast collision filtering. 7 | Assertion (or warning?) for destroying a body with things still attached 8 | Bilinearly sampled image sampler function. (override the sample method to always take bilinear samples) 9 | RGB Image Sampler class. 10 | Reorganize Chimpnuk Pro directory structure. Too flat and confusing. 11 | Improve the ACD splitting plane algorithm. 12 | Fishing game 13 | Motorcycle in a spinning cage 14 | Squid thingy like TomorrowPlusX. 15 | 16 | Investigate getting better PhysicsEditor support. 17 | 18 | Unordered 19 | Fix solver issues with the groove joint. 20 | Use a slop tolerance on joint boundaries. 21 | 22 | Website things 23 | Don't make any mistakes: http://www.gamasutra.com/view/feature/185773/the_top_10_mistakes_tool_.php 24 | Several people want a Chipmunk Facebook page. 25 | figure out and redo front page. Chipmunk js examples. 26 | 27 | Future things to think about: 28 | breakable object support functions? 29 | Serialization 30 | Tests for the query methods 31 | Building bodies from shape collections. 32 | Per body iterations and timestep? 33 | Per body damping and gravity coefs? 34 | Easy callback programable joint? 35 | Top down racing game. (Need the callback constraint) 36 | cpBodyActivateStatic() should also activate joints? 37 | 38 | Chipmunk 7: 39 | Speculative contacts 40 | User definable constraint 41 | Custom contact constraint with rolling friction and per contact surface v. 42 | Serialization 43 | API changes, different body/shape instantiation. 44 | Collision handler objects with additional callbacks. 45 | Calculate contact anchors to get rid of contact pos. (needed for speculative contacts anyway) 46 | Mass property calculation changes. 47 | Change apply force/impulse and point velocity functions. 48 | Separate doxygen docs for Objective-C parts and C parts. 49 | Cocos2D xcode templates. 50 | Custom contacts using cpArbiter user data. 51 | Built in transform type. 52 | -------------------------------------------------------------------------------- /android/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) PlayControl Software, LLC. 2 | # Eric Wing 3 | # 4 | # This is a "Prebuilt" Android Makefile provided as an example/template (or direct use if no tweaking is required). 5 | # The idea is that you have already built the chipmunk .so and .a libraries using CMake. 6 | # Now you want to use those prebuilt libraries in your own project. 7 | # Android support prebuilt exteneral modules through its ndk-build system, but you need to have all the pieces setup and in the right place. This is one of those pieces. 8 | # 9 | # This file assumes you built all your chipmunk libs and put things into a directory structure like so: 10 | # 11 | # Android.mk (this file) 12 | # libs/armeabi/libchipmunk.a 13 | # libs/armeabi/libchipmunk.a 14 | # libs/armeabi-v7a/libchipmunk.a 15 | # libs/armeabi-v7a/libchipmunk.a 16 | # libs/x86/libchipmunk.a 17 | # libs/x86/libchipmunk.a 18 | # 19 | # include/chipmunk/chipmunk.h 20 | # ... (the other header files here) 21 | # 22 | # Note that this file is copied into the directory above libs and include. 23 | # Below is the code you need to make this Makefile export the correct headers, libraries, and flags for both dynamic and static versions. 24 | 25 | # LOCAL_PATH needs to be before include 26 | LOCAL_PATH := $(call my-dir) 27 | 28 | # For the dynamic library 29 | include $(CLEAR_VARS) 30 | # This is the name of module the caller will use in LOCAL_SHARED_LIBRARIES 31 | LOCAL_MODULE := chipmunk_shared 32 | LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libchipmunk.so 33 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/chipmunk 34 | # Use LOCAL_EXPORT_CFLAGS to automatically export the correct flags (as necessary) to the calling module so the caller doesn't need to know the details. 35 | #LOCAL_EXPORT_CFLAGS := -DFOO=1 -DCP_USE_DOUBLES=1 -DCP_USE_CGPOINTS=0 36 | # The .so is already linked so we don't really need to export this. 37 | #LOCAL_EXPORT_LDLIBS := -lm 38 | include $(PREBUILT_SHARED_LIBRARY) 39 | 40 | # For the static library 41 | include $(CLEAR_VARS) 42 | # This is the name of module the caller will use in LOCAL_STATIC_LIBRARIES 43 | LOCAL_MODULE := chipmunk_static 44 | LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libchipmunk.a 45 | # Use LOCAL_EXPORT_CFLAGS to automatically export the correct flags (as necessary) to the calling module so the caller doesn't need to know the details. 46 | #LOCAL_EXPORT_CFLAGS := -DFOO=1 -DCP_USE_DOUBLES=1 -DCP_USE_CGPOINTS=0 47 | # Since the .a isn't linked, it's link dependencies must be passed on to the calling project. 48 | LOCAL_EXPORT_LDLIBS := -lm 49 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/chipmunk 50 | include $(PREBUILT_STATIC_LIBRARY) 51 | 52 | 53 | 54 | # Two other pieces are needed to make this work which fall outside the scope of this file. 55 | # First, you must have a directory convention for the calling makefile. 56 | # So let's say we put all the above in a directory called Chipmunk2D. The layout looks like this: 57 | # Chipmunk2D/ 58 | # Android.mk (this file) 59 | # libs/armeabi/libchipmunk.a 60 | # libs/armeabi/libchipmunk.a 61 | # libs/armeabi-v7a/libchipmunk.a 62 | # libs/armeabi-v7a/libchipmunk.a 63 | # libs/x86/libchipmunk.a 64 | # libs/x86/libchipmunk.a 65 | # 66 | # include/chipmunk/chipmunk.h 67 | # ... (the other header files here) 68 | 69 | # So the calling makefile looks something like: 70 | # LOCAL_PATH := $(call my-dir) 71 | # include $(CLEAR_VARS) 72 | # LOCAL_MODULE := hello-jni 73 | # LOCAL_SRC_FILES := hello-jni.c 74 | # These are the LOCAL_MODULE names as defined in the prebuilt module's Android.mk. Define either shared or static, but not both. If you use dynamic, don't forget you need to do a System.loadLibrary("chipmunk") in your Java code. 75 | # #LOCAL_SHARED_LIBRARIES := chipmunk_shared 76 | # LOCAL_STATIC_LIBRARIES := chipmunk_static 77 | # include $(BUILD_SHARED_LIBRARY) 78 | # Android build system will look for folder `Chipmunk2D` in all import paths: 79 | # $(call import-module,Chipmunk2D) 80 | # ------ end ----- 81 | 82 | # Second, you need to set the environmental variable NDK_MODULE_PATH to list the directory containing Chipmunk2D. 83 | # So if Chipmunk2D is in /Library/Frameworks/Android/PrebuiltModules 84 | # export NDK_MODULE_PATH=/Library/Frameworks/Android/PrebuiltModules 85 | # Note that NDK_MODULE_PATH may contain multiple directories like the PATH environmental variable. 86 | 87 | -------------------------------------------------------------------------------- /demo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_policy(SET CMP0015 NEW) # Convert relative paths 2 | 3 | set(OpenGL_GL_PREFERENCE GLVND) 4 | find_package(OpenGL REQUIRED) 5 | 6 | set(chipmunk_demos_include_dirs 7 | ${chipmunk_SOURCE_DIR}/include 8 | ${OPENGL_INCLUDE_DIR} 9 | ) 10 | 11 | set(chipmunk_demos_libraries 12 | chipmunk_static 13 | ${OPENGL_LIBRARIES} 14 | ) 15 | 16 | file(GLOB chipmunk_demos_source_files "*.c") 17 | 18 | if(APPLE) 19 | FIND_LIBRARY(APPKIT AppKit) 20 | FIND_LIBRARY(IOKIT IOKit) 21 | 22 | list(APPEND chipmunk_demos_libraries ${APPKIT} ${IOKIT}) 23 | list(APPEND chipmunk_demos_source_files "sokol/sokol.m") 24 | set_property(SOURCE "sokol/sokol.m" APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc") 25 | else() 26 | list(APPEND chipmunk_demos_source_files "sokol/sokol.c") 27 | endif(APPLE) 28 | 29 | IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") 30 | list(APPEND chipmunk_demos_libraries dl X11) 31 | endif() 32 | 33 | if(NOT MSVC) 34 | list(APPEND chipmunk_demos_libraries m pthread) 35 | endif(NOT MSVC) 36 | 37 | if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") 38 | list(APPEND chipmunk_demos_libraries BlocksRuntime) 39 | endif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") 40 | 41 | include_directories(${chipmunk_demos_include_dirs}) 42 | link_directories(${chipmunk_demos_library_dirs}) 43 | add_executable(chipmunk_demos ${chipmunk_demos_source_files}) 44 | target_link_libraries(chipmunk_demos ${chipmunk_demos_libraries}) 45 | 46 | if(INSTALL_DEMOS) 47 | install(TARGETS chipmunk_demos RUNTIME DESTINATION bin) 48 | endif(INSTALL_DEMOS) 49 | -------------------------------------------------------------------------------- /demo/ChipmunkDebugDraw.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | static inline cpSpaceDebugColor RGBAColor(float r, float g, float b, float a){ 23 | cpSpaceDebugColor color = {r, g, b, a}; 24 | return color; 25 | } 26 | 27 | static inline cpSpaceDebugColor LAColor(float l, float a){ 28 | cpSpaceDebugColor color = {l, l, l, a}; 29 | return color; 30 | } 31 | 32 | void ChipmunkDebugDrawInit(void); 33 | 34 | extern cpTransform ChipmunkDebugDrawVPMatrix; 35 | extern float ChipmunkDebugDrawPointLineScale; 36 | 37 | void ChipmunkDebugDrawCircle(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor); 38 | void ChipmunkDebugDrawSegment(cpVect a, cpVect b, cpSpaceDebugColor color); 39 | void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor); 40 | void ChipmunkDebugDrawPolygon(int count, const cpVect *verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor); 41 | void ChipmunkDebugDrawDot(cpFloat size, cpVect pos, cpSpaceDebugColor fillColor); 42 | void ChipmunkDebugDrawBB(cpBB bb, cpSpaceDebugColor outlineColor); 43 | 44 | // Call this at the beginning of the frame to clear out any ChipmunkDebugDraw*() commands. 45 | void ChipmunkDebugDrawClearRenderer(void); 46 | // Call this at the end of the frame to draw the ChipmunkDebugDraw*() commands to the screen. 47 | void ChipmunkDebugDrawFlushRenderer(void); 48 | 49 | // Save the current contents of the renderer. 50 | void ChipmunkDebugDrawPushRenderer(void); 51 | // Reset the renderer back to it's last pushed state. 52 | void ChipmunkDebugDrawPopRenderer(void); 53 | -------------------------------------------------------------------------------- /demo/ChipmunkDemo.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include "ChipmunkDebugDraw.h" 23 | 24 | typedef struct ChipmunkDemo ChipmunkDemo; 25 | 26 | typedef cpSpace *(*ChipmunkDemoInitFunc)(void); 27 | typedef void (*ChipmunkDemoUpdateFunc)(cpSpace *space, double dt); 28 | typedef void (*ChipmunkDemoDrawFunc)(cpSpace *space); 29 | typedef void (*ChipmunkDemoDestroyFunc)(cpSpace *space); 30 | 31 | struct ChipmunkDemo { 32 | const char *name; 33 | double timestep; 34 | 35 | ChipmunkDemoInitFunc initFunc; 36 | ChipmunkDemoUpdateFunc updateFunc; 37 | ChipmunkDemoDrawFunc drawFunc; 38 | 39 | ChipmunkDemoDestroyFunc destroyFunc; 40 | }; 41 | 42 | static inline cpFloat 43 | frand(void) 44 | { 45 | return (cpFloat)rand()/(cpFloat)RAND_MAX; 46 | } 47 | 48 | static inline cpVect 49 | frand_unit_circle(){ 50 | cpVect v = cpv(frand()*2.0f - 1.0f, frand()*2.0f - 1.0f); 51 | return (cpvlengthsq(v) < 1.0f ? v : frand_unit_circle()); 52 | } 53 | 54 | extern int ChipmunkDemoTicks; 55 | extern double ChipmunkDemoTime; 56 | extern cpVect ChipmunkDemoKeyboard; 57 | extern cpVect ChipmunkDemoMouse; 58 | extern cpBool ChipmunkDemoRightClick; 59 | extern cpBool ChipmunkDemoRightDown; 60 | 61 | extern char const *ChipmunkDemoMessageString; 62 | void ChipmunkDemoPrintString(char const *fmt, ...); 63 | 64 | extern cpShapeFilter GRAB_FILTER; 65 | extern cpShapeFilter NOT_GRABBABLE_FILTER; 66 | 67 | void ChipmunkDemoDefaultDrawImpl(cpSpace *space); 68 | void ChipmunkDemoFreeSpaceChildren(cpSpace *space); 69 | -------------------------------------------------------------------------------- /demo/ChipmunkDemoTextSupport.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include "chipmunk/chipmunk.h" 23 | 24 | extern cpTransform ChipmunkDemoTextMatrix; 25 | 26 | void ChipmunkDemoTextInit(void); 27 | void ChipmunkDemoTextDrawString(cpVect pos, char const *str); 28 | void ChipmunkDemoTextFlushRenderer(void); 29 | void ChipmunkDemoTextClearRenderer(void); 30 | 31 | void ChipmunkDemoTextPushRenderer(void); 32 | void ChipmunkDemoTextPopRenderer(void); 33 | -------------------------------------------------------------------------------- /demo/ContactPoints.c_: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "chipmunk/chipmunk_private.h" 28 | #include "ChipmunkDemo.h" 29 | 30 | static cpBool NeverCollide(cpArbiter *arb, cpSpace *space, void *data){return cpFalse;} 31 | 32 | static void 33 | update(cpSpace *space) 34 | { 35 | int steps = 1; 36 | cpFloat dt = 1.0f/60.0f/(cpFloat)steps; 37 | 38 | for(int i=0; i tolerance){ 37 | cpBody *body = cpShapeGetBody(shape); 38 | int count = cpPolyShapeGetCount(shape); 39 | 40 | // Allocate the space for the new vertexes on the stack. 41 | cpVect *verts = (cpVect *)alloca((count + 1)*sizeof(cpVect)); 42 | 43 | for(int i=0; i 23 | #include 24 | #include 25 | #include 26 | 27 | #include "chipmunk_private.h" 28 | #include "chipmunk_unsafe.h" 29 | #include "ChipmunkDemo.h" 30 | 31 | static cpShape *shape1, *shape2; 32 | 33 | static void 34 | update(cpSpace *space, cpFloat dt) 35 | { 36 | cpSpaceStep(space, dt); 37 | } 38 | 39 | static void 40 | draw(cpSpace *space) 41 | { 42 | ChipmunkDemoDefaultDrawImpl(space); 43 | struct cpContact arr[CP_MAX_CONTACTS_PER_ARBITER]; 44 | // cpCollideShapes(shape1, shape2, (cpCollisionID[]){0}, arr); 45 | cpCollisionInfo info = cpCollideShapes(shape2, shape1, 0x00000000, arr); 46 | } 47 | 48 | static cpSpace * 49 | init(void) 50 | { 51 | cpSpace *space = cpSpaceNew(); 52 | cpSpaceSetIterations(space, 5); 53 | space->damping = 0.1; 54 | 55 | cpFloat mass = 1.0f; 56 | 57 | { 58 | cpFloat size = 100.0; 59 | 60 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size))); 61 | cpBodySetPosition(body, cpv(100.0, 50.0f)); 62 | 63 | shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0)); 64 | shape1->group = 1; 65 | }{ 66 | cpFloat size = 100.0; 67 | 68 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size))); 69 | cpBodySetPosition(body, cpv(120.0, -40.0f)); 70 | cpBodySetAngle(body, 1e-2); 71 | 72 | shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0)); 73 | shape2->group = 1; 74 | } 75 | 76 | // { 77 | // cpFloat size = 100.0; 78 | // const int NUM_VERTS = 5; 79 | // 80 | // cpVect verts[NUM_VERTS]; 81 | // for(int i=0; igroup = 1; 91 | // } 92 | // { 93 | // cpFloat size = 100.0; 94 | // const int NUM_VERTS = 4; 95 | // 96 | // cpVect verts[NUM_VERTS]; 97 | // for(int i=0; igroup = 1; 107 | // } 108 | // 109 | // { 110 | // cpFloat size = 150.0; 111 | // cpFloat radius = 25.0; 112 | // 113 | // cpVect a = cpv( size/2.0, 0.0); 114 | // cpVect b = cpv(-size/2.0, 0.0); 115 | // cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); 116 | // cpBodySetPosition(body, cpv(0, 25)); 117 | // 118 | // shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius)); 119 | // shape1->group = 1; 120 | // } 121 | // { 122 | // cpFloat radius = 50.0; 123 | // 124 | // cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); 125 | // cpBodySetPosition(body, cpv(0, -25)); 126 | // 127 | // shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); 128 | // shape2->group = 1; 129 | // } 130 | 131 | return space; 132 | } 133 | 134 | static void 135 | destroy(cpSpace *space) 136 | { 137 | ChipmunkDemoFreeSpaceChildren(space); 138 | cpSpaceFree(space); 139 | } 140 | 141 | ChipmunkDemo GJK = { 142 | "GJK", 143 | 1.0f/60.0f, 144 | init, 145 | update, 146 | draw, 147 | destroy, 148 | }; 149 | -------------------------------------------------------------------------------- /demo/GLEWConfig.cmake: -------------------------------------------------------------------------------- 1 | set(GLEW_INCLUDE_DIR "@CMAKE_INSTALL_PREFIX@/include") 2 | set(GLEW_LIBRARY_DIR "@CMAKE_INSTALL_PREFIX@/lib@LIB_SUFFIX@") 3 | 4 | find_library(GLEW_LIBRARY "@GLEW_LIB_NAME@" HINTS ${GLEW_LIBRARY_DIR}) 5 | -------------------------------------------------------------------------------- /demo/GLFWConfig.cmake: -------------------------------------------------------------------------------- 1 | # - Config file for the glfw package 2 | # It defines the following variables 3 | # GLFW_INCLUDE_DIR, the path where GLFW headers are located 4 | # GLFW_LIBRARY_DIR, folder in which the GLFW library is located 5 | # GLFW_LIBRARY, library to link against to use GLFW 6 | 7 | set(GLFW_INCLUDE_DIR "@CMAKE_INSTALL_PREFIX@/include") 8 | set(GLFW_LIBRARY_DIR "@CMAKE_INSTALL_PREFIX@/lib@LIB_SUFFIX@") 9 | 10 | find_library(GLFW_LIBRARY "@GLFW_LIB_NAME@" HINTS ${GLFW_LIBRARY_DIR}) 11 | -------------------------------------------------------------------------------- /demo/OneWay.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include "chipmunk/chipmunk.h" 23 | #include "ChipmunkDemo.h" 24 | 25 | enum CollisionTypes { 26 | COLLISION_TYPE_ONE_WAY = 1, 27 | }; 28 | 29 | typedef struct OneWayPlatform { 30 | cpVect n; // direction objects may pass through 31 | } OneWayPlatform; 32 | 33 | static OneWayPlatform platformInstance; 34 | 35 | static cpBool 36 | PreSolve(cpArbiter *arb, cpSpace *space, void *ignore) 37 | { 38 | CP_ARBITER_GET_SHAPES(arb, a, b); 39 | OneWayPlatform *platform = (OneWayPlatform *)cpShapeGetUserData(a); 40 | 41 | if(cpvdot(cpArbiterGetNormal(arb), platform->n) < 0){ 42 | return cpArbiterIgnore(arb); 43 | } 44 | 45 | return cpTrue; 46 | } 47 | 48 | static void 49 | update(cpSpace *space, double dt) 50 | { 51 | cpSpaceStep(space, dt); 52 | } 53 | 54 | static cpSpace * 55 | init(void) 56 | { 57 | ChipmunkDemoMessageString = "One way platforms are trivial in Chipmunk using a very simple collision callback."; 58 | 59 | cpSpace *space = cpSpaceNew(); 60 | cpSpaceSetIterations(space, 10); 61 | cpSpaceSetGravity(space, cpv(0, -100)); 62 | 63 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); 64 | cpShape *shape; 65 | 66 | // Create segments around the edge of the screen. 67 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); 68 | cpShapeSetElasticity(shape, 1.0f); 69 | cpShapeSetFriction(shape, 1.0f); 70 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 71 | 72 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); 73 | cpShapeSetElasticity(shape, 1.0f); 74 | cpShapeSetFriction(shape, 1.0f); 75 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 76 | 77 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); 78 | cpShapeSetElasticity(shape, 1.0f); 79 | cpShapeSetFriction(shape, 1.0f); 80 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 81 | 82 | // Add our one way segment 83 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); 84 | cpShapeSetElasticity(shape, 1.0f); 85 | cpShapeSetFriction(shape, 1.0f); 86 | cpShapeSetCollisionType(shape, COLLISION_TYPE_ONE_WAY); 87 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 88 | 89 | // We'll use the data pointer for the OneWayPlatform struct 90 | platformInstance.n = cpv(0, 1); // let objects pass upwards 91 | cpShapeSetUserData(shape, &platformInstance); 92 | 93 | 94 | // Add a ball to test it out 95 | cpFloat radius = 15.0f; 96 | body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); 97 | cpBodySetPosition(body, cpv(0, -200)); 98 | cpBodySetVelocity(body, cpv(0, 170)); 99 | 100 | shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); 101 | cpShapeSetElasticity(shape, 0.0f); 102 | cpShapeSetFriction(shape, 0.9f); 103 | cpShapeSetCollisionType(shape, 2); 104 | 105 | cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_ONE_WAY); 106 | handler->preSolveFunc = PreSolve; 107 | 108 | return space; 109 | } 110 | 111 | static void 112 | destroy(cpSpace *space) 113 | { 114 | ChipmunkDemoFreeSpaceChildren(space); 115 | cpSpaceFree(space); 116 | } 117 | 118 | ChipmunkDemo OneWay = { 119 | "One Way Platforms", 120 | 1.0/60.0, 121 | init, 122 | update, 123 | ChipmunkDemoDefaultDrawImpl, 124 | destroy, 125 | }; 126 | -------------------------------------------------------------------------------- /demo/Planet.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include "chipmunk/chipmunk.h" 23 | #include "ChipmunkDemo.h" 24 | 25 | static cpBody *planetBody; 26 | 27 | static cpFloat gravityStrength = 5.0e6f; 28 | 29 | static void 30 | update(cpSpace *space, double dt) 31 | { 32 | cpSpaceStep(space, dt); 33 | } 34 | 35 | static void 36 | planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) 37 | { 38 | // Gravitational acceleration is proportional to the inverse square of 39 | // distance, and directed toward the origin. The central planet is assumed 40 | // to be massive enough that it affects the satellites but not vice versa. 41 | cpVect p = cpBodyGetPosition(body); 42 | cpFloat sqdist = cpvlengthsq(p); 43 | cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist))); 44 | 45 | cpBodyUpdateVelocity(body, g, damping, dt); 46 | } 47 | 48 | static cpVect 49 | rand_pos(cpFloat radius) 50 | { 51 | cpVect v; 52 | do { 53 | v = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius)); 54 | } while(cpvlength(v) < 85.0f); 55 | 56 | return v; 57 | } 58 | 59 | static void 60 | add_box(cpSpace *space) 61 | { 62 | const cpFloat size = 10.0f; 63 | const cpFloat mass = 1.0f; 64 | 65 | cpVect verts[4] = { 66 | cpv(-size,-size), 67 | cpv(-size, size), 68 | cpv( size, size), 69 | cpv( size,-size), 70 | }; 71 | 72 | cpFloat radius = cpvlength(cpv(size, size)); 73 | cpVect pos = rand_pos(radius); 74 | 75 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero, 0.0f))); 76 | cpBodySetVelocityUpdateFunc(body, planetGravityVelocityFunc); 77 | cpBodySetPosition(body, pos); 78 | 79 | // Set the box's velocity to put it into a circular orbit from its 80 | // starting position. 81 | cpFloat r = cpvlength(pos); 82 | cpFloat v = cpfsqrt(gravityStrength / r) / r; 83 | cpBodySetVelocity(body, cpvmult(cpvperp(pos), v)); 84 | 85 | // Set the box's angular velocity to match its orbital period and 86 | // align its initial angle with its position. 87 | cpBodySetAngularVelocity(body, v); 88 | cpBodySetAngle(body, cpfatan2(pos.y, pos.x)); 89 | 90 | cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpTransformIdentity, 0.0)); 91 | cpShapeSetElasticity(shape, 0.0f); 92 | cpShapeSetFriction(shape, 0.7f); 93 | } 94 | 95 | static cpSpace * 96 | init(void) 97 | { 98 | cpSpace *space = cpSpaceNew(); 99 | cpSpaceSetIterations(space, 20); 100 | 101 | // Create a kinematic body to control the planet manually. 102 | planetBody = cpSpaceAddBody(space, cpBodyNewKinematic()); 103 | cpBodySetAngularVelocity(planetBody, 0.2f); 104 | 105 | for(int i=0; i<30; i++){ 106 | add_box(space); 107 | } 108 | 109 | cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(planetBody, 70.0f, cpvzero)); 110 | cpShapeSetElasticity(shape, 1.0f); 111 | cpShapeSetFriction(shape, 1.0f); 112 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 113 | 114 | return space; 115 | } 116 | 117 | static void 118 | destroy(cpSpace *space) 119 | { 120 | ChipmunkDemoFreeSpaceChildren(space); 121 | cpSpaceFree(space); 122 | } 123 | 124 | ChipmunkDemo Planet = { 125 | "Planet", 126 | 1.0/60.0, 127 | init, 128 | update, 129 | ChipmunkDemoDefaultDrawImpl, 130 | destroy, 131 | }; 132 | -------------------------------------------------------------------------------- /demo/Plink.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2007 Scott Lembcke 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | 22 | #include "chipmunk/chipmunk.h" 23 | #include "ChipmunkDemo.h" 24 | 25 | static cpFloat pentagon_mass = 0.0f; 26 | static cpFloat pentagon_moment = 0.0f; 27 | 28 | // Iterate over all of the bodies and reset the ones that have fallen offscreen. 29 | static void 30 | eachBody(cpBody *body, void *unused) 31 | { 32 | cpVect pos = cpBodyGetPosition(body); 33 | if(pos.y < -260 || cpfabs(pos.x) > 340){ 34 | cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; 35 | cpBodySetPosition(body, cpv(x, 260)); 36 | } 37 | } 38 | 39 | static void 40 | update(cpSpace *space, double dt) 41 | { 42 | if(ChipmunkDemoRightDown){ 43 | cpShape *nearest = cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 0.0, GRAB_FILTER, NULL); 44 | if(nearest){ 45 | cpBody *body = cpShapeGetBody(nearest); 46 | if(cpBodyGetType(body) == CP_BODY_TYPE_STATIC){ 47 | cpBodySetType(body, CP_BODY_TYPE_DYNAMIC); 48 | cpBodySetMass(body, pentagon_mass); 49 | cpBodySetMoment(body, pentagon_moment); 50 | } else if(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC) { 51 | cpBodySetType(body, CP_BODY_TYPE_STATIC); 52 | } 53 | } 54 | } 55 | 56 | cpSpaceEachBody(space, &eachBody, NULL); 57 | cpSpaceStep(space, dt); 58 | } 59 | 60 | #define NUM_VERTS 5 61 | 62 | static cpSpace * 63 | init(void) 64 | { 65 | ChipmunkDemoMessageString = "Right click to make pentagons static/dynamic."; 66 | 67 | cpSpace *space = cpSpaceNew(); 68 | cpSpaceSetIterations(space, 5); 69 | cpSpaceSetGravity(space, cpv(0, -100)); 70 | 71 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); 72 | cpShape *shape; 73 | 74 | // Vertexes for a triangle shape. 75 | cpVect tris[4] = { 76 | cpv(-15,-15), 77 | cpv( 0, 10), 78 | cpv( 15,-15), 79 | }; 80 | 81 | // Create the static triangles. 82 | for(int i=0; i<9; i++){ 83 | for(int j=0; j<6; j++){ 84 | cpFloat stagger = (j%2)*40; 85 | cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240); 86 | shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, cpTransformTranslate(offset), 0.0)); 87 | cpShapeSetElasticity(shape, 1.0f); 88 | cpShapeSetFriction(shape, 1.0f); 89 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); 90 | } 91 | } 92 | 93 | // Create vertexes for a pentagon shape. 94 | cpVect verts[NUM_VERTS]; 95 | for(int i=0; i 23 | #include 24 | #include 25 | #include 26 | 27 | #include "chipmunk/chipmunk_private.h" 28 | #include "ChipmunkDemo.h" 29 | 30 | static cpBool DrawContacts(cpArbiter *arb, cpSpace *space, void *data){ 31 | cpContactPointSet set = cpArbiterGetContactPointSet(arb); 32 | 33 | for(int i=0; i b ? a : b) 55 | #define MIN(a, b) (a < b ? a : b) 56 | 57 | static cpSpace * 58 | init(void) 59 | { 60 | cpSpace *space = cpSpaceNew(); 61 | cpSpaceSetIterations(space, 5); 62 | cpSpaceSetDamping(space, 0.1f); 63 | 64 | cpSpaceSetDefaultCollisionHandler(space, NULL, DrawContacts, NULL, NULL, NULL); 65 | 66 | { 67 | cpFloat mass = 1.0f; 68 | cpFloat length = 100.0f; 69 | cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f); 70 | 71 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); 72 | cpBodySetPos(body, cpv(-160.0f, 80.0f)); 73 | 74 | cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 30.0f)); 75 | } 76 | 77 | { 78 | cpFloat mass = 1.0f; 79 | const int NUM_VERTS = 5; 80 | 81 | cpVect verts[NUM_VERTS]; 82 | for(int i=0; ifips 3 | # this area is managed by fips, do not edit 4 | .fips-* 5 | *.pyc 6 | ##{@title}#{children}"] 26 | end 27 | 28 | Root = Struct.new(:anchor, :children).new(nil, []) 29 | def Root.outline 30 | "
    #{self.children.map{|child| child.outline(1)}.join}
" 31 | end 32 | end 33 | 34 | def pop_open_div(name) 35 | return %{
Hide/Show #{name}