Skip to content
Browse files

Platforming stuff

  • Loading branch information...
1 parent 29396f4 commit 0b0ffd755548a7e9459262ee34c3c4a386c25d26 @asivitz committed
Showing with 65 additions and 15 deletions.
  1. +16 −0 ext/Body.cpp
  2. +4 −1 ext/Body.h
  3. +1 −1 ext/Physics.cpp
  4. +5 −1 ext/Platform.cpp
  5. +29 −12 main.rb
  6. +10 −0 matrix_graphics.rb
View
16 ext/Body.cpp
@@ -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);
+}
View
5 ext/Body.h
@@ -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<>
View
2 ext/Physics.cpp
@@ -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);
View
6 ext/Platform.cpp
@@ -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 =
View
41 main.rb
@@ -6,34 +6,56 @@
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]
@@ -41,14 +63,10 @@ def process_input key_map
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
@@ -74,5 +92,4 @@ def update
$platform.draw
process_input $platform.key_map
- update
end
View
10 matrix_graphics.rb
@@ -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

0 comments on commit 0b0ffd7

Please sign in to comment.
Something went wrong with that request. Please try again.