Added Box2D

This commit is contained in:
Julian Nießner
2018-05-06 12:43:20 +02:00
parent cb5469bb89
commit d175d433a8
351 changed files with 123545 additions and 1 deletions

View File

@@ -0,0 +1,187 @@
#include "Biped.h"
#include "BipedDef.h"
Biped::Biped(b2World* w, const b2Vec2& position)
{
m_world = w;
BipedDef def;
b2BodyDef bd;
// create body parts
bd = def.LFootDef;
bd.position += position;
LFoot = w->CreateBody(&bd);
LFoot->CreateFixture(&def.LFootPoly);
LFoot->SetMassFromShapes();
bd = def.RFootDef;
bd.position += position;
RFoot = w->CreateBody(&bd);
RFoot->CreateFixture(&def.RFootPoly);
RFoot->SetMassFromShapes();
bd = def.LCalfDef;
bd.position += position;
LCalf = w->CreateBody(&bd);
LCalf->CreateFixture(&def.LCalfPoly);
LCalf->SetMassFromShapes();
bd = def.RCalfDef;
bd.position += position;
RCalf = w->CreateBody(&bd);
RCalf->CreateFixture(&def.RCalfPoly);
RCalf->SetMassFromShapes();
bd = def.LThighDef;
bd.position += position;
LThigh = w->CreateBody(&bd);
LThigh->CreateFixture(&def.LThighPoly);
LThigh->SetMassFromShapes();
bd = def.RThighDef;
bd.position += position;
RThigh = w->CreateBody(&bd);
RThigh->CreateFixture(&def.RThighPoly);
RThigh->SetMassFromShapes();
bd = def.PelvisDef;
bd.position += position;
Pelvis = w->CreateBody(&bd);
Pelvis->CreateFixture(&def.PelvisPoly);
Pelvis->SetMassFromShapes();
bd = def.StomachDef;
bd.position += position;
Stomach = w->CreateBody(&bd);
Stomach->CreateFixture(&def.StomachPoly);
Stomach->SetMassFromShapes();
bd = def.ChestDef;
bd.position += position;
Chest = w->CreateBody(&bd);
Chest->CreateFixture(&def.ChestPoly);
Chest->SetMassFromShapes();
bd = def.NeckDef;
bd.position += position;
Neck = w->CreateBody(&bd);
Neck->CreateFixture(&def.NeckPoly);
Neck->SetMassFromShapes();
bd = def.HeadDef;
bd.position += position;
Head = w->CreateBody(&bd);
Head->CreateFixture(&def.HeadCirc);
Head->SetMassFromShapes();
bd = def.LUpperArmDef;
bd.position += position;
LUpperArm = w->CreateBody(&bd);
LUpperArm->CreateFixture(&def.LUpperArmPoly);
LUpperArm->SetMassFromShapes();
bd = def.RUpperArmDef;
bd.position += position;
RUpperArm = w->CreateBody(&bd);
RUpperArm->CreateFixture(&def.RUpperArmPoly);
RUpperArm->SetMassFromShapes();
bd = def.LForearmDef;
bd.position += position;
LForearm = w->CreateBody(&bd);
LForearm->CreateFixture(&def.LForearmPoly);
LForearm->SetMassFromShapes();
bd = def.RForearmDef;
bd.position += position;
RForearm = w->CreateBody(&bd);
RForearm->CreateFixture(&def.RForearmPoly);
RForearm->SetMassFromShapes();
bd = def.LHandDef;
bd.position += position;
LHand = w->CreateBody(&bd);
LHand->CreateFixture(&def.LHandPoly);
LHand->SetMassFromShapes();
bd = def.RHandDef;
bd.position += position;
RHand = w->CreateBody(&bd);
RHand->CreateFixture(&def.RHandPoly);
RHand->SetMassFromShapes();
// link body parts
def.LAnkleDef.body1 = LFoot;
def.LAnkleDef.body2 = LCalf;
def.RAnkleDef.body1 = RFoot;
def.RAnkleDef.body2 = RCalf;
def.LKneeDef.body1 = LCalf;
def.LKneeDef.body2 = LThigh;
def.RKneeDef.body1 = RCalf;
def.RKneeDef.body2 = RThigh;
def.LHipDef.body1 = LThigh;
def.LHipDef.body2 = Pelvis;
def.RHipDef.body1 = RThigh;
def.RHipDef.body2 = Pelvis;
def.LowerAbsDef.body1 = Pelvis;
def.LowerAbsDef.body2 = Stomach;
def.UpperAbsDef.body1 = Stomach;
def.UpperAbsDef.body2 = Chest;
def.LowerNeckDef.body1 = Chest;
def.LowerNeckDef.body2 = Neck;
def.UpperNeckDef.body1 = Chest;
def.UpperNeckDef.body2 = Head;
def.LShoulderDef.body1 = Chest;
def.LShoulderDef.body2 = LUpperArm;
def.RShoulderDef.body1 = Chest;
def.RShoulderDef.body2 = RUpperArm;
def.LElbowDef.body1 = LForearm;
def.LElbowDef.body2 = LUpperArm;
def.RElbowDef.body1 = RForearm;
def.RElbowDef.body2 = RUpperArm;
def.LWristDef.body1 = LHand;
def.LWristDef.body2 = LForearm;
def.RWristDef.body1 = RHand;
def.RWristDef.body2 = RForearm;
// create joints
LAnkle = (b2RevoluteJoint*)w->CreateJoint(&def.LAnkleDef);
RAnkle = (b2RevoluteJoint*)w->CreateJoint(&def.RAnkleDef);
LKnee = (b2RevoluteJoint*)w->CreateJoint(&def.LKneeDef);
RKnee = (b2RevoluteJoint*)w->CreateJoint(&def.RKneeDef);
LHip = (b2RevoluteJoint*)w->CreateJoint(&def.LHipDef);
RHip = (b2RevoluteJoint*)w->CreateJoint(&def.RHipDef);
LowerAbs = (b2RevoluteJoint*)w->CreateJoint(&def.LowerAbsDef);
UpperAbs = (b2RevoluteJoint*)w->CreateJoint(&def.UpperAbsDef);
LowerNeck = (b2RevoluteJoint*)w->CreateJoint(&def.LowerNeckDef);
UpperNeck = (b2RevoluteJoint*)w->CreateJoint(&def.UpperNeckDef);
LShoulder = (b2RevoluteJoint*)w->CreateJoint(&def.LShoulderDef);
RShoulder = (b2RevoluteJoint*)w->CreateJoint(&def.RShoulderDef);
LElbow = (b2RevoluteJoint*)w->CreateJoint(&def.LElbowDef);
RElbow = (b2RevoluteJoint*)w->CreateJoint(&def.RElbowDef);
LWrist = (b2RevoluteJoint*)w->CreateJoint(&def.LWristDef);
RWrist = (b2RevoluteJoint*)w->CreateJoint(&def.RWristDef);
}
Biped::~Biped(void)
{
m_world->DestroyBody(LFoot);
m_world->DestroyBody(RFoot);
m_world->DestroyBody(LCalf);
m_world->DestroyBody(RCalf);
m_world->DestroyBody(LThigh);
m_world->DestroyBody(RThigh);
m_world->DestroyBody(Pelvis);
m_world->DestroyBody(Stomach);
m_world->DestroyBody(Chest);
m_world->DestroyBody(Neck);
m_world->DestroyBody(Head);
m_world->DestroyBody(LUpperArm);
m_world->DestroyBody(RUpperArm);
m_world->DestroyBody(LForearm);
m_world->DestroyBody(RForearm);
m_world->DestroyBody(LHand);
m_world->DestroyBody(RHand);
}

View File

@@ -0,0 +1,25 @@
#ifndef BIPED_H
#define BIPED_H
#include "Box2D.h"
// Ragdoll class thanks to darkzerox.
class Biped
{
public:
Biped(b2World*, const b2Vec2& position);
~Biped();
private:
b2World* m_world;
b2Body *LFoot, *RFoot, *LCalf, *RCalf, *LThigh, *RThigh,
*Pelvis, *Stomach, *Chest, *Neck, *Head,
*LUpperArm, *RUpperArm, *LForearm, *RForearm, *LHand, *RHand;
b2RevoluteJoint *LAnkle, *RAnkle, *LKnee, *RKnee, *LHip, *RHip,
*LowerAbs, *UpperAbs, *LowerNeck, *UpperNeck,
*LShoulder, *RShoulder, *LElbow, *RElbow, *LWrist, *RWrist;
};
#endif

View File

