Skip to content

Commit

Permalink
Add Material
Browse files Browse the repository at this point in the history
Give every Body a Material, with stiffness and toughness.
  • Loading branch information
Alexander Berntsen authored and stiell committed Feb 21, 2013
1 parent 6801955 commit 59fef03
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 25 deletions.
30 changes: 20 additions & 10 deletions src/character.cxx
Expand Up @@ -24,24 +24,29 @@

int Character::_collisionGroup_ = 0;

Character::Character(state2p state, phys_t orientation) :
Character::Character(state2p state, phys_t orientation, Material* materialBody,
Material* materialHead, Material* materialLimbs,
Material* materialLimbsOff) :
shapeBody_(0.25),
shapeHead_(0.15),
shapeFoot_(0.075),
shapeHand_(0.1),
// Physical object
body_(this, state, 100, orientation, 0, momentInertia(100, 0.25, 0.4),
&shapeBody_, _collisionGroup_),
&shapeBody_, materialBody, _collisionGroup_),
head_(getStateAt(vector2p()(0.0, 0.40)), 3, orientation, 0,
momentInertia(2.5, 0.15, 0.4), &shapeHead_, _collisionGroup_),
momentInertia(2.5, 0.15, 0.4), &shapeHead_, materialHead,
_collisionGroup_),
footBack_(getStateAt(vector2p()(0.0, -0.40)), 1, orientation, 0,
momentInertia(1, 0.075, 0.4), &shapeFoot_, _collisionGroup_),
momentInertia(1, 0.075, 0.4), &shapeFoot_, materialLimbs,
_collisionGroup_),
footFront_(getStateAt(vector2p()(0.0, -0.40)), 1, orientation, 0,
momentInertia(1, 0.075, 0.4), &shapeFoot_, _collisionGroup_),
momentInertia(1, 0.075, 0.4), &shapeFoot_, materialLimbs,
_collisionGroup_),
handBack_(state, 2, orientation, 0, momentInertia(2, 0.1, 0.4),
&shapeHand_, _collisionGroup_),
&shapeHand_, materialLimbs, _collisionGroup_),
handFront_(state, 2, orientation, 0, momentInertia(2, 0.1, 0.4),
&shapeHand_, _collisionGroup_),
&shapeHand_, materialLimbs, _collisionGroup_),
// Links
neck_(&body_, &head_, 1500.0, 150.0, 1.0, 1.0),
legBack_(&body_, &footBack_, 200.0, 20.0, 1.0, 1.0),
Expand All @@ -51,7 +56,8 @@ Character::Character(state2p state, phys_t orientation) :
// Velocity
vel_(0),
// State
dead_(false) {
dead_(false),
materialLimbsOff_(materialLimbsOff) {
actions_[LEFT].intention = true;
neck_.setPosition(vector2p()(0.0, 0.40));
legBack_.setPosition(vector2p()(0.0, -0.40));
Expand Down Expand Up @@ -107,6 +113,10 @@ void Character::die() {
legBack_.setEnabled(false);
armBack_.setEnabled(false);
armFront_.setEnabled(false);
footBack_.setMaterial(materialLimbsOff_);
footFront_.setMaterial(materialLimbsOff_);
handBack_.setMaterial(materialLimbsOff_);
handFront_.setMaterial(materialLimbsOff_);
}

void Character::crouch(bool state) {
Expand Down Expand Up @@ -185,9 +195,9 @@ state2p Character::getStateAt(vector2p p) {

Character::CharacterBody::CharacterBody(Character* parent, state2p state,
phys_t mass, phys_t orientation, phys_t angVel, phys_t inertiaMoment,
Shape<phys_t>* shape, int collisionGroup) :
Shape<phys_t>* shape, Material* material, int collisionGroup) :
SmallBody(state, mass, orientation, angVel, inertiaMoment, shape,
collisionGroup), parent_(parent), walkCycle_(0.0) {
material, collisionGroup), parent_(parent), walkCycle_(0.0) {
}

bool Character::CharacterBody::interact(AstroBody* body, double deltaTime,
Expand Down
9 changes: 7 additions & 2 deletions src/character.hxx
Expand Up @@ -31,7 +31,7 @@ public:
public:
CharacterBody(Character* parent, state2p state, phys_t mass,
phys_t orientation, phys_t angVel, phys_t inertiaMoment,
Shape<phys_t>* shape, int collisionGroup);
Shape<phys_t>* shape, Material* material, int collisionGroup);
void changeMass(phys_t delta);
protected:
Character* parent_;
Expand All @@ -41,7 +41,9 @@ public:
CharacterBody(const CharacterBody&);
CharacterBody& operator=(const CharacterBody&);
};
Character(state2p state, phys_t orientation);
Character(state2p state, phys_t orientation, Material* materialBody,
Material* materialHead, Material* materialLimbs, Material*
materialLimbsOff);
void addToUniverse(GameUniverse* u);
bool isDead();
char getOrientation();
Expand All @@ -64,6 +66,8 @@ public:
/** Update power meters. */
void update(double deltaTime);
private:
Character(const Character&);
Character& operator=(const Character&);
class Action {
public:
Action();
Expand All @@ -78,6 +82,7 @@ private:
FixtureSpring neck_, legBack_, legFront_, armBack_, armFront_;
static int _collisionGroup_;
SmallBody head_, footBack_, footFront_, handBack_, handFront_;
Material* materialLimbsOff_;
state2p getStateAt(vector2p p);
friend class CharacterGraphic;
};
Expand Down
12 changes: 10 additions & 2 deletions src/collision_handler.cxx
Expand Up @@ -33,10 +33,18 @@ CollisionHandler* CollisionHandler::getInstance() {

void CollisionHandler::collide(Body* body0, Body* body1, phys_t impulse) {
std::map<Body*, Character*>::iterator it;
phys_t dmg = 0.001 * impulse * impulse * (body0->getInvMass() +
body1->getInvMass()), dmg0, dmg1;
Material* m0 = body0->getMaterial();
Material* m1 = body1->getMaterial();
phys_t s0 = m0->getStiffness();
phys_t s1 = m1->getStiffness();
dmg0 = dmg * s1 / (s0 + s1);
dmg1 = dmg - dmg0;
if ((it = monitored_.find(body0)) != monitored_.end())
it->second->hit(body0, impulse);
it->second->hit(body0, dmg0 / m0->getToughness());
if ((it = monitored_.find(body1)) != monitored_.end())
it->second->hit(body1, impulse);
it->second->hit(body1, dmg1 / m1->getToughness());
}

void CollisionHandler::monitor(Body* body, Character* character) {
Expand Down
2 changes: 2 additions & 0 deletions src/game.hxx
Expand Up @@ -67,6 +67,8 @@ private:
GameUniverse* universe_;
GLuint tex_;
GraphicFixture* planetFixture_;
Material* matCharBody_, * matCharHead_, * matCharLimbs_,
* matCharLimbsOff_, * matPlanet_;
std::vector<Label*> massIndicatorLabels_;
std::vector<MassIndicator*> massIndicators_;
std::vector<MassIndicatorGraphic*> massIndicatorGfx_;
Expand Down
15 changes: 13 additions & 2 deletions src/game/game.cxx
Expand Up @@ -65,6 +65,11 @@ Game::Game(Screen* screen) :
foreground_(NULL),
backgroundSprite_(NULL),
planetDisk_(NULL),
matCharBody_(NULL),
matCharHead_(NULL),
matCharLimbs_(NULL),
matCharLimbsOff_(NULL),
matPlanet_(NULL),
massIndicators_(),
massIndicatorGfx_() {
tex_ = getTexture(PACKAGE_GFX_DIR "background.png");
Expand Down Expand Up @@ -164,8 +169,13 @@ void Game::conceive() {
// Characters
phys_t angle = 2 * PI / _NUM_PLAYERS;
vector2p pos = { _R, 0 }, vel = { 0, _S }, a = vector2p::fromAngle(angle);
matCharBody_ = new Material(100.0, 0.5);
matCharHead_ = new Material(10000.0, 0.1);
matCharLimbs_ = new Material(50000.0, 1.5);
matCharLimbsOff_ = new Material(500.0, 1.5);
for (int i = 0; i < _NUM_PLAYERS; ++i) {
characters_.push_back(new Character(state2p()(pos, vel), i * angle));
characters_.push_back(new Character(state2p()(pos, vel), i * angle,
matCharBody_, matCharHead_, matCharLimbs_, matCharLimbsOff_));
players_.push_back(new Player(characters_[i]));
characterGraphics_.push_back(new CharacterGraphic(characters_[i]));
char font[256];
Expand All @@ -187,8 +197,9 @@ void Game::conceive() {
}
// Planets
planetCircle_ = new Circle<phys_t> (_PR);
matPlanet_ = new Material(100.0, 50);
planets_.push_back(new AstroBody(_GM, 2 * _GM * _PR * _PR / 5, -0.05,
planetCircle_));
planetCircle_, matPlanet_));
universe_ = new GameUniverse(planets_[0]);
// Graphics
backgroundSprite_ = new Sprite(tex_, 1, 1);
Expand Down
14 changes: 13 additions & 1 deletion src/physics.hxx
Expand Up @@ -69,6 +69,15 @@ struct bodystate {
const bodystate operator*(const phys_t f) const;
};

class Material {
public:
Material(phys_t stiffness, phys_t toughness);
phys_t getStiffness();
phys_t getToughness();
private:
phys_t stiffness_, toughness_;
};

class Particle {
public:
state2p getState();
Expand Down Expand Up @@ -101,6 +110,8 @@ public:
phys_t getOrientation();
phys_t getAngularVelocity();
phys_t getMomentOfInertia();
Material* getMaterial();
void setMaterial(Material* m);
Shape<phys_t>* getShape();
phys_t getAngularMomentum();
vector2p getMomentumAt(vector2p p, vector2p vp);
Expand All @@ -112,8 +123,9 @@ protected:
phys_t av_;
phys_t moi_;
Shape<phys_t>* shape_;
Material* material_;
Body(state2p s, phys_t mass, phys_t orientation, phys_t av, phys_t moi,
Shape<phys_t>* shape, bool immovable = false);
Shape<phys_t>* shape, Material* material, bool immovable = false);
void applyAngularImpulse(phys_t i);
void applyImpulseAt(vector2p i, vector2p p);
void setBodyState(bodystate s);
Expand Down
6 changes: 4 additions & 2 deletions src/physics/game_physics.hxx
Expand Up @@ -30,7 +30,8 @@ class Universe;
class SmallBody: public Body {
public:
SmallBody(state2p s, phys_t mass, phys_t orientation, phys_t av,
phys_t moi, Shape<phys_t>* shape, int collisionGroup);
phys_t moi, Shape<phys_t>* shape, Material* material,
int collisionGroup);
protected:
virtual bool interact(class AstroBody* bvz, double dt, vector2p& p,
vector2p& im);
Expand All @@ -49,7 +50,8 @@ private:
class AstroBody: public Body {
public:
const phys_t gm;
AstroBody(phys_t gm, phys_t moi, phys_t av, Shape<phys_t>* shape);
AstroBody(phys_t gm, phys_t moi, phys_t av, Shape<phys_t>* shape,
Material* material);
friend class GameUniverse;
};

Expand Down
10 changes: 6 additions & 4 deletions src/physics/game_physics/game_physics.cxx
Expand Up @@ -24,8 +24,9 @@
#include "physics/game_physics.hxx"

SmallBody::SmallBody(state2p s, phys_t mass, phys_t orientation, phys_t av,
phys_t moi, Shape<phys_t>* shape, int collisionGroup) :
Body(s, mass, orientation, av, moi, shape),
phys_t moi, Shape<phys_t>* shape, Material* material,
int collisionGroup) :
Body(s, mass, orientation, av, moi, shape, material),
nextState_(getBodyState()),
collisionGroup_(collisionGroup) {
}
Expand Down Expand Up @@ -60,9 +61,10 @@ bodystate SmallBody::getNextState(phys_t dt) {
return r;
}

AstroBody::AstroBody(phys_t gm, phys_t moi, phys_t av, Shape<phys_t>* shape) :
AstroBody::AstroBody(phys_t gm, phys_t moi, phys_t av, Shape<phys_t>* shape,
Material* material) :
Body(state2p()(0.0, 0.0, 0.0, 0.0), gm / G, 0.0, av, moi, shape,
true),
material, true),
gm(gm) {
}

Expand Down
25 changes: 23 additions & 2 deletions src/physics_inl.hxx
Expand Up @@ -140,6 +140,19 @@ inline const bodystate bodystate::operator*(const phys_t f) const {
return r;
}

inline Material::Material(phys_t stiffness, phys_t toughness) :
stiffness_(stiffness),
toughness_(toughness) {
}

inline phys_t Material::getToughness() {
return toughness_;
}

inline phys_t Material::getStiffness() {
return stiffness_;
}

inline Particle::Particle(state2p s) :
s_(s) {
}
Expand Down Expand Up @@ -193,9 +206,9 @@ inline void Mass::applyImpulse(vector2p i) {
}

inline Body::Body(state2p s, phys_t mass, phys_t orientation, phys_t av, phys_t
moi, Shape<phys_t>* shape, bool immovable) :
moi, Shape<phys_t>* shape, Material* material, bool immovable) :
Mass(s, mass, immovable), orientation_(orientation), av_(av), moi_(moi),
shape_(shape) {
shape_(shape), material_(material) {
}

inline Body::~Body() {
Expand All @@ -221,6 +234,14 @@ inline phys_t Body::getAngularMomentum() {
return av_ * moi_;
}

inline Material* Body::getMaterial() {
return material_;
}

inline void Body::setMaterial(Material* m) {
material_ = m;
}

inline vector2p Body::getMomentumAt(vector2p p, vector2p vp) {
phys_t pp = p.squared();
vector2p m = (s_.v + ~p * av_ - vp) * mass_;
Expand Down

0 comments on commit 59fef03

Please sign in to comment.