Skip to content

Commit

Permalink
working for #15, which was also added as b4-1.
Browse files Browse the repository at this point in the history
bo::reset() added to avoid multiple edits in future.
mouse position is shown in bottom.
Mass can be added using coordinate input.
  • Loading branch information
simo-11 committed Oct 15, 2018
1 parent ce743f8 commit a909364
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 60 deletions.
82 changes: 40 additions & 42 deletions Box2D/Dynamics/Joints/b2RigidPlasticJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,52 +157,50 @@ void b2RigidPlasticJoint::SolveVelocityConstraints(const b2SolverData& data)
Cdot.SetZero();
impulse.SetZero();
float32 h = data.step.dt;
if (!isOverLoaded()) {
/** Adapted from b2Friction.cpp
*
*/
// Solve linear part
b2Vec2 lCdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
if (lCdot.LengthSquared() > 0.f) {
b2Vec2 lImpulse = -b2Mul(m_linearMass, lCdot);
b2Vec2 oldLImpulse = m_linearImpulse;
m_linearImpulse += lImpulse;
if (m_linearImpulse.LengthSquared() > m_maxForce.LengthSquared())
{
m_linearImpulse.Normalize();
m_linearImpulse *= m_maxForce.Length();
}
impulse.x = m_linearImpulse.x - oldLImpulse.x;
impulse.y = m_linearImpulse.y - oldLImpulse.y;
Cdot.x = lCdot.x;
Cdot.y = lCdot.y;
wA += m_invIA * b2Cross(m_rA, lImpulse);
wB += m_invIB * b2Cross(m_rB, lImpulse);
}
// Solve angular part
float32 aCdot = wB - wA - m_dw0;
if (b2Abs(aCdot) > 0.f) {
float32 aImpulse = -m_angularMass * aCdot;
float32 oldAImpulse = m_angularImpulse;
float32 maxImpulse = h * m_maxTorque;
m_angularImpulse = b2Clamp
(m_angularImpulse + aImpulse, -maxImpulse, maxImpulse);
aImpulse = m_angularImpulse - oldAImpulse;
impulse.z = aImpulse;
/** Adapted from b2Friction.cpp
*
*/
// Solve linear part
b2Vec2 lCdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
if (lCdot.LengthSquared() > 0.f) {
b2Vec2 lImpulse = -b2Mul(m_linearMass, lCdot);
b2Vec2 oldLImpulse = m_linearImpulse;
m_linearImpulse += lImpulse;
if (m_linearImpulse.LengthSquared() > m_maxForce.LengthSquared())
{
m_linearImpulse.Normalize();
m_linearImpulse *= m_maxForce.Length();
}
Cdot.z = aCdot;
m_impulse += impulse;
impulse.x = m_linearImpulse.x - oldLImpulse.x;
impulse.y = m_linearImpulse.y - oldLImpulse.y;
Cdot.x = lCdot.x;
Cdot.y = lCdot.y;
wA += m_invIA * b2Cross(m_rA, lImpulse);
wB += m_invIB * b2Cross(m_rB, lImpulse);
}
// Solve angular part
float32 aCdot = wB - wA - m_dw0;
if (b2Abs(aCdot) > 0.f) {
float32 aImpulse = -m_angularMass * aCdot;
float32 oldAImpulse = m_angularImpulse;
float32 maxImpulse = h * m_maxTorque;
m_angularImpulse = b2Clamp
(m_angularImpulse + aImpulse, -maxImpulse, maxImpulse);
aImpulse = m_angularImpulse - oldAImpulse;
impulse.z = aImpulse;
}
Cdot.z = aCdot;
m_impulse += impulse;
#ifdef EP_LOG
if (epLogActive && epLogEnabled) {
if (b2Dot(Cdot, Cdot) > 0.f) {
epLog("RPJ:VC:%d Cdot=%g %g %g\n", id,
Cdot.x, Cdot.y, Cdot.z);
epLog("RPJ:VC:%d impulse=%g %g %g\n", id,
impulse.x, impulse.y, impulse.z);
}
if (epLogActive && epLogEnabled) {
if (b2Dot(Cdot, Cdot) > 0.f) {
epLog("RPJ:VC:%d Cdot=%g %g %g\n", id,
Cdot.x, Cdot.y, Cdot.z);
epLog("RPJ:VC:%d impulse=%g %g %g\n", id,
impulse.x, impulse.y, impulse.z);
}
#endif
}
#endif
if (nullptr != debugListener) {
debugListener->EndVelocityIteration(this, data);
}
Expand Down
68 changes: 52 additions & 16 deletions ep/ep/Beam.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ namespace {
}

namespace bo {
int b1,b2,b3;
int b1,b2,b3,b4;
void reset() {
b1 =b2=b3=b4= 0;
}
}


Expand Down Expand Up @@ -105,6 +108,9 @@ class Beam : public Test
m_bombSpawnPoint = settings->bombSpawnPoint;
LaunchBomb();
}
if (bo::b4) {
AddMass(settings->addMassPoint);
}
}
bool showMenu = true;
virtual void Ui(){
Expand Down Expand Up @@ -276,13 +282,11 @@ void Beam::BeamExtraUi()
if (ImGui::CollapsingHeader("BeamOptions", 0, true, openLists)) {
float32 uihx=0.f,by=0.f;
if (ImGui::Button("None")) {
bo::b1 = 0;
bo::b2 = 0;
bo::b3 = 0;
bo::reset();
}
if (ImGui::RadioButton("B1-1", &bo::b1,1)) {
bo::b2 = 0;
bo::b3 = 0;
bo::reset();
bo::b1 = 1;
uihx = 10;
by = 27.01f;
settings->epDebugSteps = 10;
Expand All @@ -297,8 +301,8 @@ hx=10\n\
}
ImGui::SameLine();
if (ImGui::RadioButton("B1-2", &bo::b1,2)) {
bo::b2 = 0;
bo::b3 = 0;
bo::reset();
bo::b1 = 2;
uihx = 3;
by = 13.01f;
settings->epDebugSteps = 10;
Expand Down Expand Up @@ -337,8 +341,8 @@ hx=3\n\
restartPending = true;
}
if (ImGui::RadioButton("B2-1", &bo::b2, 1)) {
bo::b1 = 0;
bo::b3 = 0;
bo::reset();
bo::b2 = 1;
uihx = 1.f;
epLogEnabled = true;
}
Expand Down Expand Up @@ -366,8 +370,8 @@ hx=1\n\
restartPending = true;
}
if (ImGui::RadioButton("B3-1", &bo::b3, 1)) {
bo::b1 = 0;
bo::b2 = 0;
bo::reset();
bo::b3 = 1;
uihx = 40.f;
epLogEnabled = true;
}
Expand All @@ -382,8 +386,8 @@ epDebug and log active\n\
}
ImGui::SameLine();
if (ImGui::RadioButton("B3-11", &bo::b3, 11)) {
bo::b1 = 0;
bo::b2 = 0;
bo::reset();
bo::b3 =11;
uihx = 40.f;
epLogEnabled = true;
}
Expand All @@ -398,8 +402,8 @@ epDebug and log active\n\
}
ImGui::SameLine();
if (ImGui::RadioButton("B3-2", &bo::b3, 2)) {
bo::b1 = 0;
bo::b2 = 0;
bo::reset();
bo::b3 = 2;
uihx = 10.f;
epLogEnabled = false;
}
Expand Down Expand Up @@ -435,6 +439,38 @@ epDebug and log not active\n\
openLists = true;
restartPending = true;
}
if (ImGui::RadioButton("B4-1", &bo::b4, 1)) {
bo::reset();
bo::b4 = 1;
uihx = 10.f;
epLogEnabled = true;
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Horizontal\n\
rigidPlastic\n\
hx=10\n\
1 10 m body\n\
epLog active\n\
addMass=500 000\n\
addMassPoint=(10,29)\n\
");
}
if (bo::b4 && uihx != 0.f) {
settings->pause = true;
hx = uihx;
so_count =1;
horizontal = true;
addSoft = false;
addHard = false;
addFriction = false;
addElasticPlastic = false;
addRigidPlastic = true;
firstIsHinge = false;
openLists = true;
restartPending = true;
settings->addMassPoint = b2Vec2(10, 29);
settings->addMass = 500000;
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion ep/ep/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,18 @@ static void sInterface()
settings.addRigidTriangles = false;
settings.addEPBeams = false;
}
if (ImGui::Button("Add at @")) {
b2Vec2 p;
test->AddMass(settings.addMassPoint);
}
ImGui::SameLine();
float mv[2];
mv[0] = settings.addMassPoint.x;
mv[1] = settings.addMassPoint.y;
if (ImGui::InputFloat2("", mv,3)) {
settings.addMassPoint.x = mv[0];
settings.addMassPoint.y = mv[1];
}
if (ImGui::IsItemHovered() && settings.addMasses) {
ImGui::SetTooltip("Use ALT-MB1");
}
Expand Down Expand Up @@ -962,7 +974,9 @@ int main(int, char**)
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
if (opened){
ImGui::SetCursorPos(ImVec2(5, (float)g_camera.m_height - 20));
ImGui::Text("%.1f ms, steppedTime=%.4f s", 1000.0 * frameTime, test->steppedTime);
b2Vec2 mp = test->GetMouseWorld();
ImGui::Text("%.1f ms, steppedTime=%.4f s mouse @(%.3f,%.3f)",
1000.0 * frameTime, test->steppedTime, mp.x,mp.y);
}
ImGui::End();
sSimulate();
Expand Down
5 changes: 4 additions & 1 deletion ep/ep/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct Settings
bombMultiplier = 10;
bombVelocity = b2Vec2(5, 0);
bombSpawnPoint = b2Vec2(-0.5f*bombWidth, 0.5f*bombWidth);
addMassPoint = b2Vec2(0, 0);
}
float32 hz;
float32 mouseJointForceScale;
Expand All @@ -148,6 +149,7 @@ struct Settings
float32 targetTime = 60;
bool singleStep;
bool addRigidTriangles, addMasses, addEPBeams,selectEPJoint; // using ALT-MB1 if active
b2Vec2 addMassPoint;
float32 addMass = 1000000, addMassSize=2;
bool initImpulses;
float32 forceScale, momentScale;
Expand Down Expand Up @@ -230,6 +232,7 @@ class Test : public b2ContactListener
B2_NOT_USED(impulse);
};
void ShiftOrigin(const b2Vec2& newOrigin);
b2Vec2 GetMouseWorld() { return m_mouseWorld; }
// ep-start
virtual void Ui(){};
virtual void UpdateCamera() {};
Expand Down Expand Up @@ -323,7 +326,7 @@ class Test : public b2ContactListener
b2Vec2 m_bombSpawnPoint;
bool m_bombSpawning;
b2Vec2 m_bombVelocity;
b2Vec2 m_mouseWorld;
b2Vec2 m_mouseWorld=b2Vec2(0,0);
int32 m_stepCount;

b2Profile m_maxProfile;
Expand Down

0 comments on commit a909364

Please sign in to comment.