@@ -0,0 +1,478 @@
#include "BipedDef.h"
int16 BipedDef::count = 0;
const float32 k_scale = 3.0f;
BipedDef::BipedDef()
{
SetMotorTorque(2.0f);
SetMotorSpeed(0.0f);
SetDensity(20.0f);
SetRestitution(0.0f);
SetLinearDamping(0.0f);
SetAngularDamping(0.005f);
SetGroupIndex(--count);
EnableMotor();
EnableLimit();
DefaultVertices();
DefaultPositions();
DefaultJoints();
LFootPoly.friction = RFootPoly.friction = 0.85f;
}
void BipedDef::IsFast(bool b)
{
B2_NOT_USED(b);
/*
LFootDef.isFast = b;
RFootDef.isFast = b;
LCalfDef.isFast = b;
RCalfDef.isFast = b;
LThighDef.isFast = b;
RThighDef.isFast = b;
PelvisDef.isFast = b;
StomachDef.isFast = b;
ChestDef.isFast = b;
NeckDef.isFast = b;
HeadDef.isFast = b;
LUpperArmDef.isFast = b;
RUpperArmDef.isFast = b;
LForearmDef.isFast = b;
RForearmDef.isFast = b;
LHandDef.isFast = b;
RHandDef.isFast = b;
*/
}
void BipedDef::SetGroupIndex(int16 i)
{
LFootPoly.filter.groupIndex = i;
RFootPoly.filter.groupIndex = i;
LCalfPoly.filter.groupIndex = i;
RCalfPoly.filter.groupIndex = i;
LThighPoly.filter.groupIndex = i;
RThighPoly.filter.groupIndex = i;
PelvisPoly.filter.groupIndex = i;
StomachPoly.filter.groupIndex = i;
ChestPoly.filter.groupIndex = i;
NeckPoly.filter.groupIndex = i;
HeadCirc.filter.groupIndex = i;
LUpperArmPoly.filter.groupIndex = i;
RUpperArmPoly.filter.groupIndex = i;
LForearmPoly.filter.groupIndex = i;
RForearmPoly.filter.groupIndex = i;
LHandPoly.filter.groupIndex = i;
RHandPoly.filter.groupIndex = i;
}
void BipedDef::SetLinearDamping(float f)
{
LFootDef.linearDamping = f;
RFootDef.linearDamping = f;
LCalfDef.linearDamping = f;
RCalfDef.linearDamping = f;
LThighDef.linearDamping = f;
RThighDef.linearDamping = f;
PelvisDef.linearDamping = f;
StomachDef.linearDamping = f;
ChestDef.linearDamping = f;
NeckDef.linearDamping = f;
HeadDef.linearDamping = f;
LUpperArmDef.linearDamping = f;
RUpperArmDef.linearDamping = f;
LForearmDef.linearDamping = f;
RForearmDef.linearDamping = f;
LHandDef.linearDamping = f;
RHandDef.linearDamping = f;
}
void BipedDef::SetAngularDamping(float f)
{
LFootDef.angularDamping = f;
RFootDef.angularDamping = f;
LCalfDef.angularDamping = f;
RCalfDef.angularDamping = f;
LThighDef.angularDamping = f;
RThighDef.angularDamping = f;
PelvisDef.angularDamping = f;
StomachDef.angularDamping = f;
ChestDef.angularDamping = f;
NeckDef.angularDamping = f;
HeadDef.angularDamping = f;
LUpperArmDef.angularDamping = f;
RUpperArmDef.angularDamping = f;
LForearmDef.angularDamping = f;
RForearmDef.angularDamping = f;
LHandDef.angularDamping = f;
RHandDef.angularDamping = f;
}
void BipedDef::SetMotorTorque(float f)
{
LAnkleDef.maxMotorTorque = f;
RAnkleDef.maxMotorTorque = f;
LKneeDef.maxMotorTorque = f;
RKneeDef.maxMotorTorque = f;
LHipDef.maxMotorTorque = f;
RHipDef.maxMotorTorque = f;
LowerAbsDef.maxMotorTorque = f;
UpperAbsDef.maxMotorTorque = f;
LowerNeckDef.maxMotorTorque = f;
UpperNeckDef.maxMotorTorque = f;
LShoulderDef.maxMotorTorque = f;
RShoulderDef.maxMotorTorque = f;
LElbowDef.maxMotorTorque = f;
RElbowDef.maxMotorTorque = f;
LWristDef.maxMotorTorque = f;
RWristDef.maxMotorTorque = f;
}
void BipedDef::SetMotorSpeed(float f)
{
LAnkleDef.motorSpeed = f;
RAnkleDef.motorSpeed = f;
LKneeDef.motorSpeed = f;
RKneeDef.motorSpeed = f;
LHipDef.motorSpeed = f;
RHipDef.motorSpeed = f;
LowerAbsDef.motorSpeed = f;
UpperAbsDef.motorSpeed = f;
LowerNeckDef.motorSpeed = f;
UpperNeckDef.motorSpeed = f;
LShoulderDef.motorSpeed = f;
RShoulderDef.motorSpeed = f;
LElbowDef.motorSpeed = f;
RElbowDef.motorSpeed = f;
LWristDef.motorSpeed = f;
RWristDef.motorSpeed = f;
}
void BipedDef::SetDensity(float f)
{
LFootPoly.density = f;
RFootPoly.density = f;
LCalfPoly.density = f;
RCalfPoly.density = f;
LThighPoly.density = f;
RThighPoly.density = f;
PelvisPoly.density = f;
StomachPoly.density = f;
ChestPoly.density = f;
NeckPoly.density = f;
HeadCirc.density = f;
LUpperArmPoly.density = f;
RUpperArmPoly.density = f;
LForearmPoly.density = f;
RForearmPoly.density = f;
LHandPoly.density = f;
RHandPoly.density = f;
}
void BipedDef::SetRestitution(float f)
{
LFootPoly.restitution = f;
RFootPoly.restitution = f;
LCalfPoly.restitution = f;
RCalfPoly.restitution = f;
LThighPoly.restitution = f;
RThighPoly.restitution = f;
PelvisPoly.restitution = f;
StomachPoly.restitution = f;
ChestPoly.restitution = f;
NeckPoly.restitution = f;
HeadCirc.restitution = f;
LUpperArmPoly.restitution = f;
RUpperArmPoly.restitution = f;
LForearmPoly.restitution = f;
RForearmPoly.restitution = f;
LHandPoly.restitution = f;
RHandPoly.restitution = f;
}
void BipedDef::EnableLimit()
{
SetLimit(true);
}
void BipedDef::DisableLimit()
{
SetLimit(false);
}
void BipedDef::SetLimit(bool b)
{
LAnkleDef.enableLimit = b;
RAnkleDef.enableLimit = b;
LKneeDef.enableLimit = b;
RKneeDef.enableLimit = b;
LHipDef.enableLimit = b;
RHipDef.enableLimit = b;
LowerAbsDef.enableLimit = b;
UpperAbsDef.enableLimit = b;
LowerNeckDef.enableLimit = b;
UpperNeckDef.enableLimit = b;
LShoulderDef.enableLimit = b;
RShoulderDef.enableLimit = b;
LElbowDef.enableLimit = b;
RElbowDef.enableLimit = b;
LWristDef.enableLimit = b;
RWristDef.enableLimit = b;
}
void BipedDef::EnableMotor()
{
SetMotor(true);
}
void BipedDef::DisableMotor()
{
SetMotor(false);
}
void BipedDef::SetMotor(bool b)
{
LAnkleDef.enableMotor = b;
RAnkleDef.enableMotor = b;
LKneeDef.enableMotor = b;
RKneeDef.enableMotor = b;
LHipDef.enableMotor = b;
RHipDef.enableMotor = b;
LowerAbsDef.enableMotor = b;
UpperAbsDef.enableMotor = b;
LowerNeckDef.enableMotor = b;
UpperNeckDef.enableMotor = b;
LShoulderDef.enableMotor = b;
RShoulderDef.enableMotor = b;
LElbowDef.enableMotor = b;
RElbowDef.enableMotor = b;
LWristDef.enableMotor = b;
RWristDef.enableMotor = b;
}
BipedDef::~BipedDef(void)
{
}
void BipedDef::DefaultVertices()
{
{ // feet
LFootPoly.vertexCount = RFootPoly.vertexCount = 5;
LFootPoly.vertices[0] = RFootPoly.vertices[0] = k_scale * b2Vec2(.033f,.143f);
LFootPoly.vertices[1] = RFootPoly.vertices[1] = k_scale * b2Vec2(.023f,.033f);
LFootPoly.vertices[2] = RFootPoly.vertices[2] = k_scale * b2Vec2(.267f,.035f);
LFootPoly.vertices[3] = RFootPoly.vertices[3] = k_scale * b2Vec2(.265f,.065f);
LFootPoly.vertices[4] = RFootPoly.vertices[4] = k_scale * b2Vec2(.117f,.143f);
}
{ // calves
LCalfPoly.vertexCount = RCalfPoly.vertexCount = 4;
LCalfPoly.vertices[0] = RCalfPoly.vertices[0] = k_scale * b2Vec2(.089f,.016f);
LCalfPoly.vertices[1] = RCalfPoly.vertices[1] = k_scale * b2Vec2(.178f,.016f);
LCalfPoly.vertices[2] = RCalfPoly.vertices[2] = k_scale * b2Vec2(.205f,.417f);
LCalfPoly.vertices[3] = RCalfPoly.vertices[3] = k_scale * b2Vec2(.095f,.417f);
}
{ // thighs
LThighPoly.vertexCount = RThighPoly.vertexCount = 4;
LThighPoly.vertices[0] = RThighPoly.vertices[0] = k_scale * b2Vec2(.137f,.032f);
LThighPoly.vertices[1] = RThighPoly.vertices[1] = k_scale * b2Vec2(.243f,.032f);
LThighPoly.vertices[2] = RThighPoly.vertices[2] = k_scale * b2Vec2(.318f,.343f);
LThighPoly.vertices[3] = RThighPoly.vertices[3] = k_scale * b2Vec2(.142f,.343f);
}
{ // pelvis
PelvisPoly.vertexCount = 5;
PelvisPoly.vertices[0] = k_scale * b2Vec2(.105f,.051f);
PelvisPoly.vertices[1] = k_scale * b2Vec2(.277f,.053f);
PelvisPoly.vertices[2] = k_scale * b2Vec2(.320f,.233f);
PelvisPoly.vertices[3] = k_scale * b2Vec2(.112f,.233f);
PelvisPoly.vertices[4] = k_scale * b2Vec2(.067f,.152f);
}
{ // stomach
StomachPoly.vertexCount = 4;
StomachPoly.vertices[0] = k_scale * b2Vec2(.088f,.043f);
StomachPoly.vertices[1] = k_scale * b2Vec2(.284f,.043f);
StomachPoly.vertices[2] = k_scale * b2Vec2(.295f,.231f);
StomachPoly.vertices[3] = k_scale * b2Vec2(.100f,.231f);
}
{ // chest
ChestPoly.vertexCount = 4;
ChestPoly.vertices[0] = k_scale * b2Vec2(.091f,.042f);
ChestPoly.vertices[1] = k_scale * b2Vec2(.283f,.042f);
ChestPoly.vertices[2] = k_scale * b2Vec2(.177f,.289f);
ChestPoly.vertices[3] = k_scale * b2Vec2(.065f,.289f);
}
{ // head
HeadCirc.radius = k_scale * .115f;
}
{ // neck
NeckPoly.vertexCount = 4;
NeckPoly.vertices[0] = k_scale * b2Vec2(.038f,.054f);
NeckPoly.vertices[1] = k_scale * b2Vec2(.149f,.054f);
NeckPoly.vertices[2] = k_scale * b2Vec2(.154f,.102f);
NeckPoly.vertices[3] = k_scale * b2Vec2(.054f,.113f);
}
{ // upper arms
LUpperArmPoly.vertexCount = RUpperArmPoly.vertexCount = 5;
LUpperArmPoly.vertices[0] = RUpperArmPoly.vertices[0] = k_scale * b2Vec2(.092f,.059f);
LUpperArmPoly.vertices[1] = RUpperArmPoly.vertices[1] = k_scale * b2Vec2(.159f,.059f);
LUpperArmPoly.vertices[2] = RUpperArmPoly.vertices[2] = k_scale * b2Vec2(.169f,.335f);
LUpperArmPoly.vertices[3] = RUpperArmPoly.vertices[3] = k_scale * b2Vec2(.078f,.335f);
LUpperArmPoly.vertices[4] = RUpperArmPoly.vertices[4] = k_scale * b2Vec2(.064f,.248f);
}
{ // forearms
LForearmPoly.vertexCount = RForearmPoly.vertexCount = 4;
LForearmPoly.vertices[0] = RForearmPoly.vertices[0] = k_scale * b2Vec2(.082f,.054f);
LForearmPoly.vertices[1] = RForearmPoly.vertices[1] = k_scale * b2Vec2(.138f,.054f);
LForearmPoly.vertices[2] = RForearmPoly.vertices[2] = k_scale * b2Vec2(.149f,.296f);
LForearmPoly.vertices[3] = RForearmPoly.vertices[3] = k_scale * b2Vec2(.088f,.296f);
}
{ // hands
LHandPoly.vertexCount = RHandPoly.vertexCount = 5;
LHandPoly.vertices[0] = RHandPoly.vertices[0] = k_scale * b2Vec2(.066f,.031f);
LHandPoly.vertices[1] = RHandPoly.vertices[1] = k_scale * b2Vec2(.123f,.020f);
LHandPoly.vertices[2] = RHandPoly.vertices[2] = k_scale * b2Vec2(.160f,.127f);
LHandPoly.vertices[3] = RHandPoly.vertices[3] = k_scale * b2Vec2(.127f,.178f);
LHandPoly.vertices[4] = RHandPoly.vertices[4] = k_scale * b2Vec2(.074f,.178f);;
}
}
void BipedDef::DefaultJoints()
{
//b.LAnkleDef.body1 = LFoot;
//b.LAnkleDef.body2 = LCalf;
//b.RAnkleDef.body1 = RFoot;
//b.RAnkleDef.body2 = RCalf;
{ // ankles
b2Vec2 anchor = k_scale * b2Vec2(-.045f,-.75f);
LAnkleDef.localAnchor1 = RAnkleDef.localAnchor1 = anchor - LFootDef.position;
LAnkleDef.localAnchor2 = RAnkleDef.localAnchor2 = anchor - LCalfDef.position;
LAnkleDef.referenceAngle = RAnkleDef.referenceAngle = 0.0f;
LAnkleDef.lowerAngle = RAnkleDef.lowerAngle = -0.523598776f;
LAnkleDef.upperAngle = RAnkleDef.upperAngle = 0.523598776f;
}
//b.LKneeDef.body1 = LCalf;
//b.LKneeDef.body2 = LThigh;
//b.RKneeDef.body1 = RCalf;
//b.RKneeDef.body2 = RThigh;
{ // knees
b2Vec2 anchor = k_scale * b2Vec2(-.030f,-.355f);
LKneeDef.localAnchor1 = RKneeDef.localAnchor1 = anchor - LCalfDef.position;
LKneeDef.localAnchor2 = RKneeDef.localAnchor2 = anchor - LThighDef.position;
LKneeDef.referenceAngle = RKneeDef.referenceAngle = 0.0f;
LKneeDef.lowerAngle = RKneeDef.lowerAngle = 0;
LKneeDef.upperAngle = RKneeDef.upperAngle = 2.61799388f;
}
//b.LHipDef.body1 = LThigh;
//b.LHipDef.body2 = Pelvis;
//b.RHipDef.body1 = RThigh;
//b.RHipDef.body2 = Pelvis;
{ // hips
b2Vec2 anchor = k_scale * b2Vec2(.005f,-.045f);
LHipDef.localAnchor1 = RHipDef.localAnchor1 = anchor - LThighDef.position;
LHipDef.localAnchor2 = RHipDef.localAnchor2 = anchor - PelvisDef.position;
LHipDef.referenceAngle = RHipDef.referenceAngle = 0.0f;
LHipDef.lowerAngle = RHipDef.lowerAngle = -2.26892803f;
LHipDef.upperAngle = RHipDef.upperAngle = 0;
}
//b.LowerAbsDef.body1 = Pelvis;
//b.LowerAbsDef.body2 = Stomach;
{ // lower abs
b2Vec2 anchor = k_scale * b2Vec2(.035f,.135f);
LowerAbsDef.localAnchor1 = anchor - PelvisDef.position;
LowerAbsDef.localAnchor2 = anchor - StomachDef.position;
LowerAbsDef.referenceAngle = 0.0f;
LowerAbsDef.lowerAngle = -0.523598776f;
LowerAbsDef.upperAngle = 0.523598776f;
}
//b.UpperAbsDef.body1 = Stomach;
//b.UpperAbsDef.body2 = Chest;
{ // upper abs
b2Vec2 anchor = k_scale * b2Vec2(.045f,.320f);
UpperAbsDef.localAnchor1 = anchor - StomachDef.position;
UpperAbsDef.localAnchor2 = anchor - ChestDef.position;
UpperAbsDef.referenceAngle = 0.0f;
UpperAbsDef.lowerAngle = -0.523598776f;
UpperAbsDef.upperAngle = 0.174532925f;
}
//b.LowerNeckDef.body1 = Chest;
//b.LowerNeckDef.body2 = Neck;
{ // lower neck
b2Vec2 anchor = k_scale * b2Vec2(-.015f,.575f);
LowerNeckDef.localAnchor1 = anchor - ChestDef.position;
LowerNeckDef.localAnchor2 = anchor - NeckDef.position;
LowerNeckDef.referenceAngle = 0.0f;
LowerNeckDef.lowerAngle = -0.174532925f;
LowerNeckDef.upperAngle = 0.174532925f;
}
//b.UpperNeckDef.body1 = Chest;
//b.UpperNeckDef.body2 = Head;
{ // upper neck
b2Vec2 anchor = k_scale * b2Vec2(-.005f,.630f);
UpperNeckDef.localAnchor1 = anchor - ChestDef.position;
UpperNeckDef.localAnchor2 = anchor - HeadDef.position;
UpperNeckDef.referenceAngle = 0.0f;
UpperNeckDef.lowerAngle = -0.610865238f;
UpperNeckDef.upperAngle = 0.785398163f;
}
//b.LShoulderDef.body1 = Chest;
//b.LShoulderDef.body2 = LUpperArm;
//b.RShoulderDef.body1 = Chest;
//b.RShoulderDef.body2 = RUpperArm;
{ // shoulders
b2Vec2 anchor = k_scale * b2Vec2(-.015f,.545f);
LShoulderDef.localAnchor1 = RShoulderDef.localAnchor1 = anchor - ChestDef.position;
LShoulderDef.localAnchor2 = RShoulderDef.localAnchor2 = anchor - LUpperArmDef.position;
LShoulderDef.referenceAngle = RShoulderDef.referenceAngle = 0.0f;
LShoulderDef.lowerAngle = RShoulderDef.lowerAngle = -1.04719755f;
LShoulderDef.upperAngle = RShoulderDef.upperAngle = 3.14159265f;
}
//b.LElbowDef.body1 = LForearm;
//b.LElbowDef.body2 = LUpperArm;
//b.RElbowDef.body1 = RForearm;
//b.RElbowDef.body2 = RUpperArm;
{ // elbows
b2Vec2 anchor = k_scale * b2Vec2(-.005f,.290f);
LElbowDef.localAnchor1 = RElbowDef.localAnchor1 = anchor - LForearmDef.position;
LElbowDef.localAnchor2 = RElbowDef.localAnchor2 = anchor - LUpperArmDef.position;
LElbowDef.referenceAngle = RElbowDef.referenceAngle = 0.0f;
LElbowDef.lowerAngle = RElbowDef.lowerAngle = -2.7925268f;
LElbowDef.upperAngle = RElbowDef.upperAngle = 0;
}
//b.LWristDef.body1 = LHand;
//b.LWristDef.body2 = LForearm;
//b.RWristDef.body1 = RHand;
//b.RWristDef.body2 = RForearm;
{ // wrists
b2Vec2 anchor = k_scale * b2Vec2(-.010f,.045f);
LWristDef.localAnchor1 = RWristDef.localAnchor1 = anchor - LHandDef.position;
LWristDef.localAnchor2 = RWristDef.localAnchor2 = anchor - LForearmDef.position;
LWristDef.referenceAngle = RWristDef.referenceAngle = 0.0f;
LWristDef.lowerAngle = RWristDef.lowerAngle = -0.174532925f;
LWristDef.upperAngle = RWristDef.upperAngle = 0.174532925f;
}
}
void BipedDef::DefaultPositions()
{
LFootDef.position = RFootDef.position = k_scale * b2Vec2(-.122f,-.901f);
LCalfDef.position = RCalfDef.position = k_scale * b2Vec2(-.177f,-.771f);
LThighDef.position = RThighDef.position = k_scale * b2Vec2(-.217f,-.391f);
LUpperArmDef.position = RUpperArmDef.position = k_scale * b2Vec2(-.127f,.228f);
LForearmDef.position = RForearmDef.position = k_scale * b2Vec2(-.117f,-.011f);
LHandDef.position = RHandDef.position = k_scale * b2Vec2(-.112f,-.136f);
PelvisDef.position = k_scale * b2Vec2(-.177f,-.101f);
StomachDef.position = k_scale * b2Vec2(-.142f,.088f);
ChestDef.position = k_scale * b2Vec2(-.132f,.282f);
NeckDef.position = k_scale * b2Vec2(-.102f,.518f);
HeadDef.position = k_scale * b2Vec2(.022f,.738f);
}

