Permalink
Browse files

Physics system directly accessible

  • Loading branch information...
1 parent e9ae471 commit 29396f4a98ed4ec3c70c4760be342a56f4f86014 @asivitz committed Jul 11, 2012
Showing with 76 additions and 137 deletions.
  1. +18 −0 ext/Body.cpp
  2. +7 −1 ext/Body.h
  3. +25 −96 ext/Physics.cpp
  4. +6 −7 ext/Physics.h
  5. +15 −28 ext/Platform.cpp
  6. +1 −2 ext/Platform.h
  7. +4 −3 main.rb
View
@@ -49,6 +49,24 @@ vec2 from_ruby<vec2>(Rice::Object x)
return VECNULL;
}
+
+/*
+template<>
+vec2 from_ruby<vec2>(Rice::Object x)
+{
+ Array arr(x);
+ if (arr.size() == 2)
+ {
+ vec2 v;
+ VALUE * carr = arr.to_c_array();
+ v.x = NUM2DBL(carr[0]);
+ v.y = NUM2DBL(carr[1]);
+ return v;
+ }
+ return VECNULL;
+}
+*/
+
Object Body::pos()
{
vec2 pos = body->GetPosition();
View
@@ -13,11 +13,17 @@ class Body // : public Rice::Director
{
public:
- b2Body * body;
+ const b2Body * body;
//Platform(Object self);
//~Body(Object self);
Object pos();
};
+template<>
+Rice::Object to_ruby<vec2>(vec2 const & param);
+
+template<>
+vec2 from_ruby<vec2>(Rice::Object x);
+
#endif
View
@@ -7,6 +7,7 @@
//
#import "Physics.h"
+#include "Body.h"
#import <Box2D/Box2D.h>
//#import "BContactListener.h"
@@ -50,6 +51,24 @@ vec2 rotate(vec2 inVec, float angle)
return res;
}
+/*
+template<>
+Rice::Object to_ruby<b2Body>(b2Body const & param)
+{
+ Body body;
+ body.body = &param;
+ return to_ruby<Body>(body);
+}
+*/
+
+Rice::Object makeBody(b2Body * b2body)
+{
+ Body * body = new Body();
+ body->body = b2body;
+ Data_Object<Body> obj(body);
+ return obj;
+}
+
Physics::Physics()
{
@@ -90,7 +109,7 @@ void Physics::initDebugDrawing()
boxworld->SetDebugDraw(m_debugDraw);
}
-b2Body * Physics::addWall(vec2 pos, vec2 extens)
+Object Physics::addWall(vec2 pos, vec2 extens)
{
b2BodyDef bodyDef;
@@ -113,97 +132,7 @@ b2Body * Physics::addWall(vec2 pos, vec2 extens)
body->CreateFixture(&crateBox, 0.0);
//body->CreateFixture(&fixtureDef);
- return body;
-}
-
-b2Body * Physics::addButton(vec2 pos, float size)
-{
- b2BodyDef bodyDef;
-
- bodyDef.type = b2_dynamicBody;
- bodyDef.position.Set(pos.x, pos.y);
- bodyDef.fixedRotation = true;
-
- b2Body* body = boxworld->CreateBody(&bodyDef);
-
- b2PolygonShape dynamicBox;
-
- dynamicBox.SetAsBox(size, size);
-
- b2FixtureDef fixtureDef;
-
- fixtureDef.shape = &dynamicBox;
-
- fixtureDef.filter = defaultFilter;
- fixtureDef.density = 0.75f;
- fixtureDef.friction = 0.0f;
- fixtureDef.restitution = 1.0;
- body->CreateFixture(&fixtureDef);
-
- return body;
-}
-
-
-b2Body * Physics::addGrate(vec2 pos, vec2 extens)
-{
- b2BodyDef bodyDef;
-
- bodyDef.position.Set(pos.x, pos.y);
-
- b2Body* body = boxworld->CreateBody(&bodyDef);
-
- b2PolygonShape crateBox;
-
- crateBox.SetAsBox(extens.x, extens.y);
-
- b2FixtureDef fixtureDef;
-
- fixtureDef.shape = &crateBox;
-
- b2Filter filter;
- filter.categoryBits = 0x0001;
- filter.maskBits = 0xFF1F;
- filter.groupIndex = 0;
-
- fixtureDef.filter = filter;
- fixtureDef.density = 1.0f;
- fixtureDef.friction = 0.0f;
- fixtureDef.restitution = 0.8;
- body->CreateFixture(&fixtureDef);
-
- return body;
-}
-
-b2Body * Physics::addDoor(vec2 pos, vec2 extens)
-{
- b2BodyDef bodyDef;
-
- bodyDef.position.Set(pos.x, pos.y);
- bodyDef.type = b2_kinematicBody;
- bodyDef.fixedRotation = true;
-
- b2Body* body = boxworld->CreateBody(&bodyDef);
-
- b2PolygonShape crateBox;
-
- crateBox.SetAsBox(extens.x, extens.y);
-
- b2Filter filter;
- filter.categoryBits = 0x0008;
- filter.maskBits = 0x0036;
- filter.groupIndex = 0;
-
- b2FixtureDef fixtureDef;
-
- fixtureDef.shape = &crateBox;
- fixtureDef.filter = filter;
- fixtureDef.density = 1.0f;
- fixtureDef.friction = 0.3f;
- fixtureDef.restitution = 0.8;
- body->CreateFixture(&fixtureDef);
- body->SetSleepingAllowed(false);
-
- return body;
+ return makeBody(body);
}
b2Body * Physics::addSensor(vec2 pos, vec2 extens)
@@ -294,7 +223,7 @@ b2Body * Physics::addParticle(vec2 pos)
return body;
}
-b2Body * Physics::addGrenade(vec2 pos, float size)
+Object Physics::addGrenade(vec2 pos, float size)
{
b2BodyDef bodyDef;
@@ -317,10 +246,10 @@ b2Body * Physics::addGrenade(vec2 pos, float size)
fixtureDef.restitution = 0.3f;
body->CreateFixture(&fixtureDef);
- return body;
+ return makeBody(body);
}
-b2Body * Physics::addPlayer(vec2 pos, float size)
+Object Physics::addPlayer(vec2 pos, float size)
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
@@ -343,7 +272,7 @@ b2Body * Physics::addPlayer(vec2 pos, float size)
fixtureDef.restitution = 0.3f;
body->CreateFixture(&fixtureDef);
- return body;
+ return makeBody(body);
}
/*
View
@@ -10,6 +10,9 @@
#include <Box2D/Box2D.h>
#include "GLES-Render.h"
+#include "rice/Class.hpp"
+
+using namespace Rice;
//#import "BContactListener.h"
//#import <vector>
@@ -43,16 +46,12 @@ class Physics
~Physics();
void initDebugDrawing();
void debugDraw();
- b2Body * addButton(vec2 pos, float size);
- b2Body * addWall(vec2 pos, vec2 extens);
- b2Body * addGrate(vec2 pos, vec2 extens);
- b2Body * addDoor(vec2 pos, vec2 extens);
+ Object addWall(vec2 pos, vec2 extens);
//b2Body * castRay(vec2 from, vec2 to);
b2Body * addParticle(vec2 pos);
b2Body * addSensor(vec2 pos, vec2 extens);
- b2Body * addWormSection(vec2 pos, float radius);
- b2Body * addGrenade(vec2 pos, float size);
- b2Body * addPlayer(vec2 pos, float size);
+ Object addGrenade(vec2 pos, float size);
+ Object addPlayer(vec2 pos, float size);
b2Body * addEmptyBody(vec2 pos);
b2Body * addDummy(vec2 pos, vec2 extens);
void remove(b2Body * obj);
View
@@ -38,32 +38,6 @@ Platform::~Platform()
delete window;
}
-Object Platform::addWall(float x, float y, float xextens, float yextens)
-{
- if (physics)
- {
- b2Body * b2body = physics->addWall(vec2(x,y), vec2(xextens, yextens));
- Body * body = new Body();
- body->body = b2body;
- Data_Object<Body> obj(body);
- return obj;
- }
- return NULL;
-}
-
-Object Platform::addGrenade(float x, float y, float size)
-{
- if (physics)
- {
- b2Body * b2body = physics->addGrenade(vec2(x,y), size);
- Body * body = new Body();
- body->body = b2body;
- Data_Object<Body> obj(body);
- return obj;
- }
- return NULL;
-}
-
void Platform::draw()
{
sf::Event event;
@@ -181,6 +155,11 @@ void Platform::setViewMatrix(Array a)
}
}
+Object Platform::getPhysics()
+{
+ return Rice::Data_Object<Physics>(physics, Rice::Data_Type<Physics>::klass(), 0, 0);
+}
+
extern "C"
void Init_engine()
{
@@ -191,11 +170,19 @@ void Init_engine()
.define_method("setViewMatrix", &Platform::setViewMatrix)
.define_method("isWindowOpen", &Platform::isWindowOpen)
.define_method("loadImage", &Platform::loadImage)
- .define_method("addWall", &Platform::addWall)
- .define_method("addGrenade", &Platform::addGrenade)
+ //.define_method("addWall", &Platform::addWall)
+ //.define_method("addGrenade", &Platform::addGrenade)
.define_method("update", &Platform::update)
+ .define_method("physics", &Platform::getPhysics)
.define_method("draw", &Platform::draw);
+ Data_Type<Physics> rb_cPhysics =
+ define_class<Physics>("Physics")
+ //.define_constructor(Constructor<Platform, Rice::Object>())
+ .define_method("addWall", &Physics::addWall)
+ .define_method("addGrenade", &Physics::addGrenade)
+ .define_method("update", &Physics::tick);
+
Data_Type<Body> rb_cBody =
define_class<Body>("Body")
.define_method("pos", &Body::pos);
View
@@ -21,12 +21,11 @@ class Platform : public Rice::Director
~Platform();
void update(double time);
void draw();
+ Object getPhysics();
void addDrawCommand(int texid, Array a);
void setViewMatrix(Array a);
bool isWindowOpen();
int loadImage(string fileName);
- Object addWall(float x, float y, float xextens, float yextens);
- Object addGrenade(float x, float y, float size);
};
#endif
View
@@ -46,9 +46,9 @@ def update
end
$platform = Platform.new
-wall = $platform.addWall(0,0,10,1)
+wall = $platform.physics.addWall([0,0],[10,1])
-grenade = $platform.addGrenade(0,10,1.0)
+grenade = $platform.physics.addGrenade([0,10],1.0)
$running = true
$one = Player.new
@@ -57,6 +57,7 @@ def update
$platform.setViewMatrix(view.flatten)
+$phys = $platform.physics
timeStep = 1/60.0
lastTime = Time.now
accumTime = 0.0
@@ -66,7 +67,7 @@ def update
lastTime = currentTime
while accumTime >= timeStep
- $platform.update(timeStep)
+ $phys.update(timeStep)
accumTime -= timeStep
end
$one.draw

0 comments on commit 29396f4

Please sign in to comment.