Skip to content

Commit f041ac5

Browse files
committed
Merge offical pull request erincatto#804 (New capsule collider).
2 parents adbfee6 + df7373c commit f041ac5

File tree

17 files changed

+460
-33
lines changed

17 files changed

+460
-33
lines changed

include/box2d/box2d.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ B2_API b2ShapeType b2Shape_GetType( b2ShapeId shapeId );
476476
/// Get the id of the body that a shape is attached to
477477
B2_API b2BodyId b2Shape_GetBody( b2ShapeId shapeId );
478478

479+
/// Get the world that owns this shape
480+
B2_API b2WorldId b2Shape_GetWorld( b2ShapeId shapeId );
481+
479482
/// Returns true If the shape is a sensor
480483
B2_API bool b2Shape_IsSensor( b2ShapeId shapeId );
481484

@@ -616,6 +619,16 @@ B2_API b2ChainId b2CreateChain( b2BodyId bodyId, const b2ChainDef* def );
616619
/// Destroy a chain shape
617620
B2_API void b2DestroyChain( b2ChainId chainId );
618621

622+
/// Get the world that owns this chain shape
623+
B2_API b2WorldId b2Chain_GetWorld( b2ChainId chainId );
624+
625+
/// Get the number of segments on this chain
626+
B2_API int b2Chain_GetSegmentCount( b2ChainId chainId );
627+
628+
/// Fill a user array with chain segment shape ids up to the specified capacity. Returns
629+
/// the actual number of segments returned.
630+
B2_API int b2Chain_GetSegments( b2ChainId chainId, b2ShapeId* segmentArray, int capacity );
631+
619632
/// Set the chain friction
620633
/// @see b2ChainDef::friction
621634
B2_API void b2Chain_SetFriction( b2ChainId chainId, float friction );
@@ -650,6 +663,9 @@ B2_API b2BodyId b2Joint_GetBodyA( b2JointId jointId );
650663
/// Get body B id on a joint
651664
B2_API b2BodyId b2Joint_GetBodyB( b2JointId jointId );
652665

666+
/// Get the world that owns this joint
667+
B2_API b2WorldId b2Joint_GetWorld( b2JointId jointId );
668+
653669
/// Get the local anchor on bodyA
654670
B2_API b2Vec2 b2Joint_GetLocalAnchorA( b2JointId jointId );
655671

include/box2d/collision.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,25 @@ B2_API b2Polygon b2MakePolygon( const b2Hull* hull, float radius );
183183
B2_API b2Polygon b2MakeOffsetPolygon( const b2Hull* hull, float radius, b2Transform transform );
184184

185185
/// Make a square polygon, bypassing the need for a convex hull.
186+
/// @param h the half-width
186187
B2_API b2Polygon b2MakeSquare( float h );
187188

188189
/// Make a box (rectangle) polygon, bypassing the need for a convex hull.
190+
/// @param hx the half-width
191+
/// @param hy the half-height
189192
B2_API b2Polygon b2MakeBox( float hx, float hy );
190193

191194
/// Make a rounded box, bypassing the need for a convex hull.
195+
/// @param hx the half-width
196+
/// @param hy the half-height
197+
/// @param radius the radius of the rounded extension
192198
B2_API b2Polygon b2MakeRoundedBox( float hx, float hy, float radius );
193199

194200
/// Make an offset box, bypassing the need for a convex hull.
201+
/// @param hx the half-width
202+
/// @param hy the half-height
203+
/// @param center the local position of the center of the box
204+
/// @param rotation the local rotation of the box
195205
B2_API b2Polygon b2MakeOffsetBox( float hx, float hy, b2Vec2 center, b2Rot rotation );
196206

197207
/// Transform a polygon. This is useful for transferring a shape from one body to another.

samples/sample_bodies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ class BadBody : public Sample
785785
b2BodyDef bodyDef = b2DefaultBodyDef();
786786
bodyDef.type = b2_dynamicBody;
787787
bodyDef.position = { 0.0f, 3.0f };
788-
bodyDef.angularVelocity = 0.2f;
788+
bodyDef.angularVelocity = 0.5f;
789789
bodyDef.rotation = b2MakeRot( 0.25f * b2_pi );
790790

791791
m_badBodyId = b2CreateBody( m_worldId, &bodyDef );

samples/sample_collision.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,9 @@ class Manifold : public Sample
22322232
m_smgcapCache2 = b2_emptyDistanceCache;
22332233

22342234
m_transform = b2Transform_identity;
2235+
m_transform.p.x = 1.0f;
2236+
m_transform.p.y = 0.0f;
2237+
//m_transform.q = b2MakeRot( 0.5f * b2_pi );
22352238
m_angle = 0.0f;
22362239
m_round = 0.1f;
22372240

