-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBounceHumans.cs
More file actions
113 lines (88 loc) · 3.49 KB
/
BounceHumans.cs
File metadata and controls
113 lines (88 loc) · 3.49 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using Box2D.NET.Shared;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.Shared.Humans;
using static Box2D.NET.Samples.Graphics.Draws;
namespace Box2D.NET.Samples.Samples.Continuous;
public class BounceHumans : Sample
{
private static readonly int SampleBounceHumans = SampleFactory.Shared.RegisterSample("Continuous", "Bounce Humans", Create);
private Human[] m_humans = new Human[5];
private int m_humanCount = 0;
private float m_countDown = 0.0f;
private float m_time = 0.0f;
//
private B2CosSin _cs1;
private B2CosSin _cs2;
private static Sample Create(SampleContext context)
{
return new BounceHumans(context);
}
public BounceHumans(SampleContext context) : base(context)
{
m_camera.center = new B2Vec2(0.0f, 0.0f);
m_camera.zoom = 12.0f;
for (int i = 0; i < m_humans.Length; ++i)
{
m_humans[i] = new Human();
}
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.material.restitution = 1.3f;
shapeDef.material.friction = 0.1f;
{
B2Segment segment = new B2Segment(new B2Vec2(-10.0f, -10.0f), new B2Vec2(10.0f, -10.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
{
B2Segment segment = new B2Segment(new B2Vec2(10.0f, -10.0f), new B2Vec2(10.0f, 10.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
{
B2Segment segment = new B2Segment(new B2Vec2(10.0f, 10.0f), new B2Vec2(-10.0f, 10.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
{
B2Segment segment = new B2Segment(new B2Vec2(-10.0f, 10.0f), new B2Vec2(-10.0f, -10.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 2.0f);
shapeDef.material.restitution = 2.0f;
b2CreateCircleShape(groundId, shapeDef, circle);
}
public override void Step()
{
if (m_humanCount < 5 && m_countDown <= 0.0f)
{
float jointFrictionTorque = 0.0f;
float jointHertz = 1.0f;
float jointDampingRatio = 0.1f;
CreateHuman(ref m_humans[m_humanCount], m_worldId, new B2Vec2(0.0f, 5.0f), 1.0f, jointFrictionTorque, jointHertz,
jointDampingRatio, 1, B2UserData.Empty, true);
// Human_SetVelocity( m_humans + m_humanCount, { 10.0f - 5.0f * m_humanCount, -20.0f + 5.0f * m_humanCount } );
m_countDown = 2.0f;
m_humanCount += 1;
}
float timeStep = 1.0f / 60.0f;
_cs1 = b2ComputeCosSin(0.5f * m_time);
_cs2 = b2ComputeCosSin(m_time);
float gravity = 10.0f;
B2Vec2 gravityVec = new B2Vec2(gravity * _cs1.sine, gravity * _cs2.cosine);
m_time += timeStep;
m_countDown -= timeStep;
b2World_SetGravity(m_worldId, gravityVec);
base.Step();
}
public override void Draw()
{
base.Draw();
DrawLine(m_draw, b2Vec2_zero, new B2Vec2(3.0f * _cs1.sine, 3.0f * _cs2.cosine), B2HexColor.b2_colorWhite);
}
}