-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathModifyGeometry.cs
More file actions
171 lines (141 loc) · 5.69 KB
/
ModifyGeometry.cs
File metadata and controls
171 lines (141 loc) · 5.69 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Numerics;
using ImGuiNET;
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.B2Diagnostics;
namespace Box2D.NET.Samples.Samples.Shapes;
// This sample shows how to modify the geometry on an existing shape. This is only supported on
// dynamic and kinematic shapes because static shapes don't look for new collisions.
public class ModifyGeometry : Sample
{
private static readonly int SampleModifyGeometry = SampleFactory.Shared.RegisterSample("Shapes", "Modify Geometry", Create);
private B2ShapeId m_shapeId;
private B2ShapeType m_shapeType;
private object m_shape;
private float m_scale;
private B2ShapeUnion m_us;
private static Sample Create(SampleContext context)
{
return new ModifyGeometry(context);
}
private void SetCircle(B2Circle circle)
{
}
public ModifyGeometry(SampleContext context) : base(context)
{
if (m_context.restart == false)
{
m_camera.zoom = 25.0f * 0.25f;
m_camera.center = new B2Vec2(0.0f, 5.0f);
}
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Polygon box = b2MakeOffsetBox(10.0f, 1.0f, new B2Vec2(0.0f, -1.0f), b2Rot_identity);
b2CreatePolygonShape(groundId, shapeDef, box);
}
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(0.0f, 4.0f);
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Polygon box = b2MakeBox(1.0f, 1.0f);
b2CreatePolygonShape(bodyId, shapeDef, box);
}
{
m_shapeType = B2ShapeType.b2_circleShape;
m_scale = 1.0f;
var circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f);
m_us.circle = circle; // todo : @ikpil, fix it!!
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_kinematicBody;
bodyDef.position = new B2Vec2(0.0f, 1.0f);
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
m_shapeId = b2CreateCircleShape(bodyId, shapeDef, circle); // todo : @ikpil, fix it!!
}
}
void UpdateShape()
{
switch (m_shapeType)
{
case B2ShapeType.b2_circleShape:
m_us.circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f * m_scale);
b2Shape_SetCircle(m_shapeId, m_us.circle);
break;
case B2ShapeType.b2_capsuleShape:
m_us.capsule = new B2Capsule(new B2Vec2(-0.5f * m_scale, 0.0f), new B2Vec2(0.0f, 0.5f * m_scale), 0.5f * m_scale);
b2Shape_SetCapsule(m_shapeId, m_us.capsule);
break;
case B2ShapeType.b2_segmentShape:
m_us.segment = new B2Segment(new B2Vec2(-0.5f * m_scale, 0.0f), new B2Vec2(0.75f * m_scale, 0.0f));
b2Shape_SetSegment(m_shapeId, m_us.segment);
break;
case B2ShapeType.b2_polygonShape:
m_us.polygon = b2MakeBox(0.5f * m_scale, 0.75f * m_scale);
b2Shape_SetPolygon(m_shapeId, ref m_us.polygon);
break;
default:
B2_ASSERT(false);
break;
}
B2BodyId bodyId = b2Shape_GetBody(m_shapeId);
b2Body_ApplyMassFromShapes(bodyId);
}
public override void UpdateGui()
{
base.UpdateGui();
float fontSize = ImGui.GetFontSize();
float height = 230.0f;
ImGui.SetNextWindowPos(new Vector2(0.5f * fontSize, m_camera.height - height - 2.0f * fontSize), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(200.0f, height));
ImGui.Begin("Modify Geometry", ImGuiWindowFlags.NoResize);
if (ImGui.RadioButton("Circle", m_shapeType == B2ShapeType.b2_circleShape))
{
m_shapeType = B2ShapeType.b2_circleShape;
UpdateShape();
}
if (ImGui.RadioButton("Capsule", m_shapeType == B2ShapeType.b2_capsuleShape))
{
m_shapeType = B2ShapeType.b2_capsuleShape;
UpdateShape();
}
if (ImGui.RadioButton("Segment", m_shapeType == B2ShapeType.b2_segmentShape))
{
m_shapeType = B2ShapeType.b2_segmentShape;
UpdateShape();
}
if (ImGui.RadioButton("Polygon", m_shapeType == B2ShapeType.b2_polygonShape))
{
m_shapeType = B2ShapeType.b2_polygonShape;
UpdateShape();
}
if (ImGui.SliderFloat("Scale", ref m_scale, 0.1f, 10.0f, "%.2f"))
{
UpdateShape();
}
B2BodyId bodyId = b2Shape_GetBody(m_shapeId);
B2BodyType bodyType = b2Body_GetType(bodyId);
if (ImGui.RadioButton("Static", bodyType == B2BodyType.b2_staticBody))
{
b2Body_SetType(bodyId, B2BodyType.b2_staticBody);
}
if (ImGui.RadioButton("Kinematic", bodyType == B2BodyType.b2_kinematicBody))
{
b2Body_SetType(bodyId, B2BodyType.b2_kinematicBody);
}
if (ImGui.RadioButton("Dynamic", bodyType == B2BodyType.b2_dynamicBody))
{
b2Body_SetType(bodyId, B2BodyType.b2_dynamicBody);
}
ImGui.End();
}
}