diff options
author | Andrew Kelley <superjoe30@gmail.com> | 2014-08-14 08:58:51 (GMT) |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2014-09-05 02:53:52 (GMT) |
commit | 7fec33d41bd0eaf5ade892a2e917c913c2691469 (patch) | |
tree | e83a34f54e52700884a49ff5fdc0c6a201a857ea | |
parent | 143dd715361dfd52e97520e5e19257a12b6f471b (diff) | |
download | mxe-7fec33d41bd0eaf5ade892a2e917c913c2691469.zip mxe-7fec33d41bd0eaf5ade892a2e917c913c2691469.tar.gz mxe-7fec33d41bd0eaf5ade892a2e917c913c2691469.tar.bz2 |
add chipmunk physics package
-rw-r--r-- | index.html | 4 | ||||
-rw-r--r-- | src/chipmunk-test.c | 68 | ||||
-rw-r--r-- | src/chipmunk.mk | 34 |
3 files changed, 106 insertions, 0 deletions
@@ -1167,6 +1167,10 @@ local-pkg-list: $(LOCAL_PKG_LIST)</pre> <td class="website"><a href="http://www.netlib.org/blas/">cblas</a></td> </tr> <tr> + <td class="package">chipmunk</td> + <td class="website"><a href="https://chipmunk-physics.net/">Chipmunk Physics</a></td> + </tr> + <tr> <td class="package">chromaprint</td> <td class="website"><a href="http://acoustid.org/chromaprint">Chromaprint</a></td> </tr> diff --git a/src/chipmunk-test.c b/src/chipmunk-test.c new file mode 100644 index 0000000..e425d34 --- /dev/null +++ b/src/chipmunk-test.c @@ -0,0 +1,68 @@ +/* + * This file is part of MXE. + * See index.html for further information. + */ + +#include <stdio.h> +#include <chipmunk/chipmunk.h> + +int main(void){ + // cpVect is a 2D vector and cpv() is a shortcut for initializing them. + cpVect gravity = cpv(0, -100); + + // Create an empty space. + cpSpace *space = cpSpaceNew(); + cpSpaceSetGravity(space, gravity); + + // Add a static line segment shape for the ground. + // We'll make it slightly tilted so the ball will roll off. + // We attach it to space->staticBody to tell Chipmunk it shouldn't be movable. + cpShape *ground = cpSegmentShapeNew(space->staticBody, cpv(-20, 5), cpv(20, -5), 0); + cpShapeSetFriction(ground, 1); + cpSpaceAddShape(space, ground); + + // Now let's make a ball that falls onto the line and rolls off. + // First we need to make a cpBody to hold the physical properties of the object. + // These include the mass, position, velocity, angle, etc. of the object. + // Then we attach collision shapes to the cpBody to give it a size and shape. + + cpFloat radius = 5; + cpFloat mass = 1; + + // The moment of inertia is like mass for rotation + // Use the cpMomentFor*() functions to help you approximate it. + cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); + + // The cpSpaceAdd*() functions return the thing that you are adding. + // It's convenient to create and add an object in one line. + cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment)); + cpBodySetPos(ballBody, cpv(0, 15)); + + // Now we create the collision shape for the ball. + // You can create multiple collision shapes that point to the same body. + // They will all be attached to the body and move around to follow it. + cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); + cpShapeSetFriction(ballShape, 0.7); + + // Now that it's all set up, we simulate all the objects in the space by + // stepping forward through time in small increments called steps. + // It is *highly* recommended to use a fixed size time step. + cpFloat timeStep = 1.0/60.0; + for(cpFloat time = 0; time < 2; time += timeStep){ + cpVect pos = cpBodyGetPos(ballBody); + cpVect vel = cpBodyGetVel(ballBody); + printf("Time is %5.2f. ballBody is at (%5.2f, %5.2f). " + "It's velocity is (%5.2f, %5.2f)\n", + time, pos.x, pos.y, vel.x, vel.y); + + cpSpaceStep(space, timeStep); + } + + // Clean up our objects and exit! + cpShapeFree(ballShape); + cpBodyFree(ballBody); + cpShapeFree(ground); + cpSpaceFree(space); + + return 0; +} diff --git a/src/chipmunk.mk b/src/chipmunk.mk new file mode 100644 index 0000000..1dfdcad --- /dev/null +++ b/src/chipmunk.mk @@ -0,0 +1,34 @@ +# This file is part of MXE. +# See index.html for further information. + +PKG := chipmunk +$(PKG)_IGNORE := +$(PKG)_VERSION := 6.2.1 +$(PKG)_CHECKSUM := 4b34fd79d232b523f80f4b7e21d7d7c866bfade0 +$(PKG)_SUBDIR := Chipmunk2D-Chipmunk-$($(PKG)_VERSION) +$(PKG)_FILE := $(PKG)-$($(PKG)_VERSION).tar.gz +$(PKG)_URL := https://github.com/slembcke/Chipmunk2D/archive/Chipmunk-$(PKG_VERSION).tar.gz +$(PKG)_DEPS := gcc + +define $(PKG)_UPDATE + echo 'TODO: write update script for $(PKG).' >&2; + echo $($(PKG)_VERSION) +endef + +define $(PKG)_BUILD + mkdir '$(1)/build' + cd '$(1)/build' && cmake .. \ + -DCMAKE_TOOLCHAIN_FILE='$(CMAKE_TOOLCHAIN_FILE)' \ + -DBUILD_DEMOS=OFF \ + -DINSTALL_DEMOS=OFF \ + -DBUILD_SHARED=OFF \ + -DBUILD_STATIC=ON \ + -DINSTALL_STATIC=ON + + $(MAKE) -C '$(1)/build' -j '$(JOBS)' install + + '$(TARGET)-gcc' \ + -W -Wall -Werror -ansi -pedantic -std=c99 \ + '$(2).c' -o '$(PREFIX)/$(TARGET)/bin/test-chipmunk.exe' \ + -lchipmunk +endef |