Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major fixes #113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions liquidfun/Box2D/Box2D/Collision/Shapes/b2PolygonShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,27 @@ void b2PolygonShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, flo
B2_NOT_USED(childIndex);

b2Vec2 pLocal = b2MulT(xf.q, p - xf.p);

// If the nearest point is a corner, then the dot products of the normals on each of its edges will be positive.
int32 prevI = m_count - 1;
for (int32 i = 0; i < m_count; ++i)
{
const b2Vec2& beforeNorm = m_normals[prevI]; // The normal of point i and the point i-1
const b2Vec2& afterNorm = m_normals[i]; // The normal of point i and the point i+1
b2Vec2 dist = pLocal - m_vertices[i];
if (b2Dot(dist, beforeNorm) > 0.0f && b2Dot(dist, afterNorm) > 0.0f)
{
*distance = dist.Length();
*normal = b2Mul(xf.q, dist);
if (*distance > 0.0f)
*normal /= *distance;
return;
}
prevI = i;
}

float32 maxDistance = -FLT_MAX;
b2Vec2 normalForMaxDistance = pLocal;

for (int32 i = 0; i < m_count; ++i)
{
float32 dot = b2Dot(m_normals[i], pLocal - m_vertices[i]);
Expand All @@ -274,31 +292,8 @@ void b2PolygonShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, flo
normalForMaxDistance = m_normals[i];
}
}

if (maxDistance > 0)
{
b2Vec2 minDistance = normalForMaxDistance;
float32 minDistance2 = maxDistance * maxDistance;
for (int32 i = 0; i < m_count; ++i)
{
b2Vec2 distance = pLocal - m_vertices[i];
float32 distance2 = distance.LengthSquared();
if (minDistance2 > distance2)
{
minDistance = distance;
minDistance2 = distance2;
}
}

*distance = b2Sqrt(minDistance2);
*normal = b2Mul(xf.q, minDistance);
normal->Normalize();
}
else
{
*distance = maxDistance;
*normal = b2Mul(xf.q, normalForMaxDistance);
}
*distance = maxDistance;
*normal = b2Mul(xf.q, normalForMaxDistance);
}

bool b2PolygonShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
Expand Down
13 changes: 13 additions & 0 deletions liquidfun/Box2D/Box2D/Common/b2Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ typedef unsigned long long uint64;
#endif
#endif


/// @file
/// Global tuning constants based on meters-kilograms-seconds (MKS) units.
///


// Collision

/// The maximum number of contact points between two convex shapes. Do
Expand Down Expand Up @@ -196,6 +198,14 @@ typedef unsigned long long uint64;
/// The time into the future that collisions against barrier particles will be detected.
#define b2_barrierCollisionTime 2.5f

/// Multiplier for the radius of a particle when colliding with a fixture.
/// Originally the diameter was used, so a multiplier of 2 would restore that behaviour.
#define b2_fixtureParticleCollisionRadiusScaler 1

/// Prevents directional bias when solving elastic triads
#define b2_elasticPreserveVelocity


// Sleep

/// The time that a body must be still before it will go to sleep.
Expand All @@ -207,6 +217,7 @@ typedef unsigned long long uint64;
/// A body cannot sleep if its angular velocity is above this tolerance.
#define b2_angularSleepTolerance (2.0f / 180.0f * b2_pi)


// Memory Allocation

/// Implement this function to use your own memory allocator.
Expand Down Expand Up @@ -236,9 +247,11 @@ void b2SetNumAllocs(const int32 numAllocs);
/// Get number of calls to b2Alloc minus number of calls to b2Free.
int32 b2GetNumAllocs();


/// Logging function.
void b2Log(const char* string, ...);


/// Version numbering scheme.
/// See http://en.wikipedia.org/wiki/Software_versioning
struct b2Version
Expand Down
5 changes: 5 additions & 0 deletions liquidfun/Box2D/Box2D/Dynamics/b2World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,11 @@ void b2World::ClearForces()
body->m_force.SetZero();
body->m_torque = 0.0f;
}

for (b2ParticleSystem* p = m_particleSystemList; p; p = p->GetNext())
{
p->m_hasForce = false;
}
}

struct b2WorldQueryWrapper
Expand Down
14 changes: 11 additions & 3 deletions liquidfun/Box2D/Box2D/Particle/b2ParticleGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ b2ParticleGroup::b2ParticleGroup()

m_timestamp = -1;
m_mass = 0;
m_invMass = 0;
m_inertia = 0;
m_invInertia = 0;
m_center = b2Vec2_zero;
m_linearVelocity = b2Vec2_zero;
m_angularVelocity = 0;
Expand Down Expand Up @@ -79,9 +81,12 @@ void b2ParticleGroup::UpdateStatistics() const
}
if (m_mass > 0)
{
m_center *= 1 / m_mass;
m_linearVelocity *= 1 / m_mass;
m_invMass = 1 / m_mass;
m_center *= m_invMass;
m_linearVelocity *= m_invMass;
}
else
m_invMass = 0;
m_inertia = 0;
m_angularVelocity = 0;
for (int32 i = m_firstIndex; i < m_lastIndex; i++)
Expand All @@ -93,8 +98,11 @@ void b2ParticleGroup::UpdateStatistics() const
}
if (m_inertia > 0)
{
m_angularVelocity *= 1 / m_inertia;
m_invInertia = 1 / m_inertia;
m_angularVelocity *= m_invInertia;
}
else
m_invInertia = 0;
m_timestamp = m_system->m_timestamp;
}
}
Expand Down
4 changes: 2 additions & 2 deletions liquidfun/Box2D/Box2D/Particle/b2ParticleGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ class b2ParticleGroup
b2ParticleGroup* m_next;

mutable int32 m_timestamp;
mutable float32 m_mass;
mutable float32 m_inertia;
mutable float32 m_mass, m_invMass;
mutable float32 m_inertia, m_invInertia;
mutable b2Vec2 m_center;
mutable b2Vec2 m_linearVelocity;
mutable float32 m_angularVelocity;
Expand Down
Loading