| 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 cpBodycxx . . $ cpBody# *planetBodycxx . . $ planetBody.; |
| 26 | |
| 27 | static cpFloatcxx . . $ cpFloat# gravityStrengthcxx . . $ gravityStrength. = 5.0e6f; |
| 28 | |
| 29 | static void |
| 30 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 31 | { |
| 32 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | planetGravityVelocityFunccxx . . $ planetGravityVelocityFunc(1f531eb4b4f6d01b).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# gravity, cpFloatcxx . . $ cpFloat# damping, cpFloatcxx . . $ 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 | cpVectcxx . . $ cpVect# p = cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(body); |
| 42 | cpFloatcxx . . $ cpFloat# sqdist = cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(p); |
| 43 | cpVectcxx . . $ cpVect# g = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(p, -gravityStrengthcxx . . $ gravityStrength. / (sqdist * cpfsqrtcxx . . $ `include/chipmunk/chipmunk_types.h:68:10`!(sqdist))); |
| 44 | |
| 45 | cpBodyUpdateVelocitycxx . . $ cpBodyUpdateVelocity(1f531eb4b4f6d01b).(body, g, damping, dt); |
| 46 | } |
| 47 | |
| 48 | static cpVectcxx . . $ cpVect# |
| 49 | rand_poscxx . . $ rand_pos(8d5769cc69967cd4).(cpFloatcxx . . $ cpFloat# radius) |
| 50 | { |
| 51 | cpVectcxx . . $ cpVect# v; |
| 52 | do { |
| 53 | v = cpvcxx . . $ cpv(3142d57d978b2176).(frandcxx . . $ frand(31fa7155c10fdd13).()*(640 - 2*radius) - (320 - radius), frandcxx . . $ frand(31fa7155c10fdd13).()*(480 - 2*radius) - (240 - radius)); |
| 54 | } while(cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(v) < 85.0f); |
| 55 | |
| 56 | return v; |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | add_boxcxx . . $ add_box(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 61 | { |
| 62 | const cpFloatcxx . . $ cpFloat# size = 10.0f; |
| 63 | const cpFloatcxx . . $ cpFloat# mass = 1.0f; |
| 64 | |
| 65 | cpVectcxx . . $ cpVect# verts[] = { |
| 66 | cpvcxx . . $ cpv(3142d57d978b2176).(-size,-size), |
| 67 | cpvcxx . . $ cpv(3142d57d978b2176).(-size, size), |
| 68 | cpvcxx . . $ cpv(3142d57d978b2176).( size, size), |
| 69 | cpvcxx . . $ cpv(3142d57d978b2176).( size,-size), |
| 70 | }; |
| 71 | |
| 72 | cpFloatcxx . . $ cpFloat# radius = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(cpvcxx . . $ cpv(3142d57d978b2176).(size, size)); |
| 73 | cpVectcxx . . $ cpVect# pos = rand_poscxx . . $ rand_pos(8d5769cc69967cd4).(radius); |
| 74 | |
| 75 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(mass, 4, verts, cpvzerocxx . . $ cpvzero., 0.0f))); |
| 76 | cpBodySetVelocityUpdateFunccxx . . $ cpBodySetVelocityUpdateFunc(54ad947a9354141a).(body, planetGravityVelocityFunccxx . . $ planetGravityVelocityFunc(1f531eb4b4f6d01b).); |
| 77 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, pos); |
| 78 | |
| 79 | // Set the box's velocity to put it into a circular orbit from its |
| 80 | // starting position. |
| 81 | cpFloatcxx . . $ cpFloat# r = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(pos); |
| 82 | cpFloatcxx . . $ cpFloat# v = cpfsqrtcxx . . $ `include/chipmunk/chipmunk_types.h:68:10`!(gravityStrengthcxx . . $ gravityStrength. / r) / r; |
| 83 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(body, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(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 | cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(body, v); |
| 88 | cpBodySetAnglecxx . . $ cpBodySetAngle(797bd3bb7a653b39).(body, cpfatan2cxx . . $ `include/chipmunk/chipmunk_types.h:72:10`!(pos.ycxx . . $ cpVect#y., pos.xcxx . . $ cpVect#x.)); |
| 89 | |
| 90 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpPolyShapeNewcxx . . $ cpPolyShapeNew(119891a3da20e8eb).(body, 4, verts, cpTransformIdentitycxx . . $ cpTransformIdentity., 0.0)); |
| 91 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 92 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.7f); |
| 93 | } |
| 94 | |
| 95 | static cpSpacecxx . . $ cpSpace# * |
| 96 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 97 | { |
| 98 | // Create a rouge body to control the planet manually. |
| 99 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 100 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 20); |
| 101 | |
| 102 | planetBodycxx . . $ planetBody. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewKinematiccxx . . $ cpBodyNewKinematic(af6db6e743345a49).()); |
| 103 | cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(planetBodycxx . . $ planetBody., 0.2f); |
| 104 | |
| 105 | for(int i=0; i<30; i++){ |
| 106 | add_boxcxx . . $ add_box(728b9c9a69722ef8).(space); |
| 107 | } |
| 108 | |
| 109 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(planetBodycxx . . $ planetBody., 70.0f, cpvzerocxx . . $ cpvzero.)); |
| 110 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 111 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 112 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 113 | |
| 114 | return space; |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 119 | { |
| 120 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 121 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 122 | } |
| 123 | |
| 124 | ChipmunkDemocxx . . $ ChipmunkDemo# Planetcxx . . $ Planet. = { |
| 125 | "Planet", |
| 126 | 1.0/60.0, |
| 127 | initcxx . . $ init(1f0082e822a7ce76)., |
| 128 | updatecxx . . $ update(fa66a0a48611083e)., |
| 129 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 130 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 131 | }; |