| 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"cxx . . $ `<file>/include/chipmunk/chipmunk.h`/ |
| 23 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 24 | |
| 25 | static cpVectcxx . . $ cpVect# QUERY_STARTcxx . . $ QUERY_START. = {0,0}; |
| 26 | |
| 27 | static void |
| 28 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 29 | { |
| 30 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 31 | |
| 32 | if(ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick.){ |
| 33 | QUERY_STARTcxx . . $ QUERY_START. = ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse.; |
| 34 | } |
| 35 | |
| 36 | cpVectcxx . . $ cpVect# start = QUERY_STARTcxx . . $ QUERY_START.; |
| 37 | cpVectcxx . . $ cpVect# end = ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse.; |
| 38 | cpFloatcxx . . $ cpFloat# radius = 10.0; |
| 39 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(start, end, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0,1,0,1)); |
| 40 | |
| 41 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("Query: Dist(%f) Point(%5.2f, %5.2f), ", cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(start, end), end.xcxx . . $ cpVect#x., end.ycxx . . $ cpVect#y.); |
| 42 | |
| 43 | cpSegmentQueryInfocxx . . $ cpSegmentQueryInfo# segInfo = {0}; |
| 44 | if(cpSpaceSegmentQueryFirstcxx . . $ cpSpaceSegmentQueryFirst(e8de41ced5effca2).(space, start, end, radius, CP_SHAPE_FILTER_ALLcxx . . $ CP_SHAPE_FILTER_ALL., &segInfo)){ |
| 45 | cpVectcxx . . $ cpVect# point = segInfo.pointcxx . . $ cpSegmentQueryInfo#point.; |
| 46 | cpVectcxx . . $ cpVect# n = segInfo.normalcxx . . $ cpSegmentQueryInfo#normal.; |
| 47 | |
| 48 | // Draw blue over the occluded part of the query |
| 49 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(start, end, segInfo.alphacxx . . $ cpSegmentQueryInfo#alpha.), end, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0,0,1,1)); |
| 50 | |
| 51 | // Draw a little red surface normal |
| 52 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(point, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(point, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, 16)), RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1,0,0,1)); |
| 53 | |
| 54 | // Draw a little red dot on the hit point. |
| 55 | ChipmunkDebugDrawDotcxx . . $ ChipmunkDebugDrawDot(227e9b49eafe2913).(3, point, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1,0,0,1)); |
| 56 | |
| 57 | |
| 58 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("Segment Query: Dist(%f) Normal(%5.2f, %5.2f)", segInfo.alphacxx . . $ cpSegmentQueryInfo#alpha.*cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(start, end), n.xcxx . . $ cpVect#x., n.ycxx . . $ cpVect#y.); |
| 59 | } else { |
| 60 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("Segment Query (None)"); |
| 61 | } |
| 62 | |
| 63 | // Draw a fat green line over the unoccluded part of the query |
| 64 | ChipmunkDebugDrawFatSegmentcxx . . $ ChipmunkDebugDrawFatSegment(972494e4e0a31225).(start, cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(start, end, segInfo.alphacxx . . $ cpSegmentQueryInfo#alpha.), radius, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0,1,0,1), LAColorcxx . . $ LAColor(197b9b65f197505a).(0,0)); |
| 65 | |
| 66 | cpPointQueryInfocxx . . $ cpPointQueryInfo# nearestInfo = {0}; |
| 67 | cpSpacePointQueryNearestcxx . . $ cpSpacePointQueryNearest(b0e549d484789d).(space, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., 100.0, CP_SHAPE_FILTER_ALLcxx . . $ CP_SHAPE_FILTER_ALL., &nearestInfo); |
| 68 | if(nearestInfo.shapecxx . . $ cpPointQueryInfo#shape.){ |
| 69 | // Draw a grey line to the closest shape. |
| 70 | ChipmunkDebugDrawDotcxx . . $ ChipmunkDebugDrawDot(227e9b49eafe2913).(3, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0.5, 0.5, 0.5, 1.0)); |
| 71 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., nearestInfo.pointcxx . . $ cpPointQueryInfo#point., RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0.5, 0.5, 0.5, 1.0)); |
| 72 | |
| 73 | // Draw a red bounding box around the shape under the mouse. |
| 74 | if(nearestInfo.distancecxx . . $ cpPointQueryInfo#distance. < 0) ChipmunkDebugDrawBBcxx . . $ ChipmunkDebugDrawBB(a084cc6f3de926d8).(cpShapeGetBBcxx . . $ cpShapeGetBB(44a9343cdf942fdb).(nearestInfo.shapecxx . . $ cpPointQueryInfo#shape.), RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1,0,0,1)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static cpSpacecxx . . $ cpSpace# * |
| 79 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 80 | { |
| 81 | QUERY_STARTcxx . . $ QUERY_START. = cpvzerocxx . . $ cpvzero.; |
| 82 | |
| 83 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 84 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 5); |
| 85 | |
| 86 | { // add a fat segment |
| 87 | cpFloatcxx . . $ cpFloat# mass = 1.0f; |
| 88 | cpFloatcxx . . $ cpFloat# length = 100.0f; |
| 89 | cpVectcxx . . $ cpVect# a = cpvcxx . . $ cpv(3142d57d978b2176).(-length/2.0f, 0.0f), b = cpvcxx . . $ cpv(3142d57d978b2176).(length/2.0f, 0.0f); |
| 90 | |
| 91 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForSegmentcxx . . $ cpMomentForSegment(13ad460f360d98d3).(mass, a, b, 0.0f))); |
| 92 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0.0f, 100.0f)); |
| 93 | |
| 94 | cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(body, a, b, 20.0f)); |
| 95 | } |
| 96 | |
| 97 | { // add a static segment |
| 98 | cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space), cpvcxx . . $ cpv(3142d57d978b2176).(0, 300), cpvcxx . . $ cpv(3142d57d978b2176).(300, 0), 0.0f)); |
| 99 | } |
| 100 | |
| 101 | { // add a pentagon |
| 102 | cpFloatcxx . . $ cpFloat# mass = 1.0f; |
| 103 | |
| 104 | cpVectcxx . . $ cpVect# verts[5]; |
| 105 | for(int i=0; i<5; i++){ |
| 106 | cpFloatcxx . . $ cpFloat# angle = -2.0f*CP_PIcxx . . $ `include/chipmunk/chipmunk_types.h:115:9`!*i/((cpFloatcxx . . $ cpFloat#) 5); |
| 107 | verts[i] = cpvcxx . . $ cpv(3142d57d978b2176).(30*cos(angle), 30*sin(angle)); |
| 108 | } |
| 109 | |
| 110 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(mass, 5, verts, cpvzerocxx . . $ cpvzero., 0.0f))); |
| 111 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(50.0f, 30.0f)); |
| 112 | |
| 113 | cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpPolyShapeNewcxx . . $ cpPolyShapeNew(119891a3da20e8eb).(body, 5, verts, cpTransformIdentitycxx . . $ cpTransformIdentity., 10.0f)); |
| 114 | } |
| 115 | |
| 116 | { // add a circle |
| 117 | cpFloatcxx . . $ cpFloat# mass = 1.0f; |
| 118 | cpFloatcxx . . $ cpFloat# r = 20.0f; |
| 119 | |
| 120 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(mass, 0.0f, r, cpvzerocxx . . $ cpvzero.))); |
| 121 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(100.0f, 100.0f)); |
| 122 | |
| 123 | cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(body, r, cpvzerocxx . . $ cpvzero.)); |
| 124 | } |
| 125 | |
| 126 | return space; |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 131 | { |
| 132 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 133 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 134 | } |
| 135 | |
| 136 | ChipmunkDemocxx . . $ ChipmunkDemo# Querycxx . . $ Query. = { |
| 137 | "Segment Query", |
| 138 | 1.0/60.0, |
| 139 | initcxx . . $ init(1f0082e822a7ce76)., |
| 140 | updatecxx . . $ update(fa66a0a48611083e)., |
| 141 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 142 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 143 | }; |