File indexing completed on 2025-08-03 03:50:01

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_ISLAND_H
0020 #define B2_ISLAND_H
0021 
0022 #include <Box2D/Common/b2Math.h>
0023 #include <Box2D/Dynamics/b2Body.h>
0024 #include <Box2D/Dynamics/b2TimeStep.h>
0025 
0026 class b2Contact;
0027 class b2Joint;
0028 class b2StackAllocator;
0029 class b2ContactListener;
0030 struct b2ContactConstraint;
0031 
0032 /// This is an internal structure.
0033 struct b2Position
0034 {
0035     b2Vec2 x;
0036     qreal a;
0037 };
0038 
0039 /// This is an internal structure.
0040 struct b2Velocity
0041 {
0042     b2Vec2 v;
0043     qreal w;
0044 };
0045 
0046 /// This is an internal class.
0047 class b2Island
0048 {
0049 public:
0050     b2Island(int32 bodyCapacity, int32 contactCapacity, int32 jointCapacity,
0051             b2StackAllocator* allocator, b2ContactListener* listener);
0052     ~b2Island();
0053 
0054     void Clear()
0055     {
0056         m_bodyCount = 0;
0057         m_contactCount = 0;
0058         m_jointCount = 0;
0059     }
0060 
0061     void Solve(const b2TimeStep& step, const b2Vec2& gravity, bool allowSleep);
0062 
0063     void SolveTOI(const b2TimeStep& subStep, const b2Body* bodyA, const b2Body* bodyB);
0064 
0065     void Add(b2Body* body)
0066     {
0067         b2Assert(m_bodyCount < m_bodyCapacity);
0068         body->m_islandIndex = m_bodyCount;
0069         m_bodies[m_bodyCount++] = body;
0070     }
0071 
0072     void Add(b2Contact* contact)
0073     {
0074         b2Assert(m_contactCount < m_contactCapacity);
0075         m_contacts[m_contactCount++] = contact;
0076     }
0077 
0078     void Add(b2Joint* joint)
0079     {
0080         b2Assert(m_jointCount < m_jointCapacity);
0081         m_joints[m_jointCount++] = joint;
0082     }
0083 
0084     void Report(const b2ContactConstraint* constraints);
0085 
0086     b2StackAllocator* m_allocator;
0087     b2ContactListener* m_listener;
0088 
0089     b2Body** m_bodies;
0090     b2Contact** m_contacts;
0091     b2Joint** m_joints;
0092 
0093     b2Position* m_positions;
0094     b2Velocity* m_velocities;
0095 
0096     int32 m_bodyCount;
0097     int32 m_jointCount;
0098     int32 m_contactCount;
0099 
0100     int32 m_bodyCapacity;
0101     int32 m_contactCapacity;
0102     int32 m_jointCapacity;
0103 };
0104 
0105 #endif