View File

@@ -0,0 +1,51 @@
#ifndef BIPED_DEF_H
#define BIPED_DEF_H
#include "Box2D.h"
class BipedDef
{
public:
BipedDef();
~BipedDef(void);
void SetMotorTorque(float);
void SetMotorSpeed(float);
void SetDensity(float);
void SetFriction(float);
void SetRestitution(float);
void SetLinearDamping(float);
void SetAngularDamping(float);
void EnableLimit();
void DisableLimit();
void SetLimit(bool);
void EnableMotor();
void DisableMotor();
void SetMotor(bool);
void SetGroupIndex(int16);
void SetPosition(float, float);
void SetPosition(b2Vec2);
void IsFast(bool);
static int16 count;
b2BodyDef LFootDef, RFootDef, LCalfDef, RCalfDef, LThighDef, RThighDef,
PelvisDef, StomachDef, ChestDef, NeckDef, HeadDef,
LUpperArmDef, RUpperArmDef, LForearmDef, RForearmDef, LHandDef, RHandDef;
b2PolygonDef LFootPoly, RFootPoly, LCalfPoly, RCalfPoly, LThighPoly, RThighPoly,
PelvisPoly, StomachPoly, ChestPoly, NeckPoly,
LUpperArmPoly, RUpperArmPoly, LForearmPoly, RForearmPoly, LHandPoly, RHandPoly;
b2CircleDef HeadCirc;
b2RevoluteJointDef LAnkleDef, RAnkleDef, LKneeDef, RKneeDef, LHipDef, RHipDef,
LowerAbsDef, UpperAbsDef, LowerNeckDef, UpperNeckDef,
LShoulderDef, RShoulderDef, LElbowDef, RElbowDef, LWristDef, RWristDef;
void DefaultVertices();
void DefaultPositions();
void DefaultJoints();
};
#endif

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BIPED_TEST_H
#define BIPED_TEST_H
#include "Biped.h"
class BipedTest : public Test
{
public:
BipedTest()
{
const float32 k_restitution = 1.4f;
{
b2BodyDef bd;
bd.position.Set(0.0f, 20.0f);
b2Body* body = m_world->CreateBody(&bd);
b2PolygonDef sd;
sd.density = 0.0f;
sd.restitution = k_restitution;
sd.SetAsBox(0.1f, 10.0f, b2Vec2(-10.0f, 0.0f), 0.0f);
body->CreateFixture(&sd);
sd.SetAsBox(0.1f, 10.0f, b2Vec2(10.0f, 0.0f), 0.0f);
body->CreateFixture(&sd);
sd.SetAsBox(0.1f, 10.0f, b2Vec2(0.0f, -10.0f), 0.5f * b2_pi);
body->CreateFixture(&sd);
sd.SetAsBox(0.1f, 10.0f, b2Vec2(0.0f, 10.0f), -0.5f * b2_pi);
body->CreateFixture(&sd);
}
m_biped = new Biped(m_world, b2Vec2(0.0f, 20.0f));
for (int32 i = 0; i < 8; ++i)
{
b2BodyDef bd;
bd.position.Set(5.0f, 20.0f + i);
bd.isBullet = true;
b2Body* body = m_world->CreateBody(&bd);
body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
body->SetAngularVelocity(RandomFloat(-50.0f, 50.0f));
b2CircleDef sd;
sd.radius = 0.25f;
sd.density = 15.0f;
sd.restitution = k_restitution;
body->CreateFixture(&sd);
body->SetMassFromShapes();
}
}
~BipedTest()
{
delete m_biped;
}
static Test* Create()
{
return new BipedTest;
}
Biped* m_biped;
};
#endif

View File

