Skip to content

Commit

Permalink
issues #800 #798 #794
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Sep 26, 2024
1 parent d077420 commit 1e6de4e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 6 deletions.
16 changes: 16 additions & 0 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ B2_API b2ShapeType b2Shape_GetType( b2ShapeId shapeId );
/// Get the id of the body that a shape is attached to
B2_API b2BodyId b2Shape_GetBody( b2ShapeId shapeId );

/// Get the world that owns this shape
B2_API b2WorldId b2Shape_GetWorld( b2ShapeId shapeId );

/// Returns true If the shape is a sensor
B2_API bool b2Shape_IsSensor( b2ShapeId shapeId );

Expand Down Expand Up @@ -600,6 +603,16 @@ B2_API b2ChainId b2CreateChain( b2BodyId bodyId, const b2ChainDef* def );
/// Destroy a chain shape
B2_API void b2DestroyChain( b2ChainId chainId );

/// Get the world that owns this chain shape
B2_API b2WorldId b2Chain_GetWorld( b2ChainId chainId );

/// Get the number of segments on this chain
B2_API int b2Chain_GetSegmentCount( b2ChainId chainId );

/// Fill a user array with chain segment shape ids up to the specified capacity. Returns
/// the actual number of segments returned.
B2_API int b2Chain_GetSegments( b2ChainId chainId, b2ShapeId* segmentArray, int capacity );

/// Set the chain friction
/// @see b2ChainDef::friction
B2_API void b2Chain_SetFriction( b2ChainId chainId, float friction );
Expand Down Expand Up @@ -634,6 +647,9 @@ B2_API b2BodyId b2Joint_GetBodyA( b2JointId jointId );
/// Get body B id on a joint
B2_API b2BodyId b2Joint_GetBodyB( b2JointId jointId );

/// Get the world that owns this joint
B2_API b2WorldId b2Joint_GetWorld( b2JointId jointId );

/// Get the local anchor on bodyA
B2_API b2Vec2 b2Joint_GetLocalAnchorA( b2JointId jointId );

Expand Down
10 changes: 10 additions & 0 deletions include/box2d/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,25 @@ B2_API b2Polygon b2MakePolygon( const b2Hull* hull, float radius );
B2_API b2Polygon b2MakeOffsetPolygon( const b2Hull* hull, float radius, b2Transform transform );

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

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

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

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

/// Transform a polygon. This is useful for transferring a shape from one body to another.
Expand Down
2 changes: 1 addition & 1 deletion samples/sample_bodies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ class BadBody : public Sample
b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = b2_dynamicBody;
bodyDef.position = { 0.0f, 3.0f };
bodyDef.angularVelocity = 0.2f;
bodyDef.angularVelocity = 0.5f;
bodyDef.rotation = b2MakeRot( 0.25f * b2_pi );

m_badBodyId = b2CreateBody( m_worldId, &bodyDef );
Expand Down
10 changes: 10 additions & 0 deletions src/body.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ void b2Body_SetLinearVelocity( b2BodyId bodyId, b2Vec2 linearVelocity )
b2World* world = b2GetWorld( bodyId.world0 );
b2Body* body = b2GetBodyFullId( world, bodyId );

if ( body->type == b2_staticBody )
{
return;
}

