Repository navigation
Expand file tree
/
Copy pathDeterminism.cs
More file actions
157 lines (129 loc) · 5.41 KB
/
Copy pathDeterminism.cs
File metadata and controls
157 lines (129 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-FileCopyrightText: 2022 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System;
using static Box2D.NET.B2Ids;
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Joints;
using static Box2D.NET.B2Constants;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.B2Timers;
using static Box2D.NET.B2Diagnostics;
namespace Box2D.NET.Shared
{
public static class Determinism
{
public static FallingHingeData CreateFallingHinges(B2WorldId worldId)
{
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(0.0f, -1.0f);
B2BodyId groundId = b2CreateBody(worldId, bodyDef);
B2Polygon box = b2MakeBox(40.0f, 1.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(groundId, shapeDef, box);
}
int columnCount = 4;
int rowCount = 20;
int bodyCount = rowCount * columnCount;
B2BodyId[] bodyIds = new B2BodyId[bodyCount];
{
float h = 0.25f;
float r = 0.1f * h;
B2Polygon box = b2MakeRoundedBox(h - r, h - r, r);
box = b2MakeSquare(h);
B2ShapeDef shapeDef = b2DefaultShapeDef();
//shapeDef.material.friction = 0.3f;
B2RevoluteJointDef jointDef = b2DefaultRevoluteJointDef();
jointDef.enableLimit = true;
jointDef.lowerAngle = -0.1f * B2_PI;
jointDef.upperAngle = 0.2f * B2_PI;
jointDef.enableSpring = true;
jointDef.hertz = 1.0f;
jointDef.dampingRatio = 1.0f;
jointDef.enableMotor = true;
jointDef.maxMotorTorque = 0.25f;
jointDef.@base.localFrameA.p = new B2Vec2(-h, h);
jointDef.@base.localFrameB.p = new B2Vec2(-h, -h);
jointDef.@base.constraintHertz = 60.0f;
jointDef.@base.constraintDampingRatio = 0.0f;
jointDef.@base.drawScale = 0.5f;
int bodyIndex = 0;
float offset = 0.4f * h;
float dx = 10.0f * h;
float xBase = -0.5f * dx * (columnCount - 1.0f);
for (int j = 0; j < columnCount; ++j)
{
float x = xBase + j * dx;
B2BodyId prevBodyId = b2_nullBodyId;
for (int i = 0; i < rowCount; ++i)
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position.X = x + offset * i;
bodyDef.position.Y = h + 2.0f * h * i;
// this tests the deterministic cosine and sine functions
float angle = (i & 1) == 0 ? -0.1f : 0.1f;
bodyDef.rotation = b2MakeRot(angle);
B2BodyId bodyId = b2CreateBody(worldId, bodyDef);
if ((i & 1) == 0)
{
prevBodyId = bodyId;
}
else
{
jointDef.@base.bodyIdA = prevBodyId;
jointDef.@base.bodyIdB = bodyId;
b2CreateRevoluteJoint(worldId, jointDef);
prevBodyId = b2_nullBodyId;
}
b2CreatePolygonShape(bodyId, shapeDef, box);
B2_ASSERT(bodyIndex < bodyCount);
bodyIds[bodyIndex] = bodyId;
bodyIndex += 1;
}
}
B2_ASSERT(bodyIndex == bodyCount);
}
FallingHingeData data = new FallingHingeData();
data.bodyIds = bodyIds;
data.bodyCount = bodyCount;
data.stepCount = 0;
data.sleepStep = -1;
data.hash = 0;
return data;
}
public static bool UpdateFallingHinges(B2WorldId worldId, ref FallingHingeData data)
{
if (data.hash == 0)
{
B2BodyEvents bodyEvents = b2World_GetBodyEvents(worldId);
if (bodyEvents.moveCount == 0)
{
int awakeCount = b2World_GetAwakeBodyCount(worldId);
B2_ASSERT(awakeCount == 0);
data.hash = B2_HASH_INIT;
Span<byte> bxf = stackalloc byte[B2SizeOf<float>.Size * 4];
for (int i = 0; i < data.bodyCount; ++i)
{
B2Transform xf = b2Body_GetTransform(data.bodyIds[i]);
xf.TryWriteBytes(bxf);
data.hash = b2Hash(data.hash, bxf, bxf.Length);
}
data.sleepStep = data.stepCount;
}
}
data.stepCount += 1;
return data.hash != 0;
}
public static void DestroyFallingHinges(ref FallingHingeData data)
{
//free( data.bodyIds );
data.bodyIds = null;
}
}
}