@@ -2371,7 +2374,7 @@ class Manifold : public Sample
23712374
b2Vec2 increment = { 4.0f, 0.0f };
23722375

23732376
b2HexColor color1 = b2_colorAquamarine;
2374-
b2HexColor color2 = b2_colorMagenta;
2377+
b2HexColor color2 = b2_colorPaleGoldenrod;
23752378

23762379
if ( m_enableCaching == false )
23772380
{
@@ -2462,20 +2465,21 @@ class Manifold : public Sample
24622465

24632466
// capsule-capsule
24642467
{
2465-
b2Capsule capsule = { { -0.5f, 0.0f }, { 0.5f, 0.0 }, 0.25f };
2468+
b2Capsule capsule1 = { { -0.5f, 0.0f }, { 0.5f, 0.0 }, 0.25f };
2469+
b2Capsule capsule2 = { { 0.25f, 0.0f }, { 1.0f, 0.0 }, 0.1f };
24662470

24672471
b2Transform transform1 = { offset, b2Rot_identity };
24682472
b2Transform transform2 = { b2Add( m_transform.p, offset ), m_transform.q };
24692473

2470-
b2Manifold m = b2CollideCapsules( &capsule, transform1, &capsule, transform2 );
2474+
b2Manifold m = b2CollideCapsules( &capsule1, transform1, &capsule2, transform2 );
24712475

2472-
b2Vec2 v1 = b2TransformPoint( transform1, capsule.center1 );
2473-
b2Vec2 v2 = b2TransformPoint( transform1, capsule.center2 );
2474-
g_draw.DrawSolidCapsule( v1, v2, capsule.radius, color1 );
2476+
b2Vec2 v1 = b2TransformPoint( transform1, capsule1.center1 );
2477+
b2Vec2 v2 = b2TransformPoint( transform1, capsule1.center2 );
2478+
g_draw.DrawSolidCapsule( v1, v2, capsule1.radius, color1 );
24752479

2476-
v1 = b2TransformPoint( transform2, capsule.center1 );
2477-
v2 = b2TransformPoint( transform2, capsule.center2 );
2478-
g_draw.DrawSolidCapsule( v1, v2, capsule.radius, color2 );
2480+
v1 = b2TransformPoint( transform2, capsule2.center1 );
2481+
v2 = b2TransformPoint( transform2, capsule2.center2 );
2482+
g_draw.DrawSolidCapsule( v1, v2, capsule2.radius, color2 );
24792483

24802484
DrawManifold( &m, transform1.p, transform2.p );
24812485

samples/sample_continuous.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,62 @@ class SkinnyBox : public Sample
389389

390390
static int sampleSkinnyBox = RegisterSample( "Continuous", "Skinny Box", SkinnyBox::Create );
391391

392+
class SpeculativeBug : public Sample
393+
{
394+
public:
395+
explicit SpeculativeBug( Settings& settings )
396+
: Sample( settings )
397+
{
398+
if ( settings.restart == false )
399+
{
400+
g_camera.m_center = { 1.0f, 5.0f };
401+
g_camera.m_zoom = 25.0f * 0.25f;
402+
}
403+
404+
{
405+
b2BodyDef bodyDef = b2DefaultBodyDef();
406+
b2BodyId groundId = b2CreateBody( m_worldId, &bodyDef );
407+
408+
b2Segment segment = { { -10.0f, 0.0f }, { 10.0f, 0.0f } };
409+
b2ShapeDef shapeDef = b2DefaultShapeDef();
410+
b2CreateSegmentShape( groundId, &shapeDef, &segment );
411+
412+
shapeDef.friction = 0.0f;
413+
b2Polygon box = b2MakeOffsetBox( 0.05f, 1.0f, { 0.0f, 1.0f }, b2Rot_identity );
414+
b2CreatePolygonShape( groundId, &shapeDef, &box );
415+
}
416+
417+
b2BodyDef bodyDef = b2DefaultBodyDef();
418+
bodyDef.type = b2_dynamicBody;
419+
for (int i = 0; i < 2; ++i)
420+
{
421+
if (i == 0)
422+
{
423+
bodyDef.position = { -0.8f, 0.25f };
424+
bodyDef.isAwake = false;
425+
}
426+
else
427+
{
428+
bodyDef.position = { 0.8f, 2.0f };
429+
bodyDef.isAwake = true;
430+
}
431+
432+
b2BodyId bodyId = b2CreateBody( m_worldId, &bodyDef );
433+
434+
b2ShapeDef shapeDef = b2DefaultShapeDef();
435+
b2Capsule capsule = { { -0.5f, 0.0f }, { 0.5f, 0.0f }, 0.25f };
436+
b2CreateCapsuleShape( bodyId, &shapeDef, &capsule );
437+
}
438+
}
439+
440+
static Sample* Create( Settings& settings )
441+
{
442+
return new SpeculativeBug( settings );
443+
}
444+
};
445+
446+
static int sampleSpeculativeBug = RegisterSample( "Continuous", "Speculative Bug", SpeculativeBug::Create );
447+
392448
// This sample shows ghost collisions
393449
class GhostCollision : public Sample
394450
{

src/array.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "core.h"
77

8-
// Macro based dynamic arrays
8+
// Macro generated functions for dynamic arrays
99
// Pros
1010
// - type safe
1111
// - array data debuggable (visible count and capacity)
@@ -46,10 +46,10 @@
4646
// Inline array functions that need the type T to be defined
4747
#define B2_ARRAY_INLINE( T, PREFIX ) \
4848
/* Resize */ \
49-
static inline void PREFIX##Array_Resize( PREFIX##Array* a, int count ) \
49+
static inline void PREFIX##Array_Resize( PREFIX##Array* a, int count ) \
5050
{ \
5151
PREFIX##Array_Reserve( a, count ); \
52-
a->count = count; \
52+
a->count = count; \
5353
} \
5454
/* Get */ \
5555
static inline T* PREFIX##Array_Get( PREFIX##Array* a, int index ) \
@@ -122,10 +122,12 @@
122122
/* Create */ \
123123
PREFIX##Array PREFIX##Array_Create( int capacity ) \
124124
{ \
125-
PREFIX##Array a; \
126-
a.data = b2Alloc( capacity * sizeof( T ) ); \
127-
a.count = 0; \
128-
a.capacity = capacity; \
125+
PREFIX##Array a = { 0 }; \
126+
if ( capacity > 0 ) \
127+
{ \
128+
a.data = b2Alloc( capacity * sizeof( T ) ); \
129+
a.capacity = capacity; \
130+
} \
129131
return a; \
130132
} \
131133
/* Reserve */ \

src/body.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,11 @@ void b2Body_SetLinearVelocity( b2BodyId bodyId, b2Vec2 linearVelocity )
762762
b2World* world = b2GetWorld( bodyId.world0 );
763763
b2Body* body = b2GetBodyFullId( world, bodyId );
764764

765+
if ( body->type == b2_staticBody )
766+
{
767+
return;
768+
}
769+
765770
if ( b2LengthSquared( linearVelocity ) > 0.0f )
766771
{
767772
b2WakeBody( world, body );
@@ -781,6 +786,11 @@ void b2Body_SetAngularVelocity( b2BodyId bodyId, float angularVelocity )
781786
b2World* world = b2GetWorld( bodyId.world0 );
782787
b2Body* body = b2GetBodyFullId( world, bodyId );
783788

789+
if (body->type == b2_staticBody || body->fixedRotation)
790+
{
791+
return;
792+
}
793+
784794
if ( angularVelocity != 0.0f )
785795
{
786796
b2WakeBody( world, body );

src/broad_phase.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// SPDX-FileCopyrightText: 2023 Erin Catto
22
// SPDX-License-Identifier: MIT
33

4+
#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
45
#define _CRT_SECURE_NO_WARNINGS
6+
#endif
57

68
#include "broad_phase.h"
79

src/core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ void b2SetAllocator( b2AllocFcn* allocFcn, b2FreeFcn* freeFcn )
8282

8383
void* b2Alloc( int size )
8484
{
85+
if (size == 0)
86+
{
87+
return NULL;
88+
}
89+
8590
// This could cause some sharing issues, however Box2D rarely calls b2Alloc.
8691
atomic_fetch_add_explicit( &b2_byteCount, size, memory_order_relaxed );
8792

src/distance.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ b2SegmentDistanceResult b2SegmentDistance( b2Vec2 p1, b2Vec2 q1, b2Vec2 p2, b2Ve
3838
b2Vec2 r = b2Sub( p1, p2 );
3939
float dd1 = b2Dot( d1, d1 );
4040
float dd2 = b2Dot( d2, d2 );
41-
float rd2 = b2Dot( r, d2 );
4241
float rd1 = b2Dot( r, d1 );
42+
float rd2 = b2Dot( r, d2 );
4343

4444
const float epsSqr = FLT_EPSILON * FLT_EPSILON;
4545

0 commit comments

Comments
 (0)