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

Commit

Permalink
Platforming stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
asivitz committed Jul 11, 2012
1 parent 29396f4 commit 0b0ffd7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
16 changes: 16 additions & 0 deletions ext/Body.cpp
Expand Up @@ -76,3 +76,19 @@ Object Body::pos()
//arr.push(pos.y);
//return arr;
}

void Body::push(vec2 vel)
{
body->ApplyLinearImpulse(vel, VECZERO);
}

Object Body::getVel()
{
vec2 vel = body->GetLinearVelocity();
return to_ruby<vec2>(vel);
}

void Body::setVel(vec2 vel)
{
body->SetLinearVelocity(vel);
}
5 changes: 4 additions & 1 deletion ext/Body.h
Expand Up @@ -13,10 +13,13 @@ class Body // : public Rice::Director
{

public:
const b2Body * body;
b2Body * body;
//Platform(Object self);
//~Body(Object self);
Object pos();
void push(vec2 vel);
void setVel(vec2 vel);
Object getVel();
};

template<>
Expand Down
2 changes: 1 addition & 1 deletion ext/Physics.cpp
Expand Up @@ -72,7 +72,7 @@ Rice::Object makeBody(b2Body * b2body)
Physics::Physics()
{

vec2 gravity = vec2(0.0f, -9.8f);
vec2 gravity = vec2(0.0f, -20.0f);
boxworld = new b2World(gravity);
//m_contactListener = new BContactListener();
//boxworld->SetContactListener(m_contactListener);
Expand Down
6 changes: 5 additions & 1 deletion ext/Platform.cpp
Expand Up @@ -181,11 +181,15 @@ void Init_engine()
//.define_constructor(Constructor<Platform, Rice::Object>())
.define_method("addWall", &Physics::addWall)
.define_method("addGrenade", &Physics::addGrenade)
.define_method("addPlayer", &Physics::addPlayer)
.define_method("update", &Physics::tick);

Data_Type<Body> rb_cBody =
define_class<Body>("Body")
.define_method("pos", &Body::pos);
.define_method("pos", &Body::pos)
.define_method("vel", &Body::getVel)
.define_method("vel=", &Body::setVel)
.define_method("push", &Body::push);

/*
Data_Type<Renderer> rb_cRenderer =
Expand Down
41 changes: 29 additions & 12 deletions main.rb
Expand Up @@ -6,49 +6,67 @@
require_relative 'matrix_graphics'
require_relative 'engine'

MAX_VEL = 5.0
MOVE_IMP = 2.0
JUMP_IMP = 20.0
class Player
attr_accessor :pos, :move_imp

def initialize
@pos = vec2(0.0,0.0)
@move_imp = vec2(0.0,0.0)

@texid = $platform.loadImage "images/triangle.png"
@body = $platform.physics.addPlayer([0,5], 2.0)
end

def mat
m = Matrix.identity(4)
m = m.translate(@pos.x,@pos.y,0)
pos = @body.pos
m = m.translate(pos.x,pos.y,0)
m = m.scale(4,4,1)
end

def draw
$platform.addDrawCommand(@texid, self.mat.flatten)
end

def move_right
if @body.vel.x < MAX_VEL
@body.push([MOVE_IMP,0.0])
end
end

def move_left
if @body.vel.x > -MAX_VEL
@body.push([-MOVE_IMP,0.0])
end
end

def jump
@body.push([0.0,JUMP_IMP])
end
end

def process_input key_map
if key_map[71]
$one.move_imp.x = -1.0
$one.move_left
elsif key_map[72]
$one.move_imp.x = 1.0
else
$one.move_imp.x = 0
$one.move_right
end

if key_map[73]
$one.jump
end

if key_map[16]
$running = false
end
end

def update
$one.pos += $one.move_imp
end

$platform = Platform.new
wall = $platform.physics.addWall([0,0],[10,1])

grenade = $platform.physics.addGrenade([0,10],1.0)
$grenade = $platform.physics.addGrenade([0,10],1.0)

$running = true
$one = Player.new
Expand All @@ -74,5 +92,4 @@ def update
$platform.draw

process_input $platform.key_map
update
end
10 changes: 10 additions & 0 deletions matrix_graphics.rb
Expand Up @@ -4,6 +4,16 @@ def vec2(x,y)
Matrix.column_vector([x,y])
end

class Array
def x
self[0]
end

def y
self[1]
end
end

class Matrix
#vector stuff
def x
Expand Down

0 comments on commit 0b0ffd7

Please sign in to comment.