@@ -0,0 +1,603 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
/* Testbed example showing deformable and breakable bodies using the soft
* b2DistanceJoint and a small,liteweight triangle mesher.
* 2008-05-09 / nimodo
*/
#ifndef BREAKABLE_BODY_H
#define BREAKABLE_BODY_H
#include "TriangleMesh.h"
/// utility macro
#define H(x) (x)/2.0f
#define N_MAXVERTEX 256
class BreakableBody : public Test
{
public:
BreakableBody()
{
/// geometries
float32 gx = 100.0f, gy = 1.0f,
dx = 34.0f, br = 0.3f;
float32 sx=-dx-H(dx), sy = 30.f;
/// break joint, if the reactionforce exceeds:
maxAllowableForce = 100.0f;
m_drawMode = m_staticBodies = false;
m_drawCount = 0;
/// ground
{
b2PolygonDef sd;
b2BodyDef bd;
b2Body* ground;
bd.position.Set(0.0f, 0.0f);
ground = m_world->CreateBody(&bd);
/// bottom
sd.SetAsBox( H(gx), H(gy) );
ground->CreateFixture(&sd);
sd.SetAsBox( H(dx), H(gy), b2Vec2(-dx,sy-1.0f), 0.0f );
ground->CreateFixture(&sd);
}
/// dyn bodies
{
b2PolygonDef pd;
b2DistanceJointDef dj;
dj.dampingRatio = 0.0f;
dj.collideConnected = true;
ExampleData('B');
dj.frequencyHz = 20.f;
pd.density = 1.0f/70.0f;
pd.friction = 0.4f;
pd.restitution = 0.01f;
CreateSoftBody( b2Vec2(sx,sy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
ExampleData('@');
dj.frequencyHz = 20.f;
pd.density = 1.0f/36.0f;
pd.friction = 0.1f;
pd.restitution = 0.5f;
CreateSoftBody( b2Vec2(sx+6.f,sy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
ExampleData('x');
dj.frequencyHz = 20.0f;
pd.density = 1.0f/60.0f;
pd.friction = 0.6f;
pd.restitution = 0.0f;
CreateSoftBody( b2Vec2(sx+13.f,sy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
ExampleData('2');
pd.density = 0.01f;
pd.friction = 0.3f;
pd.restitution = 0.3f;
CreateSoftBody( b2Vec2(sx+20.f,sy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
ExampleData('D');
CreateSoftBody( b2Vec2(sx+28.f,sy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
ExampleData('b');
dj.frequencyHz = 10.0f;
dj.dampingRatio = 20.0f;
pd.friction = 0.9f;
pd.restitution = 0.01f;
pd.density = 0.01f;
CreateSoftBody( b2Vec2(-5.f,5.f*gy), 0, 0, pd, dj,
nodes,n_nodes, segments,n_segments, holes,n_holes) ;
b2CircleDef cd;
b2BodyDef bd;
b2Body* b;
cd.radius = br;
cd.density= 0.001f;
bd.position.Set(0.0f,10.0f*gy);
for (int32 i=0; i<60; i++ )
{
b = m_world->CreateBody(&bd);
b->CreateFixture (&cd);
b->SetMassFromShapes();
}
}
}
/// Create compound (soft) body using a triangle mesh
/// If meshDensity is 0, a minimal grid is generated.
/// Actually pd and dj define the behaviour for all triangles
void CreateSoftBody(b2Vec2 pos, int32 meshDensity,int32 options,
b2PolygonDef pd, b2DistanceJointDef dj,
tmVertex* nodes,int32 n_nodes,
tmSegmentId *segments=NULL, int32 n_segments=0,
tmVertex* holes=NULL, int32 n_holes=0)
{
int32 i;
/// TriangleMesh defs
tmTriangle *triangles;
TriangleMesh md;
/// box2d defs
b2BodyDef bd;
b2Body *b;
/// in case of meshDensit>3 ...
md.SetMaxVertexCount(meshDensity);
if (options>0) md.SetOptions(options);
/// triangulator main
md.Mesh( nodes, n_nodes, segments,n_segments, holes, n_holes );
md.PrintData();
/// bodies (triangles)
triangles = md.GetTriangles();
if ( triangles==NULL ) return;
pd.vertexCount = 3;
for ( i=0; i<md.GetTriangleCount(); i++ )
{
if ( triangles[i].inside )
{
/// triangle -> b2PolygonDef
pd.vertices[0].Set(triangles[i].v[0]->x, triangles[i].v[0]->y);
pd.vertices[1].Set(triangles[i].v[1]->x, triangles[i].v[1]->y);
pd.vertices[2].Set(triangles[i].v[2]->x, triangles[i].v[2]->y);
bd.position.Set(pos.x,pos.y);
b = m_world->CreateBody(&bd);
b->CreateFixture(&pd);
b->SetMassFromShapes();
/// we need the body pointer in the triangles for the joints later
triangles[i].userData = (void *)b;
}
}
/// joints
/// for each triangle-pair in edges, connect with a distance joint
tmEdge *edges;
tmTriangle *t0,*t1;
b2Body *b1,*b2;
edges = md.GetEdges();
for ( i=0; i<md.GetEdgeCount(); i++ )
{
t0 = edges[i].t[0];
t1 = edges[i].t[1];
if ( (t0->inside==false) || (t1->inside==false) ) continue;
/// Get bodies
b1 = (b2Body*)t0->userData;
b2 = (b2Body*)t1->userData;
if ( b1==NULL || b2==NULL ) continue;
dj.Initialize( b1,b2, b1->GetWorldCenter(), b2->GetWorldCenter());
m_world->CreateJoint(&dj);
}
/// clean TriangleMesh
md.FreeMemory();
}
/// maybe here to check for maximal reaction forces to break a body
void Step(Settings* settings)
{
b2Joint *jStressed=NULL;
float32 F=0.0f, tmp;
Test::Step(settings);
for (b2Joint* j = m_world->GetJointList(); j; j = j->GetNext())
{
tmp = j->GetReactionForce(settings->hz).Length();
if ( tmp>F )
{
F = tmp;
jStressed = j;
}
}
if ( jStressed && (F>maxAllowableForce) )
{
m_world->DestroyJoint(jStressed);
}
m_debugDraw.DrawString(1, m_textLine,"max.reactionforce=%.0f allowable=%.0f change:-+", (float)F,(float)maxAllowableForce);
m_textLine += 12;
m_debugDraw.DrawString(1, m_textLine,"drawmode(%s):d mesh:m static(%s):s", (m_drawMode)?"on":"off", (m_staticBodies)?"on":"off");
m_textLine += 12;
for ( int32 i=0; i<m_drawCount-1; i++ )
{
b2Vec2 p1,p2;
p1.Set(m_drawVertices[i].x,m_drawVertices[i].y);
p2.Set(m_drawVertices[i+1].x,m_drawVertices[i+1].y);
m_debugDraw.DrawSegment(p1,p2,b2Color(0.6f,0.2f,0.2f));
}
}
/// default constructor for TestEntries.cpp
static Test* Create()
{
return new BreakableBody;
}
void Keyboard(unsigned char key)
{
switch (key)
{
case '-':
maxAllowableForce -= 5.0f;
break;
case '+':
maxAllowableForce += 5.0f;
break;
case 'd':
m_drawMode = !m_drawMode;
break;
case 's':
m_staticBodies = !m_staticBodies;
break;
case 'm':
if ( m_drawCount>0 )
{
b2PolygonDef pd;
b2DistanceJointDef dj;
dj.collideConnected = true;
dj.frequencyHz = 20.f;
dj.dampingRatio = 10.0f;
pd.density = (m_staticBodies) ? 0.0f : 1.0f/32.0f;
pd.friction = 0.99f;
pd.restitution = 0.01f;
CreateSoftBody( b2Vec2(0.0f,0.0f), 0, tmO_SEGMENTBOUNDARY|tmO_GRADING,
pd, dj, m_drawVertices, m_drawCount) ;
m_drawCount = 0;
m_drawMode = false;
}
break;
}
}
void MouseDown(const b2Vec2& p)
{
if ( m_drawMode && (m_drawCount<N_MAXVERTEX) )
{
m_drawVertices[m_drawCount].x = p.x;
m_drawVertices[m_drawCount].y = p.y;
m_drawCount++;
}
else Test::MouseDown(p);
}
/*
void MouseMove(const b2Vec2& p)
{
m_lastPoint = p;
if (m_drawMode)
{
}
}
*/
void MouseUp(const b2Vec2& p)
{
Test::MouseUp(p);
}
/// examples
void ExampleData(char which)
{
/// @ - ring
static tmVertex ring_nodes[] = {
{ 6.00f, 3.00f},
{ 5.12f, 5.12f},
{ 3.00f, 6.00f},
{ 0.88f, 5.12f},
{ 0.00f, 3.00f},
{ 0.88f, 0.88f},
{ 3.00f, 0.00f},
{ 5.12f, 0.88f},
{ 4.50f, 3.00f},
{ 4.06f, 4.06f},
{ 3.00f, 4.50f},
{ 1.94f, 4.06f},
{ 1.50f, 3.00f},
{ 1.94f, 1.94f},
{ 3.00f, 1.50f},
{ 4.06f, 1.94f}
};
static tmSegmentId ring_segments[] = {
{ 9, 10 },
{ 10, 11 },
{ 11, 12 },
{ 12, 13 },
{ 13, 14 },
{ 14, 15 },
{ 15, 16 },
{ 16, 9 }
};
static tmVertex ring_holes[] = {
{ 3.00f, 3.00f}
};
/// 'B'
static tmVertex B_nodes[] = {
{ 0.00f, 0.00f},
{ 4.00f, 0.00f},
{ 5.00f, 2.00f},
{ 5.00f, 4.00f},
{ 4.00f, 5.00f},
{ 5.00f, 6.00f},
{ 5.00f, 8.00f},
{ 4.00f, 9.00f},
{ 0.00f, 9.00f},
{ 0.00f, 5.00f},
{ 1.50f, 1.50f},
{ 3.50f, 1.50f},
{ 3.50f, 4.00f},
{ 1.50f, 4.00f},
{ 1.50f, 6.00f},
{ 3.50f, 6.00f},
{ 3.50f, 8.50f},
{ 1.50f, 8.50f}
};
static tmSegmentId B_segments[] = {
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 8 },
{ 8, 9 },
{ 9, 10 },
{ 10, 1 },
{ 11, 12 },
{ 12, 13 },
{ 13, 14 },
{ 14, 11 },
{ 15, 16 },
{ 16, 17 },
{ 17, 18 },
{ 18, 15 }
};
static tmVertex B_holes[] = {
{ 5.00f, 5.00f},
{ 2.50f, 2.50f},
{ 2.50f, 7.00f}
};
/// 'D'
static tmVertex D_nodes[] = {
{ 0.00f, 0.00f},
{ 4.00f, 0.00f},
{ 5.00f, 2.50f},
{ 5.00f, 7.00f},
{ 4.00f, 9.00f},
{ 0.00f, 9.00f},
{ 0.00f, 5.00f},
{ 1.50f, 2.50f},
{ 3.50f, 2.50f},
{ 3.50f, 7.00f},
{ 1.50f, 7.00f},
};
static tmSegmentId D_segments[] = {
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 1 },
{ 8, 9 },
{ 9, 10 },
{ 10, 11 },
{ 11, 8 },
};
static tmVertex D_holes[] = {
{ 2.50f, 5.00f},
};
/// 'x'
static tmVertex x_nodes[] = {
{ 0.00f, 0.00f},
{ 1.00f, 0.00f},
{ 5.00f, 0.00f},
{ 6.00f, 0.00f},
{ 6.00f, 1.00f},
{ 6.00f, 5.00f},
{ 6.00f, 6.00f},
{ 1.00f, 6.00f},
{ 5.00f, 6.00f},
{ 0.00f, 6.00f},
{ 0.00f, 5.00f},
{ 0.00f, 1.00f},
{ 3.00f, 2.00f},
{ 4.00f, 3.00f},
{ 3.00f, 4.00f},
{ 2.00f, 3.00f}
};
static tmSegmentId x_segments[] = {
{ 2, 13 },
{ 3, 13 },
{ 5, 14 },
{ 6, 14 },
{ 8, 15 },
{ 9, 15 },
{ 11, 16 },
{ 12, 16 }
};
static tmVertex x_holes[] = {
{ 3.00f, 1.00f},
{ 5.00f, 3.00f},
{ 3.00f, 5.00f},
{ 1.00f, 3.00f},
};
/// '2'
static tmVertex two_nodes[] = {
{ 0.00f, 0.00f},
{ 6.00f, 0.00f},
{ 6.00f, 1.00f},
{ 2.00f, 1.00f},
{ 2.00f, 2.00f},
{ 6.00f, 6.00f},
{ 6.00f, 8.00f},
{ 5.00f, 9.00f},
{ 2.00f, 9.00f},
{ 1.00f, 7.50f},
{ 0.00f, 2.50f},
{ 5.00f, 6.50f},
{ 5.00f, 8.00f},
{ 2.50f, 8.00f},
{ 2.00f, 7.50f},
};
static tmSegmentId two_segments[] = {
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 8 },
{ 8, 9 },
{ 9, 10 },
{ 10, 15 },
{ 11, 12 },
{ 12, 13 },
{ 13, 14 },
{ 14, 15 },
};
static tmVertex two_holes[] = {
{ 3.00f, 5.00f},
{ 4.00f, 3.00f},
};
/// '-' beam
static tmVertex beam_nodes[] = {
{ 0.00f, 0.00f},
{ 32.00f, 0.00f},
{ 32.00f, 3.00f},
{ 0.00f, 3.00f},
};
static tmSegmentId *beam_segments = NULL;
static tmVertex *beam_holes = NULL;
/// 'b' a box
static tmVertex b_nodes[] = {
{ 0.00f, 0.00f},
{ 10.00f, 0.00f},
{ 10.00f, 10.00f},
{ 0.00f, 10.00f},
{ 2.00f, 2.00f},
{ 8.00f, 2.00f},
{ 8.00f, 8.00f},
{ 2.00f, 8.00f},
};
static tmSegmentId b_segments[] = {
{ 5, 6 },
{ 6, 7 },
{ 7, 8 },
{ 8, 5 },
};
static tmVertex b_holes[] = {
{ 5.0f, 5.0f},
};
/// choose...
switch( which )
{
case 'B':
nodes = B_nodes;
segments = B_segments;
holes = B_holes;
n_nodes = sizeof(B_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(B_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(B_holes)/sizeof(tmVertex) : 0;
break;
case 'D':
nodes = D_nodes;
segments = D_segments;
holes = D_holes;
n_nodes = sizeof(D_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(D_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(D_holes)/sizeof(tmVertex) : 0;
break;
case 'x':
nodes = x_nodes;
segments = x_segments;
holes = x_holes;
n_nodes = sizeof(x_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(x_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(x_holes)/sizeof(tmVertex) : 0;
break;
case '@':
nodes = ring_nodes;
segments = ring_segments;
holes = ring_holes ;
n_nodes = sizeof(ring_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(ring_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(ring_holes)/sizeof(tmVertex) : 0;
break;
case '2':
nodes = two_nodes;
segments = two_segments;
holes = two_holes ;
n_nodes = sizeof(two_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(two_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(two_holes)/sizeof(tmVertex) : 0;
break;
case '-':
nodes = beam_nodes;
segments = beam_segments;
holes = beam_holes ;
n_nodes = sizeof(beam_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(beam_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(beam_holes)/sizeof(tmVertex) : 0;
break;
case 'b':
nodes = b_nodes;
segments = b_segments;
holes = b_holes;
n_nodes = sizeof(b_nodes)/sizeof(tmVertex);
n_segments = (segments) ? sizeof(b_segments)/sizeof(tmSegmentId) : 0;
n_holes = (holes) ? sizeof(b_holes)/sizeof(tmVertex) : 0;
break;
}
}
///
bool m_drawMode, m_staticBodies;
tmVertex m_drawVertices[N_MAXVERTEX];
int32 m_drawCount;
///
float32 maxAllowableForce;
/// temporary vars to hold the examples
tmVertex *nodes;
int32 n_nodes;
tmVertex *holes;
int32 n_holes;
tmSegmentId *segments;
int32 n_segments;
};
#undef H
#endif

View File

@@ -0,0 +1,215 @@
/*
* Copyright (c) 2008-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef CAR_H
#define CAR_H
// Adapted from SpiritWalkers by darkzerox
class Car : public Test
{
public:
Car()
{
{ // car body
b2PolygonDef poly1, poly2;
// bottom half
poly1.vertexCount = 5;
poly1.vertices[4].Set(-2.2f,-0.74f);
poly1.vertices[3].Set(-2.2f,0);
poly1.vertices[2].Set(1.0f,0);
poly1.vertices[1].Set(2.2f,-0.2f);
poly1.vertices[0].Set(2.2f,-0.74f);
poly1.filter.groupIndex = -1;
poly1.density = 20.0f;
poly1.friction = 0.68f;
poly1.filter.groupIndex = -1;
// top half
poly2.vertexCount = 4;
poly2.vertices[3].Set(-1.7f,0);
poly2.vertices[2].Set(-1.3f,0.7f);
poly2.vertices[1].Set(0.5f,0.74f);
poly2.vertices[0].Set(1.0f,0);
poly2.filter.groupIndex = -1;
poly2.density = 5.0f;
poly2.friction = 0.68f;
poly2.filter.groupIndex = -1;
b2BodyDef bd;
bd.position.Set(-35.0f, 2.8f);
m_vehicle = m_world->CreateBody(&bd);
m_vehicle->CreateFixture(&poly1);
m_vehicle->CreateFixture(&poly2);
m_vehicle->SetMassFromShapes();
}
{ // vehicle wheels
b2CircleDef circ;
circ.density = 40.0f;
circ.radius = 0.38608f;
circ.friction = 0.8f;
circ.filter.groupIndex = -1;
b2BodyDef bd;
bd.allowSleep = false;
bd.position.Set(-33.8f, 2.0f);
m_rightWheel = m_world->CreateBody(&bd);
m_rightWheel->CreateFixture(&circ);
m_rightWheel->SetMassFromShapes();
bd.position.Set(-36.2f, 2.0f);
m_leftWheel = m_world->CreateBody(&bd);
m_leftWheel->CreateFixture(&circ);
m_leftWheel->SetMassFromShapes();
}
{ // join wheels to chassis
b2Vec2 anchor;
b2RevoluteJointDef jd;
jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel->GetWorldCenter());
jd.collideConnected = false;
jd.enableMotor = true;
jd.maxMotorTorque = 10.0f;
jd.motorSpeed = 0.0f;
m_leftJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel->GetWorldCenter());
jd.collideConnected = false;
m_rightJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
}
{ // ground
b2PolygonDef box;
box.SetAsBox(19.5f, 0.5f);
box.friction = 0.62f;
b2BodyDef bd;
bd.position.Set(-25.0f, 1.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
{ // more ground
b2PolygonDef box;
b2BodyDef bd;
box.SetAsBox(9.5f, 0.5f, b2Vec2_zero, 0.1f * b2_pi);
box.friction = 0.62f;
bd.position.Set(27.0f - 30.0f, 3.1f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
{ // more ground
b2PolygonDef box;
b2BodyDef bd;
box.SetAsBox(9.5f, 0.5f, b2Vec2_zero, -0.1f * b2_pi);
box.friction = 0.62f;
bd.position.Set(55.0f - 30.0f, 3.1f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
{ // more ground
b2PolygonDef box;
b2BodyDef bd;
box.SetAsBox(9.5f, 0.5f, b2Vec2_zero, 0.03f * b2_pi);
box.friction = 0.62f;
bd.position.Set(41.0f, 2.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
{ // more ground
b2PolygonDef box;
b2BodyDef bd;
box.SetAsBox(5.0f, 0.5f, b2Vec2_zero, 0.15f * b2_pi);
box.friction = 0.62f;
bd.position.Set(50.0f, 4.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
{ // more ground
b2PolygonDef box;
b2BodyDef bd;
box.SetAsBox(20.0f, 0.5f);
box.friction = 0.62f;
bd.position.Set(85.0f, 2.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&box);
}
}
void Step(Settings* settings)
{
m_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d");
m_textLine += 15;
Test::Step(settings);
}
void Keyboard(unsigned char key)
{
switch (key)
{
case 'a':
m_leftJoint->SetMaxMotorTorque(800.0f);
m_leftJoint->SetMotorSpeed(12.0f);
break;
case 's':
m_leftJoint->SetMaxMotorTorque(100.0f);
m_leftJoint->SetMotorSpeed(0.0f);
break;
case 'd':
m_leftJoint->SetMaxMotorTorque(1200.0f);
m_leftJoint->SetMotorSpeed(-36.0f);
break;
}
}
static Test* Create()
{
return new Car;
}
b2Body* m_leftWheel;
b2Body* m_rightWheel;
b2Body* m_vehicle;
b2RevoluteJoint* m_leftJoint;
b2RevoluteJoint* m_rightJoint;
};
#endif

View File

@@ -0,0 +1,260 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
// Contributed by caspin.
#ifndef CONTACT_CB_H
#define CONTACT_CB_H
#include <set>
#include <deque>
#include <sstream>
#include <string>
#include <iostream>
bool key_comp( const ContactPoint& lhs, const ContactPoint& rhs )
{
if( lhs.fixtureA < rhs.fixtureA ) return true;
if( lhs.fixtureA == rhs.fixtureA && lhs.fixtureB < rhs.fixtureB ) return true;
if( lhs.fixtureA == rhs.fixtureA && lhs.fixtureB == rhs.fixtureB && lhs.id.key < rhs.id.key ) return true;
return false;
}
class ContactCB : public Test
{
public:
ContactCB()
: m_set(&key_comp)
{
b2PolygonDef sd;
sd.friction = 0;
sd.vertexCount = 3;
sd.vertices[0].Set(10,10);
sd.vertices[1].Set(9,7);
sd.vertices[2].Set(10,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(9,7);
sd.vertices[1].Set(8,0);
sd.vertices[2].Set(10,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(9,7);
sd.vertices[1].Set(8,5);
sd.vertices[2].Set(8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(8,5);
sd.vertices[1].Set(7,4);
sd.vertices[2].Set(8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(7,4);
sd.vertices[1].Set(5,0);
sd.vertices[2].Set(8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(7,4);
sd.vertices[1].Set(5,3);
sd.vertices[2].Set(5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(5,3);
sd.vertices[1].Set(2,2);
sd.vertices[2].Set(5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(2,2);
sd.vertices[1].Set(0,0);
sd.vertices[2].Set(5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[0].Set(2,2);
sd.vertices[1].Set(-2,2);
sd.vertices[2].Set(0,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-2,2);
sd.vertices[1].Set(0,0);
sd.vertices[0].Set(-5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-5,3);
sd.vertices[1].Set(-2,2);
sd.vertices[0].Set(-5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-7,4);
sd.vertices[1].Set(-5,3);
sd.vertices[0].Set(-5,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-7,4);
sd.vertices[1].Set(-5,0);
sd.vertices[0].Set(-8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-8,5);
sd.vertices[1].Set(-7,4);
sd.vertices[0].Set(-8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-9,7);
sd.vertices[1].Set(-8,5);
sd.vertices[0].Set(-8,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-9,7);
sd.vertices[1].Set(-8,0);
sd.vertices[0].Set(-10,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.vertices[2].Set(-10,10);
sd.vertices[1].Set(-9,7);
sd.vertices[0].Set(-10,0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.SetAsBox(.5,6,b2Vec2(10.5,6),0);
m_world->GetGroundBody()->CreateFixture(&sd);
sd.SetAsBox(.5,6,b2Vec2(-10.5,6),0);
m_world->GetGroundBody()->CreateFixture(&sd);
b2BodyDef bd;
bd.position.Set(9.5,60);
b2Body* m_ball = m_world->CreateBody( &bd );
#if 1
b2PolygonDef cd;
cd.vertexCount = 8;
float32 w = 0.95f;
float32 b = w / (2.0f + sqrtf(2.0f));
float32 s = sqrtf(2.0f) * b;
cd.vertices[0].Set(0.5f * s, 0.0f);
cd.vertices[1].Set(0.5f * w, b);
cd.vertices[2].Set(0.5f * w, b + s);
cd.vertices[3].Set(0.5f * s, w);
cd.vertices[4].Set(-0.5f * s, w);
cd.vertices[5].Set(-0.5f * w, b + s);
cd.vertices[6].Set(-0.5f * w, b);
cd.vertices[7].Set(-0.5f * s, 0.0f);
cd.density = 1.0f;
#else
b2CircleDef cd;
cd.radius = 0.33f;
cd.friction = 0;
cd.density = 1;
#endif
m_ball_shape = m_ball->CreateFixture(&cd);
m_ball->SetMassFromShapes();
}
void Step(Settings* settings)
{
Test::Step(settings);
std::ostringstream oss;
oss << std::hex;
for (int32 i=0; i< m_pointCount; ++i)
{
#if 0
if (m_points[i].shape1 > m_points[i].shape2)
{
b2Swap(m_points[i].shape1, m_points[i].shape2);
m_points[i].normal *= -1.0f;
m_points[i].velocity *= -1.0f;
}
#endif
oss.str("");
switch( m_points[i].state )
{
case e_contactAdded:
{
if( ! m_set.insert( m_points[i] ).second )
{
oss << "ERROR ";
}
else
{
oss << " ";
}
oss << "added: " << m_points[i].fixtureA << " -> " << m_points[i].fixtureB;
oss << " : " << m_points[i].id.key;
m_strings.push_back( oss.str() );
std::cout << oss.str() << std::endl;
break;
}
case e_contactRemoved:
{
if( m_set.find( m_points[i] ) == m_set.end() )
{
oss << "ERROR ";
}
else
{
oss << " ";
}
oss << "removed: " << m_points[i].fixtureA << " -> " << m_points[i].fixtureB;
oss << " : " << m_points[i].id.key;
m_strings.push_back( oss.str() );
std::cout << oss.str() << std::endl;
m_set.erase( m_points[i] );
break;
}
case e_contactPersisted:
{
if( m_set.find( m_points[i] ) == m_set.end() )
{
oss << "ERROR persist: " << m_points[i].fixtureA << " -> ";
oss << m_points[i].fixtureB << " : " << m_points[i].id.key;
m_strings.push_back( oss.str() );
std::cout << oss.str() << std::endl;
}
break;
}
}
}
while( m_strings.size() > 15 )
{
m_strings.pop_front();
}
for( unsigned i=0; i<m_strings.size(); ++i )
{
m_debugDraw.DrawString(5, m_textLine, m_strings[i].c_str() );
m_textLine += 15;
}
}
static Test* Create()
{
return new ContactCB;
}
b2Body* m_ball;
b2Body* m_bullet;
b2Fixture* m_ball_shape;
std::set<ContactPoint,bool(*)(const ContactPoint&,const ContactPoint&)> m_set;
std::deque<std::string> m_strings;
};
#endif // CONTACT_CB_H

View File

@@ -0,0 +1,294 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef DYNAMIC_EDGES_H
#define DYNAMIC_EDGES_H
class DynamicEdges : public Test
{
public:
DynamicEdges()
{
{
b2BodyDef bd;
bd.position.Set(0.0f, -10.0f);
b2Body* body = m_world->CreateBody(&bd);
b2PolygonDef sd;
sd.SetAsBox(50.0f, 10.0f);
body->CreateFixture(&sd);
}
{
b2CircleDef sd1;
sd1.radius = 0.5f;
sd1.localPosition.Set(-0.5f, 0.5f);
sd1.density = 2.0f;
b2CircleDef sd2;
sd2.radius = 0.5f;
sd2.localPosition.Set(0.5f, 0.5f);
sd2.density = 0.0f; // massless
for (int i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
b2PolygonDef sd1;
sd1.SetAsBox(0.25f, 0.5f);
sd1.density = 2.0f;
b2PolygonDef sd2;
sd2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi);
sd2.density = 2.0f;
for (int i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
b2XForm xf1;
xf1.R.Set(0.3524f * b2_pi);
xf1.position = b2Mul(xf1.R, b2Vec2(1.0f, 0.0f));
b2PolygonDef sd1;
sd1.vertexCount = 3;
sd1.vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
sd1.vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
sd1.vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
sd1.density = 2.0f;
b2XForm xf2;
xf2.R.Set(-0.3524f * b2_pi);
xf2.position = b2Mul(xf2.R, b2Vec2(-1.0f, 0.0f));
b2PolygonDef sd2;
sd2.vertexCount = 3;
sd2.vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
sd2.vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
sd2.vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
sd2.density = 2.0f;
for (int32 i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x, 2.05f + 2.5f * i);
bd.angle = 0.0f;
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
b2PolygonDef sd_bottom;
sd_bottom.SetAsBox( 1.5f, 0.15f );
sd_bottom.density = 4.0f;
b2PolygonDef sd_left;
sd_left.SetAsBox(0.15f, 2.7f, b2Vec2(-1.45f, 2.35f), 0.2f);
sd_left.density = 4.0f;
b2PolygonDef sd_right;
sd_right.SetAsBox(0.15f, 2.7f, b2Vec2(1.45f, 2.35f), -0.2f);
sd_right.density = 4.0f;
b2BodyDef bd;
bd.position.Set( 0.0f, 2.0f );
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd_bottom);
body->CreateFixture(&sd_left);
body->CreateFixture(&sd_right);
body->SetMassFromShapes();
}
{
float32 loop1[] =
{
0.063134534f,8.3695248f,
0.94701801f,9.3165428f,
0.0f,9.0640047f,
-0.12626907f,10.326695f,
1.4520943f,11.77879f,
2.2728432f,10.137292f,
2.3991123f,11.147444f,
3.5986685f,10.958041f,
3.9143411f,7.3593722f,
4.1668793f,9.4428119f,
5.4295699f,9.3165428f,
6.2503189f,8.3063903f,
6.6922606f,10.137292f,
4.9876282f,9.8216191f,
4.7350901f,10.958041f,
7.2604714f,11.652521f,
10.732871f,11.147444f,
10.480333f,10.642368f,
10.732871f,9.8216191f,
11.55362f,9.4428119f,
12.374369f,9.3796773f,
13.005714f,9.8216191f,
13.195118f,10.38983f,
13.005714f,10.768637f,
12.626907f,10.894906f,
12.753176f,11.526252f,
13.573925f,11.715655f,
14.836616f,11.399982f,
16.351844f,10.768637f,
17.867073f,11.399982f,
17.803939f,10.263561f,
17.361997f,8.3063903f,
17.803939f,8.1801212f,
18.056477f,9.5059464f,
18.182746f,11.336848f,
18.561553f,11.210579f,
18.561553f,9.6322155f,
18.561553f,7.7381795f,
18.687822f,5.5284708f,
19.382302f,5.6547398f,
19.066629f,8.1801212f,
19.003495f,10.263561f,
19.066629f,11.463117f,
19.887378f,11.841924f,
20.708127f,11.273713f,
21.0238f,10.011023f,
20.708127f,7.2962377f,
21.086934f,6.2860852f,
21.150069f,3.7607038f,
20.392455f,2.5611476f,
18.624688f,2.5611476f,
20.771262f,2.1192059f,
20.771262f,0.22516988f,
18.624688f,-0.2799064f,
13.826463f,0.16203534f,
14.015867f,1.7403987f,
13.195118f,2.1823404f,
12.626907f,1.5509951f,
12.879445f,0.85651522f,
12.626907f,0.35143895f,
10.543467f,1.298457f,
11.490485f,3.9501074f,
13.889598f,3.6344347f,
13.889598f,2.9399549f,
14.584077f,3.8869729f,
11.932427f,5.2127981f,
9.7227183f,4.0132419f,
10.796005f,3.5081657f,
9.7858528f,3.2556275f,
10.796005f,2.4980131f,
7.9549513f,1.7403987f,
9.6595837f,1.424726f,
9.217642f,0.66711162f,
8.270624f,-0.090502792f,
7.0079333f,0.85651522f,
6.1240498f,-0.15363733f,
6.1240498f,3.192493f,
5.6821081f,2.4348786f,
4.9876282f,2.1192059f,
4.1037447f,1.8666678f,
3.0304576f,1.8666678f,
2.0834396f,2.245475f,
1.6414979f,2.6242822f,
1.3258252f,3.5081657f,
1.2626907f,0.47770802f,
0.63134534f,0.035766276f,
0.063134534f,0.98278429f
};
float32 loop2[] =
{
8.270624f,6.1598161f,
8.270624f,5.3390672f,
8.7757003f,5.086529f,
9.4701801f,5.5284708f,
9.217642f,6.033547f,
8.7757003f,6.4123542f
};
b2Vec2 b2Loop1[87];
b2Vec2 b2Loop2[6];
for (int32 i = 86; i >= 0; i--) {
b2Loop1[86 - i].Set(loop1[i*2] + 10.0f, loop1[i*2 + 1] + 1.0f);
}
/*for (int32 i = 0; i < 87; i++) {
b2Loop1[i].Set(loop1[i*2] + 10.0f, loop1[i*2 + 1] + 1.0f);
}*/
for (int32 i = 0; i < 6; i++) {
b2Loop2[i].Set(loop2[i*2], loop2[i*2 + 1]);
}
b2BodyDef bd;
bd.position.Set( 0.0f, 0.0f );
b2Body* body = m_world->CreateBody(&bd);
b2CircleDef weight;
weight.filter.maskBits = 0x0000;
weight.density = 4.0f;
weight.radius = 0.5f;
weight.localPosition.Set(8.9f, 5.75f);
body->CreateFixture(&weight);
b2EdgeChainDef edgeDef;
edgeDef.vertexCount = 6;
edgeDef.vertices = b2Loop2;
b2CreateEdgeChain(body, &edgeDef);
body->SetMassFromShapes();
body = m_world->CreateBody(&bd);
weight.radius = 5.0f;
weight.localPosition.Set(20.5f, 7.0f);
body->CreateFixture(&weight);
edgeDef.vertexCount = 87;
edgeDef.vertices = b2Loop1;
b2CreateEdgeChain(body, &edgeDef);
body->SetMassFromShapes();
}
}
static Test* Create()
{
return new DynamicEdges;
}
};
#endif

View File

@@ -0,0 +1,465 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef ELASTIC_BODY_H
#define ELASTIC_BODY_H
class ElasticBody : public Test
{
public:
b2Body* bodies[64];
b2Body* m_ground;
b2Body* m_elev;
b2PrismaticJoint* m_joint_elev;
/// Main...
ElasticBody()
{
/// Bottom static body
{
b2PolygonDef sd;
sd.SetAsBox(50.0f, 2.0f);
sd.friction = 0.1f;
sd.restitution = 0.1f;
b2BodyDef bd;
bd.position.Set(-1.0f, -7.5f);
m_ground = m_world->CreateBody(&bd);
m_ground->CreateFixture(&sd);
}
/// Upper static body
{
b2PolygonDef sd;
sd.SetAsBox(20.0f, 0.50f,b2Vec2(0.f,0.f),0.047f*b2_pi);
sd.friction = 0.01f;
sd.restitution = 0.001f;
b2BodyDef bd;
bd.position.Set(-20.f, 93.0f);
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
sd.SetAsBox(15.f, 0.50f,b2Vec2(-15.0f,12.5f),0.0f);
g->CreateFixture(&sd);
sd.SetAsBox(20.f,0.5f,b2Vec2(0.0f,-25.0f),-0.5f);
g->CreateFixture(&sd);
}
/// Left channel left wall
{
b2PolygonDef sd;
sd.SetAsBox(0.7f, 55.0f);
sd.friction = 0.1f;
sd.restitution = 0.1f;
b2BodyDef bd;
bd.position.Set(-49.3f, 50.0f);
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
}
/// Right wall
{
b2PolygonDef sd;
sd.SetAsBox(0.7f, 55.0f);
sd.friction = 0.1f;
sd.restitution = 0.1f;
b2BodyDef bd;
bd.position.Set(45.f, 50.0f);
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
}
/// Left channel right upper wall
{
b2PolygonDef sd;
sd.SetAsBox(0.5f, 20.0f);
sd.friction = 0.05f;
sd.restitution = 0.01f;
b2BodyDef bd;
bd.position.Set(-42.0f, 70.0f);
bd.angle = -0.03f*b2_pi;
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
}
/// Left channel right lower wall
{
b2PolygonDef sd;
sd.SetAsBox(0.50f, 23.0f);
sd.friction = 0.05f;
sd.restitution = 0.01f;
b2BodyDef bd;
bd.position.Set(-44.0f, 27.0f);
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
/// Bottom motors
b2CircleDef cd;
cd.radius = 3.0f;
cd.density = 15.0f;
cd.friction = 1.f;
cd.restitution = 0.2f;
/// 1.
bd.position.Set(-40.0f,2.5f);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
b2RevoluteJointDef jr;
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
jr.maxMotorTorque = 30000.f;
jr.enableMotor = true;
jr.motorSpeed = 20.f;
m_world->CreateJoint(&jr);
/// 1. left down
bd.position.Set(-46.0f,-2.5f);
cd. radius = 1.5f; jr.motorSpeed = -20.f;
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
sd.SetAsBox(2.0f, 0.50f);
body->CreateFixture(&sd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter());
m_world->CreateJoint(&jr);
/// 2.
cd.radius = 3.0f; jr.motorSpeed = 20.f;
bd.position.Set(-32.0f,2.5f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 3.
jr.motorSpeed = 20.f;
bd.position.Set(-24.0f,1.5f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 4.
bd.position.Set(-16.0f,0.8f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 5.
bd.position.Set(-8.0f,0.5f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 6.
bd.position.Set(0.0f,0.1f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 7.
bd.position.Set(8.0f,-0.5f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&cd);
sd.SetAsBox(3.7f, 0.5f);
body->CreateFixture(&sd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter()+b2Vec2(0.f,1.f));
m_world->CreateJoint(&jr);
/// 8. right rotator
sd.SetAsBox(5.f, 0.5f);
sd.density = 2.0f;
bd.position.Set(18.0f,1.f);
b2Body* rightmotor = m_world->CreateBody(&bd);
rightmotor->CreateFixture(&sd);
sd.SetAsBox(4.5f, 0.5f, b2Vec2(0.f,0.f),b2_pi/3.f);
rightmotor->CreateFixture(&sd);
sd.SetAsBox(4.5f, 0.5f, b2Vec2(0.f,0.f),b2_pi*2.f/3.f);
rightmotor->CreateFixture(&sd);
cd.radius = 4.2f;
rightmotor->CreateFixture(&cd);
rightmotor->SetMassFromShapes();
jr.Initialize (g,rightmotor,rightmotor->GetWorldCenter());
jr.maxMotorTorque = 70000.f;
jr.motorSpeed = -4.f;
m_world->CreateJoint(&jr);
/// 9. left rotator
sd.SetAsBox(8.5f, 0.5f);
sd.density = 2.0f;
bd.position.Set(-34.0f,17.f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&sd);
sd.SetAsBox(8.5f, 0.5f, b2Vec2(0.f,0.f),b2_pi*.5f);
body->CreateFixture(&sd);
cd.radius = 7.f;
cd.friction = 0.9f;
body->CreateFixture(&cd);
body->SetMassFromShapes();
jr.Initialize (g,body,body->GetWorldCenter());
jr.maxMotorTorque = 100000.f;
jr.motorSpeed = -5.f;
m_world->CreateJoint(&jr);
/// big compressor
sd.SetAsBox(3.0f,4.f);
sd.density = 10.0f;
bd.position.Set(-16.0f,17.f);
b2Body *hammerleft = m_world->CreateBody(&bd);
hammerleft->CreateFixture(&sd);
hammerleft->SetMassFromShapes();
b2DistanceJointDef jd;
jd.Initialize(body, hammerleft, body->GetWorldCenter()+b2Vec2(0.f,6.f), hammerleft->GetWorldCenter() );
m_world->CreateJoint(&jd);
bd.position.Set(4.0f,17.f);
b2Body *hammerright = m_world->CreateBody(&bd);
hammerright->CreateFixture(&sd);
hammerright->SetMassFromShapes();
jd.Initialize(body, hammerright, body->GetWorldCenter()-b2Vec2(0.f,6.f), hammerright->GetWorldCenter() );
m_world->CreateJoint(&jd);
/// pusher
sd.SetAsBox(6.f,0.75f);
bd.position.Set(-21.0f,9.f);
b2Body* pusher = m_world->CreateBody(&bd);
pusher->CreateFixture(&sd);
sd.SetAsBox(2.f,1.5f,b2Vec2(-5.f,0.f),0.f);
pusher->SetMassFromShapes();
pusher->CreateFixture(&sd);
jd.Initialize(rightmotor,pusher,rightmotor->GetWorldCenter()+b2Vec2(-8.0f,0.f),
pusher->GetWorldCenter()+b2Vec2(5.0f,0.f) );
m_world->CreateJoint(&jd);
}
/// Static bodies above motors
{
b2PolygonDef sd;
b2CircleDef cd;
sd.SetAsBox(9.0f, 0.5f);
sd.friction = 0.05f;
sd.restitution = 0.01f;
b2BodyDef bd;
bd.position.Set(-15.5f, 12.f);
bd.angle = 0.0;
b2Body* g = m_world->CreateBody(&bd);
g->CreateFixture(&sd);
sd.SetAsBox(8.f, 0.5f, b2Vec2(23.f,0.f),0.f);
g->CreateFixture(&sd);
/// compressor statics
sd.SetAsBox(7.0f, 0.5f, b2Vec2(-2.f,9.f),0.f);
g->CreateFixture(&sd);
sd.SetAsBox(9.0f, 0.5f, b2Vec2(22.f,9.f),0.f);
g->CreateFixture(&sd);
sd.SetAsBox(19.0f, 0.5f, b2Vec2(-9.f,15.f),-0.05f);
g->CreateFixture(&sd);
sd.SetAsBox(4.7f, 0.5f, b2Vec2(15.f,11.5f),-0.5f);
g->CreateFixture(&sd);
/// below compressor
sd.SetAsBox(26.0f, 0.3f, b2Vec2(17.f,-4.4f),-0.02f);
g->CreateFixture(&sd);
cd.radius = 1.0f; cd.friction = 1.0;
cd.localPosition = b2Vec2(29.f,-6.f);
g->CreateFixture(&cd);
cd.radius = 0.7f;
cd.localPosition = b2Vec2(-2.f,-4.5f);
g->CreateFixture(&cd);
}
/// Elevator
{
b2BodyDef bd;
b2CircleDef cd;
b2PolygonDef sd;
bd.position.Set(40.0f,4.0f);
m_elev = m_world->CreateBody(&bd);
sd.SetAsBox(0.5f, 2.5f,b2Vec2(3.0f,-3.0f), 0.f);
sd.density = 1.f;
sd.friction = 0.01f;
m_elev->CreateFixture(&sd);
sd.SetAsBox(7.0f, 0.5f, b2Vec2(-3.5f,-5.5f), 0.f);
m_elev->CreateFixture(&sd);
sd.SetAsBox(0.5f, 2.5f, b2Vec2(-11.f,-3.5f), 0.f);
m_elev->CreateFixture(&sd);
m_elev->SetMassFromShapes();
b2PrismaticJointDef jp;
jp.Initialize(m_ground,m_elev, bd.position, b2Vec2(0.0f, 1.0f));
jp.lowerTranslation = 0.0f;
jp.upperTranslation = 100.0f;
jp.enableLimit = true;
jp.enableMotor = true;
jp.maxMotorForce = 10000.f;
jp.motorSpeed = 0.f;
m_joint_elev = (b2PrismaticJoint*)m_world->CreateJoint(&jp);
/// Korb
sd.SetAsBox(2.3f, 0.5f,b2Vec2(1.f,0.0f), 0.0f);
sd.density = 0.5f;
bd.position.Set(29.0f,6.5f);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd);
sd.SetAsBox(2.5f, 0.5f,b2Vec2(3.0f,-2.f), b2_pi/2.f);
body->CreateFixture(&sd);
sd.SetAsBox(4.6f, 0.5f,b2Vec2(7.8f,-4.0f), 0.f);
body->CreateFixture(&sd);
sd.SetAsBox(0.5f, 4.5f,b2Vec2(12.f,0.0f), 0.f);
body->CreateFixture(&sd);
sd.SetAsBox(0.5f, 0.5f,b2Vec2(13.f,4.0f), 0.f);
body->CreateFixture(&sd);
cd.radius = 0.7f; cd.density = 1.f; cd.friction = 0.01f;
cd.localPosition = b2Vec2(0.f,0.f);
body->CreateFixture(&cd);
body->SetMassFromShapes();
b2RevoluteJointDef jr;
jr.Initialize(m_elev,body, bd.position);
jr.enableLimit = true;
jr.lowerAngle = -0.2f;
jr.upperAngle = b2_pi*1.1f;
jr.collideConnected = true;
m_world->CreateJoint(&jr);
/// upper body exit
sd.SetAsBox(14.0f, 0.5f,b2Vec2(-3.5f,-10.0f), 0.0f);
bd.position.Set(17.5f,96.0f);
body = m_world->CreateBody(&bd);
body->CreateFixture(&sd);
}
/// "Elastic body" 64 bodies - something like a lin. elastic compound
/// connected via dynamic forces (springs)
{
b2PolygonDef sd;
sd.SetAsBox(0.55f, 0.55f);
sd.density = 1.5f;
sd.friction = 0.01f;
sd.filter.groupIndex = -1;
b2Vec2 startpoint(30.f,20.f);
b2BodyDef bd;
bd.isBullet = false;
bd.allowSleep = false;
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
{
bd.position.Set(j*1.02f, 2.51f + 1.02f * i);
bd.position += startpoint;
b2Body* body = m_world->CreateBody(&bd);
bodies[8*i+j] = body;
body->CreateFixture(&sd);
body->SetMassFromShapes();
}
}
}
}
/// Apply dynamic forces (springs) and check elevator state
void Step(Settings* settings)
{
Test::Step(settings);
for (int i=0; i<8; ++i){
for (int j=0; j<8; ++j){
b2Vec2 zero(0.0f,0.0f);
b2Vec2 down(0.0f, -0.5f);
b2Vec2 up(0.0f, 0.5f);
b2Vec2 right(0.5f, 0.0f);
b2Vec2 left(-0.5f, 0.0f);
int ind = i*8+j;
int indr = ind+1;
int indd = ind+8;
float32 spring = 500.0f;
float32 damp = 5.0f;
if (j<7) {
AddSpringForce(*(bodies[ind]),zero,*(bodies[indr]),zero,spring, damp, 1.0f);
AddSpringForce(*(bodies[ind]),right,*(bodies[indr]),left,0.5f*spring, damp, 0.0f);
}
if (i<7) {
AddSpringForce(*(bodies[ind]),zero,*(bodies[indd]),zero,spring, damp, 1.0f);
AddSpringForce(*(bodies[ind]),up,*(bodies[indd]),down,0.5f*spring,damp,0.0f);
}
int inddr = indd + 1;
int inddl = indd - 1;
float32 drdist = sqrtf(2.0f);
if (i < 7 && j < 7){
AddSpringForce(*(bodies[ind]),zero,*(bodies[inddr]),zero,spring, damp, drdist);
}
if (i < 7 && j > 0){
AddSpringForce(*(bodies[ind]),zero,*(bodies[inddl]),zero,spring, damp, drdist);
}
indr = ind+2;
indd = ind+8*2;
if (j<6) {
AddSpringForce(*(bodies[ind]),zero,*(bodies[indr]),zero,spring, damp, 2.0f);
}
if (i<6) {
AddSpringForce(*(bodies[ind]),zero,*(bodies[indd]),zero,spring,damp,2.0f);
}
inddr = indd + 2;
inddl = indd - 2;
drdist = sqrtf(2.0f)*2.0f;
if (i < 6 && j < 6){
AddSpringForce(*(bodies[ind]),zero,*(bodies[inddr]),zero,spring, damp, drdist);
}
if (i < 6 && j > 1){
AddSpringForce(*(bodies[ind]),zero,*(bodies[inddl]),zero,spring, damp, drdist);
}
}
}
/// Check if bodies are near elevator
/// Look if the body to lift is near the elevator
b2Vec2 p1 = bodies[0]->GetWorldCenter();
b2Vec2 p2 = bodies[63]->GetWorldCenter();
/// m_elev: elevator prism. joint
b2Vec2 e = m_elev->GetWorldCenter() + b2Vec2(0.f,7.f);
// maybe not the best way to do it...
// Bodies reached the elevator side
if ( p1.x>e.x || p2.x>e.x ) {
// go up
if ( ( p1.y<e.y || p2.y<e.y ) &&
( m_joint_elev->GetJointTranslation()<=m_joint_elev->GetLowerLimit()+1.f ) )
{
m_joint_elev->SetMotorSpeed(20.f);
//printf("lift goes up trans: %G\n",m_joint_elev->GetJointTranslation());
}
}
// go down
if ( (m_joint_elev->GetJointTranslation()>=m_joint_elev->GetUpperLimit()-2.f) )
{
m_joint_elev->SetMotorSpeed(-15.f);
//printf("lift goes down: %G\n",m_joint_elev->GetJointTranslation());
}
}
/// Add a spring force
void AddSpringForce(b2Body& bA, b2Vec2& localA, b2Body& bB, b2Vec2& localB, float32 k, float32 friction, float32 desiredDist)
{
b2Vec2 pA = bA.GetWorldPoint(localA);
b2Vec2 pB = bB.GetWorldPoint(localB);
b2Vec2 diff = pB - pA;
//Find velocities of attach points
b2Vec2 vA = bA.GetLinearVelocity() - b2Cross(bA.GetWorldVector(localA), bA.GetAngularVelocity());
b2Vec2 vB = bB.GetLinearVelocity() - b2Cross(bB.GetWorldVector(localB), bB.GetAngularVelocity());
b2Vec2 vdiff = vB-vA;
float32 dx = diff.Normalize(); //normalizes diff and puts length into dx
float32 vrel = vdiff.x*diff.x + vdiff.y*diff.y;
float32 forceMag = -k*(dx-desiredDist) - friction*vrel;
diff *= forceMag; // diff *= forceMag
bB.ApplyForce(diff, bA.GetWorldPoint(localA));
diff *= -1.0f;
bA.ApplyForce(diff, bB.GetWorldPoint(localB));
}
/// Default constructor
static Test* Create()
{
return new ElasticBody;
}
};
#endif

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef PYRAMID_STATIC_EDGES_H
#define PYRAMID_STATIC_EDGES_H
class PyramidStaticEdges : public Test
{
public:
PyramidStaticEdges()
{
{
float32 coords[] =
{
50.0f,0.0f,
-50.0f,0.0f
};
b2Vec2 verts[2];
for (int32 i = 0; i < 2; i++)
{
verts[i].Set(coords[i*2], coords[i*2 + 1]);
}
b2BodyDef bd;
bd.position.Set( 0.0f, 0.0f );
b2Body* body = m_world->CreateBody(&bd);
b2EdgeDef edgeDef;
edgeDef.vertex1 = verts[0];
edgeDef.vertex2 = verts[1];
body->CreateFixture(&edgeDef);
//body->SetMassFromShapes();
}
{
b2PolygonDef sd;
float32 a = 0.5f;
sd.SetAsBox(a, a);
sd.density = 5.0f;
b2Vec2 x(-10.0f, 1.0f);
b2Vec2 y;
b2Vec2 deltaX(0.5625f, 2.0f);
b2Vec2 deltaY(1.125f, 0.0f);
const int32 N = 2;
for (int32 i = 0; i < N; ++i)
{
y = x;
for (int32 j = i; j < N; ++j)
{
b2BodyDef bd;
bd.position = y;
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd);
body->SetMassFromShapes();
y += deltaY;
}
x += deltaX;
}
}
}
static Test* Create()
{
return new PyramidStaticEdges;
}
};
#endif

View File

@@ -0,0 +1,278 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef STATIC_EDGES_H
#define STATIC_EDGES_H
class StaticEdges : public Test
{
public:
StaticEdges()
{
#if 0
{
b2CircleDef sd;
sd.radius = 0.5f;
sd.localPosition.SetZero();
sd.density = 2.0f;
b2BodyDef bd;
bd.position.Set(0.0f, 2.0f);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd);
body->SetMassFromShapes();
}
#endif
{
b2CircleDef sd1;
sd1.radius = 0.5f;
sd1.localPosition.Set(-0.5f, 0.5f);
sd1.density = 2.0f;
b2CircleDef sd2;
sd2.radius = 0.5f;
sd2.localPosition.Set(0.5f, 0.5f);
sd2.density = 0.0f; // massless
for (int i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
b2PolygonDef sd1;
sd1.SetAsBox(0.25f, 0.5f);
sd1.density = 2.0f;
b2PolygonDef sd2;
sd2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi);
sd2.density = 2.0f;
for (int i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
b2XForm xf1;
xf1.R.Set(0.3524f * b2_pi);
xf1.position = b2Mul(xf1.R, b2Vec2(1.0f, 0.0f));
b2PolygonDef sd1;
sd1.vertexCount = 3;
sd1.vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
sd1.vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
sd1.vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
sd1.density = 2.0f;
b2XForm xf2;
xf2.R.Set(-0.3524f * b2_pi);
xf2.position = b2Mul(xf2.R, b2Vec2(-1.0f, 0.0f));
b2PolygonDef sd2;
sd2.vertexCount = 3;
sd2.vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
sd2.vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
sd2.vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
sd2.density = 2.0f;
for (int32 i = 0; i < 10; ++i)
{
float32 x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.position.Set(x, 2.05f + 2.5f * i);
bd.angle = 0.0f;
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&sd1);
body->CreateFixture(&sd2);
body->SetMassFromShapes();
}
}
{
float32 loop1[] =
{
0.063134534f,8.3695248f,
0.94701801f,9.3165428f,
0.0f,9.0640047f,
-0.12626907f,10.326695f,
1.4520943f,11.77879f,
2.2728432f,10.137292f,
2.3991123f,11.147444f,
3.5986685f,10.958041f,
3.9143411f,7.3593722f,
4.1668793f,9.4428119f,
5.4295699f,9.3165428f,
6.2503189f,8.3063903f,
6.6922606f,10.137292f,
4.9876282f,9.8216191f,
4.7350901f,10.958041f,
7.2604714f,11.652521f,
10.732871f,11.147444f,
10.480333f,10.642368f,
10.732871f,9.8216191f,
11.55362f,9.4428119f,
12.374369f,9.3796773f,
13.005714f,9.8216191f,
13.195118f,10.38983f,
13.005714f,10.768637f,
12.626907f,10.894906f,
12.753176f,11.526252f,
13.573925f,11.715655f,
14.836616f,11.399982f,
16.351844f,10.768637f,
17.867073f,11.399982f,
17.803939f,10.263561f,
17.361997f,8.3063903f,
17.803939f,8.1801212f,
18.056477f,9.5059464f,
18.182746f,11.336848f,
18.561553f,11.210579f,
18.561553f,9.6322155f,
18.561553f,7.7381795f,
18.687822f,5.5284708f,
19.382302f,5.6547398f,
19.066629f,8.1801212f,
19.003495f,10.263561f,
19.066629f,11.463117f,
19.887378f,11.841924f,
20.708127f,11.273713f,
21.0238f,10.011023f,
20.708127f,7.2962377f,
21.086934f,6.2860852f,
21.150069f,3.7607038f,
20.392455f,2.5611476f,
18.624688f,2.5611476f,
20.771262f,2.1192059f,
20.771262f,0.22516988f,
18.624688f,-0.2799064f,
13.826463f,0.16203534f,
14.015867f,1.7403987f,
13.195118f,2.1823404f,
12.626907f,1.5509951f,
12.879445f,0.85651522f,
12.626907f,0.35143895f,
10.543467f,1.298457f,
11.490485f,3.9501074f,
13.889598f,3.6344347f,
13.889598f,2.9399549f,
14.584077f,3.8869729f,
11.932427f,5.2127981f,
9.7227183f,4.0132419f,
10.796005f,3.5081657f,
9.7858528f,3.2556275f,
10.796005f,2.4980131f,
7.9549513f,1.7403987f,
9.6595837f,1.424726f,
9.217642f,0.66711162f,
8.270624f,-0.090502792f,
7.0079333f,0.85651522f,
6.1240498f,-0.15363733f,
6.1240498f,3.192493f,
5.6821081f,2.4348786f,
4.9876282f,2.1192059f,
4.1037447f,1.8666678f,
3.0304576f,1.8666678f,
2.0834396f,2.245475f,
1.6414979f,2.6242822f,
1.3258252f,3.5081657f,
1.2626907f,0.47770802f,
0.63134534f,0.035766276f,
0.063134534f,0.98278429f
};
float32 loop2[] =
{
8.270624f,6.1598161f,
8.270624f,5.3390672f,
8.7757003f,5.086529f,
9.4701801f,5.5284708f,
9.217642f,6.033547f,
8.7757003f,6.4123542f
};
float32 loop3[] =
{
-5.0f, 10.0f,
5.0f, 10.0f,
5.0f, 0.0f,
-5.0f, 0.0f,
};
b2Vec2 pointLoop1[87];
b2Vec2 pointLoop2[6];
b2Vec2 pointLoop3[4];
for (int32 i = 0; i < 87; i++)
{
pointLoop1[i].Set(loop1[i*2] - 10.0f, loop1[i*2 + 1]);
}
for (int32 i = 0; i < 6; i++)
{
pointLoop2[i].Set(loop2[i*2] - 10.0f, loop2[i*2 + 1]);
}
for (int32 i = 0; i < 4; i++)
{
pointLoop3[i].Set(loop3[i*2], loop3[i*2 + 1]);
}
b2BodyDef bd;
bd.position.Set( 0.0f, 0.0f );
b2Body* body = m_world->CreateBody(&bd);
b2EdgeChainDef edgeDef;
edgeDef.vertexCount = 87;
edgeDef.vertices = pointLoop1;
b2CreateEdgeChain(body, &edgeDef);
edgeDef.vertexCount = 6;
edgeDef.vertices = pointLoop2;
b2CreateEdgeChain(body, &edgeDef);
//edgeDef.vertexCount = 4;
//edgeDef.vertices = pointLoop3;
//b2CreateEdgeChain(body, &edgeDef);
}
}
static Test* Create()
{
return new StaticEdges;
}
};
#endif