Created
February 24, 2012 05:57
-
-
Save kristianlm/1898187 to your computer and use it in GitHub Desktop.
hello-world from Chicken Scheme with Box2D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require-extension coops bind cplusplus-object ) | |
#> | |
#include <Box2D.h> | |
// is there a way to access c++ class-fields without these | |
// two silly helpers? | |
float getBodyPositionX (b2Body* b) { return b->GetPosition ().x; } | |
float getBodyPositionY (b2Body* b) { return b->GetPosition ().y; } | |
<# | |
; Box2D.scm must be modified: remove constructor and desctuctors of | |
; private ones (body, fixtures) | |
(include "Box2D.scm") | |
(bind "float getBodyPositionX(b2Body* b);") | |
(bind "float getBodyPositionY(b2Body* b);") | |
(define (getBodyY obj) | |
(getBodyPositionY (slot-value obj 'this))) | |
(define (getBodyX obj) | |
(getBodyPositionX (slot-value obj 'this))) | |
(define (pointer obj) | |
(slot-value obj 'this)) | |
(define (my-new . args) (apply new args)) | |
(define gravity (make-b2Vec2 0 1 )) | |
(define world (my-new <b2World> gravity)) | |
(print "gravity is " (b2Vec2-x gravity) ", " (b2Vec2-y gravity)) | |
(print "world is " world) | |
(define groundBodyDef (apply make-b2BodyDef '(2 ; type | |
0 ;ang | |
0 ;ang vel | |
0 ; lin damp | |
0 ; ang damp | |
#t ; sleepable | |
#t ;awake | |
#f ; fixed rot | |
#f ; bullet | |
#t ; active | |
#f ;userdata | |
1 ; grav scale | |
))) | |
(define boxBody (CreateBody world groundBodyDef)) | |
(define boxShape (my-new <b2PolygonShape> )) | |
(SetAsBox boxShape 10 10 (make-b2Vec2 0 0) 0) | |
(CreateFixture boxBody boxShape 0) | |
(let loop ([ n 30000]) | |
(if (> n 0) | |
(begin | |
(Step world (/ 1 60) 6 1) | |
(print "body pos = " (getBodyPositionY (pointer boxBody))) | |
(loop (sub1 n))))) | |
(repl) | |
(delete world) | |
;(SetGravity world (make-b2Vec2 0 -1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment