Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Physics stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
asivitz committed Jul 10, 2012
1 parent cb6527b commit 7639d12
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
56 changes: 42 additions & 14 deletions ext/Physics.cpp
Expand Up @@ -53,15 +53,16 @@ vec2 rotate(vec2 inVec, float angle)
Physics::Physics()
{

boxworld = new b2World(VECZERO);
//m_contactListener = new BContactListener();
//boxworld->SetContactListener(m_contactListener);
vec2 gravity = vec2(0.0f, -9.8f);
boxworld = new b2World(gravity);
//m_contactListener = new BContactListener();
//boxworld->SetContactListener(m_contactListener);

defaultFilter.categoryBits = 0x0001;
defaultFilter.maskBits = 0xFFFF;
defaultFilter.groupIndex = 0;
defaultFilter.categoryBits = 0x0001;
defaultFilter.maskBits = 0xFFFF;
defaultFilter.groupIndex = 0;

initDebugDrawing();
initDebugDrawing();
}

void Physics::debugDraw()
Expand Down Expand Up @@ -106,10 +107,11 @@ b2Body * Physics::addWall(vec2 pos, vec2 extens)
fixtureDef.shape = &crateBox;
fixtureDef.filter = defaultFilter;

fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.8;
body->CreateFixture(&fixtureDef);
//fixtureDef.density = 1.0f;
//fixtureDef.friction = 0.0f;
//fixtureDef.restitution = 0.8;
body->CreateFixture(&crateBox, 0.0);
//body->CreateFixture(&fixtureDef);

return body;
}
Expand Down Expand Up @@ -299,7 +301,7 @@ b2Body * Physics::addGrenade(vec2 pos, float size)
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(pos.x, pos.y);
//bodyDef.fixedRotation = true;
bodyDef.bullet = true;
//bodyDef.bullet = true;

b2Body* body = boxworld->CreateBody(&bodyDef);
b2CircleShape dynamicCircle;
Expand All @@ -311,8 +313,34 @@ b2Body * Physics::addGrenade(vec2 pos, float size)

fixtureDef.filter = defaultFilter;
fixtureDef.density = 1.0f;
fixtureDef.friction = 10.0f;
fixtureDef.restitution = 1.0;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.3f;
body->CreateFixture(&fixtureDef);

return body;
}

b2Body * Physics::addPlayer(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 crateBox;

crateBox.SetAsBox(0.5 * size, size);

b2FixtureDef fixtureDef;

fixtureDef.shape = &crateBox;

fixtureDef.filter = defaultFilter;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.3f;
body->CreateFixture(&fixtureDef);

return body;
Expand Down
1 change: 1 addition & 0 deletions ext/Physics.h
Expand Up @@ -52,6 +52,7 @@ class Physics
b2Body * addSensor(vec2 pos, vec2 extens);
b2Body * addWormSection(vec2 pos, float radius);
b2Body * addGrenade(vec2 pos, float size);
b2Body * addPlayer(vec2 pos, float size);
b2Body * addEmptyBody(vec2 pos);
b2Body * addDummy(vec2 pos, vec2 extens);
void remove(b2Body * obj);
Expand Down
10 changes: 8 additions & 2 deletions ext/Platform.cpp
Expand Up @@ -64,7 +64,7 @@ Object Platform::addGrenade(float x, float y, float size)
return NULL;
}

void Platform::update()
void Platform::draw()
{
sf::Event event;
while (window->pollEvent(event))
Expand Down Expand Up @@ -99,6 +99,11 @@ void Platform::update()
window->display();
}

void Platform::update(double time)
{
physics->tick(time);
}

bool Platform::isWindowOpen()
{
return window->isOpen();
Expand Down Expand Up @@ -188,7 +193,8 @@ void Init_engine()
.define_method("loadImage", &Platform::loadImage)
.define_method("addWall", &Platform::addWall)
.define_method("addGrenade", &Platform::addGrenade)
.define_method("update", &Platform::update);
.define_method("update", &Platform::update)
.define_method("draw", &Platform::draw);

Data_Type<Body> rb_cBody =
define_class<Body>("Body")
Expand Down
3 changes: 2 additions & 1 deletion ext/Platform.h
Expand Up @@ -19,7 +19,8 @@ class Platform : public Rice::Director
public:
Platform(Object self);
~Platform();
void update();
void update(double time);
void draw();
void addDrawCommand(int texid, Array a);
void setViewMatrix(Array a);
bool isWindowOpen();
Expand Down
22 changes: 16 additions & 6 deletions main.rb
Expand Up @@ -19,7 +19,7 @@ def initialize
def mat
m = Matrix.identity(4)
m = m.translate(@pos.x,@pos.y,0)
m = m.scale(40,40,1)
m = m.scale(4,4,1)
end

def draw
Expand All @@ -46,21 +46,31 @@ def update
end

$platform = Platform.new
wall = $platform.addWall(10,10,100,10)
$platform.addWall(10,40,100,10)
wall = $platform.addWall(0,0,10,1)

grenade = $platform.addGrenade(10,60,5)
grenade = $platform.addGrenade(0,10,1.0)

$running = true
$one = Player.new

view = Matrix.ortho(-150, 150, -150, 150, -30, 1)
view = Matrix.ortho(-15, 15, -15, 15, -30, 1)

$platform.setViewMatrix(view.flatten)

timeStep = 1/60.0
lastTime = Time.now
accumTime = 0.0
while $platform.isWindowOpen and $running
currentTime = Time.now
accumTime += (currentTime - lastTime).to_f
lastTime = currentTime

while accumTime >= timeStep
$platform.update(timeStep)
accumTime -= timeStep
end
$one.draw
$platform.update
$platform.draw

process_input $platform.key_map
update
Expand Down

0 comments on commit 7639d12

Please sign in to comment.