Skip to content

Commit

Permalink
Rolling resistance (#876)
Browse files Browse the repository at this point in the history
Added rolling resistance. Rolling resistance is a static resistance with
a constraint, so it can work on ramps. The resistance scales based on
the contact normal forces and the radius of the shape.

Added tangent speed to all shapes for conveyor belts, etc.

Added b2SurfaceMaterial to support separate properties for each segment
of a chain shape.
  • Loading branch information
erincatto authored Jan 27, 2025
1 parent cad8599 commit 73324a7
Show file tree
Hide file tree
Showing 43 changed files with 1,029 additions and 445 deletions.
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/joint_grid.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,2565.3
2,1533.85
3,1053.01
4,865.702
5,748.989
6,658.623
7,613.633
8,578.973
1,2528.51
2,1460.58
3,1004.38
4,822.484
5,712.691
6,638.201
7,583.016
8,540.158
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/large_pyramid.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,1584.03
2,841.862
3,598.747
4,471.697
5,396.306
6,347.983
7,309.086
8,308.832
1,1579.54
2,849.264
3,591.984
4,466.064
5,393.207
6,347.593
7,305.863
8,305.205
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/many_pyramids.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,2663.34
2,1407.85
3,934.506
4,725.271
5,590.288
6,502.513
7,422.693
8,395.369
1,2592.03
2,1412.01
3,953.71
4,731.394
5,601.527
6,511.144
7,441.201
8,393.566
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/rain.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,6526.9
2,4017.74
3,3052.28
4,2493.5
5,2149.12
6,1911.45
7,1735.97
8,1625.71
1,6462.19
2,3806.63
3,2907
4,2413.23
5,2069.53
6,1852.14
7,1671.47
8,1547.16
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/smash.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,1562.06
2,1020.55
3,781.193
4,661.318
5,580.115
6,530.502
7,489.672
8,467.998
1,1533.42
2,981.749
3,763.27
4,642.928
5,574.053
6,527.394
7,492.883
8,468.971
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/spinner.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,4106.89
2,2635.21
3,1986.32
4,1614.21
5,1420.63
6,1272.06
7,1157.16
8,1091.25
1,4126.1
2,2491.09
3,1918.78
4,1543.11
5,1360.84
6,1216.53
7,1110.95
8,1043.96
16 changes: 8 additions & 8 deletions benchmark/amd7950x_avx2/tumbler.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
threads,ms
1,1613.69
2,1061.88
3,810.738
4,670.874
5,578.959
6,519.473
7,479.842
8,437.851
1,1624.23
2,1028.59
3,796.538
4,651.347
5,563.763
6,510.42
7,468.899
8,445.968
10 changes: 7 additions & 3 deletions include/box2d/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,18 @@ typedef struct b2ManifoldPoint
/// @note Box2D uses speculative collision so some contact points may be separated.
typedef struct b2Manifold
{
/// The manifold points, up to two are possible in 2D
b2ManifoldPoint points[2];

/// The unit normal vector in world space, points from shape A to bodyB
b2Vec2 normal;

/// Angular impulse applied for rolling resistance. N * m * s = kg * m^2 / s
float rollingImpulse;

/// The manifold points, up to two are possible in 2D
b2ManifoldPoint points[2];

/// The number of contacts points, will be 0, 1, or 2
int pointCount;

} b2Manifold;

/// Compute the contact manifold between two circles
Expand Down
52 changes: 37 additions & 15 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,11 @@ typedef struct b2ShapeDef
float restitution;

/// The rolling resistance usually in the range [0,1].
/// todo
float rollingResistance;

/// The tangent speed for conveyor belts
float tangentSpeed;

/// User material identifier. This is passed with query results and to friction and restitution
/// combining functions. It is not used internally.
int material;
Expand Down Expand Up @@ -394,6 +396,35 @@ typedef struct b2ShapeDef
/// @ingroup shape
B2_API b2ShapeDef b2DefaultShapeDef( void );

/// Surface materials allow chain shapes to have per segment surface properties.
/// @ingroup shape
typedef struct b2SurfaceMaterial
{
/// The Coulomb (dry) friction coefficient, usually in the range [0,1].
float friction;

/// The coefficient of restitution (bounce) usually in the range [0,1].
/// https://en.wikipedia.org/wiki/Coefficient_of_restitution
float restitution;

/// The rolling resistance usually in the range [0,1].
float rollingResistance;

/// The tangent speed for conveyor belts
float tangentSpeed;

/// User material identifier. This is passed with query results and to friction and restitution
/// combining functions. It is not used internally.
int material;

/// Custom debug draw color.
uint32_t customColor;
} b2SurfaceMaterial;

/// Use this to initialize your surface material
/// @ingroup shape
B2_API b2SurfaceMaterial b2DefaultSurfaceMaterial( void );

/// Used to create a chain of line segments. This is designed to eliminate ghost collisions with some limitations.
/// - chains are one-sided
/// - chains have no mass and should be used on static bodies
Expand All @@ -420,28 +451,19 @@ typedef struct b2ChainDef
/// The point count, must be 4 or more.
int count;

/// The friction coefficient, usually in the range [0,1].
float friction;

/// The restitution (elasticity) usually in the range [0,1].
float restitution;
/// Surface materials for each segment. These are cloned.
const b2SurfaceMaterial* materials;

/// User material identifier. This is passed with query results and to friction and restitution
/// combining functions. It is not used internally.
int material;
/// The material count. Must be 1 or count. This allows you to provide one
/// material for all segments or a unique material per segment.
int materialCount;

/// Contact filtering data.
b2Filter filter;

/// Custom debug draw color.
uint32_t customColor;

/// Indicates a closed chain formed by connecting the first and last points
bool isLoop;

/// Generate events when a sensor overlaps this chain
bool enableSensorEvents;

/// Used internally to detect a valid definition. DO NOT SET.
int internalValue;
} b2ChainDef;
Expand Down
1 change: 1 addition & 0 deletions samples/car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void Car::Spawn( b2WorldId worldId, b2Vec2 position, float scale, float hertz, f

shapeDef.density = 2.0f / scale;
shapeDef.friction = 1.5f;
shapeDef.rollingResistance = 0.1f;

bodyDef.position = b2Add( { -1.0f * scale, 0.35f * scale }, position );
bodyDef.allowFastRotation = true;
Expand Down
66 changes: 66 additions & 0 deletions samples/data/ramp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 2 additions & 11 deletions samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,6 @@ int main( int, char** )

float frameTime = 0.0;

// int32_t frame = 0;

while ( !glfwWindowShouldClose( g_mainWindow ) )
{
double time1 = glfwGetTime();
Expand Down Expand Up @@ -754,21 +752,14 @@ int main( int, char** )

// Limit frame rate to 60Hz
double time2 = glfwGetTime();
double targetTime = time1 + 1.0f / 60.0f;
// int loopCount = 0;
double targetTime = time1 + 1.0 / 60.0;
while ( time2 < targetTime )
{
b2Yield();
time2 = glfwGetTime();
//++loopCount;
}

frameTime = (float)( time2 - time1 );
// if (frame % 17 == 0)
//{
// printf("loop count = %d, frame time = %.1f\n", loopCount, 1000.0f * frameTime);
// }
//++frame;
frameTime = float( time2 - time1 );
}

delete s_sample;
Expand Down
Loading

0 comments on commit 73324a7

Please sign in to comment.