Permalink
Browse files

Start integrating contact listener

  • Loading branch information...
1 parent 0b0ffd7 commit cba1601567d0f07333ab284672765718cb13b869 @asivitz committed Jul 11, 2012
Showing with 179 additions and 5 deletions.
  1. +2 −0 engine.rb
  2. +125 −0 ext/BContactListener.cpp
  3. +25 −0 ext/BContactListener.h
  4. +6 −0 ext/Body.cpp
  5. +1 −0 ext/Body.h
  6. +12 −3 ext/Physics.cpp
  7. +2 −2 ext/Physics.h
  8. +1 −0 ext/Platform.cpp
  9. +5 −0 main.rb
View
@@ -1,4 +1,6 @@
#platform is the game engine, not a real platform. bad name choice probably
+#most of the class is defined in the C++ extension.
+#this stuff is just extra hooks for input handling
class Platform
def key_map
@key_map ||= {}
View
@@ -0,0 +1,125 @@
+/*
+ * BContactListener.cpp
+ * Button
+ *
+ * Created by Axis Sivitz on 12/22/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#include "BContactListener.h"
+#include <inttypes.h>
+#include "Physics.h"
+
+void BContactListener::BeginContact(b2Contact* contact)
+{
+ //[contactHandler handleBeginContact:contact];
+
+
+ /*
+ b2Body * bodyA = contact->GetFixtureA()->GetBody();
+ b2Body * bodyB = contact->GetFixtureB()->GetBody();
+
+ PhysicsObject * objA = physObjForBody(bodyA);
+ PhysicsObject * objB = physObjForBody(bodyB);
+
+ if ([objA respondsToSelector:@selector(enter:)])
+ {
+ [(id)objA enter:bodyB];
+ }
+ else if ([objB respondsToSelector:@selector(enter:)])
+ {
+ [(id)objB enter:bodyA];
+ }*/
+}
+
+void BContactListener::EndContact(b2Contact* contact)
+{
+ //[contactHandler handleEndContact:contact];
+
+ /*
+ b2Body * bodyA = contact->GetFixtureA()->GetBody();
+ b2Body * bodyB = contact->GetFixtureB()->GetBody();
+
+ PhysicsObject * objA = physObjForBody(bodyA);
+ PhysicsObject * objB = physObjForBody(bodyB);
+
+ if ([objA respondsToSelector:@selector(exit:)])
+ {
+ [(id)objA exit:bodyB];
+ }
+ else if ([objB respondsToSelector:@selector(exit:)])
+ {
+ [(id)objB exit:bodyA];
+ }*/
+}
+
+void BContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
+{
+ //[contactHandler handlePreSolve:contact];
+
+ /*
+ b2Body * bodyA = contact->GetFixtureA()->GetBody();
+ b2Body * bodyB = contact->GetFixtureB()->GetBody();
+
+ int enta = (int)bodyA->GetUserData();
+ int entb = (int)bodyB->GetUserData();
+
+ if (enta == entb)
+ {
+ contact->SetEnabled(false);
+ }
+ */
+
+ /*
+ b2WorldManifold worldManifold;
+ contact->GetWorldManifold(&worldManifold);
+
+ vec2 contactPoint = VECZERO;
+ for (int i = 0; i < contact->GetManifold()->pointCount; i++)
+ {
+ contactPoint += worldManifold.points[i];
+ }
+ contactPoint *= (1.0/contact->GetManifold()->pointCount);
+
+
+ b2Body * bodyA = contact->GetFixtureA()->GetBody();
+ b2Body * bodyB = contact->GetFixtureB()->GetBody();
+
+ PhysicsObject * objA = physObjForBody(bodyA);
+ PhysicsObject * objB = physObjForBody(bodyB);
+
+ if ([objA handlePreCollision:objB ContactPoint:contactPoint MyFixture:contact->GetFixtureA()])
+ contact->SetEnabled(false);
+
+ if ([objB handlePreCollision:objA ContactPoint:contactPoint MyFixture:contact->GetFixtureB()])
+ contact->SetEnabled(false);*/
+}
+
+void BContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
+{
+ b2Body * bodyA = contact->GetFixtureA()->GetBody();
+ b2Body * bodyB = contact->GetFixtureB()->GetBody();
+
+ int enta = (intptr_t)bodyA->GetUserData();
+ int entb = (intptr_t)bodyB->GetUserData();
+
+ b2WorldManifold worldManifold;
+ contact->GetWorldManifold(&worldManifold);
+
+ vec2 contactPoint = VECZERO;
+ for (int i = 0; i < contact->GetManifold()->pointCount; i++)
+ {
+ contactPoint += worldManifold.points[i];
+ }
+ contactPoint *= (1.0/contact->GetManifold()->pointCount);
+
+ float imp = 0;
+ for (int i = 0; i < contact->GetManifold()->pointCount; i++)
+ {
+ imp += fabs(impulse->normalImpulses[i]);
+ // NSLog(@"Normal impulse:%f Tangent Impulse:%f", impulse->normalImpulses[i], impulse->tangentImpulses[i]);
+ }
+
+ //[contactHandler addCollisionBetweenEntA:enta entB:entb contactPoint:contactPoint impulse:imp];
+}
View
@@ -0,0 +1,25 @@
+/*
+ * BContactListener.h
+ * Button
+ *
+ * Created by Axis Sivitz on 12/22/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#ifndef __B_CONTACT_LISTENER_H_
+#define __B_CONTACT_LISTENER_H_
+
+#include <Box2D/Box2D.h>
+
+class BContactListener : public b2ContactListener
+{
+
+ public:
+ void BeginContact(b2Contact* contact);
+ void EndContact(b2Contact* contact);
+ void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
+ void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
+};
+
+#endif
View
@@ -1,4 +1,5 @@
#include "Body.h"
+#include <inttypes.h>
#include <iostream>
#include "rice/Class.hpp"
@@ -92,3 +93,8 @@ void Body::setVel(vec2 vel)
{
body->SetLinearVelocity(vel);
}
+
+int Body::getId()
+{
+ return (intptr_t)body->GetUserData();
+}
View
@@ -20,6 +20,7 @@ class Body // : public Rice::Director
void push(vec2 vel);
void setVel(vec2 vel);
Object getVel();
+ int getId();
};
template<>
View
@@ -10,7 +10,7 @@
#include "Body.h"
#import <Box2D/Box2D.h>
-//#import "BContactListener.h"
+#import "BContactListener.h"
//#import "RayCastCallbacks.h"
//#import "QueryCallbacks.h"
//#import "global.h"
@@ -51,6 +51,12 @@ vec2 rotate(vec2 inVec, float angle)
return res;
}
+int globalId = 0;
+int getNewBodyId()
+{
+ return(globalId++);
+}
+
/*
template<>
Rice::Object to_ruby<b2Body>(b2Body const & param)
@@ -74,8 +80,8 @@ Physics::Physics()
vec2 gravity = vec2(0.0f, -20.0f);
boxworld = new b2World(gravity);
- //m_contactListener = new BContactListener();
- //boxworld->SetContactListener(m_contactListener);
+ m_contactListener = new BContactListener();
+ boxworld->SetContactListener(m_contactListener);
defaultFilter.categoryBits = 0x0001;
defaultFilter.maskBits = 0xFFFF;
@@ -116,6 +122,7 @@ Object Physics::addWall(vec2 pos, vec2 extens)
bodyDef.position.Set(pos.x, pos.y);
b2Body* body = boxworld->CreateBody(&bodyDef);
+ body->SetUserData((void *)getNewBodyId());
b2PolygonShape crateBox;
@@ -233,6 +240,7 @@ Object Physics::addGrenade(vec2 pos, float size)
//bodyDef.bullet = true;
b2Body* body = boxworld->CreateBody(&bodyDef);
+ body->SetUserData((void *)getNewBodyId());
b2CircleShape dynamicCircle;
dynamicCircle.m_radius = size;
@@ -257,6 +265,7 @@ Object Physics::addPlayer(vec2 pos, float size)
bodyDef.fixedRotation = true;
b2Body* body = boxworld->CreateBody(&bodyDef);
+ body->SetUserData((void *)getNewBodyId());
b2PolygonShape crateBox;
View
@@ -10,10 +10,10 @@
#include <Box2D/Box2D.h>
#include "GLES-Render.h"
+#include "BContactListener.h"
#include "rice/Class.hpp"
using namespace Rice;
-//#import "BContactListener.h"
//#import <vector>
@@ -34,7 +34,7 @@ using namespace std;
class Physics
{
private:
- //BContactListener * m_contactListener;
+ BContactListener * m_contactListener;
public:
b2Filter defaultFilter;
View
@@ -189,6 +189,7 @@ void Init_engine()
.define_method("pos", &Body::pos)
.define_method("vel", &Body::getVel)
.define_method("vel=", &Body::setVel)
+ .define_method("id", &Body::getId)
.define_method("push", &Body::push);
/*
View
@@ -6,17 +6,22 @@
require_relative 'matrix_graphics'
require_relative 'engine'
+# maps physics body ids to game objects
+$body_map = {}
+
MAX_VEL = 5.0
MOVE_IMP = 2.0
JUMP_IMP = 20.0
class Player
attr_accessor :pos, :move_imp
+ attr_reader :body
def initialize
@move_imp = vec2(0.0,0.0)
@texid = $platform.loadImage "images/triangle.png"
@body = $platform.physics.addPlayer([0,5], 2.0)
+ $body_map[@body.id] = self
end
def mat

0 comments on commit cba1601

Please sign in to comment.