if ( b2LengthSquared( linearVelocity ) > 0.0f )
{
b2WakeBody( world, body );
Expand All @@ -763,6 +768,11 @@ void b2Body_SetAngularVelocity( b2BodyId bodyId, float angularVelocity )
b2World* world = b2GetWorld( bodyId.world0 );
b2Body* body = b2GetBodyFullId( world, bodyId );

if (body->type == b2_staticBody || body->fixedRotation)
{
return;
}

if ( angularVelocity != 0.0f )
{
b2WakeBody( world, body );
Expand Down
2 changes: 2 additions & 0 deletions src/broad_phase.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "broad_phase.h"

Expand Down
2 changes: 2 additions & 0 deletions src/distance_joint.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "body.h"
#include "core.h"
Expand Down
6 changes: 6 additions & 0 deletions src/joint.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ b2BodyId b2Joint_GetBodyB( b2JointId jointId )
return b2MakeBodyId( world, joint->edges[1].bodyId );
}

b2WorldId b2Joint_GetWorld( b2JointId jointId )
{
b2World* world = b2GetWorld( jointId.world0 );
return ( b2WorldId ){ jointId.world0 + 1, world->revision };
}

b2Vec2 b2Joint_GetLocalAnchorA( b2JointId jointId )
{
b2World* world = b2GetWorld( jointId.world0 );
Expand Down
2 changes: 2 additions & 0 deletions src/revolute_joint.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "body.h"
#include "core.h"
Expand Down
35 changes: 35 additions & 0 deletions src/shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,35 @@ void b2DestroyChain( b2ChainId chainId )
b2ValidateSolverSets( world );
}

b2WorldId b2Chain_GetWorld(b2ChainId chainId)
{
b2World* world = b2GetWorld( chainId.world0 );
return ( b2WorldId ){ chainId.world0 + 1, world->revision };
}

int b2Chain_GetSegmentCount(b2ChainId chainId)
{
b2World* world = b2GetWorldLocked( chainId.world0 );
b2ChainShape* chain = b2GetChainShape( world, chainId );
return chain->count;
}

int b2Chain_GetSegments(b2ChainId chainId, b2ShapeId* segmentArray, int capacity)
{
b2World* world = b2GetWorldLocked( chainId.world0 );
b2ChainShape* chain = b2GetChainShape( world, chainId );

int count = b2MinInt(chain->count, capacity);
for ( int i = 0; i < count; ++i )
{
int shapeId = chain->shapeIndices[i];
b2Shape* shape = b2ShapeArray_Get( &world->shapes, shapeId );
segmentArray[i] = ( b2ShapeId ){ shapeId + 1, chainId.world0, shape->revision };
}

return count;
}

b2AABB b2ComputeShapeAABB( const b2Shape* shape, b2Transform xf )
{
switch ( shape->type )
Expand Down Expand Up @@ -726,6 +755,12 @@ b2BodyId b2Shape_GetBody( b2ShapeId shapeId )
return b2MakeBodyId( world, shape->bodyId );
}

b2WorldId b2Shape_GetWorld(b2ShapeId shapeId)
{
b2World* world = b2GetWorld( shapeId.world0 );
return ( b2WorldId ){ shapeId.world0 + 1, world->revision };
}

void b2Shape_SetUserData( b2ShapeId shapeId, void* userData )
{
b2World* world = b2GetWorld( shapeId.world0 );
Expand Down
19 changes: 14 additions & 5 deletions src/solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@
#include <stdbool.h>
#include <stddef.h>

#if defined( B2_CPU_ARM )
#if ( defined( __GNUC__ ) || defined( __clang__ ) ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
static inline void b2Pause( void )
{
__asm__ __volatile__( "isb\n" );
__asm__ __volatile__( "pause\n" );
}
#elif defined( B2_CPU_X86_X64 )
#include <immintrin.h>
#elif ( defined( __arm__ ) && defined( __ARM_ARCH ) && __ARM_ARCH >= 7 ) || defined( __aarch64__ )
static inline void b2Pause( void )
{
__asm__ __volatile__( "yield" ::: "memory" );
}
#elif defined( _MSC_VER ) && ( defined( _M_IX86 ) || defined( _M_X64 ) )
//#include <immintrin.h>
static inline void b2Pause( void )
{
_mm_pause();
}
#elif defined( _MSC_VER ) && ( defined( _M_ARM ) || defined( _M_ARM64 ) )
static inline void b2Pause( void )
{
__yield();
}
#else
static inline void b2Pause( void )
{
// no threading will likely be used in web assembly
}
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/world.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "world.h"

Expand Down

0 comments on commit 1e6de4e

Please sign in to comment.