-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCharacter.cs
More file actions
201 lines (164 loc) · 7.67 KB
/
Character.cs
File metadata and controls
201 lines (164 loc) · 7.67 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
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;
namespace Box2D.NET.Samples.Samples.Bodies;
/// This is a test of typical character collision scenarios. This does not
/// show how you should implement a character in your application.
/// Instead this is used to test smooth collision on chain shapes.
public class Character : Sample
{
private static readonly int SampleCharacter = SampleFactory.Shared.RegisterSample("Bodies", "Character", Create);
private B2BodyId m_circleCharacterId;
private B2BodyId m_capsuleCharacterId;
private B2BodyId m_boxCharacterId;
private static Sample Create(SampleAppContext ctx, Settings settings)
{
return new Character(ctx, settings);
}
public Character(SampleAppContext ctx, Settings settings) : base(ctx, settings)
{
if (settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(-2.0f, 7.0f);
m_context.camera.m_zoom = 25.0f * 0.4f;
}
// Ground body
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Segment segment = new B2Segment(new B2Vec2(-20.0f, 0.0f), new B2Vec2(20.0f, 0.0f));
b2CreateSegmentShape(groundId, ref shapeDef, ref segment);
}
// Collinear edges with no adjacency information.
// This shows the problematic case where a box shape can hit
// an internal vertex.
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Segment segment1 = new B2Segment(new B2Vec2(-8.0f, 1.0f), new B2Vec2(-6.0f, 1.0f));
b2CreateSegmentShape(groundId, ref shapeDef, ref segment1);
B2Segment segment2 = new B2Segment(new B2Vec2(-6.0f, 1.0f), new B2Vec2(-4.0f, 1.0f));
b2CreateSegmentShape(groundId, ref shapeDef, ref segment2);
B2Segment segment3 = new B2Segment(new B2Vec2(-4.0f, 1.0f), new B2Vec2(-2.0f, 1.0f));
b2CreateSegmentShape(groundId, ref shapeDef, ref segment3);
}
// Chain shape
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.rotation = b2MakeRot(0.25f * B2_PI);
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2Vec2[] points = new B2Vec2[] { new(8.0f, 7.0f), new(7.0f, 8.0f), new B2Vec2(6.0f, 8.0f), new B2Vec2(5.0f, 7.0f) };
B2ChainDef chainDef = b2DefaultChainDef();
chainDef.points = points;
chainDef.count = 4;
chainDef.isLoop = true;
b2CreateChain(groundId, ref chainDef);
}
// Square tiles. This shows that adjacency shapes may have non-smooth collision. Box2D has no solution
// to this problem.
// todo_erin try this: https://briansemrau.github.io/dealing-with-ghost-collisions/
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Polygon box = b2MakeOffsetBox(1.0f, 1.0f, new B2Vec2(4.0f, 3.0f), b2Rot_identity);
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
box = b2MakeOffsetBox(1.0f, 1.0f, new B2Vec2(6.0f, 3.0f), b2Rot_identity);
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
box = b2MakeOffsetBox(1.0f, 1.0f, new B2Vec2(8.0f, 3.0f), b2Rot_identity);
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
}
// Square made from a chain loop. Collision should be smooth.
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2Vec2[] points = new B2Vec2[] { new(-1.0f, 3.0f), new(1.0f, 3.0f), new(1.0f, 5.0f), new(-1.0f, 5.0f) };
B2ChainDef chainDef = b2DefaultChainDef();
chainDef.points = points;
chainDef.count = 4;
chainDef.isLoop = true;
b2CreateChain(groundId, ref chainDef);
}
// Chain loop. Collision should be smooth.
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(-10.0f, 4.0f);
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);
B2Vec2[] points = new B2Vec2[10]
{
new B2Vec2(0.0f, 0.0f),
new B2Vec2(6.0f, 0.0f),
new B2Vec2(6.0f, 2.0f),
new B2Vec2(4.0f, 1.0f),
new B2Vec2(2.0f, 2.0f),
new B2Vec2(0.0f, 2.0f),
new B2Vec2(-2.0f, 2.0f),
new B2Vec2(-4.0f, 3.0f),
new B2Vec2(-6.0f, 2.0f),
new B2Vec2(-6.0f, 0.0f),
};
B2ChainDef chainDef = b2DefaultChainDef();
chainDef.points = points;
chainDef.count = 10;
chainDef.isLoop = true;
b2CreateChain(groundId, ref chainDef);
}
// Circle character
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(-7.0f, 6.0f);
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.fixedRotation = true;
bodyDef.enableSleep = false;
m_circleCharacterId = b2CreateBody(m_worldId, ref bodyDef);
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.25f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 20.0f;
shapeDef.material.friction = 0.2f;
b2CreateCircleShape(m_circleCharacterId, ref shapeDef, ref circle);
}
// Capsule character
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(3.0f, 5.0f);
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.fixedRotation = true;
bodyDef.enableSleep = false;
m_capsuleCharacterId = b2CreateBody(m_worldId, ref bodyDef);
B2Capsule capsule = new B2Capsule(new B2Vec2(0.0f, 0.25f), new B2Vec2(0.0f, 0.75f), 0.25f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 20.0f;
shapeDef.material.friction = 0.2f;
b2CreateCapsuleShape(m_capsuleCharacterId, ref shapeDef, ref capsule);
}
// Square character
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(-3.0f, 8.0f);
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.fixedRotation = true;
bodyDef.enableSleep = false;
m_boxCharacterId = b2CreateBody(m_worldId, ref bodyDef);
B2Polygon box = b2MakeBox(0.4f, 0.4f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 20.0f;
shapeDef.material.friction = 0.2f;
b2CreatePolygonShape(m_boxCharacterId, ref shapeDef, ref box);
}
}
public override void Draw(Settings settings)
{
base.Draw(settings);
m_context.draw.DrawString(5, m_textLine, "This tests various character collision shapes.");
m_textLine += m_textIncrement;
m_context.draw.DrawString(5, m_textLine, "Limitation: square and hexagon can snag on aligned boxes.");
m_textLine += m_textIncrement;
}
}