-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKinematic.cs
More file actions
83 lines (64 loc) · 2.4 KB
/
Copy pathKinematic.cs
File metadata and controls
83 lines (64 loc) · 2.4 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
using System;
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 shows how to drive a kinematic body to reach a target
public class Kinematic : Sample
{
private static readonly int SampleKinematic = SampleFactory.Shared.RegisterSample("Bodies", "Kinematic", Create);
private B2BodyId m_bodyId;
private float m_amplitude;
private float m_time;
private float m_timeStep;
private static Sample Create(SampleAppContext ctx, Settings settings)
{
return new Kinematic(ctx, settings);
}
public Kinematic(SampleAppContext ctx, Settings settings) : base(ctx, settings)
{
if (settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(0.0f, 0.0f);
m_context.camera.m_zoom = 4.0f;
}
m_amplitude = 2.0f;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_kinematicBody;
bodyDef.position.X = 2.0f * m_amplitude;
m_bodyId = b2CreateBody(m_worldId, ref bodyDef);
B2Polygon box = b2MakeBox(0.1f, 1.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(m_bodyId, ref shapeDef, ref box);
}
m_time = 0.0f;
}
public override void Step(Settings settings)
{
m_timeStep = settings.hertz > 0.0f ? 1.0f / settings.hertz : 0.0f;
if (settings.pause && settings.singleStep == false)
{
m_timeStep = 0.0f;
}
base.Step(settings);
m_time += m_timeStep;
}
public override void Draw(Settings settings)
{
base.Draw(settings);
if (m_timeStep > 0.0f)
{
B2Vec2 point;
point.X = 2.0f * m_amplitude * MathF.Cos(m_time);
point.Y = m_amplitude * MathF.Sin(2.0f * m_time);
B2Rot rotation = b2MakeRot(2.0f * m_time);
B2Vec2 axis = b2RotateVector(rotation, new B2Vec2(0.0f, 1.0f));
m_context.draw.DrawSegment(point - 0.5f * axis, point + 0.5f * axis, B2HexColor.b2_colorPlum);
m_context.draw.DrawPoint(point, 10.0f, B2HexColor.b2_colorPlum);
b2Body_SetTargetTransform(m_bodyId, new B2Transform(point, rotation), m_timeStep);
}
}
}