Repository navigation
Expand file tree
/
Copy pathCircleStack.cs
More file actions
117 lines (91 loc) · 3.5 KB
/
Copy pathCircleStack.cs
File metadata and controls
117 lines (91 loc) · 3.5 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.Samples.Graphics.Draws;
namespace Box2D.NET.Samples.Samples.Stackings;
// A simple circle stack that also shows how to collect hit events
public class CircleStack : Sample
{
private static readonly int SampleCircleStack = SampleFactory.Shared.RegisterSample("Stacking", "Circle Stack", Create);
private List<Event> m_events = new List<Event>();
private static Sample Create(SampleContext context)
{
return new CircleStack(context);
}
public struct Event
{
public int indexA, indexB;
public Event(int indexA, int indexB)
{
this.indexA = indexA;
this.indexB = indexB;
}
};
public CircleStack(SampleContext context) : base(context)
{
if (m_context.restart == false)
{
m_camera.center = new B2Vec2(0.0f, 5.0f);
m_camera.zoom = 6.0f;
}
int shapeIndex = 0;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.userData = B2UserData.Signed(shapeIndex);
shapeIndex += 1;
B2Segment segment = new B2Segment(new B2Vec2(-10.0f, 0.0f), new B2Vec2(10.0f, 0.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
b2World_SetGravity(m_worldId, new B2Vec2(0.0f, -20.0f));
b2World_SetContactTuning(m_worldId, 0.25f * 360.0f, 10.0f, 3.0f);
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
B2Circle circle = new B2Circle();
circle.radius = 0.5f;
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.enableHitEvents = true;
//shapeDef.rollingResistance = 0.2f;
shapeDef.material.friction = 0.0f;
shapeDef.material.restitution = 0.8f;
float y = 0.75f;
for (int i = 0; i < 4; ++i)
{
bodyDef.position.Y = y;
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
shapeDef.userData = B2UserData.Signed(shapeIndex);
shapeDef.density = 1.0f + 4.0f * i;
shapeIndex += 1;
b2CreateCircleShape(bodyId, shapeDef, circle);
y += 1.25f;
}
}
}
public override void Draw()
{
base.Draw();
B2ContactEvents events = b2World_GetContactEvents(m_worldId);
for (int i = 0; i < events.hitCount; ++i)
{
ref B2ContactHitEvent @event = ref events.hitEvents[i];
var userDataA = b2Shape_GetUserData(@event.shapeIdA);
var userDataB = b2Shape_GetUserData(@event.shapeIdB);
int indexA = (int)userDataA.GetSigned(-1);
int indexB = (int)userDataB.GetSigned(-1);
DrawPoint(m_draw, @event.point, 10.0f, B2HexColor.b2_colorWhite);
m_events.Add(new Event(indexA, indexB));
}
int eventCount = m_events.Count;
for (int i = 0; i < eventCount; ++i)
{
DrawScreenTextLine($"{m_events[i].indexA}, {m_events[i].indexB}");
}
}
}